-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsGraphics.c
1892 lines (1684 loc) · 68.9 KB
/
csGraphics.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "csGraphics.h"
coSprite global;
Uint32 startTime; /**< not set in-engine; if you want to collect a framerate from drawCScene(), set this right before you start your loop */
/** \brief Initializes a cSprite object. You may want to create a wrapper method.
*
* \param sprite - a pointer to your sprite.
* \param texture - an SDL_Texture with your sprite's image. Pass NULL to have your texture auto-initialized
* \param textureFilepath - a char* that holds your texture's filepath
* \param drawRect - draw position (in camera coordinates)
* \param srcClipRect - tilesheet/img position (in px)
* \param center - center of your sprite (in camera coordinates). NULL to be (w/2, h/2)
* \param scale - size * this == drawn size
* \param flip - SDL_RenderFlip value
* \param degrees - rotation angle in degrees
* \param subclass - void*. Do with it what you will, isn't used internally
* \param fixed - if true, won't be affected by a scene's camera
* \param renderLayer - 0 - not drawn. 1-`renderLayers` - drawn. Lower number = drawn later
*/
void initCSprite(cSprite* sprite, SDL_Texture* texture, char* textureFilepath, int id, cDoubleRect drawRect, cDoubleRect srcClipRect, cDoublePt* center, double scale, SDL_RendererFlip flip, double degrees, bool fixed, bool global, void* subclass, int renderLayer)
{
if (texture)
sprite->texture = texture;
else
loadIMG(textureFilepath, &(sprite->texture));
strncpy(sprite->textureFilepath, textureFilepath, MAX_PATH);
sprite->id = id;
sprite->drawRect = drawRect;
sprite->srcClipRect = srcClipRect;
if (center != NULL)
sprite->center = *center;
else
sprite->center = (cDoublePt) {drawRect.w / 2, drawRect.h / 2};
sprite->scale = scale;
sprite->degrees = degrees;
sprite->flip = flip;
sprite->fixed = fixed;
sprite->global = global;
sprite->subclass = subclass;
sprite->renderLayer = renderLayer;
}
/** \brief clears out a cSprite and its memory
*
* \param sprite - cSprite pointer
*/
void destroyCSprite(cSprite* sprite)
{
SDL_DestroyTexture(sprite->texture);
sprite->drawRect = (cDoubleRect) {0, 0, 0, 0};
sprite->srcClipRect = (cDoubleRect) {0, 0, 0, 0};
sprite->id = 0;
sprite->scale = 0;
sprite->degrees = 0;
sprite->flip = SDL_FLIP_NONE;
sprite->fixed = false;
sprite->global = false;
free(sprite->subclass);
sprite->subclass = NULL;
sprite->renderLayer = 0;
}
/** \brief draws a cSprite to the screen
*
* \param sprite - cSprite you want drawn
* \param camera - cCamera to be used for drawing
* \param update - if true, immediately presents renderer
* \param fixedOverride - if true, acts as if sprite.fixed is true
*/
void drawCSprite(cSprite sprite, cCamera camera, bool update, bool fixedOverride)
{
//find the total scaling factor
const double scale = sprite.scale * (sprite.fixed ? 1.0 : camera.zoom);
//uncomment the last half to have model scaling center around the center pt rather than the x/y coords
//it takes the rect point and offsets it by the delta width / 2 (so that the sprite can scale around its visual center rather than its x/y coords
cDoublePt point = (cDoublePt) {sprite.drawRect.x / camera.rect.w * global.windowW /*- ((sprite.drawRect.w * (sprite.scale - 1)) / 2)*/,
sprite.drawRect.y / camera.rect.h * global.windowH /*- ((sprite.drawRect.h * (sprite.scale - 1)) / 2)*/};
if (!(sprite.fixed || fixedOverride))
{ //if we are able to relate it to the camera
//scale the points relative to the center of the window
point.x = (point.x - global.windowW / 2.0) * camera.zoom + global.windowW / 2.0;
point.y = (point.y - global.windowH / 2.0) * camera.zoom + global.windowH / 2.0;
}
//rotate the point around its center
point = rotatePoint(point, (cDoublePt) {sprite.drawRect.x + sprite.center.x * scale, sprite.drawRect.y + sprite.center.y * scale}, sprite.degrees);
if (!(sprite.fixed || fixedOverride))
{ //if we are able to relate it to the camera
//rotate the point around the camera's center
point = rotatePoint(point, (cDoublePt) {global.windowW / 2, global.windowH / 2}, camera.degrees);
//apply the camera offset
point.x -= (camera.rect.x * global.windowW / camera.rect.w);
point.y -= (camera.rect.y * global.windowH / camera.rect.h);
}
SDL_RenderCopyExF(global.mainRenderer, sprite.texture, &((SDL_Rect) {sprite.srcClipRect.x, sprite.srcClipRect.y, sprite.srcClipRect.w, sprite.srcClipRect.h}),
&((SDL_FRect) {.x = point.x /*- ((sprite.drawRect.w / 2) * sprite.scale * camera.zoom)*/, .y = point.y /*- ((sprite.drawRect.h / 2) * sprite.scale * camera.zoom)*/, .w = sprite.drawRect.w * scale * global.windowW / camera.rect.w, .h = sprite.drawRect.h * scale * global.windowH / camera.rect.h}),
sprite.degrees + (!sprite.fixed * camera.degrees), &((SDL_FPoint) {0, 0}), sprite.flip);
if (update)
SDL_RenderPresent(global.mainRenderer);
}
/** \brief Import a cSprite from string data
*
* \param sprite cSprite* - the sprite struct you want filled in
* \param data char* - the data you want converted to a cSprite
*/
void importCSprite(cSprite* sprite, char* data)
{
char* savePtr = data;
//texture
char* filepath = strtok_r(data, "{,}", &savePtr); //grab the filepath
char* saveFilepath = filepath;
strncpy(sprite->textureFilepath, strtok_r(filepath, "\"", &saveFilepath), MAX_PATH); //take off the quotes
loadIMG(sprite->textureFilepath, &(sprite->texture));
//data
sprite->id = strtol(strtok_r(savePtr, "{,}", &savePtr), NULL, 10);
sprite->drawRect.x = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->drawRect.y = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->drawRect.w = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->drawRect.h = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->srcClipRect.x = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->srcClipRect.y = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->srcClipRect.w = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->srcClipRect.h = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->center.x = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->center.y = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->scale = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->flip = strtol(strtok_r(savePtr, "{,}", &savePtr), NULL, 10);
sprite->degrees = strtod(strtok_r(savePtr, "{,}", &savePtr), NULL);
sprite->renderLayer = strtol(strtok_r(savePtr, "{,}", &savePtr), NULL, 10);
sprite->fixed = strtol(strtok_r(savePtr, "{,}", &savePtr), NULL, 10);
sprite->subclass = NULL; //subclass data not stored
sprite->global = false; //global flag also not stored
}
/** \brief Export a cSprite to string format
*
* \param sprite cSprite - the sprite you want converted to string
* \return char* - the string containing your sprite data (must be free()'d manually after use)
*/
char* exportCSprite(cSprite sprite)
{
char* spriteData = calloc(2048, sizeof(char));
snprintf(spriteData, 2048, "{\"%s\",%d,%f,%f,%f,%f,%.2f,%.2f,%.2f,%.2f,%f,%f,%f,%d,%f,%d,%d}", sprite.textureFilepath, sprite.id,
sprite.drawRect.x, sprite.drawRect.y, sprite.drawRect.w, sprite.drawRect.h,
sprite.srcClipRect.x, sprite.srcClipRect.y, sprite.srcClipRect.w,
sprite.srcClipRect.h, sprite.center.x, sprite.center.y, sprite.scale,
sprite.flip, sprite.degrees, sprite.renderLayer, sprite.fixed);
return spriteData;
}
/** \brief Initializes a c2DModel object.
*
* \param model - a pointer to your model.
* \param sprites - a pointer that holds your sprites.
* \param position - x/y of your model (in camera coordinates). Width and height will be filled in automatically.
* \param center - a pointer to a cDoublePt that is the relative center. NULL to be (w/2, h/2).
* \param scale - size * this == drawn size
* \param flip - SDL_RenderFlip value
* \param degrees - rotation angle in degrees
* \param fixed - if true, won't be affected by a scene's camera
* \param subclass - void*. Do with it what you will, isn't used internally
* \param renderLayer - 0 - not drawn. 1-`renderLayers` - drawn. Lower number = drawn later
*/
void initC2DModel(c2DModel* model, cSprite* sprites, int numSprites, cDoublePt position, cDoublePt* center, double scale, SDL_RendererFlip flip, double degrees, bool fixed, void* subclass, int renderLayer)
{
model->sprites = calloc(numSprites, sizeof(cSprite));
memcpy(model->sprites, sprites, numSprites * sizeof(cSprite));
//model->sprites = (numSprites) ? sprites : NULL;
model->numSprites = numSprites;
model->rect = (cDoubleRect) {position.x, position.y, 0, 0};
if (center != NULL)
model->center = *center;
else
model->center = (cDoublePt) {model->rect.w / 2, model->rect.h / 2};
model->scale = scale;
model->flip = flip;
model->degrees = degrees;
model->fixed = fixed;
model->subclass = subclass;
model->renderLayer = renderLayer;
//sortCSpritesInModel(model);
}
/** \brief clears out a c2DModel and its memory
*
* \param model - c2DModel pointer
*/
void destroyC2DModel(c2DModel* model)
{
for(int i = 0; i < model->numSprites; i++)
destroyCSprite(&model->sprites[i]);
free(model->sprites);
model->rect = (cDoubleRect) {0, 0, 0, 0};
model->scale = 0;
model->degrees = 0;
model->flip = SDL_FLIP_NONE;
model->fixed = false;
free(model->subclass);
model->subclass = NULL;
model->renderLayer = 0;
}
/** \brief loads a C2DModel from a file
*
* \param model - c2DModel you want the data to be loaded to
* \param filepath - where to get the file from
*/
void importC2DModel(c2DModel* model, char* filepath)
{ //each model->something = strtok() is a field from the data, in the order called
char* data = calloc(2048, sizeof(char));
readLine(filepath, 0, 2048, &data);
model->numSprites = strtol(strtok(data, "{,}"), NULL, 10);
model->rect.x = strtod(strtok(NULL, "{,}"), NULL);
model->rect.y = strtod(strtok(NULL, "{,}"), NULL);
model->rect.w = strtod(strtok(NULL, "{,}"), NULL);
model->rect.h = strtod(strtok(NULL, "{,}"), NULL);
model->center.x = strtod(strtok(NULL, "{,}"), NULL);
model->center.y = strtod(strtok(NULL, "{,}"), NULL);
model->scale = strtod(strtok(NULL, "{,}"), NULL);
model->flip = strtol(strtok(NULL, "{,}"), NULL, 10);
model->degrees = strtod(strtok(NULL, "{,}"), NULL);
model->renderLayer = strtol(strtok(NULL, "{,}"), NULL, 10);
model->fixed = strtol(strtok(NULL, "{,}"), NULL, 10);
model->sprites = calloc(model->numSprites, sizeof(cSprite));
//Get the individual sprite data
for(int i = 0; i < model->numSprites; i++)
{
readLine(filepath, i + 1, 2048, &data);
strncpy(model->sprites[i].textureFilepath, strtok(data, "{,}"), MAX_PATH);
if (i == 0) //if the first sprite
loadIMG(model->sprites[i].textureFilepath, &(model->sprites[i].texture)); //load its texture
else
{ //after the first sprite
model->sprites[i].texture = NULL;
for(int x = i - 1; x >= 0; x--)
{
if (!strcmp(model->sprites[i].textureFilepath, model->sprites[x].textureFilepath))
{ //if the texture used is the same as any of the others
model->sprites[i].texture = model->sprites[x].texture; //share the texture
break;
}
}
if (!model->sprites[i].texture) //if we didn't share a texture
loadIMG(model->sprites[i].textureFilepath, &(model->sprites[i].texture)); //load in the one that's supposed to go here
}
model->sprites[i].id = strtol(strtok(NULL, "{,}"), NULL, 10);
model->sprites[i].drawRect.x = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].drawRect.y = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].drawRect.w = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].drawRect.h = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].srcClipRect.x = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].srcClipRect.y = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].srcClipRect.w = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].srcClipRect.h = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].center.x = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].center.y = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].scale = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].flip = strtol(strtok(NULL, "{,}"), NULL, 10);
model->sprites[i].degrees = strtod(strtok(NULL, "{,}"), NULL);
model->sprites[i].renderLayer = strtol(strtok(NULL, "{,}"), NULL, 10);
model->sprites[i].fixed = strtol(strtok(NULL, "{,}"), NULL, 10);
model->sprites[i].subclass = NULL; //subclass data not stored
model->sprites[i].global = false; //global flag also not stored
}
free(data);
model->subclass = NULL; //subclass not stored
//sortCSpritesInModel(model); //sort for drawing efficiency
}
/** \brief converts a c2DModel into text and saves it to a file (not including subclass)
*
* \param model - c2DModel you want saved
* \param filepath - where to save the file to
*/
void exportC2DModel(c2DModel* model, char* filepath)
{
createFile(filepath);
char* data = calloc(2048, sizeof(char));
snprintf(data, 2048, "{%d,%f,%f,%f,%f,%f,%f,%f,%d,%f,%d,%d}", model->numSprites, model->rect.x, model->rect.y, model->rect.w,
model->rect.h, model->center.x, model->center.y, model->scale, model->flip, model->degrees,
model->renderLayer, model->fixed);
appendLine(filepath, data, true);
free(data);
for(int i = 0; i < model->numSprites; i++)
{
data = exportCSprite(model->sprites[i]);
appendLine(filepath, data, true);
free(data);
}
}
/** \brief Sorts a model's sprites by draw priority to optimize speed
*
* \param model - model you want sorted
*/
void sortCSpritesInModel(c2DModel* model)
{ //sorts from highest to lowest
//insertion sort (sorry)
for(int i = 1; i < model->numSprites; i++)
{
for(int j = i; j > 0; j--)
{
if (model->sprites[j].renderLayer > model->sprites[j - 1].renderLayer) //if they are not in highest to lowest
{ //swap
cSprite temp = model->sprites[j];
model->sprites[j] = model->sprites[j - 1];
model->sprites[j - 1] = temp;
}
}
}
//printf("------ %d\n", model->sprites[0].renderLayer);
}
//TODO: Sort by priority then draw
/** \brief draws a c2DModel to the screen
*
* \param model - c2DModel you want drawn
* \param camera - cCamera to be used for drawing
* \param update - if true, immediately presents renderer
*/
void drawC2DModel(c2DModel model, cCamera camera, bool update)
{
for(int priority = camera.renderLayers; priority >= 1; priority--)
{
for(int i = 0; i < model.numSprites; i++)
{
if (model.sprites[i].renderLayer == priority)
{
double scale = model.scale * model.sprites[i].scale * (model.fixed || model.sprites[i].fixed ? 1.0 : camera.zoom);
//old point definition
//cDoublePt point = {(model.sprites[i].drawRect.x + model.rect.x) * scale, (model.sprites[i].drawRect.y + model.rect.y) * scale};
//uncomment the last half to have model scaling center around the center pt rather than the x/y coords
cDoublePt point = {model.rect.x / camera.rect.w * global.windowW + (model.sprites[i].drawRect.x / camera.rect.w * global.windowW * model.scale * model.sprites[i].scale) /*- (model.sprites[i].drawRect.w * (model.scale * model.sprites[i].scale - 1)) / 2*/,
model.rect.y / camera.rect.h * global.windowH + (model.sprites[i].drawRect.y / camera.rect.h * global.windowH * model.scale * model.sprites[i].scale) /*- (model.sprites[i].drawRect.h * (model.scale * model.sprites[i].scale - 1)) / 2*/};
cDoublePt modelCtrPt = {(model.rect.x + model.center.x) / camera.rect.w * global.windowW/* - (model.sprites[i].drawRect.w * (model.scale * model.sprites[i].scale - 1)) / 2*/,
(model.rect.y + model.center.y) / camera.rect.h * global.windowH/* - (model.sprites[i].drawRect.h * (model.scale * model.sprites[i].scale - 1)) / 2*/};
//*
if (!(model.sprites[i].fixed || model.fixed))
{ //if we are able to relate it to the camera
//scale the points relative to the center of the window
point.x = (point.x - global.windowW / 2.0) * camera.zoom + global.windowW / 2.0;
point.y = (point.y - global.windowH / 2.0) * camera.zoom + global.windowH / 2.0;
modelCtrPt.x = (modelCtrPt.x - global.windowW / 2.0) * camera.zoom + global.windowW / 2.0;
modelCtrPt.y = (modelCtrPt.y - global.windowH / 2.0) * camera.zoom + global.windowH / 2.0;
}
//*/
//rotate first around the model center, then the individual sprite's center
point = rotatePoint(
rotatePoint(point, (cDoublePt) {point.x + model.sprites[i].center.x * scale, point.y + model.sprites[i].center.y * scale}, model.sprites[i].degrees),
modelCtrPt, model.degrees);
if (!(model.sprites[i].fixed || model.fixed))
{ //if we are able to relate it to the camera
//rotate the point around the camera
point = rotatePoint(point, (cDoublePt) {global.windowW / 2 , global.windowH / 2}, camera.degrees);
//apply the camera offset
point.x -= camera.rect.x * global.windowW / camera.rect.w;
point.y -= camera.rect.y * global.windowH / camera.rect.h;
}
SDL_RenderCopyExF(global.mainRenderer, model.sprites[i].texture, &((SDL_Rect) {model.sprites[i].srcClipRect.x, model.sprites[i].srcClipRect.y, model.sprites[i].srcClipRect.w, model.sprites[i].srcClipRect.h}),
&((SDL_FRect) {.x = point.x /*- ((model.sprites[i].drawRect.w / 2) * model.scale * model.sprites[i].scale * camera.zoom)*/, .y = point.y /*- ((model.sprites[i].drawRect.h / 2) * model.scale * model.sprites[i].scale * camera.zoom)*/,
.w = (model.sprites[i].drawRect.w / camera.rect.w * global.windowW) * scale, .h = (model.sprites[i].drawRect.h / camera.rect.h * global.windowH) * scale}),
model.sprites[i].degrees + model.degrees + (!model.sprites[i].fixed * camera.degrees),
&((SDL_FPoint) {0, 0}), model.flip == model.sprites[i].flip ? SDL_FLIP_NONE : (model.sprites[i].flip + model.flip) % 4);
if (update)
SDL_RenderPresent(global.mainRenderer);
/*model.sprites[i].drawRect.x += model.rect.x;
model.sprites[i].drawRect.y += model.rect.y;
drawCSprite(model.sprites[i], camera, update, model.fixed);
model.sprites[i].drawRect.x -= model.rect.x;
model.sprites[i].drawRect.y -= model.rect.y;*/
}
}
}
}
/** \brief initializes a cText object
*
* \param text - a pointer to your cText
* \param string - the string you want
* \param rect - cDoubleRect containing bounding box of text (x/y in camera coordinates, w and h in px)
* \param textColor - color of text
* \param bgColor - color of background box
* \param font - ptr to the cFont you wish to use. Pass NULL for default (global.mainFont)
* \param degrees - rotation angle in degrees
* \param flip - SDL_RenderFlip value
* \param fixed - if true, won't be affected by a scene's camera
* \param renderLayer - 0 - not drawn. 1-`renderLayers` - drawn. Lower number = drawn later
*/
void initCText(cText* text, char* str, cDoubleRect rect, double maxW, SDL_Color textColor, SDL_Color bgColor, cFont* font, double scale, SDL_RendererFlip flip, double degrees, bool fixed, int renderLayer)
{
text->str = calloc(strlen(str) + 1, sizeof(char));
if (text->str)
strncpy(text->str, str, strlen(str));
text->rect = rect;
text->maxW = maxW;
text->textColor = textColor;
text->bgColor = bgColor;
if (font != NULL)
{
text->font = malloc(sizeof(cFont));
initCFont(text->font, font->filepath, font->fontSize);
}
else
text->font = &(global.mainFont);
text->scale = scale;
text->flip = flip;
text->degrees = degrees;
text->fixed = fixed;
text->renderLayer = renderLayer;
//init text texture
int* wh = loadTextTexture(text->str, &text->texture, text->maxW, text->textColor, text->font->font, true);
text->rect.w = wh[0];
text->rect.h = wh[1];
}
/** \brief updates string in a cText obj
*
* \param text - cText to modify
* \param str - new string for the cText to display
*/
void updateCText(cText* text, char* str)
{
int safeLen = strlen(text->str);
if (strlen(str) > safeLen)
{
char* temp = calloc(strlen(str) + 1, sizeof(char));
if (temp != NULL)
{
free(text->str);
text->str = temp;
safeLen = strlen(str);
}
else
{
//oop
printf("updateCText() error: cannot calloc new string\n");
}
}
strncpy(text->str, str, safeLen);
//init new text texture
SDL_DestroyTexture(text->texture);
int* wh = loadTextTexture(text->str, &text->texture, text->maxW, text->textColor, text->font->font, true);
text->rect.w = wh[0];
text->rect.h = wh[1];
}
/** \brief clears out a cText and its memory
*
* \param text - cText pointer
*/
void destroyCText(cText* text)
{
free(text->str);
text->str = NULL;
text->rect = (cDoubleRect) {0, 0, 0, 0};
text->maxW = 0;
text->textColor = (SDL_Color) {0, 0, 0, 0};
text->bgColor = (SDL_Color) {0, 0, 0, 0};
text->scale = 1.0;
text->fixed = false;
text->renderLayer = 0;
SDL_DestroyTexture(text->texture);
}
/** \brief draws a cText to the screen
*
* \param text - cText you want drawn
* \param camera - cCamera to be used for drawing
* \param update - if true, immediately presents renderer
*/
void drawCText(cText text, cCamera camera, bool update)
{
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(global.mainRenderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(global.mainRenderer, text.textColor.r, text.textColor.g, text.textColor.b, text.textColor.a);
cDoublePt point = (cDoublePt) {text.rect.x / camera.rect.w * global.windowW, text.rect.y / camera.rect.h * global.windowH};
if (!text.fixed)
{
point = rotatePoint(point, (cDoublePt) {global.windowW / 2 - text.rect.w / 2, global.windowH / 2 - text.rect.h / 2}, camera.degrees);
point.x = point.x - (camera.rect.x * global.windowW / camera.rect.w);
point.y = point.y - (camera.rect.y * global.windowH / camera.rect.h);
}
SDL_RenderCopyExF(global.mainRenderer, text.texture, NULL, &((SDL_FRect) {point.x, point.y, text.rect.w * text.scale, text.rect.h * text.scale}),
text.degrees + !text.fixed * camera.degrees, NULL, text.flip);
SDL_SetRenderDrawColor(global.mainRenderer, r, g, b, a);
if (update)
SDL_RenderPresent(global.mainRenderer);
}
/** \brief Loads in an image resource
*
* \param res - cResource pointer
* \param subclass - struct containing your data
* \param drawingMethod - function pointer to your drawing method. Must have only one argument, which is your subclass
* \param drawingMethod - function pointer to your cleanup method. Must have only one argument, which is your subclass
* \param renderLayer - the layer it should be rendered on
*/
void initCResource(cResource* res, void* subclass, void (*drawingRoutine)(void*, cCamera), void (*cleanupRoutine)(void*), int renderLayer)
{
res->subclass = subclass;
res->drawingRoutine = drawingRoutine;
res->cleanupRoutine = cleanupRoutine;
res->renderLayer = renderLayer;
}
/** \brief draws a CResource
*
* \param res - pointer to your cResource
*/
void drawCResource(cResource* res, cCamera camera)
{
(*res->drawingRoutine)(res->subclass, camera);
}
/** \brief clears out a cResource and its memory
*
* \param res - cResource pointer
*/
void destroyCResource(cResource* res)
{
if (res->cleanupRoutine != NULL)
res->cleanupRoutine(res->subclass);
res->subclass = NULL;
res->drawingRoutine = NULL;
res->cleanupRoutine = NULL;
res->renderLayer = 0;
}
/** \brief initializes a cCamera and its memory
*
* \param camera - a cCamera pointer
* \param rect - the bounding rect of the camera
* \param degrees - angle of rotation in degrees
*/
void initCCamera(cCamera* camera, cDoubleRect rect, double zoom, double degrees, int renderLayers)
{
camera->rect = rect;
camera->zoom = zoom;
camera->degrees = degrees;
camera->renderLayers = renderLayers;
}
cDoublePt cWindowCoordToCameraCoord(cDoublePt pt, cCamera camera)
{
//add back the offsets
pt.x += (camera.rect.x * global.windowW / camera.rect.w);
pt.y += (camera.rect.y * global.windowH / camera.rect.h);
//rotate the point back around
pt = rotatePoint(pt, (cDoublePt) {global.windowW / 2, global.windowH / 2}, -1.0 * camera.degrees);
//un-zoom the points relative to the center of the window
pt.x = (pt.x - global.windowW / 2.0) / camera.zoom + global.windowW / 2.0;
pt.y = (pt.y - global.windowH / 2.0) / camera.zoom + global.windowH / 2.0;
//convert from px to camera scaling units
pt.x *= camera.rect.w / global.windowW;
pt.y *= camera.rect.h / global.windowH;
return pt;
}
cDoublePt cCameraCoordToWindowCoord(cDoublePt pt, cCamera camera)
{
//convert from camera scaling units to px
pt.x /= camera.rect.w / global.windowW;
pt.y /= camera.rect.h / global.windowH;
//zoom the points relative to the center of the window
pt.x = (pt.x - global.windowW / 2.0) * camera.zoom + global.windowW / 2.0;
pt.y = (pt.y - global.windowH / 2.0) * camera.zoom + global.windowH / 2.0;
//rotate the point around
pt = rotatePoint(pt, (cDoublePt) {global.windowW / 2, global.windowH / 2}, camera.degrees);
//subtract out the offsets
pt.x -= (camera.rect.x * global.windowW / camera.rect.w);
pt.y -= (camera.rect.y * global.windowH / camera.rect.h);
return pt;
}
/** \brief clears out a cCamera and its memory
*
* \param camera - a cCamera pointer
*/
void destroyCCamera(cCamera* camera)
{
camera->rect = (cDoubleRect) {0, 0, 0, 0};
camera->zoom = 1.0;
camera->degrees = 0.0;
}
/** \brief Initializes a cScene object.
*
* \param scenePtr - pointer to your cScene
* \param sprites[] - array of cSprites to be drawn
* \param spriteCount - how many elements in sprites[]
* \param models[] - array of c2DModels to be drawn
* \param modelCount - how many elements in model[]
* \param resources[] - array of cResources
* \param resCount - how many elements in resources[]
* \param strings[] - array of cTexts
* \param stringCount - how many elements in strings[]
*/
void initCScene(cScene* scenePtr, SDL_Color bgColor, cCamera* camera, cSprite* sprites[], int spriteCount, c2DModel* models[], int modelCount, cResource* resources[], int resCount, cText* strings[], int stringCount)
{
scenePtr->camera = camera;
scenePtr->bgColor = bgColor;
if (spriteCount > 0 && sprites != NULL)
{
scenePtr->sprites = calloc(spriteCount, sizeof(cSprite*));
for(int i = 0; i < spriteCount; i++)
scenePtr->sprites[i] = sprites[i];
scenePtr->spriteCount = spriteCount;
}
else
{
scenePtr->sprites = NULL;
scenePtr->spriteCount = 0;
}
if (modelCount > 0 && models != NULL)
{
scenePtr->models = calloc(modelCount, sizeof(c2DModel*));
for(int i = 0; i < modelCount; i++)
scenePtr->models[i] = models[i];
scenePtr->modelCount = modelCount;
}
else
{
scenePtr->models = NULL;
scenePtr->modelCount = 0;
}
if (resCount > 0 && resources != NULL)
{
scenePtr->resources = calloc(resCount, sizeof(cResource*));
for(int i = 0; i < resCount; i++)
scenePtr->resources[i] = resources[i];
scenePtr->resCount = resCount;
}
else
{
scenePtr->resources = NULL;
scenePtr->resCount = 0;
}
if (stringCount > 0 && strings != NULL)
{
scenePtr->strings = calloc(stringCount, sizeof(cText*));
for(int i = 0; i < stringCount; i++)
scenePtr->strings[i] = strings[i];
scenePtr->stringCount = stringCount;
}
else
{
scenePtr->strings = NULL;
scenePtr->stringCount = 0;
}
}
/** \brief Adds a cSprite to a scene.
*
* \param scenePtr - pointer to a cScene
* \param sprite - pointer to a sprite
* \return true if succeeded, false if not
*/
int addSpriteToCScene(cScene* scenePtr, cSprite* sprite)
{
bool success = true;
cSprite** tempSprites = NULL;
if (scenePtr->spriteCount == 0)
tempSprites = calloc(scenePtr->spriteCount + 1, sizeof(cSprite*));
else
tempSprites = realloc(scenePtr->sprites, (scenePtr->spriteCount + 1) * sizeof(cSprite*));
if (tempSprites != NULL)
{
scenePtr->sprites = tempSprites;
scenePtr->sprites[scenePtr->spriteCount] = sprite;
scenePtr->spriteCount++;
}
else
{
printf("addSpriteToCScene: Failed to resize sprites array (attempted size %d)", scenePtr->spriteCount + 1);
success = false;
}
return success;
}
//fix the rest of the add methods
/** \brief Removes a specified sprite, or the sprite in the given index from a scene.
*
* \param scenePtr - pointer to a cScene
* \param sprite - pointer to the sprite to match addresses (NULL to use index instead)
* \param index - where in the sprite array the sprite is (-1 to make sure it is not used)
* \param free - if true, cleans up the referenced sprite from memory
* \return true if succeeded, false if not
*/
int removeSpriteFromCScene(cScene* scenePtr, cSprite* sprite, int index, bool free)
{
bool success = true;
if (sprite != NULL)
{
index = -1;
for(int i = 0; i < scenePtr->spriteCount; i++)
{
if (scenePtr->sprites[i] == sprite)
index = i;
}
}
if (index >= 0)
{
if (free)
destroyCSprite(scenePtr->sprites[index]);
scenePtr->sprites[index] = NULL;
for(int i = index; i < scenePtr->spriteCount - 1; i++)
scenePtr->sprites[i] = scenePtr->sprites[i + 1];
scenePtr->spriteCount--;
}
else
{
printf("removeSpriteFromCScene: Failed to find sprite\n");
success = false;
}
return success;
}
/** \brief Adds a c2DModel to a scene.
*
* \param scenePtr - pointer to a cScene
* \param model - pointer to a c2DModel
* \return true if succeeded, false if not
*/
int add2DModelToCScene(cScene* scenePtr, c2DModel* model)
{
bool success = true;
c2DModel** tempModels;
if (scenePtr->modelCount == 0)
tempModels = calloc(scenePtr->modelCount + 1, sizeof(c2DModel*));
else
tempModels = realloc(scenePtr->models, (scenePtr->modelCount + 1) * sizeof(c2DModel*));
if (tempModels != NULL)
{
scenePtr->models = tempModels;
scenePtr->models[scenePtr->modelCount] = model;
scenePtr->modelCount++;
}
else
{
printf("add2DModelToCScene: Failed to resize models array (attempted size %d)", scenePtr->modelCount + 1);
success = false;
}
return success;
}
/** \brief Removes a specified model, or the model in the given index from a scene.
*
* \param scenePtr - pointer to a cScene
* \param model - pointer to the model to match addresses (NULL to use index instead)
* \param index - where in the sprite array the sprite is (-1 to make sure it is not used)
* \param free - if true, cleans up the referenced model from memory
* \return true if succeeded, false if not
*/
int remove2DModelFromCScene(cScene* scenePtr, c2DModel* model, int index, bool free)
{
bool success = true;
if (model != NULL)
{
index = -1;
for(int i = 0; i < scenePtr->modelCount; i++)
{
if (scenePtr->models[i] == model)
index = i;
}
}
if (index >= 0)
{
if (free)
destroyC2DModel(scenePtr->models[index]);
scenePtr->models[index] = NULL;
for(int i = index; i < scenePtr->modelCount - 1; i++)
scenePtr->models[i] = scenePtr->models[i + 1];
scenePtr->modelCount--;
}
else
{
printf("remove2DModelFromCScene: Failed to find model\n");
success = false;
}
return success;
}
/** \brief Adds a cText to a specified scene.
*
* \param scenePtr - pointer to a cScene
* \param text- pointer to a cText
* \return true if succeeded, false if not
*/
int addTextToCScene(cScene* scenePtr, cText* text)
{
bool success = true;
cText** tempTexts;
if (scenePtr->stringCount == 0)
tempTexts = calloc(scenePtr->stringCount + 1, sizeof(cText*));
else
tempTexts = realloc(scenePtr->strings, scenePtr->stringCount * sizeof(cText*));
if (tempTexts != NULL)
{
scenePtr->strings = tempTexts;
scenePtr->strings[scenePtr->stringCount] = text;
scenePtr->stringCount++;
}
else
{
printf("addTextToCScene: Failed to resize strings array (attempted size %d)", scenePtr->stringCount + 1);
success = false;
}
return success;
}
/** \brief Removes a text, or the text in a given index from a scene.
*
* \param scenePtr - pointer to a cScene
* \param text - pointer to the text to match addresses (NULL to use index instead)
* \param index - where in the sprite array the sprite is (-1 to make sure it is not used)
* \param free - if true, cleans up the referenced text from memory
* \return true if succeeded, false if not
*/
int removeTextFromCScene(cScene* scenePtr, cText* text, int index, bool free)
{
bool success = true;
if (text != NULL)
{
index = -1;
for(int i = 0; i < scenePtr->stringCount; i++)
{
if (scenePtr->strings[i] == text)
index = i;
}
}
if (index >= 0)
{
if (free)
destroyCText(scenePtr->strings[index]);
scenePtr->strings[index] = NULL;
for(int i = index; i < scenePtr->stringCount - 1; i++)
scenePtr->strings[i] = scenePtr->strings[i + 1];
scenePtr->stringCount--;
}
else
{
printf("removeTextFromCScene: Failed to find text\n");
success = false;
}
return success;
}
/** \brief Adds a cResource to the specified scene.
*
* \param scenePtr - pointer to a cScene
* \param resource - pointer to a cResource
* \return true if succeeded, false if not
*/
int addResourceToCScene(cScene* scenePtr, cResource* resource)
{
bool success = true;
cResource** tempResources;
if (scenePtr->resCount == 0)
tempResources = calloc(scenePtr->resCount + 1, sizeof(cResource*));
else
tempResources = realloc(scenePtr->resources, (scenePtr->resCount + 1) * sizeof(cResource*));
if (tempResources != NULL)
{
scenePtr->resources = tempResources;
scenePtr->resources[scenePtr->resCount] = resource;
scenePtr->resCount++;
}
else
{
printf("addResourceToCScene: Failed to resize resources array (attempted size %d)", scenePtr->resCount--);
success = false;
}
return success;
}
/** \brief Removes a resource, or the resource in a given index from a scene.
*
* \param scenePtr - pointer to a cScene
* \param resource - pointer to the resource to match addresses (NULL to use index instead)
* \param index - where in the sprite array the sprite is (-1 to make sure it is not used)
* \param free - if true, cleans up the referenced resource from memory
* \return true if succeeded, false if not
*/
int removeResourceFromCScene(cScene* scenePtr, cResource* resource, int index, bool free)
{
bool success = true;
if (resource != NULL)
{
index = -1;
for(int i = 0; i < scenePtr->resCount; i++)
{
if (scenePtr->resources[i] == resource)
index = i;
}
}
if (index >= 0)
{
if (free)
destroyCResource(scenePtr->resources[index]);
scenePtr->resources[index] = NULL;
for(int i = index; i < scenePtr->resCount - 1; i++)
scenePtr->resources[i] = scenePtr->resources[i + 1];
scenePtr->resCount--;
}
else
{
printf("removeResourceFromCScene: Failed to find resource\n");
success = false;
}
return success;
}
/** \brief clears out a cScene and all its memory, including sprites.
*
* \param scenePtr cScene* - pointer to your cScene
*/
void destroyCScene(cScene* scenePtr)
{
destroyCCamera(scenePtr->camera);
if (scenePtr->spriteCount > 0)
{
for(int i = 0; i < scenePtr->spriteCount; i++)
{
if (!scenePtr->sprites[i]->global)
destroyCSprite(scenePtr->sprites[i]);
}
scenePtr->spriteCount = 0;
free(scenePtr->sprites);
}
if (scenePtr->modelCount > 0)
{