-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx_draw.c
2007 lines (1663 loc) · 54.1 KB
/
gfx_draw.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
/**
* @file gfx_draw.c
* @author Alex Hoffman
* @date 27 August 2019
* @brief A SDL2 based library to implement work queue based drawing of graphical
* elements. Allows for drawing using SDL2 from multiple threads.
*
* @verbatim
----------------------------------------------------------------------
Copyright (C) Alexander Hoffman, 2019
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------
@endverbatim
*/
#include <linux/limits.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include <pthread.h>
#include "gfx_draw.h"
#include "gfx_font.h"
#include "gfx_utils.h"
#include "gfx_print.h"
#define ONE_BYTE 8
#define TWO_BYTES 16
#define THREE_BYTES 24
#define MAX_8_BIT 255
#define ALPHA_SOLID MAX_8_BIT
#define FIRST_BYTE 0x000000ff
#define SECOND_BYTE 0x0000ff00
#define THIRD_BYTE 0x00ff0000
#define FOURTH_BYTE 0xff000000
#define RED_PORTION(COLOUR) (COLOUR & 0xFF0000) >> TWO_BYTES
#define GREEN_PORTION(COLOUR) (COLOUR & 0x00FF00) >> ONE_BYTE
#define BLUE_PORTION(COLOUR) (COLOUR & 0x0000FF)
#define ZERO_ALPHA 0
typedef enum {
DRAW_NONE = 0,
DRAW_CLEAR,
DRAW_ARC,
DRAW_ELLIPSE,
DRAW_TEXT,
DRAW_RECT,
DRAW_FILLED_RECT,
DRAW_CIRCLE,
DRAW_LINE,
DRAW_POLY,
DRAW_TRIANGLE,
DRAW_IMAGE,
DRAW_LOADED_IMAGE,
DRAW_LOADED_IMAGE_CROP,
DRAW_SCALED_IMAGE,
DRAW_ARROW,
} draw_job_type_t;
typedef struct loaded_image {
char *filename;
FILE *file;
SDL_Texture *tex;
SDL_RWops *ops;
SDL_Surface *surf;
int w;
int h;
float scale;
//TODO make this atomic
unsigned int ref_count;
unsigned char pending_free;
struct loaded_image *next;
} loaded_image_t;
typedef struct loaded_image_crop {
loaded_image_t *image;
int x;
int y;
int c_x;
int c_y;
int c_w;
int c_h;
} loaded_image_crop_t;
typedef struct spritesheet_sequence {
char *name;
unsigned start_row;
unsigned start_col;
enum sprite_sequence_direction direction;
unsigned frames;
struct spritesheet_sequence *next;
} spritesheet_sequence_t;
typedef struct spritesheet {
loaded_image_t *image;
unsigned x;
unsigned y;
unsigned width;
unsigned height;
unsigned sprite_width;
unsigned sprite_height;
unsigned sprite_cols;
unsigned sprite_rows;
unsigned padding_x;
unsigned padding_y;
} spritesheet_t;
typedef struct animated_image {
spritesheet_t *spritesheet;
spritesheet_sequence_t *sequences;
} animated_image_t;
typedef struct animated_sequence_instance {
unsigned frame_period_ms;
signed current_frame;
unsigned prev_frame_timestamp;
unsigned cur_frame_timestamp;
animated_image_t *image;
spritesheet_sequence_t *sequence;
} animated_sequence_instance_t;
typedef struct clear_data {
unsigned int colour;
} clear_data_t;
typedef struct arc_data {
signed short x;
signed short y;
signed short radius;
signed short start;
signed short end;
unsigned int colour;
} arc_data_t;
typedef struct ellipse_data {
signed short x;
signed short y;
signed short rx;
signed short ry;
unsigned int colour;
} ellipse_data_t;
typedef struct rect_data {
signed short x;
signed short y;
signed short w;
signed short h;
unsigned int colour;
} rect_data_t;
typedef struct circle_data {
signed short x;
signed short y;
signed short radius;
unsigned int colour;
} circle_data_t;
typedef struct line_data {
signed short x1;
signed short y1;
signed short x2;
signed short y2;
unsigned char thickness;
unsigned int colour;
} line_data_t;
typedef struct poly_data {
coord_t *points;
unsigned int n;
unsigned int colour;
} poly_data_t;
typedef struct triangle_data {
coord_t *points;
unsigned int colour;
} triangle_data_t;
typedef struct image_data {
char *filename;
SDL_Texture *tex;
signed short x;
signed short y;
} image_data_t;
typedef struct loaded_image_data {
loaded_image_t *img;
signed short x;
signed short y;
} loaded_image_data_t;
typedef struct scaled_image_data {
image_data_t image;
float scale;
} scaled_image_data_t;
typedef struct text_data {
char *str;
signed short x;
signed short y;
unsigned int colour;
TTF_Font *font;
} text_data_t;
typedef struct arrow_data {
signed short x1;
signed short y1;
signed short x2;
signed short y2;
signed short head_length;
unsigned char thickness;
unsigned int colour;
} arrow_data_t;
union data_u {
clear_data_t clear;
arc_data_t arc;
ellipse_data_t ellipse;
rect_data_t rect;
circle_data_t circle;
line_data_t line;
poly_data_t poly;
triangle_data_t triangle;
image_data_t image;
loaded_image_data_t loaded_image;
loaded_image_crop_t loaded_image_crop;
scaled_image_data_t scaled_image;
text_data_t text;
arrow_data_t arrow;
};
typedef struct draw_job {
draw_job_type_t type;
union data_u *data;
struct draw_job *next;
} draw_job_t;
pthread_mutex_t job_list_lock = PTHREAD_MUTEX_INITIALIZER;
draw_job_t job_list_head = { 0 };
struct global_offsets {
int x;
int y;
pthread_mutex_t lock;
};
struct global_offsets global_offset = {
.lock = PTHREAD_MUTEX_INITIALIZER,
};
pthread_mutex_t loaded_images_lock = PTHREAD_MUTEX_INITIALIZER;
loaded_image_t loaded_images_list = { 0 };
const int screen_height = SCREEN_HEIGHT;
const int screen_width = SCREEN_WIDTH;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_GLContext context = NULL;
char *error_message = NULL;
static uint32_t swapBytes(unsigned int x)
{
return ((x &FIRST_BYTE) << THREE_BYTES) +
((x &SECOND_BYTE) << ONE_BYTE) +
((x &THIRD_BYTE) >> ONE_BYTE) +
((x &FOURTH_BYTE) >> THREE_BYTES);
}
void _setErrorMessage(char *msg)
{
if (error_message) {
free(error_message);
}
error_message = calloc(strlen(msg) + 1, sizeof(char));
if (error_message == NULL) {
return;
}
strcpy(error_message, msg);
}
#define PRINT_SDL_ERROR(msg, ...) \
PRINT_ERROR("[SDL Error] %s\n" #msg, (char *)SDL_GetError(), \
##__VA_ARGS__)
static draw_job_t *_pushDrawJob(void)
{
draw_job_t *iterator;
draw_job_t *job = calloc(1, sizeof(draw_job_t));
if (job == NULL) {
return NULL;
}
pthread_mutex_lock(&job_list_lock);
for (iterator = &job_list_head; iterator->next;
iterator = iterator->next)
;
iterator->next = job;
pthread_mutex_unlock(&job_list_lock);
return job;
}
static int _waitingDrawJobs(void)
{
int ret = 1;
pthread_mutex_lock(&job_list_lock);
if (job_list_head.next == NULL) {
ret = 0;
}
pthread_mutex_unlock(&job_list_lock);
return ret;
}
static draw_job_t *popDrawJob(void)
{
draw_job_t *ret = job_list_head.next;
if (ret) {
pthread_mutex_lock(&job_list_lock);
if (ret->next) {
job_list_head.next = ret->next;
}
else {
job_list_head.next = NULL;
}
pthread_mutex_unlock(&job_list_lock);
}
return ret;
}
static int _clearDisplay(unsigned int colour)
{
SDL_SetRenderDrawColor(renderer, (colour >> 16) & 0xFF,
(colour >> 8) & 0xFF, colour & 0xFF,
ALPHA_SOLID);
SDL_RenderClear(renderer);
return 0;
}
static int _drawRectangle(signed short x, signed short y, signed short w,
signed short h, unsigned int colour)
{
rectangleColor(renderer, x + w, y, x, y + h,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static int _drawFilledRectangle(signed short x, signed short y, signed short w,
signed short h, unsigned int colour)
{
boxColor(renderer, x + w, y, x, y + h,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static int _drawArc(signed short x, signed short y, signed short radius,
signed short start, signed short end, unsigned int colour)
{
arcColor(renderer, x, y, radius, start, end,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static int _drawEllipse(signed short x, signed short y, signed short rx,
signed short ry, unsigned int colour)
{
ellipseColor(renderer, x, y, rx, ry,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static int _drawCircle(signed short x, signed short y, signed short radius,
unsigned int colour)
{
filledCircleColor(renderer, x, y, radius,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static int _drawLine(signed short x1, signed short y1, signed short x2,
signed short y2, unsigned char thickness,
unsigned int colour)
{
thickLineColor(renderer, x1, y1, x2, y2, thickness,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static int _drawPoly(coord_t *points, unsigned int n, int x_offset,
int y_offset, signed short colour)
{
signed short *x_coords = calloc(1, sizeof(signed short) * n);
signed short *y_coords = calloc(1, sizeof(signed short) * n);
unsigned int i;
for (i = 0; i < n; i++) {
x_coords[i] = points[i].x + x_offset;
y_coords[i] = points[i].y + y_offset;
}
polygonColor(renderer, x_coords, y_coords, n,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
free(x_coords);
free(y_coords);
return 0;
}
static int _drawTriangle(coord_t *points, int x_offset, int y_offset,
unsigned int colour)
{
filledTrigonColor(renderer, points[0].x + x_offset,
points[0].y + y_offset, points[1].x + x_offset,
points[1].y + y_offset, points[2].x + x_offset,
points[2].y + y_offset,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID));
return 0;
}
static SDL_Texture *_loadImage(char *filename, SDL_Renderer *ren)
{
SDL_Texture *tex =
IMG_LoadTexture(ren, gfxUtilFindResourcePath(filename));
return tex;
}
static int _renderCroppedImage(SDL_Texture *tex, SDL_Renderer *ren,
signed short x, signed short y, signed short c_x,
signed short c_y, int w, int h)
{
SDL_Rect src, dst;
src.x = c_x;
src.y = c_y;
src.w = w;
src.h = h;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
return SDL_RenderCopy(ren, tex, &src, &dst);
}
static int _renderScaledImage(SDL_Texture *tex, SDL_Renderer *ren,
signed short x, signed short y, int w, int h)
{
SDL_Rect dst;
dst.w = w;
dst.h = h;
dst.x = x;
dst.y = y;
return SDL_RenderCopy(ren, tex, NULL, &dst);
}
static int _getImageSize(char *filename, int *w, int *h)
{
SDL_Texture *tex = _loadImage(filename, renderer);
if (tex == NULL) {
return -1;
}
SDL_QueryTexture(tex, NULL, NULL, w, h);
SDL_DestroyTexture(tex);
return 0;
}
gfx_animation_handle_t
gfxDrawAnimationCreate(gfx_spritesheet_handle_t spritesheet)
{
if (spritesheet == NULL) {
PRINT_ERROR("Creating animation requires a valid spritesheet");
goto err;
}
animated_image_t *ret = calloc(1, sizeof(animated_image_t));
if (ret == NULL) {
PRINT_ERROR("Allocating animation failed");
goto err;
}
ret->spritesheet = spritesheet;
return (gfx_animation_handle_t)ret;
err:
return NULL;
}
int gfxDrawAnimationAddSequence(
gfx_animation_handle_t animation, char *name, unsigned start_row,
unsigned start_col,
enum sprite_sequence_direction sprite_step_direction, unsigned frames)
{
if (animation == NULL) {
PRINT_ERROR("Animation handle is not valid");
goto err;
}
if (name == NULL) {
PRINT_ERROR("Sequence requires a valid name");
goto err;
}
animated_image_t *anim = (animated_image_t *)animation;
spritesheet_sequence_t *seq = calloc(1, sizeof(spritesheet_sequence_t));
if (seq == NULL) {
PRINT_ERROR("Could not allocate animation sequence");
goto err;
}
seq->name = strdup(name);
if (seq->name == NULL) {
PRINT_ERROR("Could not allocate sequence name");
goto err_name;
}
seq->start_row = start_row;
seq->start_col = start_col;
seq->direction = sprite_step_direction;
seq->frames = frames;
if (anim->sequences == NULL) {
anim->sequences = seq;
}
else {
spritesheet_sequence_t *iterator = anim->sequences;
for (; iterator->next; iterator = iterator->next)
;
iterator->next = seq;
}
return 0;
err_name:
free(seq);
err:
return -1;
}
gfx_sequence_handle_t
gfxDrawAnimationSequenceInstantiate(gfx_animation_handle_t animation,
char *sequence_name,
unsigned frame_period_ms)
{
if (animation == NULL) {
PRINT_ERROR(
"Animation provided for sequence instantiation was invalid");
goto err;
}
if (sequence_name == NULL) {
PRINT_ERROR("Sequence name is invalid");
goto err;
}
if (frame_period_ms == 0) {
PRINT_ERROR("Sequence frame period cannot be zero");
goto err;
}
animated_sequence_instance_t *ret =
calloc(1, sizeof(animated_sequence_instance_t));
if (ret == NULL) {
PRINT_ERROR("Could not create sequence '%s' instance",
sequence_name);
goto err;
}
ret->image = (animated_image_t *)animation;
spritesheet_sequence_t *iterator;
for (iterator = ret->image->sequences; iterator;
iterator = iterator->next)
if (!strcmp(iterator->name, sequence_name)) {
ret->sequence = iterator;
break;
}
if (ret->sequence == NULL) {
PRINT_ERROR("Could not find sequence '%s'", sequence_name);
goto err_sequence;
}
ret->frame_period_ms = frame_period_ms;
return ret;
err_sequence:
free(ret);
err:
return NULL;
}
static int freeLoadedImage(loaded_image_t **img)
{
int ret = -1;
pthread_mutex_lock(&loaded_images_lock);
loaded_image_t *iterator = &loaded_images_list;
loaded_image_t *delete;
for (; iterator->next; iterator = iterator->next)
if (iterator->next == *img) {
break;
}
if (iterator->next == *img) {
delete = iterator->next;
if (!iterator->next->next) {
iterator->next = NULL;
}
else {
iterator->next = delete->next;
}
SDL_FreeSurface(delete->surf);
SDL_RWclose(delete->ops);
SDL_DestroyTexture(delete->tex);
free(delete->filename);
free(delete);
*img = (loaded_image_t *)NULL;
ret = 0;
}
pthread_mutex_unlock(&loaded_images_lock);
return ret;
}
static void vPutLoadedImage(gfx_image_handle_t img)
{
loaded_image_t *loaded_img = (loaded_image_t *)img;
loaded_img->ref_count--;
if (loaded_img->pending_free && !loaded_img->ref_count) {
freeLoadedImage((loaded_image_t **)&img);
}
}
int xDrawLoadedImageCropped(loaded_image_t *img, SDL_Renderer *ren,
signed short x, signed short y, signed short c_x,
signed short c_y, signed short c_w,
signed short c_h)
{
return _renderCroppedImage(img->tex, ren, x, y, c_x, c_y, c_w, c_h);
}
int xDrawLoadedImage(loaded_image_t *img, SDL_Renderer *ren, signed short x,
signed short y)
{
return _renderScaledImage(img->tex, ren, x, y, img->w *img->scale,
img->h *img->scale);
}
static int _drawScaledImage(SDL_Texture *tex, SDL_Renderer *ren, signed short x,
signed short y, float scale)
{
int w, h;
SDL_QueryTexture(tex, NULL, NULL, &w, &h);
if (!w || !h) {
return -1;
}
if (_renderScaledImage(tex, ren, x, y, w * scale, h * scale)) {
return -1;
}
SDL_DestroyTexture(tex);
return 0;
}
static int _drawImage(SDL_Texture *tex, SDL_Renderer *ren, signed short x,
signed short y)
{
return _drawScaledImage(tex, ren, x, y, 1);
}
static int _drawText(char *string, signed short x, signed short y,
unsigned int colour, TTF_Font *font)
{
SDL_Color color = { RED_PORTION(colour), GREEN_PORTION(colour),
BLUE_PORTION(colour), ZERO_ALPHA
};
SDL_Surface *surface = TTF_RenderText_Solid(font, string, color);
gfxFontPutFont(font);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dst = { 0 };
SDL_QueryTexture(texture, NULL, NULL, &dst.w, &dst.h);
dst.x = x;
dst.y = y;
SDL_RenderCopy(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
return 0;
}
static int _getTextSize(char *string, int *width, int *height)
{
SDL_Color color = { 0 };
TTF_Font *font = gfxFontGetCurFont();
SDL_Surface *surface = TTF_RenderText_Solid(font, string, color);
gfxFontPutFont(font);
if (surface == NULL) {
goto err_surface;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
goto err_texture;
}
if (SDL_QueryTexture(texture, NULL, NULL, width, height)) {
goto err_query;
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
return 0;
err_query:
SDL_DestroyTexture(texture);
err_texture:
SDL_FreeSurface(surface);
err_surface:
return -1;
}
static int _drawArrow(signed short x1, signed short y1, signed short x2,
signed short y2, signed short head_length,
unsigned char thickness, unsigned int colour)
{
// Line vector
unsigned short dx = x2 - x1;
unsigned short dy = y2 - y1;
// Normalize
float length = sqrt(dx *dx + dy *dy);
signed short unit_dx = (signed short)(dx / length);
signed short unit_dy = (signed short)(dy / length);
signed short head_x1 =
roundf(x2 - unit_dx *head_length - unit_dy *head_length);
signed short head_y1 =
roundf(y2 - unit_dy *head_length + unit_dx *head_length);
signed short head_x2 =
roundf(x2 - unit_dx *head_length + unit_dy *head_length);
signed short head_y2 =
roundf(y2 - unit_dy *head_length - unit_dx *head_length);
if (thickLineColor(renderer, x1, y1, x2, y2, thickness,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID))) {
return -1;
}
if (thickLineColor(renderer, head_x1, head_y1, x2, y2, thickness,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID))) {
return -1;
}
if (thickLineColor(renderer, head_x2, head_y2, x2, y2, thickness,
swapBytes((colour << ONE_BYTE) | ALPHA_SOLID))) {
return -1;
}
return 0;
}
static int vHandleDrawJob(draw_job_t *job)
{
int ret = 0;
static int x_offset = 0;
static int y_offset = 0;
;
if (!pthread_mutex_unlock(&global_offset.lock)) {
x_offset = global_offset.x;
y_offset = global_offset.y;
}
else {
return -1;
}
if (job == NULL) {
return -1;
}
if (job->data == NULL) {
return -1;
}
switch (job->type) {
case DRAW_CLEAR:
ret = _clearDisplay(job->data->clear.colour);
break;
case DRAW_ARC:
ret = _drawArc(job->data->arc.x + x_offset,
job->data->arc.y + y_offset,
job->data->arc.radius, job->data->arc.start,
job->data->arc.end, job->data->arc.colour);
break;
case DRAW_ELLIPSE:
ret = _drawEllipse(job->data->ellipse.x + x_offset,
job->data->ellipse.y, job->data->ellipse.rx,
job->data->ellipse.ry,
job->data->ellipse.colour);
break;
case DRAW_TEXT:
ret = _drawText(job->data->text.str,
job->data->text.x + x_offset,
job->data->text.y + y_offset,
job->data->text.colour, job->data->text.font);
free(job->data->text.str);
break;
case DRAW_RECT:
ret = _drawRectangle(job->data->rect.x + x_offset,
job->data->rect.y + y_offset,
job->data->rect.w, job->data->rect.h,
job->data->rect.colour);
break;
case DRAW_FILLED_RECT:
ret = _drawFilledRectangle(job->data->rect.x + x_offset,
job->data->rect.y + y_offset,
job->data->rect.w, job->data->rect.h,
job->data->rect.colour);
break;
case DRAW_CIRCLE:
ret = _drawCircle(job->data->circle.x + x_offset,
job->data->circle.y + y_offset,
job->data->circle.radius,
job->data->circle.colour);
break;
case DRAW_LINE:
ret = _drawLine(job->data->line.x1 + x_offset,
job->data->line.y1 + y_offset,
job->data->line.x2 + x_offset,
job->data->line.y2 + y_offset,
job->data->line.thickness,
job->data->line.colour);
break;
case DRAW_POLY:
ret = _drawPoly(job->data->poly.points, job->data->poly.n,
x_offset, y_offset, job->data->poly.colour);
break;
case DRAW_TRIANGLE:
ret = _drawTriangle(job->data->triangle.points, x_offset,
y_offset, job->data->triangle.colour);
break;
case DRAW_IMAGE:
job->data->image.tex =
_loadImage(job->data->image.filename, renderer);
ret = _drawImage(job->data->image.tex, renderer,
job->data->image.x + x_offset,
job->data->image.y + y_offset);
break;
case DRAW_LOADED_IMAGE:
ret = xDrawLoadedImage(job->data->loaded_image.img, renderer,
job->data->loaded_image.x + x_offset,
job->data->loaded_image.y + y_offset);
vPutLoadedImage(job->data->loaded_image.img);
break;
case DRAW_LOADED_IMAGE_CROP:
ret = xDrawLoadedImageCropped(
job->data->loaded_image_crop.image, renderer,
job->data->loaded_image_crop.x + x_offset,
job->data->loaded_image_crop.y + y_offset,
job->data->loaded_image_crop.c_x,
job->data->loaded_image_crop.c_y,
job->data->loaded_image_crop.c_w,
job->data->loaded_image_crop.c_h);
vPutLoadedImage(job->data->loaded_image_crop.image);
break;
case DRAW_SCALED_IMAGE:
job->data->scaled_image.image.tex = _loadImage(
job->data->scaled_image.image.filename, renderer);
ret = _drawScaledImage(
job->data->scaled_image.image.tex, renderer,
job->data->scaled_image.image.x + x_offset,
job->data->scaled_image.image.y + y_offset,
job->data->scaled_image.scale);
free(job->data->scaled_image.image.filename);
break;
case DRAW_ARROW:
ret = _drawArrow(job->data->arrow.x1 + x_offset,
job->data->arrow.y1 + y_offset,
job->data->arrow.x2 + x_offset,
job->data->arrow.y2 + y_offset,
job->data->arrow.head_length,
job->data->arrow.thickness,
job->data->arrow.colour);
default:
break;
}
free(job->data);
return ret;
}
#define INIT_JOB(JOB, TYPE) \
draw_job_t *JOB = _pushDrawJob(); \
if (!JOB) \
return -1; \
union data_u *data = calloc(1, sizeof(union data_u)); \
if (data == NULL) \
logCriticalError("job->data alloc"); \
JOB->data = data; \
JOB->type = TYPE;
static void logCriticalError(char *msg)
{
printf("[ERROR] %s\n", msg);
exit(-1);
}
#define NS_IN_SECOND 1000000000.0
#define MS_IN_SECOND 1000.0
#define NS_IN_MS 1000000.0
#if (configFPS_LIMIT == 1)
static float timespecDiffMilli(struct timespec *start, struct timespec *stop)
{
if ((stop->tv_nsec - start->tv_nsec) < 0)
return (stop->tv_sec - start->tv_sec - 1) * MS_IN_SECOND +
(stop->tv_nsec - start->tv_nsec + NS_IN_SECOND) /
NS_IN_MS;
return (stop->tv_sec - start->tv_sec) * MS_IN_SECOND +
(stop->tv_nsec - start->tv_nsec) / NS_IN_MS;
}
#ifdef configFPS_LIMIT_RATE
#define FRAMELIMIT configFPS_LIMIT_RATE
#else
#define FRAMELIMIT 50
#endif //configFPS_LIMIT_RATE
#define FRAMELIMIT_PERIOD 1000.0 / FRAMELIMIT
#endif //configFPS_LIMIT
int gfxDrawUpdateScreen(void)
{
gfxDrawBindThread(); // Setup Rendering handle with correct GL context
if (gfxUtilIsCurGLThread()) {
PRINT_ERROR(
"Updating screen from thread that does not hold GL context");
goto err;
}
#if (configFPS_LIMIT == 1)
static struct timespec last_time = { 0 }, cur_time = { 0 };
if (clock_gettime(CLOCK_MONOTONIC, &cur_time)) {
PRINT_ERROR("Failed to get monotonic clock");
goto err;
}
if (timespecDiffMilli(&last_time, &cur_time) <
(float)FRAMELIMIT_PERIOD) {