-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl_tool.cpp
More file actions
345 lines (301 loc) · 9.94 KB
/
sdl_tool.cpp
File metadata and controls
345 lines (301 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "sdl_tool.h"
#include <iostream>
#include <math.h>
#include <string>
#include "font.h"
#include "sdl_camera.h"
#include "sdl_button.h"
/********************************************************************/
SDLTool::SDLTool(
SDLCamera* camera
) : Tool(), camera_(camera), texture_(nullptr) {
}
SDLTool::~SDLTool() {
if( texture_ != nullptr ) {
SDL_DestroyTexture(texture_);
}
}
void SDLTool::activate() {
camera_->setTool(this);
}
void SDLTool::deactivate() {
if( camera_->tool() == this ) {
camera_->setTool(nullptr);
}
}
void SDLTool::handleEvent() {
const SDL_Event& event = camera_->event();
switch( event.type ) {
case SDL_MOUSEMOTION:
mouseMoved(event.motion.x, event.motion.y);
break;
case SDL_MOUSEBUTTONDOWN:
mousePressed(event.button.button);
break;
case SDL_MOUSEBUTTONUP:
mouseReleased(event.button.button);
break;
case SDL_KEYDOWN:
std::string str(SDL_GetKeyName( event.key.keysym.sym ));
keyPressed(str);
break;
}
}
void SDLTool::mousePressed(int) {
}
void SDLTool::mouseMoved(int, int) {
// Nothing to do for the moment
}
void SDLTool::keyPressed(const std::string& key) {
std::ignore = key;
//Logger::debug() << "KeyPressed: " << key << Logger::endl;
}
void SDLTool::mouseReleased(int) {
}
/********************************************************************/
SDLBuildTool::SDLBuildTool(
SDLCamera* camera,
const std::string& icon_name,
int type
) : SDLTool(camera) {
surface_ = Utility::IMGLoad(icon_name.c_str());
type_ = type;
}
SDLBuildTool::~SDLBuildTool() {
if( surface_ != nullptr ) {
SDL_FreeSurface(surface_);
}
}
SDL_Texture* SDLBuildTool::getTexture(SDL_Renderer* renderer) {
if( texture_ == nullptr ) {
texture_ = SDL_CreateTextureFromSurface(renderer, surface_);
if( texture_ == nullptr ) {
Logger::error() << "CreateRGBSurface failed: " << SDL_GetError() << Logger::endl;
}
SDL_SetTextureAlphaMod( texture_, 192 );
SDL_FreeSurface(surface_);
surface_ = nullptr;
}
return texture_;
}
void SDLBuildTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
if( camera()->mapView()->getCurTilePosition(x,y) ) {
Position position = {x,y};
const Tile& cur = camera()->mapView()->data()->tile(x,y);
if( type_ == WALLTOOL ) {
if( Tile::isWall(cur) || Tile::isDoor(cur) || camera()->mapView()->data()->getObject(position) != nullptr ) {
return;
}
camera()->mapView()->addWallJob(x,y);
} else if( type_ == FLOORTOOL ) {
if( Tile::isWall(cur) || Tile::isFloor(cur) || camera()->mapView()->data()->getObject(position) != nullptr ) {
return;
}
camera()->mapView()->addFloorJob(x,y);
} else if( type_ == DOORTOOL ) {
if( Tile::isWall(cur) || Tile::isDoor(cur) || camera()->mapView()->data()->getObject(position) != nullptr ) {
return;
}
if( !Tile::isFloor(cur) ) {
Logger::info() << tr("First, you need to build a floor in order to put this door") << Logger::endl;
} else {
camera()->mapView()->addDoorJob(x,y);
}
}
}
}
/********************************************************************/
SDLBuildObjectTool::SDLBuildObjectTool(
SDLCamera* camera,
const std::string& icon_name,
const std::string& object_name
) : SDLTool(camera) {
surface_ = Utility::IMGLoad(icon_name.c_str());
object_name_ = object_name;
}
SDLBuildObjectTool::~SDLBuildObjectTool() {
if( surface_ != nullptr ) {
SDL_FreeSurface(surface_);
}
}
void SDLBuildObjectTool::activate() {
CommandCenter* cc = CommandCenter::cur_command_center;
if( cc == nullptr ) return;
int nb = cc->countedItems(object_name_);
if( nb == 0 ) {
Logger::warning() << tr("No such item: please craft this item before using this tool.") << Logger::endl;
return;
}
SDLTool::activate();
}
SDL_Texture* SDLBuildObjectTool::getTexture(SDL_Renderer* renderer) {
if( texture_ == nullptr ) {
texture_ = SDL_CreateTextureFromSurface(renderer, surface_);
if( texture_ == nullptr ) {
Logger::error() << "CreateRGBSurface failed: " << SDL_GetError() << Logger::endl;
}
SDL_SetTextureAlphaMod( texture_, 192 );
SDL_FreeSurface(surface_);
surface_ = nullptr;
}
return texture_;
}
void SDLBuildObjectTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
MapView* map_view = camera()->mapView();
if( map_view->getCurTilePosition(x,y) ) {
// check that an object is not already here !!
if( map_view->getObject(x,y) == nullptr ) {
map_view->addObjectJob(object_name_,x,y);
}
}
}
/********************************************************************/
SDLUnbuildObjectTool::SDLUnbuildObjectTool(
SDLCamera* camera,
const std::string& icon_name
) : SDLTool(camera) {
surface_ = Utility::IMGLoad(icon_name.c_str());
}
SDLUnbuildObjectTool::~SDLUnbuildObjectTool() {
if( surface_ != nullptr ) {
SDL_FreeSurface(surface_);
}
}
SDL_Texture* SDLUnbuildObjectTool::getTexture(SDL_Renderer* renderer) {
if( texture_ == nullptr ) {
texture_ = SDL_CreateTextureFromSurface(renderer, surface_);
if( texture_ == nullptr ) {
Logger::error() << "CreateRGBSurface failed: " << SDL_GetError() << Logger::endl;
}
SDL_SetTextureAlphaMod( texture_, 192 );
SDL_FreeSurface(surface_);
surface_ = nullptr;
}
return texture_;
}
void SDLUnbuildObjectTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
MapView* map_view = camera()->mapView();
if( map_view->getCurTilePosition(x,y) ) {
// check that an object is already here !!
if( map_view->getObject(x,y) != nullptr ) {
map_view->removeObjectJob(x,y);
}
}
}
/********************************************************************/
SDLExtractTool::SDLExtractTool(
SDLCamera* camera,
const std::string& icon_name,
int nb
) : SDLTool(camera) {
icon_surface_ = Utility::IMGLoad(icon_name.c_str());
Uint32 key = SDL_MapRGB(icon_surface_->format, 0, 255, 0);
SDL_SetColorKey(icon_surface_ , SDL_TRUE, key);
nb_ = nb;
}
SDLExtractTool::~SDLExtractTool() {
if( icon_surface_ != nullptr ) {
SDL_FreeSurface(icon_surface_);
}
}
SDL_Texture* SDLExtractTool::getTexture(SDL_Renderer* renderer) {
if( texture_ == nullptr ) {
texture_ = SDL_CreateTextureFromSurface(renderer, icon_surface_);
if( texture_ == nullptr ) {
Logger::error() << "CreateRGBSurface failed: " << SDL_GetError() << Logger::endl;
}
SDL_SetTextureAlphaMod( texture_, 192 );
SDL_FreeSurface(icon_surface_);
icon_surface_ = nullptr;
}
return texture_;
}
void SDLExtractTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
if( camera()->mapView()->getCurTilePosition(x,y) ) {
camera()->mapView()->extractItemJob(x,y,nb_);
}
}
/********************************************************************/
SDLTransformTool::SDLTransformTool(
SDLCamera* camera,
const std::string& icon_name,
int type
) : SDLBuildTool(camera, icon_name, type) {
}
void SDLTransformTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
if( camera()->mapView()->getCurTilePosition(x,y) ) {
Position position = {x,y};
const Tile& cur = camera()->mapView()->data()->tile(x,y);
Logger::debug() << "SDLTransformTool " << type() << Logger::endl;
if( type() == FIELDTOOL ) {
// check that tile is grass or dirt
if( cur.background_type() != Tile::GRASS && cur.background_type() != Tile::DIRT ) {
return;
}
if( Tile::isWall(cur) || Tile::isFloor(cur) || camera()->mapView()->data()->getObject(position) != nullptr ) {
return;
}
camera()->mapView()->addFieldJob(x,y);
}
}
}
/********************************************************************/
SDLCleanTool::SDLCleanTool(
SDLCamera* camera,
const std::string& icon_name
) : SDLTool(camera) {
surface_ = Utility::IMGLoad(icon_name.c_str());
Uint32 key = SDL_MapRGB(surface_->format, 0, 255, 0);
SDL_SetColorKey(surface_ , SDL_TRUE, key);
}
SDLCleanTool::~SDLCleanTool() {
if( surface_ != nullptr ) {
SDL_FreeSurface(surface_);
}
}
SDL_Texture* SDLCleanTool::getTexture(SDL_Renderer* renderer) {
if( texture_ == nullptr ) {
texture_ = SDL_CreateTextureFromSurface(renderer, surface_);
if( texture_ == nullptr ) {
Logger::error() << "CreateRGBSurface failed: " << SDL_GetError() << Logger::endl;
}
SDL_SetTextureAlphaMod( texture_, 192 );
SDL_FreeSurface(surface_);
surface_ = nullptr;
}
return texture_;
}
void SDLCleanTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
MapView* mv = camera()->mapView();
if( mv->getCurTilePosition(x,y) ) {
const Tile& cur_tile = mv->data()->tile(x,y);
if( cur_tile.counted_items().size() > 0 ) {
mv->cleanItemJob(x,y);
}
}
}
/********************************************************************/
SDLUnbuildTool::SDLUnbuildTool(SDLCamera* camera, const std::string& icon_name, int type) : SDLBuildTool(camera, icon_name, type) {
}
void SDLUnbuildTool::mousePressed(int button) {
SDLTool::mousePressed(button);
int x,y;
if( camera()->mapView()->getCurTilePosition(x,y) ) {
if( type() == FOUNDATIONTOOL ) {
camera()->mapView()->removeFoundationJob(x,y);
}
}
}
/********************************************************************/