-
Notifications
You must be signed in to change notification settings - Fork 1
/
warper.c
625 lines (526 loc) · 25.9 KB
/
warper.c
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include "warper.h"
cLogger warperLogger;
warperOptions options;
/** \brief Initializes an animated sprite based on a cSprite.
*
* \param animatedSpr warperAnimatedSprite* - animated sprite struct pointer you want filled in
* \param spr cSprite* - pointer to the base sprite information
* \param srcRectDiffs cDoubleRect* - array of `cDoubleRect`s that provide coordinate differences between positions of source image frames
* \param diffsLength int - length of `srcRectDiffs`
* \param loops int - if != 0, the animation will continually play (MUST provide one extra diff that resets coordinates back to beginning). Negative loops forever
*/
void initWarperAnimatedSprite(warperAnimatedSprite* animatedSpr, cSprite* spr, cDoubleRect* srcRectDiffs, double* rotationDiffs, double* scaleDiffs, SDL_RendererFlip* flipSettings, cDoublePt* centerDiffs, int* layerSettings, int diffsLength, int loops)
{
animatedSpr->sprite = spr;
animatedSpr->srcRectDiffs = calloc(diffsLength, sizeof(cDoubleRect));
animatedSpr->rotationDiffs = calloc(diffsLength, sizeof(double));
animatedSpr->scaleDiffs = calloc(diffsLength, sizeof(double));
animatedSpr->flipSettings = calloc(diffsLength, sizeof(SDL_RendererFlip));
animatedSpr->centerDiffs = calloc(diffsLength, sizeof(cDoublePt));
animatedSpr->layerSettings = calloc(diffsLength, sizeof(int));
if (!animatedSpr->srcRectDiffs || !animatedSpr->rotationDiffs || !animatedSpr->scaleDiffs || !animatedSpr->flipSettings || !animatedSpr->centerDiffs || !animatedSpr->layerSettings)
{
//printf("Warper error: cannot initialize animated sprite");
cLogEvent(warperLogger, "WARNING", "WARPER: Animated Sprite", "Cannot initialize draw deltas");
animatedSpr->numDiffs = 0;
return;
}
for(int i = 0; i < diffsLength; i++)
{
if (srcRectDiffs)
animatedSpr->srcRectDiffs[i] = srcRectDiffs[i];
if (rotationDiffs)
animatedSpr->rotationDiffs[i] = rotationDiffs[i];
if (scaleDiffs)
animatedSpr->scaleDiffs[i] = scaleDiffs[i];
if (flipSettings)
animatedSpr->flipSettings[i] = flipSettings[i];
else
animatedSpr->flipSettings[i] = SDL_FLIP_NONE; //not sure if this is default after calloc()'ing, so I just do this in case
if (centerDiffs)
animatedSpr->centerDiffs[i] = centerDiffs[i];
if (layerSettings)
animatedSpr->layerSettings[i] = layerSettings[i];
else
animatedSpr->layerSettings[i] = 1;
}
animatedSpr->numDiffs = diffsLength;
animatedSpr->loops = loops;
animatedSpr->curFrame = 0;
}
void iterateWarperAnimatedSprite(warperAnimatedSprite* animatedSpr)
{
if (animatedSpr && (animatedSpr->curFrame < animatedSpr->numDiffs || animatedSpr->loops != 0))
{
animatedSpr->sprite->srcClipRect.x += animatedSpr->srcRectDiffs[animatedSpr->curFrame].x; //go to next source frame
animatedSpr->sprite->srcClipRect.y += animatedSpr->srcRectDiffs[animatedSpr->curFrame].y;
animatedSpr->sprite->srcClipRect.w += animatedSpr->srcRectDiffs[animatedSpr->curFrame].w;
animatedSpr->sprite->srcClipRect.h += animatedSpr->srcRectDiffs[animatedSpr->curFrame].h;
animatedSpr->sprite->degrees += animatedSpr->rotationDiffs[animatedSpr->curFrame];
animatedSpr->sprite->scale += animatedSpr->scaleDiffs[animatedSpr->curFrame];
animatedSpr->sprite->flip = animatedSpr->flipSettings[animatedSpr->curFrame];
animatedSpr->sprite->center.x += animatedSpr->centerDiffs[animatedSpr->curFrame].x;
animatedSpr->sprite->center.y += animatedSpr->centerDiffs[animatedSpr->curFrame].y;
animatedSpr->sprite->renderLayer = animatedSpr->layerSettings[animatedSpr->curFrame];
animatedSpr->curFrame++;
if (animatedSpr->curFrame >= animatedSpr->numDiffs && animatedSpr->loops != 0)
{
animatedSpr->curFrame = 0;
if (animatedSpr->loops > 0)
animatedSpr->loops--;
}
}
}
void drawWarperAnimatedSpr(void* spr, cCamera camera)
{
warperAnimatedSprite* animatedSpr = (warperAnimatedSprite*) spr;
drawCSprite(*animatedSpr->sprite, camera, false, false);
iterateWarperAnimatedSprite(animatedSpr);
}
void cleanupWarperAnimatedSpr(void* spr)
{
warperAnimatedSprite* animatedSpr = (warperAnimatedSprite*) spr;
destroyWarperAnimatedSprite(animatedSpr, true);
}
void destroyWarperAnimatedSprite(warperAnimatedSprite* animatedSpr, bool destroySprite)
{
if (destroySprite)
destroyCSprite(animatedSpr->sprite);
animatedSpr->sprite = NULL;
free(animatedSpr->srcRectDiffs);
free(animatedSpr->rotationDiffs);
free(animatedSpr->scaleDiffs);
free(animatedSpr->flipSettings);
free(animatedSpr->centerDiffs);
free(animatedSpr->layerSettings);
animatedSpr->numDiffs = 0;
}
/** \brief Imports an animated sprite from string data
*
* \param aSpr warperAnimatedSprite* - expects aSpr->spr to be filled with an allocated cSprite*, if sprIndex != NULL
* \param data char* - the data you want to import
* \param spriteIndex int* - if not NULL, will be filled in with the index that this sprite occurs at (only for if sprite data wasn't exported and the index was instead)
*/
void importWarperAnimatedSprite(warperAnimatedSprite* aSpr, char* data, int* spriteIndex)
{
char* savePtr = data; //setup for strtok_r()
char* nextToken = strtok_r(data, "{}", &savePtr); //get the cSprite data
if (nextToken[0] == '\"')
{ //if our exported sprite data was actual data, and not a referenced index
char* tempData = calloc(strlen(nextToken) + 3, sizeof(char));
snprintf(tempData, strlen(nextToken) + 3, "{%s}", nextToken); //put it back in the {} (which strtok_r() gets rid of)
//printf("> %s\n", tempData);
importCSprite(aSpr->sprite, tempData); //import the sprite to the (already allocated) cSprite memory space
free(tempData); //free the data surrounded by {} since we have the sprite data imported now
//printf("> %s\n", aSpr->sprite->textureFilepath);
}
else
{ //if it was an index
if (spriteIndex != NULL)
{ //and the caller provided an address for spriteIndex
*spriteIndex = strtol(nextToken, NULL, 10); //write the sprite index
}
else
{ //error occurred
cLogEvent(warperLogger, "ERROR", "WARPER: Animated Sprite", "Sprite data stored as ref index not expected");
}
}
aSpr->numDiffs = strtol(strtok_r(savePtr, ";", &savePtr), NULL, 10); //get the number of different frames
aSpr->loops = strtol(strtok_r(savePtr, ";", &savePtr), NULL, 10); //get the number of loops we will do
//printf("> %d, %d\n", aSpr->numDiffs, aSpr->loops);
aSpr->srcRectDiffs = calloc(aSpr->numDiffs, sizeof(cDoubleRect)); //allocate each array according to how many frames there will be
aSpr->rotationDiffs = calloc(aSpr->numDiffs, sizeof(double));
aSpr->scaleDiffs = calloc(aSpr->numDiffs, sizeof(double));
aSpr->flipSettings = calloc(aSpr->numDiffs, sizeof(SDL_RendererFlip));
aSpr->centerDiffs = calloc(aSpr->numDiffs, sizeof(cDoublePt));
aSpr->layerSettings = calloc(aSpr->numDiffs, sizeof(int));
//src rect diffs
nextToken = strtok_r(savePtr, "[]", &savePtr); //get the first array (src rects)
//strcpy(tempData, nextToken);
char* saveArr = nextToken; //start at the next gotten token (without updating nextToken's address)
//printf("> %s\n", nextToken);
for(int i = 0; i < aSpr->numDiffs; i++)
{
aSpr->srcRectDiffs[i].x = strtod(strtok_r(saveArr, "(,)", &saveArr), NULL); //get the x, then y, then w, then h values
aSpr->srcRectDiffs[i].y = strtod(strtok_r(saveArr, "(,)", &saveArr), NULL);
aSpr->srcRectDiffs[i].w = strtod(strtok_r(saveArr, "(,)", &saveArr), NULL);
aSpr->srcRectDiffs[i].h = strtod(strtok_r(saveArr, "(,)", &saveArr), NULL);
}
//rotation diffs
nextToken = strtok_r(savePtr, "[;]", &savePtr); //do the same for rotation
saveArr = nextToken;
//printf("> %s\n", nextToken);
for(int i = 0; i < aSpr->numDiffs; i++)
aSpr->rotationDiffs[i] = strtod(strtok_r(saveArr, ",", &saveArr), NULL);
//scale diffs
nextToken = strtok_r(savePtr, "[;]", &savePtr); //and scale
saveArr = nextToken;
//printf("> %s\n", nextToken);
for(int i = 0; i < aSpr->numDiffs; i++)
aSpr->scaleDiffs[i] = strtod(strtok_r(saveArr, ",", &saveArr), NULL);
//flip settings
nextToken = strtok_r(savePtr, "[;]", &savePtr); //and flip
saveArr = nextToken;
//printf("> %s\n", nextToken);
for(int i = 0; i < aSpr->numDiffs; i++)
aSpr->flipSettings[i] = (SDL_RendererFlip) strtol(strtok_r(saveArr, ",", &saveArr), NULL, 10);
//center diffs
nextToken = strtok_r(savePtr, "[;]", &savePtr); //and center
saveArr = nextToken;
//printf("> %s\n", nextToken);
for(int i = 0; i < aSpr->numDiffs; i++)
{
aSpr->centerDiffs[i].x = strtod(strtok_r(saveArr, "(,)", &saveArr), NULL);
aSpr->centerDiffs[i].y = strtod(strtok_r(saveArr, "(,)", &saveArr), NULL);
}
//render layer settings
nextToken = strtok_r(savePtr, "[;]", &savePtr); //and layers
saveArr = nextToken;
//printf("> %s\n", nextToken);
for(int i = 0; i < aSpr->numDiffs; i++)
aSpr->layerSettings[i] = (int) strtol(strtok_r(saveArr, ",", &saveArr), NULL, 10);
}
/** \brief Export an animated sprite into string form
*
* \param animatedSpr warperAnimatedSprite - animated sprite to be converted
* \param cSprIndex int - if > -1, will replace the cSprite data to be exported with the given value
* \return char* - allocated string holding the animated sprite data.
*/
char* exportWarperAnimatedSprite(warperAnimatedSprite animatedSpr, int cSprIndex)
{
//I've kinda just given up on calculating the expected size of this string
int spriteSize = 0;
const int rectsSize = ((8 * 4) + 3) * animatedSpr.numDiffs + 3; //4 nums per rect, 7 characters plus one separator per num. 3 characters for parentheses and separator, and this times how ever many diffs we have, plus 2 characters for the containing brackets, plus 1 for good measure
const int rotationsSize = 8 * animatedSpr.numDiffs + 3; //11 digits + 1 separator character per diff, plus 2 characters for the containing brackets, plus 1 for good measure
const int scalesSize = 8 * animatedSpr.numDiffs + 3; //11 digits + 1 separator character per diff, plus 2 characters for the containing brackets, plus 1 for good measure
const int flipsSize = 2 * animatedSpr.numDiffs + 3; //1 digit + 1 separator per diff, plus 2 for containing brackets, plus 1 for good measure
const int centersSize = ((2 * 8) + 3) * animatedSpr.numDiffs + 3; //2 nums per pt, 11 digits + 1 separator character per num, 2 characters for parentheses and 1 for separator, times num diffs, plus 2 for containing brackets, plus 1 for good measure
const int layersSize = 4 * animatedSpr.numDiffs + 3; //3 digits max per item + 1 separator character, times num diffs, plus 2 for parentheses and 1 for good measure
char* spriteData;
if (cSprIndex < 0)
{
spriteData = exportCSprite(*animatedSpr.sprite);
spriteSize = strlen(spriteData) + 1; //plus 1 for good measure
}
else
{
spriteSize = 2 + digits(cSprIndex) + 1; //2 chars for brackets, n for digits, 1 for good measure
spriteData = calloc(spriteSize, sizeof(char));
snprintf(spriteData, spriteSize, "{%d}", cSprIndex);
}
const int dataSize = spriteSize + rectsSize + rotationsSize + scalesSize + flipsSize + centersSize + layersSize + 9; //8 separators plus 1 for good measure
char* data = calloc(dataSize, sizeof(char));
char* srcData = calloc(rectsSize, sizeof(char));
char* rotationData = calloc(rotationsSize, sizeof(char));
char* scaleData = calloc(scalesSize, sizeof(char));
char* flipData = calloc(flipsSize, sizeof(char));
char* centerData = calloc(centersSize, sizeof(char));
char* layerData = calloc(layersSize, sizeof(char));
char* temp = calloc(512, sizeof(char));
strcat(srcData, "[");
strcat(rotationData, "[");
strcat(scaleData, "[");
strcat(flipData, "[");
strcat(centerData, "[");
strcat(layerData, "[");
for(int i = 0; i < animatedSpr.numDiffs; i++)
{
snprintf(temp, 512, "(%.2f,%.2f,%.2f,%.2f)", animatedSpr.srcRectDiffs[i].x, animatedSpr.srcRectDiffs[i].y, animatedSpr.srcRectDiffs[i].w, animatedSpr.srcRectDiffs[i].h);
strcat(srcData, temp);
snprintf(temp, 512, "%.2f", animatedSpr.rotationDiffs[i]);
strcat(rotationData, temp);
snprintf(temp, 512, "%.2f", animatedSpr.scaleDiffs[i]);
strcat(scaleData, temp);
snprintf(temp, 512, "%d", (int) animatedSpr.flipSettings[i]);
strcat(flipData, temp);
snprintf(temp, 512, "(%.2f,%.2f)", animatedSpr.centerDiffs[i].x, animatedSpr.centerDiffs[i].y);
strcat(centerData, temp);
snprintf(temp, 512, "%d", animatedSpr.layerSettings[i]);
strcat(layerData, temp);
if (i < animatedSpr.numDiffs - 1)
{
strcat(srcData, ",");
strcat(rotationData, ",");
strcat(scaleData, ",");
strcat(flipData, ",");
strcat(centerData, ",");
strcat(layerData, ",");
}
}
free(temp);
strcat(srcData, "]");
strcat(rotationData, "]");
strcat(scaleData, "]");
strcat(flipData, "]");
strcat(centerData, "]");
strcat(layerData, "]");
snprintf(data, dataSize, "%s;%d;%d;%s;%s;%s;%s;%s;%s", spriteData, animatedSpr.numDiffs, animatedSpr.loops, srcData, rotationData, scaleData, flipData, centerData, layerData);
free(spriteData);
free(srcData);
free(rotationData);
free(scaleData);
free(flipData);
free(centerData);
free(layerData);
return data;
}
/* Not really used anymore
void initWarperTilemap(warperTilemap* tilemap, int** spritemap, int** collisionmap, int width, int height)
{
tilemap->width = width;
tilemap->height = height;
tilemap->spritemap_layer1 = calloc(width, sizeof(int*));
tilemap->collisionmap = calloc(width, sizeof(int*));
for(int x = 0; x < width; x++)
{
tilemap->spritemap_layer1[x] = calloc(height, sizeof(int));
tilemap->collisionmap[x] = calloc(height, sizeof(int));
for(int y = 0; y < height; y++)
{
tilemap->spritemap_layer1[x][y] = spritemap[x][y];
tilemap->collisionmap[x][y] = collisionmap[x][y];
}
}
}
//*/
/** \brief Imports a tilemap from a file
*
* \param tilemap warperTilemap* - tilemap pointer to be filled in
* \param filepath char* - filepath to map data
*/
void importWarperTilemap(warperTilemap* tilemap, char* filepath, int line)
{
char* importedMap = calloc(7, sizeof(char));
if (!importedMap)
{
cLogEvent(warperLogger, "ERROR", "WARPER: Tilemap", "Cannot initialize tilemap's size buffer");
tilemap->height = -1;
tilemap->width = -1;
tilemap->spritemap_layer1 = NULL;
tilemap->spritemap_layer2 = NULL;
tilemap->collisionmap = NULL;
return;
}
readLine(filepath, line, 7, &importedMap); //read the first 6 characters of the line for the width/height information
//printf("%s\n", importedMap);
char dimData[4] = "\0";
strncpy(dimData, importedMap, 3); //the first three characters of the map file indicate its width
tilemap->width = strtol(dimData, NULL, 16);
strncpy(dimData, importedMap + 3, 3); //the next three characters indicate the map's height
tilemap->height = strtol(dimData, NULL, 16);
free(importedMap);
int importedLength = 3 * 3 * tilemap->width * tilemap->height + 3 + 3;
//this string is long enough to hold data that fills 3 arrays with tiles, using 3-digit tile codes, over the whole width and height of the map
//plus the width and height 'bytes' which is how we know how big this map is in the first place
//(3 arrays * 3 digits * width * height + width 'bytes' + height 'bytes')
importedMap = calloc(importedLength + 1, sizeof(char));
if (!importedMap)
{
cLogEvent(warperLogger, "ERROR", "WARPER: Tilemap", "Cannot initialize tilemap data buffer");
tilemap->height = -1;
tilemap->width = -1;
tilemap->spritemap_layer1 = NULL;
tilemap->spritemap_layer2 = NULL;
tilemap->collisionmap = NULL;
return;
}
readLine(filepath, line, importedLength + 1, &importedMap);
loadTilemap(tilemap, importedMap);
free(importedMap);
tilemap->tileSize = TILE_SIZE;
}
/** \brief Imports a tilemap from text (hex) data
*
* \param tilemap warperTilemap* - expects tilemap->width and ->height to be filled in
* \param importedData char* - your map data
*/
void loadTilemap(warperTilemap* tilemap, char* importedData)
{
char* tileData = calloc(4, sizeof(char));
tilemap->spritemap_layer1 = calloc(tilemap->width, sizeof(int*));
tilemap->spritemap_layer2 = calloc(tilemap->width, sizeof(int*));
tilemap->collisionmap = calloc(tilemap->width, sizeof(int*));
if (!tilemap->spritemap_layer1 || !tilemap->spritemap_layer2 || !tilemap->collisionmap)
{
cLogEvent(warperLogger, "ERROR", "WARPER: Tilemap", "Cannot initialize tilemap column memory");
tilemap->height = -1;
tilemap->width = -1;
tilemap->spritemap_layer1 = NULL;
tilemap->spritemap_layer2 = NULL;
tilemap->collisionmap = NULL;
return;
}
int x = -1, y = tilemap->height + 1; //triggers if statement upon first execution to start the wraparound code (allocating new memory for each horizontal line of tiles)
while(x <= tilemap->width)
{
if (y >= tilemap->height)
{
y = 0;
x++;
if (x < tilemap->width)
{
tilemap->spritemap_layer1[x] = calloc(tilemap->height, sizeof(int));
tilemap->spritemap_layer2[x] = calloc(tilemap->height, sizeof(int));
tilemap->collisionmap[x] = calloc(tilemap->height, sizeof(int));
if (!tilemap->spritemap_layer1[x] || !tilemap->spritemap_layer2[x] || !tilemap->collisionmap[x])
{
cLogEvent(warperLogger, "ERROR", "WARPER: Tilemap", "Cannot initialize tilemap row memory");
tilemap->height = -1;
tilemap->width = -1;
tilemap->spritemap_layer1 = NULL;
tilemap->spritemap_layer2 = NULL;
tilemap->collisionmap = NULL;
return;
}
}
else
break; //we have loaded all of the data
}
//start importing tilemap data AFTER the width/height 'bytes' (6 characters in total), then iterate through for every x/y position
strncpy(tileData, (importedData + 6 + (x * tilemap->height + y) * 3 * 3), 3); //starts at importedData + 6 + (pos * 3 digits * 3 different maps)
tilemap->spritemap_layer1[x][y] = strtol(tileData, NULL, 16);
strncpy(tileData, (importedData + 9 + (x * tilemap->height + y) * 3 * 3), 3);
tilemap->spritemap_layer2[x][y] = strtol(tileData, NULL, 16);
strncpy(tileData, (importedData + 12 + (x * tilemap->height + y) * 3 * 3), 3);
tilemap->collisionmap[x][y] = strtol(tileData, NULL, 16);
y++;
}
free(tileData);
}
/** \brief turns tilemap data into c2DModels to draw
*
* \param tilemap warperTilemap - the tilemap data to draw
* \param layer1 c2DModel* - the lower layer to draw
* \param layer2 c2DModel* - the higher layer to draw
*/
void loadTilemapModels(warperTilemap tilemap, c2DModel* layer1, c2DModel* layer2)
{
SDL_Texture* tilesetTexture;
loadIMG("assets/worldTilesheet.png", &tilesetTexture);
cSprite* tileSprites_layer1 = calloc(tilemap.width * tilemap.height, sizeof(cSprite)), * tileSprites_layer2 = calloc(tilemap.width * tilemap.height, sizeof(cSprite));
if (!tileSprites_layer1 || !tileSprites_layer2)
{
cLogEvent(warperLogger, "ERROR", "WARPER: Tilemap", "Cannot initialize tilemap sprites");
layer1->numSprites = 0;
layer2->numSprites = 0;
return;
}
for(int x = 0; x < tilemap.width; x++)
{
for(int y = 0; y < tilemap.height; y++)
{
initCSprite(&tileSprites_layer1[x * tilemap.height + y], tilesetTexture, "assets/worldTilesheet.png", tilemap.spritemap_layer1[x][y],
(cDoubleRect) {tilemap.tileSize * x, tilemap.tileSize * y, tilemap.tileSize, tilemap.tileSize},
(cDoubleRect) {(tilemap.spritemap_layer1[x][y] / 20) * tilemap.tileSize / 2, (tilemap.spritemap_layer1[x][y] % 20) * tilemap.tileSize / 2, tilemap.tileSize / 2, tilemap.tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, NULL, 5);
initCSprite(&tileSprites_layer2[x * tilemap.height + y], tilesetTexture, "assets/worldTilesheet.png", tilemap.spritemap_layer2[x][y],
(cDoubleRect) {tilemap.tileSize * x, tilemap.tileSize * y, tilemap.tileSize, tilemap.tileSize},
(cDoubleRect) {(tilemap.spritemap_layer2[x][y] / 20) * tilemap.tileSize / 2, (tilemap.spritemap_layer2[x][y] % 20) * tilemap.tileSize / 2, tilemap.tileSize / 2, tilemap.tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, NULL, 5);
}
}
initC2DModel(layer1, tileSprites_layer1, tilemap.width * tilemap.height, (cDoublePt) {0, 0}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 5);
initC2DModel(layer2, tileSprites_layer2, tilemap.width * tilemap.height, (cDoublePt) {0, 0}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 3);
free(tileSprites_layer1);
free(tileSprites_layer2);
}
/** \brief Prints the exported map data into an allocated exportedData
*
* \param tilemap warperTilemap - tilemap data to draw
* \param exportedData char* - an alloc'd string where the data will go
*/
void exportTilemap(warperTilemap tilemap, char* exportedData)
{
char* tileString = calloc(4, sizeof(char));
snprintf(tileString, 4, "%.3X", tilemap.width);
strcat(exportedData, tileString);
snprintf(tileString, 4, "%.3X", tilemap.height);
strcat(exportedData, tileString);
for(int x = 0; x < tilemap.width; x++)
{
for(int y = 0; y < tilemap.height; y++)
{
snprintf(tileString, 4, "%.3X", tilemap.spritemap_layer1[x][y]);
strcat(exportedData, tileString);
snprintf(tileString, 4, "%.3X", tilemap.spritemap_layer2[x][y]);
strcat(exportedData, tileString);
snprintf(tileString, 4, "%.3X", tilemap.collisionmap[x][y]);
strcat(exportedData, tileString);
}
}
free(tileString);
}
void destroyWarperTilemap(warperTilemap* tilemap)
{
for(int x = 0; x < tilemap->width; x++)
{
free(tilemap->spritemap_layer1[x]);
free(tilemap->spritemap_layer2[x]);
free(tilemap->collisionmap[x]);
}
free(tilemap->spritemap_layer1);
free(tilemap->spritemap_layer2);
free(tilemap->collisionmap);
tilemap->width = 0;
tilemap->height = 0;
}
/** \brief Loads the Warper options into the global struct. If the options file exist, creates it first
*/
void loadWarperOptions()
{
if (checkFile(WARPER_OPTIONS_FILE) <= 0)
{
//default options
options.framerate = WARPER_FRAME_LIMIT;
options.difficulty = 1; //Beginner. Should it be something more like Medium?
options.gridOpacity = 0x00; //what should the default be here?
options.musicVolume = MIX_MAX_VOLUME;
options.soundFxVolume = MIX_MAX_VOLUME;
saveWarperOptions();
}
else
{
char* optionsText = calloc(80, sizeof(char));
readLine(WARPER_OPTIONS_FILE, 0, 80, &optionsText); //framerate
char* savePtr = optionsText;
strtok_r(savePtr, ":", &savePtr);
options.framerate = strtol(savePtr, NULL, 10);
readLine(WARPER_OPTIONS_FILE, 1, 80, &optionsText); //difficulty
savePtr = optionsText;
strtok_r(savePtr, ":", &savePtr);
options.difficulty = strtol(savePtr, NULL, 10);
readLine(WARPER_OPTIONS_FILE, 2, 80, &optionsText); //grid opacity
savePtr = optionsText;
strtok_r(savePtr, ":", &savePtr);
options.gridOpacity = (Uint8) strtol(savePtr, NULL, 10) / 100.0 * GRID_MAX_OPACITY;
readLine(WARPER_OPTIONS_FILE, 3, 80, &optionsText); //music volume
savePtr = optionsText;
strtok_r(savePtr, ":", &savePtr);
options.musicVolume = (int) strtol(savePtr, NULL, 10) / 100.0 * MIX_MAX_VOLUME;
readLine(WARPER_OPTIONS_FILE, 4, 80, &optionsText); //sfx volume
savePtr = optionsText;
strtok_r(savePtr, ":", &savePtr);
options.soundFxVolume = (int) strtol(savePtr, NULL, 10) / 100.0 * MIX_MAX_VOLUME;
//TODO - complete
free(optionsText);
}
}
/** \brief Writes the Warper options that are in the global struct to the file
*/
void saveWarperOptions()
{
createFile(WARPER_OPTIONS_FILE); //erase existing file
char* optionsText = calloc(80, sizeof(char));
snprintf(optionsText, 80, "framerate:%d", options.framerate);
appendLine(WARPER_OPTIONS_FILE, optionsText, true);
snprintf(optionsText, 80, "difficulty:%d", options.difficulty);
appendLine(WARPER_OPTIONS_FILE, optionsText, true);
snprintf(optionsText, 80, "gridOpacity:%d", (int) (options.gridOpacity / ((double) GRID_MAX_OPACITY) * 100));
appendLine(WARPER_OPTIONS_FILE, optionsText, true);
snprintf(optionsText, 80, "musicVolume:%d", (int) (options.musicVolume / ((double) MIX_MAX_VOLUME) * 100));
appendLine(WARPER_OPTIONS_FILE, optionsText, true);
snprintf(optionsText, 80, "soundFxVolume:%d", (int) (options.soundFxVolume / ((double) MIX_MAX_VOLUME) * 100));
appendLine(WARPER_OPTIONS_FILE, optionsText, true);
//TODO - complete
free(optionsText);
}