-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdleditor.hxx
483 lines (410 loc) · 10 KB
/
sdleditor.hxx
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#ifndef SDLEDITOR_H
#define SDLEDITOR_H
#include <SDL2/SDL.h>
#define DATE(YYYY,MM,DD) ((YYYY*100*100)+(MM*100)+DD)
namespace {
static const unsigned long BuildID = DATE(2014, 6, 18);
static const unsigned long Version = 0.06;
static const unsigned long MinorBuild = 4;
static const char * const ProgName = "GameEditor (SDL)";
class point {
public:
Sint32 x;
Sint32 y;
point(): x(0), y(0) {}
point(const int w, const int h): x(w), y(h) {}
void clamp(const point min, const point max = point(0,0)) {
if(x < min.x) { x = min.x; }
if(y < min.y) { y = min.y; }
if(x > max.x) { x = max.x; }
if(y > max.y) { y = max.y; }
}
void math2d(const point xy, const point lim) {
x+=xy.x;y+=xy.y;
if(x >= lim.x) { ++y; x=0; }
if(y >= lim.y) { y=0; }
}
point operator+(const point xy) const {
return point(x + xy.x, y + xy.y);
}
point operator-(const point xy) const {
return point(x - xy.x, y - xy.y);
}
};
class rect {
public:
SDL_Rect quad;
rect(const int x, const int y, const int w, const int h) {
quad.x = x;
quad.y = y;
quad.w = w;
quad.h = h;
}
};
class config {
public:
int fps, delay;
int shift, tileSize;
point editorSize;
point spriteViewport;
point spriteMapSize;
point mapSize;
void init() {
int tmp = 0;
while (this->tileSize >> tmp) { ++tmp; }
this->shift = --tmp;
this->delay = ( 1000 / this->fps );
}
int toTile(const int pixelVal) {
return (pixelVal >> this->shift);
}
int toPixel(const int pixelVal) {
return (pixelVal << this->shift);
}
};
config conf;
struct tmap {
Uint8 magic[4];
Uint32 size;
Uint16 width;
Uint16 height;
Uint16 origin_x;
Uint16 origin_y;
Uint32 data[];
};
class map {
tmap* tileMap;
static const int struct_size = sizeof(struct tmap);
union { Uint32 i; Uint16 s[2];} layerformat;
static short toShort(const point xy) {
return (((xy.x & 0xff) << 8) + (xy.y & 0xff));
}
static point toPoint(const short data) {
return point((data >> 8), (data & 0xff));
}
public:
map(const char * const mapname) {
SDL_RWops* file = SDL_RWFromFile(mapname, "r");
tmap* l;
int tmap_size;
if (file) {
l = (tmap*) malloc(this->struct_size);
SDL_RWread(file, l, this->struct_size, 1);
l = (tmap*) realloc(l, l->size);
SDL_RWread(file, &(l->data), (l->size - this->struct_size), 1);
SDL_RWclose(file);
} else {
tmap_size = (this->struct_size + ((conf.mapSize.x * conf.mapSize.y) * sizeof(Uint32)));
l = (tmap*) calloc(1, tmap_size);
*((int*)l->magic) = *(int*)"Tmap";
l->width = (short) (conf.mapSize.x & 0xffff);
l->height = (short) (conf.mapSize.y & 0xffff);
l->size = tmap_size;
}
this->tileMap = l;
}
~map() {
free(this->tileMap);
}
void moveOrigin(point change) {
tmap* l = this->tileMap;
point p = point(l->origin_x, l->origin_y) + change;
p.clamp(point(0,0), point(l->width, l->height) - conf.editorSize);
l->origin_x = p.x;
l->origin_y = p.y;
}
void write(const char * const filename) {
SDL_RWops* tmp = SDL_RWFromFile(filename, "w");
SDL_RWwrite(tmp, this->tileMap, this->tileMap->size, 1);
SDL_RWclose(tmp);
}
int offset(const point loc) {
tmap* x = this->tileMap;
int tmpOffset = (x->width * x->origin_y) + x->origin_x;
return tmpOffset + ((x->width * loc.y) + loc.x);
}
point get(point loc, int layer) {
layerformat.i = this->tileMap->data[this->offset(loc)];
return this->toPoint(layerformat.s[layer]);
}
void put(point loc, point data, int layer) {
layerformat.i = this->tileMap->data[this->offset(loc)];
layerformat.s[layer] = this->toShort(data);
this->tileMap->data[this->offset(loc)] = layerformat.i;
}
};
class graphics {
SDL_Window* win;
SDL_Renderer* ren;
SDL_Texture* tex;
public:
int tilePosition;
graphics(const char * const tilemap, const point size, const point loc, const long wflags, const long rflags) {
SDL_Init(SDL_INIT_EVERYTHING);
this->tilePosition = 0;
SDL_Surface* tmp = SDL_LoadBMP(tilemap);
conf.spriteMapSize = point(conf.toTile(tmp->w), conf.toTile(tmp->h));
SDL_SetColorKey(tmp, SDL_TRUE, SDL_MapRGB(tmp->format, 0xff, 0xff, 0xff));
//window
this->win = SDL_CreateWindow(ProgName, loc.x, loc.y, size.x, size.y, wflags);
//renderer
this->ren = SDL_CreateRenderer(this->win, -1, rflags);
//tilemap texture
this->tex = SDL_CreateTextureFromSurface(this->ren, tmp);
SDL_FreeSurface(tmp);
//clear the framebuffer, draw the spritemap and display
SDL_SetRenderDrawColor(this->ren, 0, 0, 0, 255);
this->clear();
this->drawTileMap(0);
this->flip();
}
~graphics() {
SDL_DestroyTexture(this->tex);
SDL_DestroyRenderer(this->ren);
SDL_DestroyWindow(this->win);
SDL_Quit();
}
void drawTileMap(const int origin) {
static const int height = conf.toPixel(SDL_min(conf.spriteMapSize.y, conf.spriteViewport.y));
static SDL_Rect src = rect(0, 0, conf.toPixel(conf.spriteViewport.x), height).quad;
static const SDL_Rect dest = rect(
conf.toPixel(conf.editorSize.x),
0,
conf.toPixel(conf.spriteViewport.x),
height
).quad;
this->tilePosition += origin;
this->fill(dest); //clear the tilemap
this->tilePosition = SDL_min(SDL_max(this->tilePosition, 0),conf.spriteMapSize.x - conf.spriteViewport.x);
src.x = conf.toPixel(this->tilePosition);
this->copy(src, dest);
this->flip();
}
void copy(const SDL_Rect src, const SDL_Rect dest) {
SDL_RenderCopy(this->ren, this->tex, &src, &dest);
}
void clear() {
SDL_RenderClear(this->ren);
}
void fill(const SDL_Rect area) {
SDL_RenderFillRect(this->ren, &area);
}
void flip() {
SDL_RenderPresent(this->ren);
}
void copyTile(const point src, const point dest) {
const int x = conf.tileSize;
static SDL_Rect srcRect = rect(0, 0, x, x).quad;
static SDL_Rect destRect = rect(0, 0, x, x).quad;
srcRect.x = conf.toPixel(src.x);
srcRect.y = conf.toPixel(src.y);
destRect.x = conf.toPixel(dest.x);
destRect.y = conf.toPixel(dest.y);
this->copy(srcRect, destRect);
}
};
class input {
public:
SDL_Event e;
bool quitEvent;
bool mouseClick;
bool keyClick;
char ascii;
int keysym;
point clickPos;
void run() {
quitEvent = false;
mouseClick = false;
keyClick = false;
keysym = 0;
clickPos = point(-1,-1);
while(SDL_PollEvent(&e)) {
switch(e.type) {
case SDL_QUIT:
quitEvent = true;
break;
case SDL_MOUSEBUTTONDOWN:
mouseClick = true;
clickPos = point(conf.toTile(e.button.x), conf.toTile(e.button.y));
break;
case SDL_KEYDOWN:
keyClick = true;
keysym = e.key.keysym.sym;
break;
case SDL_TEXTINPUT:
ascii = e.text.text[0];
break;
default: continue;
}
}
}
void enableTextInput() {
SDL_StartTextInput();
}
void dsableTextInput() {
SDL_StopTextInput();
}
};
class editor {
input* i;
graphics* g;
map* m;
char layer;
public:
point cursor;
point cursorTile;
point selection;
bool running;
bool cursorY;
editor(input* a, graphics* b, map* c, point cur): i(a), g(b), m(c) {
this->layer = 0;
this->cursorY = false;
this->restore();
this->cursorTile = cur;
this->drawCursor();
}
void loadLayers(const point& cur) {
g->copyTile(m->get(cur, 0), cur);
g->copyTile(m->get(cur, 1), cur);
}
void moveCursor(point dest) {
this->setCursor(this->cursor + dest);
}
void setCursor(point dest) {
this->loadLayers(this->cursor);
this->cursor = dest;
this->cursor.clamp(point(0,0), conf.editorSize - point(1,1));
this->drawCursor();
}
void drawCursor() {
g->copyTile(this->cursorTile, this->cursor);
g->flip();
}
void fill(point fillTile) {
point loc(0,0);
do {
m->put(loc, fillTile, this->layer);
g->copyTile(fillTile, loc);
loc.math2d(point(1,0), conf.editorSize);
} while (loc.x || loc.y);
}
void restore() {
point loc(0,0);
do {
this->loadLayers(loc);
loc.math2d(point(1,0), conf.editorSize);
} while (loc.x || loc.y);
}
void draw() {
point& cur = this->cursor;
m->put(cur, this->selection, this->layer);
this->loadLayers(cur);
if(i->keyClick) {
if(this->cursorY) {
cur.math2d(point(0,1), conf.editorSize);
} else {
cur.math2d(point(1,0), conf.editorSize);
}
}
this->drawCursor();
}
void handleEvents() {
this->running = true;
bool keyContinue = false;
i->run();
if(i->quitEvent) {
this->running = false;
}
/* Mouse Input */
if (i->mouseClick) {
if(i->clickPos.x < conf.editorSize.x) {
this->setCursor(i->clickPos);
if(i->e.button.button == SDL_BUTTON_LEFT) {
this->draw();
}
} else {
this->selection = i->clickPos;
this->selection.x -= conf.editorSize.x;
this->selection.x += g->tilePosition;
this->draw();
}
}
/* Keyboard Input */
if (i->keyClick) {
switch(i->keysym) {
case SDLK_q:
case SDLK_ESCAPE:
this->running = false;
break;
case SDLK_SPACE:
case SDLK_RETURN:
this->draw();
break;
case SDLK_r:
g->clear();
g->drawTileMap(0);
this->restore();
this->drawCursor();
break;
case SDLK_x:
this->cursorY = !this->cursorY;
break;
case SDLK_f:
this->fill(this->selection);
this->drawCursor();
break;
case SDLK_1:
layer = 0;
break;
case SDLK_2:
layer = 1;
break;
default: keyContinue = true;
}
}
if(keyContinue) {
keyContinue = false;
switch(i->keysym) {
case SDLK_w:
this->moveCursor(point(0,-1));
break;
case SDLK_a:
this->moveCursor(point(-1,0));
break;
case SDLK_s:
this->moveCursor(point(0,1));
break;
case SDLK_d:
this->moveCursor(point(1,0));
break;
default: keyContinue = true;
}
}
if(keyContinue) {
switch(i->keysym) {
case SDLK_h:
m->moveOrigin(point((conf.editorSize.x/2)*-1, 0));
break;
case SDLK_j:
m->moveOrigin(point(0, (conf.editorSize.x/2)*-1));
break;
case SDLK_k:
m->moveOrigin(point(0, (conf.editorSize.x/2)));
break;
case SDLK_l:
m->moveOrigin(point((conf.editorSize.x/2), 0));
break;
case SDLK_PAGEDOWN:
g->drawTileMap(-10);
break;
case SDLK_PAGEUP:
g->drawTileMap(10);
break;
}
this->restore();
this->drawCursor();
}
}
};
}
#endif //SDLEDITOR_H