-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
engine.c
1235 lines (1051 loc) · 32.3 KB
/
engine.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
internal void
getColorComponents(uint32_t color, uint8_t *r, uint8_t *g, uint8_t *b) {
*r = color & 0xFF;
*g = (color & (0xFF << 8)) >> 8;
*b = (color & (0xFF << 16)) >> 16;
}
internal void
ENGINE_openLogFile(ENGINE* engine) {
char* filename = "DOME-out.log";
engine->debug.logFile = fopen(filename, "w+");
}
internal void
ENGINE_writeToLogFile(ENGINE* engine, const char* buffer) {
if (engine->debug.logFile == NULL) {
ENGINE_openLogFile(engine);
}
if (engine->debug.logFile != NULL) {
// Output to file
fputs(buffer, engine->debug.logFile);
fflush(engine->debug.logFile);
}
}
internal void
ENGINE_writeToLog(ENGINE* engine, const char* buffer) {
// Output to console
printf("%s", buffer);
ENGINE_writeToLogFile(engine, buffer);
}
#define RENDER_VARIADIC_STRING(buffer, line, argList) \
va_list args;\
va_copy(args, argList);\
size_t bufSize = vsnprintf(NULL, 0, line, args) + 1;\
va_end(args);\
char buffer[bufSize];\
buffer[0] = '\0';\
va_copy(args, argList);\
vsnprintf(buffer, bufSize, line, args);\
va_end(args);
internal void
ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) {
RENDER_VARIADIC_STRING(buffer, line, argList);
ENGINE_writeToLog(engine, buffer);
}
internal void
ENGINE_printLog(ENGINE* engine, char* line, ...) {
// Args is mutated by each vsnprintf call,
// so it needs to be reinitialised.
va_list args;
va_start(args, line);
ENGINE_printLogVariadic(engine, line, args);
va_end(args);
}
internal ENGINE_WRITE_RESULT
ENGINE_writeFile(ENGINE* engine, const char* path, const char* buffer, size_t length) {
const char* fullPath;
if (path[0] != '/') {
const char* base = BASEPATH_get();
fullPath = malloc(strlen(base)+strlen(path)+1);
strcpy((void*)fullPath, base); /* copy name into the new var */
strcat((void*)fullPath, path); /* add the extension */
} else {
fullPath = path;
}
ENGINE_printLog(engine, "Writing to filesystem: %s\n", path);
int result = writeEntireFile(fullPath, buffer, length);
if (result == ENOENT) {
result = ENGINE_WRITE_PATH_INVALID;
} else {
result = ENGINE_WRITE_SUCCESS;
}
if (path[0] != '/') {
free((void*)fullPath);
}
return result;
}
internal char*
ENGINE_readFile(ENGINE* engine, const char* path, size_t* lengthPtr, char** reason) {
char pathBuf[PATH_MAX];
if (strncmp(path, "./", 2) == 0) {
strcpy(pathBuf, path + 2);
} else {
strcpy(pathBuf, path);
}
if (engine->tar != NULL) {
ENGINE_printLog(engine, "Reading from bundle: %s\n", pathBuf);
char* file = NULL;
int err = readFileFromTar(engine->tar, pathBuf, lengthPtr, &file);
if (err == MTAR_ESUCCESS) {
return file;
}
const char* message = mtar_strerror(err);
if (DEBUG_MODE) {
ENGINE_printLog(engine, "Couldn't read %s from bundle: %s. Falling back\n", pathBuf, message);
}
}
if (path[0] != '/') {
strcpy(pathBuf, BASEPATH_get());
strcat(pathBuf, path);
}
#ifdef __EMSCRIPTEN__
emscripten_wget(pathBuf, pathBuf);
#endif
if (!doesFileExist(pathBuf)) {
if (reason != NULL) {
strncpy(*reason, "File doesn't exist", 1024);
}
return NULL;
}
ENGINE_printLog(engine, "Reading from filesystem: %s\n", pathBuf);
return readEntireFile(pathBuf, lengthPtr, reason);
}
internal bool
ENGINE_fileExists(ENGINE* engine, const char* path) {
char pathBuf[PATH_MAX];
pathBase(path, pathBuf);
return (fileInfo(pathBuf) == 1) ? true : false;
}
internal bool
ENGINE_directoryExists(ENGINE* engine, const char* path) {
char pathBuf[PATH_MAX];
pathBase(path, pathBuf);
return (fileInfo(pathBuf) == 2) ? true : false;
}
internal int
ENGINE_taskHandler(ABC_TASK* task) {
if (task->type == TASK_PRINT) {
printf("%s\n", (char*)task->data);
task->resultCode = 0;
// TODO: Push to SDL Event Queue
} else if (task->type == TASK_LOAD_FILE) {
FILESYSTEM_loadEventHandler(task->data);
} else if (task->type == TASK_WRITE_FILE) {
}
return 0;
}
internal bool
ENGINE_setupRenderer(ENGINE* engine, bool vsync) {
engine->vsyncEnabled = vsync;
if (engine->renderer != NULL) {
SDL_DestroyRenderer(engine->renderer);
}
int flags = SDL_RENDERER_ACCELERATED;
if (vsync) {
flags |= SDL_RENDERER_PRESENTVSYNC;
}
engine->renderer = SDL_CreateRenderer(engine->window, -1, flags);
if (engine->renderer == NULL) {
return false;
}
SDL_RenderSetLogicalSize(engine->renderer, engine->canvas.width, engine->canvas.height);
engine->texture = SDL_CreateTexture(engine->renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, engine->canvas.width, engine->canvas.height);
if (engine->texture == NULL) {
return false;
}
SDL_RenderGetViewport(engine->renderer, &(engine->viewport));
return true;
}
internal ENGINE*
ENGINE_init(ENGINE* engine) {
engine->handleText = true;
engine->fused = false;
engine->window = NULL;
engine->renderer = NULL;
engine->texture = NULL;
engine->blitBuffer.pixels = calloc(0, 0);
engine->blitBuffer.width = 0;
engine->blitBuffer.height = 0;
engine->lockstep = false;
engine->debug.avgFps = 58;
engine->debugEnabled = false;
engine->debug.alpha = 0.9;
engine->debug.errorBufMax = 0;
engine->debug.errorBuf = NULL;
engine->debug.errorBufLen = 0;
engine->debug.errorDialog = true;
engine->logLevel = 4; // INFO
engine->logColor = isatty(STDOUT_FILENO);
engine->padding = 5;
// Initialise the canvas offset.
engine->canvas.pixels = NULL;
engine->canvas.offsetX = 0;
engine->canvas.offsetY = 0;
engine->canvas.width = GAME_WIDTH;
engine->canvas.height = GAME_HEIGHT;
DOME_RECT clip = {
.x = 0,
.y = 0,
.w = GAME_WIDTH,
.h = GAME_HEIGHT
};
engine->canvas.clip = clip;
engine->argv = NULL;
engine->argc = 0;
// TODO: handle if we can't allocate memory.
PLUGIN_COLLECTION_init(engine);
return engine;
}
internal int
ENGINE_start(ENGINE* engine) {
int result = EXIT_SUCCESS;
uint32_t windowFlags = SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE;
#if defined (_WIN32) || defined (__MINGW32__)
SDL_setenv("SDL_AUDIODRIVER", "directsound", true);
#else
windowFlags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
ENGINE_printLog(engine, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
result = EXIT_FAILURE;
goto engine_init_end;
}
//Create window
engine->window = SDL_CreateWindow("DOME", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, windowFlags);
if(engine->window == NULL)
{
char* message = "Window could not be created! SDL_Error: %s\n";
ENGINE_printLog(engine, message, SDL_GetError());
result = EXIT_FAILURE;
goto engine_init_end;
}
ENGINE_setupRenderer(engine, true);
if (engine->renderer == NULL)
{
char* message = "Could not create a renderer: %s";
ENGINE_printLog(engine, message, SDL_GetError());
result = EXIT_FAILURE;
goto engine_init_end;
}
engine->canvas.pixels = calloc(engine->canvas.width * engine->canvas.height, sizeof(char) * 4);
if (engine->canvas.pixels == NULL) {
result = EXIT_FAILURE;
goto engine_init_end;
}
engine->audioEngine = AUDIO_ENGINE_init();
if (engine->audioEngine == NULL) {
result = EXIT_FAILURE;
goto engine_init_end;
}
SDL_StopTextInput();
ENGINE_EVENT_TYPE = SDL_RegisterEvents(1);
ABC_FIFO_create(&engine->fifo);
engine->fifo.taskHandler = ENGINE_taskHandler;
MAP_init(&engine->moduleMap);
engine->running = true;
engine_init_end:
return result;
}
internal void
ENGINE_finishAsync(ENGINE* engine) {
if (!engine->fifo.shutdown) {
ABC_FIFO_close(&engine->fifo);
}
}
internal void
ENGINE_free(ENGINE* engine) {
if (engine == NULL) {
return;
}
SDL_StopTextInput();
ENGINE_finishAsync(engine);
PLUGIN_COLLECTION_free(engine);
if (engine->audioEngine) {
AUDIO_ENGINE_free(engine->audioEngine);
free(engine->audioEngine);
engine->audioEngine = NULL;
}
if (engine->tar != NULL) {
mtar_close(engine->tar);
}
if (engine->moduleMap.head != NULL) {
MAP_free(&engine->moduleMap);
}
if (engine->blitBuffer.pixels != NULL) {
free(engine->blitBuffer.pixels);
}
if (engine->canvas.pixels != NULL) {
free(engine->canvas.pixels);
}
if (engine->texture != NULL) {
SDL_DestroyTexture(engine->texture);
}
if (engine->renderer != NULL) {
SDL_DestroyRenderer(engine->renderer);
}
if (engine->window != NULL) {
SDL_DestroyWindow(engine->window);
}
if (engine->mouse.cursor != NULL) {
SDL_FreeCursor(engine->mouse.cursor);
}
if (engine->argv != NULL) {
free(engine->argv[1]);
free(engine->argv);
}
// DEBUG features
if (engine->debug.logFile != NULL) {
fclose(engine->debug.logFile);
}
if (engine->debug.errorBuf != NULL) {
free(engine->debug.errorBuf);
}
}
internal uint32_t
ENGINE_pget(ENGINE* engine, int64_t x, int64_t y) {
int32_t width = engine->canvas.width;
int32_t height = engine->canvas.height;
if (0 <= x && x < width && 0 <= y && y < height) {
return ((uint32_t*)(engine->canvas.pixels))[width * y + x];
}
return 0xFF000000;
}
inline internal void
ENGINE_pset(ENGINE* engine, int64_t x, int64_t y, uint32_t c) {
CANVAS canvas = engine->canvas;
// Account for canvas offset
x += canvas.offsetX;
y += canvas.offsetY;
// Draw pixel at (x,y)
int32_t width = canvas.width;
DOME_RECT zone = canvas.clip;
uint8_t newA = ((0xFF000000 & c) >> 24);
if (newA == 0) {
return;
} else if (zone.x <= x && x < zone.x + zone.w && zone.y <= y && y < zone.y + zone.h) {
if (newA < 0xFF) {
uint32_t current = ((uint32_t*)(canvas.pixels))[width * y + x];
double normA = newA / (double)UINT8_MAX;
double diffA = 1 - normA;
uint8_t oldR, oldG, oldB, newR, newG, newB;
getColorComponents(current, &oldR, &oldG, &oldB);
getColorComponents(c, &newR, &newG, &newB);
uint8_t a = 0xFF;
uint8_t r = (diffA * oldR + normA * newR);
uint8_t g = (diffA * oldG + normA * newG);
uint8_t b = (diffA * oldB + normA * newB);
c = (a << 24) | (b << 16) | (g << 8) | r;
}
// This is a very hot line, so we use pointer arithmetic for
// speed!
*(((uint32_t*)canvas.pixels) + (width * y + x)) = c;
}
}
internal void
ENGINE_unsafePsetNoBlend(ENGINE* engine, int64_t x, int64_t y, uint32_t c) {
CANVAS canvas = engine->canvas;
// Draw pixel at (x,y)
// This is a very hot line, so we use pointer arithmetic for
// speed!
*(((uint32_t*)canvas.pixels) + (canvas.width * y + x)) = c;
}
internal void
ENGINE_unsafePset(ENGINE* engine, int64_t x, int64_t y, uint32_t c) {
CANVAS canvas = engine->canvas;
// Draw pixel at (x,y)
int32_t width = canvas.width;
uint8_t newA = ((0xFF000000 & c) >> 24);
uint32_t current = ((uint32_t*)(canvas.pixels))[width * y + x];
double normA = newA / (double)UINT8_MAX;
double diffA = 1 - normA;
uint8_t oldR, oldG, oldB, newR, newG, newB;
getColorComponents(current, &oldR, &oldG, &oldB);
getColorComponents(c, &newR, &newG, &newB);
uint8_t a = 0xFF;
uint8_t r = (diffA * oldR + normA * newR);
uint8_t g = (diffA * oldG + normA * newG);
uint8_t b = (diffA * oldB + normA * newB);
c = (a << 24) | (b << 16) | (g << 8) | r;
// This is a very hot line, so we use pointer arithmetic for
// speed!
*(((uint32_t*)canvas.pixels) + (width * y + x)) = c;
}
internal void
ENGINE_blitBuffer(ENGINE* engine, int32_t x, int32_t y) {
PIXEL_BUFFER buffer = engine->blitBuffer;
uint32_t* blitBuffer = buffer.pixels;
for (size_t j = 0; j < buffer.height; j++) {
for (size_t i = 0; i < buffer.width; i++) {
uint32_t c = *(blitBuffer + (j * buffer.width + i));
ENGINE_pset(engine, x + i, y + j, c);
}
}
}
internal uint32_t*
ENGINE_resizeBlitBuffer(ENGINE* engine, size_t width, size_t height) {
PIXEL_BUFFER* buffer = &engine->blitBuffer;
size_t oldBufferSize = buffer->width * buffer->height * sizeof(uint32_t);
size_t newBufferSize = width * height * sizeof(uint32_t);
if (oldBufferSize < newBufferSize) {
buffer->pixels = realloc(buffer->pixels, newBufferSize);
oldBufferSize = newBufferSize;
}
memset(buffer->pixels, 0, oldBufferSize);
buffer->width = width;
buffer->height = height;
return buffer->pixels;
}
internal unsigned const char*
defaultFontLookup(utf8_int32_t codepoint) {
const local_persist unsigned char empty[8] = { 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };
if (codepoint >= 0 && codepoint < 0x7F) {
return font8x8_basic[codepoint];
} else if (codepoint >= 0x80 && codepoint <= 0x9F) {
codepoint = codepoint - 0x80;
return font8x8_control[codepoint];
} else if (codepoint >= 0xA0 && codepoint <= 0xFF) {
codepoint = codepoint - 0xA0;
return font8x8_ext_latin[codepoint];
} else if (codepoint >= 0x390 && codepoint <= 0x3C9) {
codepoint = codepoint - 0x390;
return font8x8_greek[codepoint];
} else if (codepoint >= 0x2500 && codepoint <= 0x257F) {
codepoint = codepoint - 0x2500;
return font8x8_box[codepoint];
} else if (codepoint >= 0x2580 && codepoint <= 0x259F) {
codepoint = codepoint - 0x2580;
return font8x8_block[codepoint];
} else if (codepoint >= 0x3040 && codepoint <= 0x309F) {
codepoint = codepoint - 0x3040;
return font8x8_hiragana[codepoint];
} else if (codepoint >= 0xE541 && codepoint <= 0xE55A) {
codepoint = codepoint - 0xE541;
return font8x8_sga[codepoint];
} else {
return empty;
}
}
internal void
ENGINE_print(ENGINE* engine, char* text, int64_t x, int64_t y, uint32_t c) {
int fontWidth = 8;
int fontHeight = 8;
int spacing = (fontHeight / 4);
int cursor = 0;
utf8_int32_t codepoint;
void* v = utf8codepoint(text, &codepoint);
size_t len = utf8len(text);
for (size_t pos = 0; pos < len; pos++) {
if (text[pos] == '\n') {
cursor = 0;
y += fontHeight + spacing;
v = utf8codepoint(v, &codepoint);
continue;
}
uint8_t* glyph = (uint8_t*)defaultFontLookup(codepoint);
for (int j = 0; j < fontHeight; j++) {
for (int i = 0; i < fontWidth; i++) {
uint8_t v = (glyph[j] >> i) & 1;
if (v != 0) {
ENGINE_pset(engine, x + cursor + i, y + j, c);
}
}
}
cursor += fontWidth;
v = utf8codepoint(v, &codepoint);
}
}
internal void
blitPixel(void* dest, size_t pitch, int64_t x, int64_t y, uint32_t c) {
uint32_t* pixel = (uint32_t*)dest + (y * pitch + x);
*pixel = c;
}
internal void
blitLine(void* dest, size_t destPitch, int64_t x, int64_t y, int64_t w, uint32_t* buf) {
size_t pitch = destPitch;
char* pixels = dest;
int64_t startX = mid(0, x, pitch);
int64_t endX = mid(0, x + w, pitch);
size_t lineWidth = endX - startX;
uint32_t* bufStart = buf + (size_t)fabs(fmin(0, x));
char* line = pixels + ((y * pitch + startX) * 4);
memcpy(line, bufStart, lineWidth * 4);
}
internal void
ENGINE_blitLine(ENGINE* engine, int64_t x, int64_t y, int64_t w, uint32_t* buf) {
CANVAS canvas = engine->canvas;
DOME_RECT zone = canvas.clip;
y += canvas.offsetY;
if (y < max(0, zone.y) || y >= min(canvas.height, zone.y + zone.h)) {
return;
}
int64_t end = zone.w + zone.x;
int64_t offsetX = canvas.offsetX;
int64_t pitch = canvas.width;
char* pixels = canvas.pixels;
int64_t screenX = x + offsetX;
uint32_t* bufStart = buf;
int64_t readX = 0;
if (screenX < zone.x) {
readX += (zone.x - screenX);
}
int64_t readLength = mid(0, w - readX, w);
int64_t writeX = mid(zone.x, screenX, end);
int64_t writeLength = readLength;
if (screenX > end - readLength) {
writeLength -= (screenX + readLength) - end;
}
writeLength = mid(0, writeLength, zone.w);
writeLength = max(0, writeLength);
bufStart += readX;
char* line = pixels + ((y * pitch + writeX) * 4);
memcpy(line, bufStart, writeLength * 4);
}
internal uint32_t*
ENGINE_createMask(ENGINE* engine, uint64_t originalSize, uint32_t color) {
uint64_t size = originalSize;
uint32_t* mask = ENGINE_resizeBlitBuffer(engine, size, size);
for (uint64_t y = 0; y < size; y++) {
for (uint64_t x = 0; x < size; x++) {
blitPixel(mask, size, x, y, color);
}
}
return mask;
}
internal void
ENGINE_drawMask(ENGINE* engine, int64_t x, int64_t y) {
size_t offset = engine->blitBuffer.width / 2;
ENGINE_blitBuffer(engine, x - offset, y - offset);
}
internal void
ENGINE_line_high(ENGINE* engine, int64_t x1, int64_t y1, int64_t x2, int64_t y2, uint32_t c) {
int64_t dx = x2 - x1;
int64_t dy = y2 - y1;
int64_t xi = 1;
if (dx < 0) {
xi = -1;
dx = -dx;
}
int64_t p = 2 * dx - dy;
int64_t y = y1;
int64_t x = x1;
while(y <= y2) {
ENGINE_drawMask(engine, x, y);
if (p > 0) {
x += xi;
p = p - 2 * dy;
}
p = p + 2 * dx;
y++;
}
}
internal void
ENGINE_line_low(ENGINE* engine, int64_t x1, int64_t y1, int64_t x2, int64_t y2, uint32_t c) {
int64_t dx = x2 - x1;
int64_t dy = y2 - y1;
int64_t yi = 1;
if (dy < 0) {
yi = -1;
dy = -dy;
}
int64_t p = 2 * dy - dx;
int64_t y = y1;
int64_t x = x1;
while(x <= x2) {
ENGINE_drawMask(engine, x, y);
if (p > 0) {
y += yi;
p = p - 2 * dx;
}
p = p + 2 * dy;
x++;
}
}
internal void
ENGINE_line(ENGINE* engine, int64_t x1, int64_t y1, int64_t x2, int64_t y2, uint32_t c, uint64_t size) {
ENGINE_createMask(engine, size, c);
if (llabs(y2 - y1) < llabs(x2 - x1)) {
if (x1 > x2) {
ENGINE_line_low(engine, x2, y2, x1, y1, c);
} else {
ENGINE_line_low(engine, x1, y1, x2, y2, c);
}
} else {
if (y1 > y2) {
ENGINE_line_high(engine, x2, y2, x1, y1, c);
} else {
ENGINE_line_high(engine, x1, y1, x2, y2, c);
}
}
}
internal void
ENGINE_circle_filled(ENGINE* engine, int64_t x0, int64_t y0, int64_t r, uint32_t c) {
int64_t x = 0;
int64_t y = r;
int64_t d = round(M_PI - (2*r));
uint16_t alpha = (0xFF000000 & c) >> 24;
size_t bufWidth = r * 2 + 1;
uint32_t buf[bufWidth];
for (size_t i = 0; i < bufWidth; i++) {
buf[i] = c;
}
if (alpha == 0xFF) {
while (x <= y) {
size_t lineWidthX = x * 2 + 1;
size_t lineWidthY = y * 2 + 1;
ENGINE_blitLine(engine, x0 - x, y0 + y, lineWidthX, buf);
ENGINE_blitLine(engine, x0 - x, y0 - y, lineWidthX, buf);
ENGINE_blitLine(engine, x0 - y, y0 + x, lineWidthY, buf);
ENGINE_blitLine(engine, x0 - y, y0 - x, lineWidthY, buf);
if (d < 0) {
d = d + (M_PI * x) + (M_PI * 2);
} else {
d = d + (M_PI * (x - y)) + (M_PI * 3);
y--;
}
x++;
}
} else {
uint32_t* blitBuffer = ENGINE_resizeBlitBuffer(engine, bufWidth, bufWidth);
size_t pitch = engine->blitBuffer.width;
while (x <= y) {
int64_t c = r;
size_t lineWidthX = x * 2 + 1;
size_t lineWidthY = y * 2 + 1;
blitLine(blitBuffer, pitch, c - x, c + y, lineWidthX, buf);
if (y != 0) {
blitLine(blitBuffer, pitch, c - x, c - y, lineWidthX, buf);
}
blitLine(blitBuffer, pitch, c - y, c + x, lineWidthY, buf);
if (x != 0) {
blitLine(blitBuffer, pitch, c - y, c - x, lineWidthY, buf);
}
if (d < 0) {
d = d + (M_PI * x) + (M_PI * 2);
} else {
d = d + (M_PI * (x - y)) + (M_PI * 3);
y--;
}
x++;
}
ENGINE_blitBuffer(engine, x0 - r, y0 - r);
}
}
internal void
ENGINE_circle(ENGINE* engine, int64_t x0, int64_t y0, int64_t r, uint32_t c) {
int64_t x = 0;
int64_t y = r;
int64_t d = round(M_PI - (2*r));
size_t pitch = r * 2 + 1;
uint32_t* blitBuffer = ENGINE_resizeBlitBuffer(engine, pitch, pitch);
pitch = engine->blitBuffer.width;
while (x <= y) {
blitPixel(blitBuffer, pitch, r + x, r + y , c);
blitPixel(blitBuffer, pitch, r - x, r - y , c);
blitPixel(blitBuffer, pitch, r - x, r + y , c);
blitPixel(blitBuffer, pitch, r + x, r - y , c);
blitPixel(blitBuffer, pitch, r + y, r + x , c);
blitPixel(blitBuffer, pitch, r - y, r - x , c);
blitPixel(blitBuffer, pitch, r - y, r + x , c);
blitPixel(blitBuffer, pitch, r + y, r - x , c);
if (d < 0) {
d = d + (M_PI * x) + (M_PI * 2);
} else {
d = d + (M_PI * (x - y)) + (M_PI * 3);
y--;
}
x++;
}
ENGINE_blitBuffer(engine, x0 - r, y0 - r);
}
internal inline double
ellipse_getRegion(double x, double y, int32_t rx, int32_t ry) {
double rxSquare = rx * rx;
double rySquare = ry * ry;
return (rySquare*x) / (rxSquare*y);
}
internal void
ENGINE_ellipsefill(ENGINE* engine, int64_t x0, int64_t y0, int64_t x1, int64_t y1, uint32_t c) {
// Calculate radius
int64_t swap = x1;
if (x1 < x0) {
x1 = x0;
x0 = swap;
}
swap = y1;
if (y1 < y0) {
y1 = y0;
y0 = swap;
}
int32_t rx = (x1 - x0) / 2; // Radius on x
int32_t ry = (y1 - y0) / 2; // Radius on y
uint32_t rxSquare = rx*rx;
uint32_t rySquare = ry*ry;
uint32_t rx2ry2 = rxSquare * rySquare;
int32_t dx = (rx + 1) * 2;
int32_t dy = (ry + 1) * 2;
size_t bufWidth = max(dx, dy);
uint32_t buf[bufWidth];
for (size_t i = 0; i < bufWidth; i++) {
buf[i] = c;
}
uint32_t* blitBuffer = ENGINE_resizeBlitBuffer(engine, dx, dy);
size_t pitch = engine->blitBuffer.width;
// Start drawing at (0, ry)
int32_t x = 0;
int32_t y = ry;
double d = 0;
while (fabs(ellipse_getRegion(x, y, rx, ry)) < 1) {
x++;
size_t lineWidthX = x * 2 + 1;
double xSquare = x*x;
// evaluate decision parameter
d = rySquare * xSquare + rxSquare * pow(y - 0.5, 2) - rx2ry2;
if (d > 0) {
y--;
}
blitLine(blitBuffer, pitch, rx - x, ry + y, lineWidthX, buf);
blitLine(blitBuffer, pitch, rx - x, ry - y, lineWidthX, buf);
}
while (y > 0) {
y--;
double ySquare = y*y;
// evaluate decision parameter
d = rxSquare * ySquare + rySquare * pow(x + 0.5, 2) - rx2ry2;
if (d <= 0) {
x++;
}
size_t lineWidthY = x * 2 + 1;
blitLine(blitBuffer, pitch, rx - x, ry + y, lineWidthY, buf);
blitLine(blitBuffer, pitch, rx - x, ry - y, lineWidthY, buf);
};
ENGINE_blitBuffer(engine, x0, y0);
}
internal void
ENGINE_ellipse(ENGINE* engine, int64_t x0, int64_t y0, int64_t x1, int64_t y1, uint32_t c) {
int64_t swap = x1;
if (x1 < x0) {
x1 = x0;
x0 = swap;
}
swap = y1;
if (y1 < y0) {
y1 = y0;
y0 = swap;
}
// Calculate radius
int32_t rx = llabs(x1 - x0) / 2; // Radius on x
int32_t ry = llabs(y1 - y0) / 2; // Radius on y
int32_t rxSquare = rx*rx;
int32_t rySquare = ry*ry;
int32_t rx2ry2 = rxSquare * rySquare;
// Start drawing at (0, ry)
double x = 0;
double y = ry;
double d = 0;
int32_t width = (rx + 1) * 2;
int32_t height = (ry + 1) * 2;
uint32_t* blitBuffer = ENGINE_resizeBlitBuffer(engine, width, height);
size_t pitch = engine->blitBuffer.width;
blitPixel(blitBuffer, pitch, rx + x, ry + y , c);
blitPixel(blitBuffer, pitch, rx + x, ry - y , c);
while (fabs(ellipse_getRegion(x, y, rx, ry)) < 1) {
x++;
double xSquare = x*x;
// evaluate decision parameter
d = rySquare * xSquare + rxSquare * pow(y - 0.5, 2) - rx2ry2;
if (d > 0) {
y--;
}
blitPixel(blitBuffer, pitch, rx + x, ry + y , c);
blitPixel(blitBuffer, pitch, rx - x, ry - y , c);
blitPixel(blitBuffer, pitch, rx - x, ry + y , c);
blitPixel(blitBuffer, pitch, rx + x, ry - y , c);
}
while (y > 0) {
y--;
double ySquare = y*y;
// evaluate decision parameter
d = rxSquare * ySquare + rySquare * pow(x + 0.5, 2) - rx2ry2;
if (d <= 0) {
x++;
}
blitPixel(blitBuffer, pitch, rx + x, ry + y , c);
blitPixel(blitBuffer, pitch, rx - x, ry - y , c);
blitPixel(blitBuffer, pitch, rx - x, ry + y , c);
blitPixel(blitBuffer, pitch, rx + x, ry - y , c);
};
ENGINE_blitBuffer(engine, x0, y0);
}
internal void
ENGINE_triangle(ENGINE* engine, float x0, float y0, float x1, float y1, float x2, float y2, uint32_t c) {
ENGINE_line(engine, x0, y0, x1, y1, c, 1);
ENGINE_line(engine, x1, y1, x2, y2, c, 1);
ENGINE_line(engine, x2, y2, x0, y0, c, 1);
}
internal void
ENGINE_trianglefillFlatBottom(ENGINE* engine, float x0, float y0, float x1, float y1, float x2, float y2, uint32_t c) {
float invslope0 = (x1 - x0) / (y1 - y0);
float invslope1 = (x2 - x0) / (y2 - y0);
float curx0 = x0;
float curx1 = x0;
for (int scanlineY = y0; scanlineY <= y1; scanlineY++) {
ENGINE_line(engine, curx0, scanlineY, curx1, scanlineY, c, 1);
curx0 += invslope0;
curx1 += invslope1;
}
}
internal void
ENGINE_trianglefillFlatTop(ENGINE* engine, float x0, float y0, float x1, float y1, float x2, float y2, uint32_t c) {
float invslope0 = (x2 - x0) / (y2 - y0);
float invslope1 = (x2 - x1) / (y2 - y1);
float curx0 = x2;
float curx1 = x2;
for (int scanlineY = y2; scanlineY > y0; scanlineY--) {
ENGINE_line(engine, curx0, scanlineY, curx1, scanlineY, c, 1);
curx0 -= invslope0;
curx1 -= invslope1;
}
}
internal void
ENGINE_trianglefill(ENGINE* engine, float x0, float y0, float x1, float y1, float x2, float y2, uint32_t c) {
if (y1 < y0) {
swap(&x1, &x0);
swap(&y1, &y0);
}
if (y2 < y0) {
swap(&x2, &x0);
swap(&y2, &y0);
}
if (y2 < y1) {
swap(&x2, &x1);
swap(&y2, &y1);
}
x0 += 0.5;
y0 += 0.5;
x1 += 0.5;
y1 += 0.5;
x2 += 0.5;
y2 += 0.5;
if (y1 == y2) {
ENGINE_trianglefillFlatBottom(engine, x0, y0, x1, y1, x2, y2, c);
} else if (y0 == y1) {
ENGINE_trianglefillFlatTop(engine, x0, y0, x1, y1, x2, y2, c);
} else {
float x3 = (x0 + ((y1 - y0) / (y2 - y0)) * (x2 - x0));
ENGINE_trianglefillFlatBottom(engine, x0, y0, x1, y1, x3, y1, c);
ENGINE_trianglefillFlatTop(engine, x1, y1, x3, y1, x2, y2, c);
}
}
internal void
ENGINE_rect(ENGINE* engine, int64_t x, int64_t y, int64_t w, int64_t h, uint32_t c) {
w = w - 1;
h = h - 1;
ENGINE_line(engine, x, y, x, y+h, c, 1);
ENGINE_line(engine, x, y, x+w, y, c, 1);
ENGINE_line(engine, x, y+h, x+w, y+h, c, 1);
ENGINE_line(engine, x+w, y, x+w, y+h, c, 1);
}