-
Notifications
You must be signed in to change notification settings - Fork 9
/
classChunks.cpp
506 lines (423 loc) · 12.7 KB
/
classChunks.cpp
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
This file is part of Retro Graphics Toolkit
Retro Graphics Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
Retro Graphics Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
Copyright Sega16 (or whatever you wish to call me) (2012-2018)
*/
#include <FL/fl_ask.H>
#include <exception>
#include "macros.h"
#include "callback_chunk.h"
#include "filemisc.h"
#include "undo.h"
#include "gamedef.h"
#include "gui.h"
#include "class_global.h"
#include "compressionWrapper.h"
#include "filereader.h"
ChunkClass::ChunkClass(Project*prj) {
this->prj = prj;
chunks.resize(256);
amt = 1;
wi = hi = 16; //16*16=256
useBlocks = false;
usePlane = 0;
}
ChunkClass::ChunkClass(const ChunkClass& other, Project*prj) {
this->prj = prj;
usePlane = other.usePlane;
wi = other.wi;
hi = other.hi;
amt = other.amt;
useBlocks = other.useBlocks;
chunks = other.chunks;
}
ChunkClass::~ChunkClass(void) {
chunks.clear();
}
void ChunkClass::insert(uint32_t at) {
struct ChunkAttrs tmp;
memset(&tmp, 0, sizeof(tmp));
chunks.insert(chunks.begin() + (at * sizeOfChunk()), sizeOfChunk(), tmp);
++amt;
}
void ChunkClass::setElm(uint32_t id, uint32_t x, uint32_t y, struct ChunkAttrs c) {
struct ChunkAttrs*ch = chunks.data() + (id * wi * hi) + (y * wi) + x;
ch->flags = c.flags;
ch->block = c.block;
}
struct ChunkAttrs ChunkClass::getElm(uint32_t id, uint32_t x, uint32_t y)const {
return chunks[(id * wi * hi) + (y * wi) + x];
}
void ChunkClass::removeAt(uint32_t at) {
if (amt < 2) {
fl_alert("If you don't want chunks uncheck \"have chunks\" instead of deleting");
return;
}
try {
chunks.erase(chunks.begin() + (at * wi * hi), chunks.begin() + ((at + 1)*wi * hi));
} catch (std::exception&e) {
fl_alert("Error cannot remove tile %d\nAdditional details: %s", at, e.what());
exit(1);
}
--amt;
if (window && window->chunk_select->value() >= amt) {
window->chunk_select->value(amt - 1);
currentChunk = amt - 1;
}
}
void ChunkClass::resizeAmt(uint32_t amtnew) {
chunks.resize(amtnew * wi * hi);
amt = amtnew;
if (window && window->chunk_select->value() >= amt) {
window->chunk_select->value(amt - 1);
currentChunk = amt - 1;
}
}
void ChunkClass::resizeAmt(void) {
resizeAmt(amt);
}
void ChunkClass::getXYblock(unsigned id, unsigned x, unsigned y, unsigned& xo, unsigned& yo)const {
xo = x % prj->tms->maps[usePlane].mapSizeW;
yo = (chunks[(id * wi * hi) + (y * wi / prj->tms->maps[usePlane].mapSizeH) + (x / prj->tms->maps[usePlane].mapSizeW)].block
* prj->tms->maps[usePlane].mapSizeH)
+ (y % prj->tms->maps[usePlane].mapSizeH);
}
bool ChunkClass::getPrio_t(uint32_t id, uint32_t x, uint32_t y)const { //The _t means based on tiles not blocks
if (useBlocks) {
getXYblock(id, x, y, x, y);
return prj->tms->maps[usePlane].get_prio(x, y);
} else
return ((getFlag(id, x, y) >> 2) & 1) ? true : false;
}
uint8_t ChunkClass::getTileRow_t(uint32_t id, uint32_t x, uint32_t y)const {
if (useBlocks) {
getXYblock(id, x, y, x, y);
return prj->tms->maps[usePlane].getPalRow(x, y);
} else
return (getFlag(id, x, y) >> 3) & 3;
}
unsigned ChunkClass::getTile_t(uint32_t id, uint32_t x, uint32_t y)const {
if (useBlocks) {
getXYblock(id, x, y, x, y);
return prj->tms->maps[usePlane].get_tile(x, y);
} else
return (getFlag(id, x, y) >> 3) & 3;
}
unsigned ChunkClass::getSolid(uint32_t id, uint32_t x, uint32_t y)const {
unsigned shift;
if (useBlocks)
shift = 2;
else
shift = 5;
return (chunks[getOff(id, x, y)].flags >> shift) & 3;
}
uint32_t ChunkClass::getBlock(uint32_t id, uint32_t x, uint32_t y)const {
return chunks[getOff(id, x, y)].block;
}
bool ChunkClass::getHflip(uint32_t id, uint32_t x, uint32_t y)const {
return chunks[getOff(id, x, y)].flags & 1;
}
bool ChunkClass::getVflip(uint32_t id, uint32_t x, uint32_t y)const {
return (chunks[getOff(id, x, y)].flags & 2) >> 1;
}
unsigned ChunkClass::getOff(const uint32_t id, const uint32_t x, const uint32_t y)const {
return (id * wi * hi) + (y * wi) + x;
}
bool ChunkClass::getPrio(uint32_t id, uint32_t x, uint32_t y)const {
return (chunks[getOff(id, x, y)].flags & 4) >> 2;
}
void ChunkClass::setBlock(uint32_t id, uint32_t x, uint32_t y, uint32_t block) {
chunks[getOff(id, x, y)].block = block;
/*This contains which block/tile to use*/
}
void ChunkClass::setFlag(uint32_t id, uint32_t x, uint32_t y, uint32_t flag) {
chunks[getOff(id, x, y)].flags = flag;
/*!If not using blocks flags will contain the following
bit 0 hflip
bit 1 vflip
bit 2 priority
bit 3,4 palette row
All other bits are unused and can be used for video game usage
If using blocks flags will simply contain video game settings
Here are video game settings used. If using tiles instead of blocks add 3 to bit count and ignore x and y flip
bit 0 x-flip
bit 1 y-flip
bit 2,3 solidity 00 means not solid, 01 means top solid, 10 means left/right/bottom solid, and 11 means all solid.
*/
}
uint32_t ChunkClass::getFlag(uint32_t id, uint32_t x, uint32_t y)const {
return chunks[getOff(id, x, y)].flags;
}
void ChunkClass::setSolid(uint32_t id, uint32_t x, uint32_t y, unsigned solid) {
unsigned shift;
if (useBlocks)
shift = 2;
else
shift = 5;
unsigned off = getOff(id, x, y);
chunks[off].flags &= ~(3 << shift);
chunks[off].flags |= solid << shift;
}
void ChunkClass::setHflip(uint32_t id, uint32_t x, uint32_t y, bool hflip) {
unsigned off = getOff(id, x, y);
if (hflip)
chunks[off].flags |= 1;
else
chunks[off].flags &= ~1;
}
void ChunkClass::setVflip(uint32_t id, uint32_t x, uint32_t y, bool vflip) {
unsigned off = getOff(id, x, y);
if (vflip)
chunks[off].flags |= 2;
else
chunks[off].flags &= ~2;
}
void ChunkClass::setPrio(uint32_t id, uint32_t x, uint32_t y, bool prio) {
unsigned off = getOff(id, x, y);
if (prio)
chunks[off].flags |= 4;
else
chunks[off].flags &= ~4;
}
void ChunkClass::drawChunk(uint32_t id, int xo, int yo, int zoom, int scrollX, int scrollY) {
if (!window)
return;
struct ChunkAttrs * cptr = chunks.data();
for (uint32_t y = scrollY; y < hi; ++y) {
cptr = &chunks[(id * wi * hi) + (y * wi) + scrollX];
int xoo = xo;
for (uint32_t x = scrollX; x < wi; ++x) {
if (useBlocks) {
prj->tms->maps[usePlane].drawBlock(cptr->block, xoo, yo, cptr->flags & 3, zoom);
xoo += prj->tms->maps[usePlane].mapSizeW * prj->tileC->width() * zoom;
} else {
prj->tileC->draw_tile(xoo, yo, cptr->block, zoom, (cptr->flags >> 3) & 3, cptr->flags & 1, (cptr->flags >> 1) & 1);
xoo += prj->tileC->width() * zoom;
}
cptr++;
if ((xoo) > (window->w()))
break;
}
if (useBlocks)
yo += prj->tileC->height() * zoom * prj->tms->maps[usePlane].mapSizeH;
else
yo += prj->tileC->height() * zoom;
if (yo > (window->h()))
break;
}
}
void ChunkClass::scrollChunks(void) {
if (!window)
return;
unsigned oldS = window->chunkX->value();
int zoom = window->chunk_tile_size->value();
int off;
if (useBlocks)
off = (wi * prj->tms->maps[usePlane].mapSizeW) - ((window->w() - ChunkOff[0]) / (zoom * prj->tileC->width()));
else
off = wi - ((window->w() - ChunkOff[0]) / (zoom * prj->tileC->width()));
if (oldS > off)
scrollChunks_G[0] = oldS = off;
if (off > 0) {
window->chunkX->show();
window->chunkX->value(oldS, 1, 0, off + 2);
} else
window->chunkX->hide();
oldS = window->chunkY->value();
if (useBlocks)
off = (hi * prj->tms->maps[usePlane].mapSizeH) - ((window->h() - ChunkOff[1]) / (zoom * prj->tileC->height()));
else
off = hi - ((window->h() - ChunkOff[1]) / (zoom * prj->tileC->height()));
if (oldS > off)
scrollChunks_G[1] = oldS = off;
if (off > 0) {
window->chunkY->show();
window->chunkY->value(oldS, 1, 0, off + 2);
} else
window->chunkY->hide();
}
static void errorNum(void) {
fl_alert("Please enter a value greater than zero.");
}
void ChunkClass::importSonic1(bool append) {
if (fl_ask("Custom width and height?")) {
char*ptr = (char*)fl_input("Width");
if (!ptr)
return;
if (!verify_str_number_only(ptr))
return;
int witmp = atoi(ptr);
if (witmp <= 0) {
errorNum();
return;
}
ptr = (char*)fl_input("Height");
if (!ptr)
return;
if (!verify_str_number_only(ptr))
return;
int hitmp = atoi(ptr);
if (hitmp <= 0) {
errorNum();
return;
}
if (append)
resize(witmp, hitmp);
wi = witmp;
hi = hitmp;
} else
wi = hi = 16;
pushChunksAll();
uint16_t* dat;
size_t fileSize;
filereader f = filereader(boost::endian::order::big, 2, "Select a Sonic One chunk file");
unsigned i = f.selDat();
dat = (uint16_t*)f.dat[i];
fileSize = f.lens[i];
uint32_t off;
if (append)
off = amt;
else
off = 0;
if (window)
window->updateChunkSize(wi, hi);
amt = (fileSize / (wi * hi * 2)) + off;
chunks.resize(amt * wi * hi);
struct ChunkAttrs*cptr = chunks.data();
cptr += off * wi * hi;
uint16_t * datC = dat;
for (uint32_t l = 0; l < (fileSize / (wi * hi * 2)); ++l) {
for (uint32_t y = 0; y < hi; ++y) {
for (uint32_t x = 0; x < wi; ++x) {
cptr->block = *datC & 1023;
cptr->flags = (*datC >> 11) & 15;
++cptr;
++datC;
}
}
}
}
void ChunkClass::exportSonic1(void)const {
FILE*fp;
int clipboard;
fileType_t type = askSaveType();
size_t fileSize;
if (type != fileType_t::tBinary) {
clipboard = clipboardAsk();
if (clipboard == 2)
return;
} else
clipboard = 0;
bool pickedFile;
std::string the_file;
if (clipboard)
pickedFile = true;
else
pickedFile = loadOrSaveFile(the_file, "Save tilemap to", true);
if (pickedFile) {
CompressionType compression = compressionAsk();
if (compression == CompressionType::Cancel)
return;
if (clipboard)
fp = 0;
else if (type != fileType_t::tBinary)
fp = fopen(the_file.c_str(), "w");
else
fp = fopen(the_file.c_str(), "wb");
if (likely(fp || clipboard)) {
const struct ChunkAttrs*cptr = chunks.data();
uint16_t*tmp = (uint16_t*)malloc(wi * hi * 2 * amt);
uint16_t*ptmp = tmp;
fileSize = wi * hi * amt * 2;
for (uint32_t i = 0; i < wi * hi * amt; ++i) {
uint32_t temp = cptr->block;
if (temp > 1023) {
printf("Block overflow %d\n", temp);
temp = 1023;
}
temp |= (cptr->flags & 15) << 11;
*ptmp++ = temp;
++cptr;
}
if (compression != CompressionType::Uncompressed) {
void*tmpold = tmp;
// The endian must be corrected before compressing.
uint16_t * ptr = (uint16_t*)tmp;
for (unsigned i = 0; i < wi * hi * amt; ++i) {
uint16_t tmp = *ptr;
boost::endian::conditional_reverse_inplace<boost::endian::order::native, boost::endian::order::big>(tmp);
*ptr++ = tmp;
}
tmp = (uint16_t*)encodeType(tmp, fileSize, fileSize, compression);
free(tmpold);
}
char temp[2048];
snprintf(temp, 2048, "Width: %d Height: %d Amount: %d %s", wi, hi, amt, typeToText(compression));
if (!saveBinAsText(tmp, fileSize, fp, type, temp, "mapDat", compression != CompressionType::Uncompressed ? 8 : 16,
compression != CompressionType::Uncompressed ? boost::endian::order::native : boost::endian::order::big)) {
free(tmp);
return;
}
free(tmp);
if (fp)
fclose(fp);
}
}
}
void ChunkClass::resize(uint32_t wnew, uint32_t hnew) {
if ((wnew == wi) && (hnew == hi))
return;
struct ChunkAttrs*tmp = (struct ChunkAttrs*)malloc(sizeof(struct ChunkAttrs) * wi * hi * amt);
memcpy(tmp, chunks.data(), sizeof(struct ChunkAttrs)*wi * hi * amt);
chunks.resize(amt * wnew * hnew);
struct ChunkAttrs*cptr = chunks.data(), *tptr = tmp;
for (uint32_t z = 0; z < amt; ++z) {
for (uint32_t y = 0; y < std::min(hi, hnew); ++y) {
if (wnew > wi) {
memcpy(cptr, tptr, wi * sizeof(struct ChunkAttrs));
memset(cptr + wi, 0, (wnew - wi)*sizeof(struct ChunkAttrs));
} else
memcpy(cptr, tptr, wnew * sizeof(struct ChunkAttrs));
cptr += wnew;
tptr += wi;
}
if (hnew > hi) {
for (uint32_t y = hi; y < hnew; ++y) {
memset(cptr, 0, wnew * sizeof(struct ChunkAttrs));
cptr += wnew;
}
} else
tptr += (hi - hnew) * wi;
}
wi = wnew;
hi = hnew;
free(tmp);
scrollChunks();
}
void ChunkClass::subBlock(unsigned oid, unsigned nid) {
uint_fast32_t x, y, i;
int_fast32_t temp;
for (i = 0; i < amt; ++i) {
for (y = 0; y < hi; ++y) {
for (x = 0; x < wi; ++x) {
temp = getBlock(i, x, y);
if (temp == oid)
setBlock(i, x, y, nid);
else if (temp > oid) {
temp--;
if (temp < 0)
temp = 0;
setBlock(i, x, y, temp);
}
}
}
}
}