-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_lib.c
1477 lines (1196 loc) · 33.7 KB
/
sdl_lib.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <gprolog.h>
#include <SDL.h>
#include "sdllib_common.h"
// These are created during SDL_Init to avoid repetition
int g_atomEmptyList = 0;
int g_atomTextInput = 0;
int g_atomTextEditing = 0;
int g_atomDropFile = 0;
int g_atomDisplayMode = 0;
int g_atomUserEvent = 0;
int g_atomBlendModeNone = 0;
int g_atomBlendModeBlend = 0;
int g_atomBlendModeAdd = 0;
int g_atomBlendModeMod = 0;
int g_atomDollarRecord = 0;
int g_atomDollarGesture = 0;
// stderr for SDL_Log can be redirected to a file
FILE* g_errFile = NULL;
FILE* g_oldStdErr;
//typedef void (*SDL_LogOutputFunction)(void *userdata, int category, SDL_LogPriority priority, const char *message);
#define EV(F) F(&ev, &szTerm[0])
#define EVB(F) F(&ev, &szTerm[0]); break
#define EVWRAPPER(F) void F(SDL_Event *e, char* t)
// These create response with Pl_Read_From_String
EVWRAPPER(evKbd);
EVWRAPPER(evQuit);
EVWRAPPER(evWindow);
EVWRAPPER(evMouseButton);
EVWRAPPER(evMouseMotion);
EVWRAPPER(evMouseWheel);
#define EVB_TERM(F) F(&ev, &szTerm[0], &term); break
#define EVWRAPPER_TERM(F) void F(SDL_Event *e, char* t, PlTerm *o)
// These create response with Pl_Mk_Compound
EVWRAPPER_TERM(evTextEditing);
EVWRAPPER_TERM(evTextInput);
EVWRAPPER_TERM(evDropEvent);
EVWRAPPER_TERM(evUserEvent);
EVWRAPPER_TERM(evDollarEvent);
// Maps event codes into strings for atom conversion
const char* evWindowType(int);
const char* evButtonName(Uint32);
/**
* Perform standard SDL library initialisation.
*
* @param PlLong flags mask
*
* @return PL_TRUE | PL_FALSE
*
*/
PlBool gp_SDL_Init(PlLong flags)
{
// input event types
g_atomTextInput = Pl_Create_Atom("text_input");
g_atomTextEditing = Pl_Create_Atom("text_editing");
g_atomDropFile = Pl_Create_Atom("dropfile");
g_atomDisplayMode = Pl_Create_Atom("display_mode");
g_atomUserEvent = Pl_Create_Atom("user_event");
// texture blending modes
g_atomBlendModeNone = Pl_Create_Atom("none");
g_atomBlendModeBlend = Pl_Create_Atom("blend");
g_atomBlendModeAdd = Pl_Create_Atom("add");
g_atomBlendModeMod = Pl_Create_Atom("mod");
// gesture events
g_atomDollarRecord = Pl_Create_Atom("dollar_record");
g_atomDollarGesture = Pl_Create_Atom("dollar_gesture");
// generic
g_atomEmptyList = Pl_Create_Atom("[]");
if (flags > 0) {
if (0 == SDL_Init(flags)) {
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_Init);
}
return PL_FALSE;
}
/**
* Shutdown the SDL library.
*
* @return PL_TRUE
*
*/
PlBool gp_SDL_Quit() {
SDL_Quit();
return PL_TRUE;
}
/**
* SDL_CreateWindow
*
* Create a window of a given size, position and attributes.
*
* @param char* title atom or string containing the window title
* @param PlLong x centered | undefined | <integer-value>
* @param PlLong y centered | undefined | <integer-value>
* @param PlLong w width of the window
* @param PlLong h height of the window
* @param PlLong flags one or more SDL_WindowFlags as a bit mask
* @param PlTerm handle an UNINSTANTIATED variable to hold the handle
*
* @return PL_TRUE | PL_FALSE
*
*/
PlBool gp_SDL_CreateWindow(char* title,
PlLong xpos, PlLong ypos,
PlLong width, PlLong height,
PlLong flags, PlLong *handle)
{
SDL_Window* wnd = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (wnd) {
*handle = (PlLong)wnd;
#ifdef __DEBUG__
fprintf(stdout, "gp_SDL_CreateWindow: ok: %p\n", wnd);
#endif
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_CreateWindow);
}
/**
* SDL_DestroyWindow
*
* Use this function to destroy a window.
*
* @param PlLong handle references the window to be killed
*
* @return PL_TRUE
*
*/
PlBool gp_SDL_DestroyWindow(PlLong handle)
{
SDL_DestroyWindow((SDL_Window*)handle);
return PL_TRUE;
}
PlBool gp_SDL_ShowWindow(PlLong handle)
{
SDL_ShowWindow((SDL_Window*)handle);
return PL_TRUE;
}
PlBool gp_SDL_HideWindow(PlLong handle)
{
SDL_HideWindow((SDL_Window*)handle);
return PL_TRUE;
}
PlBool gp_SDL_RaiseWindow(PlLong handle)
{
SDL_RaiseWindow((SDL_Window*)handle);
return PL_TRUE;
}
PlBool gp_SDL_SetWindowTitle(PlLong wnd, char* title)
{
SDL_SetWindowTitle((SDL_Window*)wnd, title);
return PL_TRUE;
}
PlBool gp_SDL_SetWindowSize(PlLong wnd, PlLong width, PlLong height)
{
SDL_SetWindowSize((SDL_Window*)wnd, (int)width, (int)height);
return PL_TRUE;
}
PlBool gp_SDL_GetWindowSize(PlLong wnd, PlLong *width, PlLong *height)
{
int ww, wh;
SDL_GetWindowSize((SDL_Window*)wnd, &ww, &wh);
*width = (PlLong)ww;
*height = (PlLong)wh;
return PL_TRUE;
}
PlBool gp_SDL_SetWindowPosition(PlLong wnd, PlLong x, PlLong y)
{
SDL_SetWindowPosition((SDL_Window*)wnd, (int)x, (int)y);
return PL_TRUE;
}
PlBool gp_SDL_GetWindowPosition(PlLong wnd, PlLong *x, PlLong *y)
{
int wx, wy;
SDL_GetWindowPosition((SDL_Window*)wnd, &wx, &wy);
*x = (PlLong)wx;
*y = (PlLong)wy;
return PL_TRUE;
}
/**
* SDL_CreateRenderer
*
* Use this function to create a 2D rendering context for a window.
*
* @param PlTerm handle a valid window handle from gp_create_window
* @param PlLong index index of the rendering driver
* @param PlLong flags one or more SDL_RendererFlags as a bit mask
*
* @return PL_TRUE | PL_FALSE
*
*/
PlBool gp_SDL_CreateRenderer(PlLong *handle, PlLong index, PlLong flags, PlLong *r)
{
SDL_Window *wnd = (SDL_Window*)handle;
SDL_Renderer *renderer = SDL_CreateRenderer(wnd, index, flags);
if (renderer) {
*r = (PlLong)renderer;
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_CreateRenderer);
}
PlBool gp_SDL_GetRenderer(PlLong window, PlLong *rndr)
{
SDL_Renderer *r = SDL_GetRenderer((SDL_Window*)window);
if (r) {
*rndr = (PlLong)r;
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_GetRenderer);
}
/**
* SDL_CreateWindowAndRenderer
*
* Use this function to create a window and default renderer.
*
* @param PlLong handle a valid window handle from gp_create_window
* @param PlLong index index of the rendering driver
* @param PlLong flags one or more SDL_RendererFlags as a bit mask
*
* @return PL_TRUE | PL_FALSE
*
*/
PlBool gp_SDL_CreateWindowAndRenderer(
PlLong width, PlLong height, PlLong flags,
PlLong *wnd, PlLong *renderer)
{
SDL_Window *w;
SDL_Renderer *r;
if (SDL_CreateWindowAndRenderer(width, height, flags, &w, &r)) {
*wnd = (PlLong)wnd;
*renderer = (PlLong)renderer;
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_CreateWindowAndRenderer);
}
/**
* SDL_DestroyRenderer
*
* Use this function to destroy the rendering context for a window and
* free associated textures.
*
* @param PlLong handle references the renderer to be killed
*
* @return PL_TRUE
*
*/
PlBool gp_SDL_DestroyRenderer(PlLong handle)
{
SDL_DestroyRenderer((SDL_Renderer*)handle);
return PL_TRUE;
}
PlBool gp_SDL_SetRenderDrawColor(PlLong rndr, PlLong r, PlLong g, PlLong b, PlLong a)
{
SDL_SetRenderDrawColor((SDL_Renderer*)rndr, r, g, b, a);
return PL_TRUE;
}
PlBool gp_SDL_RenderClear(PlLong rndr)
{
SDL_RenderClear((SDL_Renderer*)rndr);
return PL_TRUE;
}
PlBool gp_SDL_RenderPresent(PlLong rndr)
{
SDL_RenderPresent((SDL_Renderer*)rndr);
return PL_TRUE;
}
PlBool gp_SDL_RenderDrawPoint(PlLong rndr, PlLong x, PlLong y)
{
SDL_RenderDrawPoint((SDL_Renderer*)rndr, (int)x, (int)y);
return PL_TRUE;
}
PlBool gp_SDL_RenderDrawLine(
PlLong rndr,
PlLong x1, PlLong y1,
PlLong x2, PlLong y2)
{
SDL_RenderDrawLine((SDL_Renderer*)rndr, (int)x1, (int)y1, (int)x2, (int)y2);
return PL_TRUE;
}
PlBool gp_SDL_RenderDrawRect(
PlLong rndr,
PlLong x, PlLong y,
PlLong w, PlLong h)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_RenderDrawRect((SDL_Renderer*)rndr, &rect);
return PL_TRUE;
}
PlBool gp_SDL_RenderFillRect(
PlLong rndr,
PlLong x, PlLong y,
PlLong w, PlLong h)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_RenderFillRect((SDL_Renderer*)rndr, &rect);
return PL_TRUE;
}
PlBool gp_SDL_CreateTexture_C(
PlLong rndr,
PlLong format, PlLong access,
PlLong w, PlLong h,
PlLong *texture)
{
SDL_Texture* pTexture = SDL_CreateTexture(
(SDL_Renderer*)rndr,
format,
access,
w, h);
if (pTexture) {
*texture = (PlLong)pTexture;
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_CreateTexture);
}
PlBool gp_SDL_DestroyTexture(PlLong texture)
{
SDL_DestroyTexture((SDL_Texture*)texture);
return PL_TRUE;
}
PlBool gp_SDL_PollEvent(PlTerm *event)
{
SDL_Event ev;
PlTerm term;
char szTerm[63+1];
if (0 == SDL_PollEvent(&ev)) {
*event = Pl_Mk_Integer(-1);
}
else {
switch(ev.type)
{
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: EVB(evMouseButton);
case SDL_MOUSEMOTION: EVB(evMouseMotion);
case SDL_MOUSEWHEEL: EVB(evMouseWheel);
case SDL_WINDOWEVENT: EVB(evWindow);
case SDL_KEYDOWN:
case SDL_KEYUP: EVB(evKbd);
case SDL_TEXTEDITING: EVB_TERM(evTextEditing);
case SDL_TEXTINPUT: EVB_TERM(evTextInput);
case SDL_DOLLARGESTURE:
case SDL_DOLLARRECORD: EVB_TERM(evDollarEvent);
case SDL_QUIT: EVB(evQuit);
case SDL_DROPFILE: EVB_TERM(evDropEvent);
case SDL_USEREVENT: EVB_TERM(evUserEvent);
default:
sprintf(szTerm, "unhandled(%u)", ev.type);
}
// Only copy the term if the term string was written to
// otherwise we assume the *handler* wrote into "term"
if (szTerm[0]) {
term = Pl_Read_From_String(szTerm);
}
#ifdef __DEBUG__
Pl_Write(term); fprintf(stdout, "\n");
#endif
Pl_Copy_Term(event, &term);
}
return PL_TRUE;
}
PlBool gp_SDL_GetTicks(PlLong *ticks)
{
*ticks = (PlLong)SDL_GetTicks();
return PL_TRUE;
}
PlBool gp_SDL_Delay(PlLong delay_in_milliseconds)
{
SDL_Delay(delay_in_milliseconds);
return PL_TRUE;
}
PlBool gp_SDL_RemoveTimer(PlLong timer)
{
if (SDL_RemoveTimer((SDL_TimerID)timer)) {
return PL_TRUE;
}
return PL_FALSE;
}
Uint32 timerCallback(Uint32 interval, void* param)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = interval;
event.user.data1 = 0;
event.user.data2 = 0;
int status = SDL_PushEvent(&event);
#ifdef __DEBUG__
switch(status) {
case 1: fprintf(stdout, "timer+user-event => pushed ok\n"); break;
case 0: fprintf(stdout, "timer+user-event => filtered!\n"); break;
default:
SHOW_SDL_FAIL(SDL_PushEvent);
break;
}
#endif
// abort subsequent callbacks unless success
return (status < 0) ? 0 : interval;
}
PlBool gp_SDL_AddTimer(PlLong interval, PlLong *timerId)
{
SDL_TimerID id = SDL_AddTimer(interval, timerCallback, NULL);
if (id) {
*timerId = (PlLong)id;
return PL_TRUE;
}
RETURN_SDL_FAIL(SDL_AddTimer);
}
/**
* SDL_ShowSimpleMessageBox
*
* @param SDL_Window* parent the parent window, 0 for no parent.
* @param PlLong flags simple message box flags
* @param char* title C-string title of the message
* @param char* message C-string content of the message
*
* @return PL_TRUE
*/
PlBool gp_SDL_ShowSimpleMessageBox_C(
PlLong parent, char* title,
char* message, PlLong flags)
{
SDL_ShowSimpleMessageBox(flags, title, message, (SDL_Window*)parent);
return PL_TRUE;
}
PlBool gp_SDL_StartTextInput()
{
SDL_StartTextInput();
return PL_TRUE;
}
PlBool gp_SDL_StopTextInput()
{
SDL_StopTextInput();
return PL_TRUE;
}
PlBool gp_SDL_SetTextInputRect(PlLong x, PlLong y, PlLong w, PlLong h)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_SetTextInputRect(&rect);
return PL_TRUE;
}
// pass "" to restore output to whatver is the default
PlBool gp_SDL_LogToFile(char* filename)
{
if (strlen(filename)) {
if (!g_errFile) {
g_oldStdErr = stderr;
g_errFile = freopen(filename, "a+", stderr);
if (NULL == g_errFile) {
fprintf(stderr, "Failed to redirect: %i\n", errno);
return PL_FALSE;
}
}
}
else {
fclose(stderr);
stderr = g_oldStdErr;
g_oldStdErr = NULL;
g_errFile = NULL;
}
return PL_TRUE;
}
PlBool gp_SDL_Log(char* text)
{
SDL_Log("%s", text);
return PL_TRUE;
}
PlBool gp_SDL_LogMessage(PlLong category, PlLong priority, char* text)
{
SDL_LogMessage((int)category, (int)priority, "%s", text);
return PL_TRUE;
}
//====================================================================
//
// Event functors for sdl_PollEvent
//
// Each wrapper returns a functor+arguments to sdl_PollEvent(), the
// order of arguments is the same as the standard SDL documentation so
// it remains is to figure it out. Each comment header below contains
// a description of the form, where <foo> is a variable value
// extracted from the event record and things not in angle brackets
// are atoms.
//
//====================================================================
//--------------------------------------------------------------------
// quit( <timestamp> ).
//--------------------------------------------------------------------
EVWRAPPER(evQuit) { sprintf(t,"quit(%u)", e->quit.timestamp); }
//--------------------------------------------------------------------
// keydown / keyup( <timestamp>,
// <windowID>,
// pressed / released,
// repeat / first,
// <scancode>,
// <sym>
// <mod> ).
//--------------------------------------------------------------------
EVWRAPPER(evKbd) {
sprintf(t,
"%s(%u,%u,%s,%s,%u,%u,%u)",
e->type == SDL_KEYDOWN ? "keydown" : "keyup",
e->key.timestamp,
e->key.windowID,
e->key.state == SDL_PRESSED ? "pressed" : "released",
e->key.repeat ? "repeat" : "first",
e->key.keysym.scancode,
e->key.keysym.sym,
e->key.keysym.mod); }
//--------------------------------------------------------------------
// text_editing( <timestamp>, <windowID>, <text>, <start>, <length> ).
//--------------------------------------------------------------------
EVWRAPPER_TERM(evTextEditing)
{
PlTerm args[] = {
Pl_Mk_Integer((PlLong)e->edit.timestamp),
Pl_Mk_Integer((PlLong)e->edit.windowID),
Pl_Mk_Codes(e->edit.text),
Pl_Mk_Integer(e->edit.start),
Pl_Mk_Integer(e->edit.length)
};
int argc = sizeof(args) / sizeof(PlTerm);
*t = 0;
*o = Pl_Mk_Compound(g_atomTextEditing, 5, &args[0]);
}
//--------------------------------------------------------------------
// text_input( <timestamp>, <windowID>, <text> ).
//--------------------------------------------------------------------
EVWRAPPER_TERM(evTextInput)
{
PlTerm timestamp = Pl_Mk_Integer((PlLong)e->text.timestamp);
PlTerm windowID = Pl_Mk_Integer((PlLong)e->text.windowID);
PlTerm textBuffer = Pl_Mk_Codes(e->text.text);
PlTerm args[] = {timestamp, windowID, textBuffer};
*t = 0;
*o = Pl_Mk_Compound(g_atomTextInput, 3, &args[0]);
}
//--------------------------------------------------------------------
// window( <timestamp>, <windowID>, <window-event> ).
//--------------------------------------------------------------------
EVWRAPPER(evWindow)
{
sprintf(t,
"window(%u,%u,%s)",
e->window.timestamp,
e->window.windowID,
evWindowType(e->window.event));
}
//--------------------------------------------------------------------
// dropfile( <timestamp>, <filename> ).
//--------------------------------------------------------------------
EVWRAPPER_TERM(evDropEvent)
{
#ifdef __DEBUG__
fprintf(stdout, "DROP FILE: %s\n", e->drop.file);
#endif
PlTerm timestamp = Pl_Mk_Integer((PlLong)e->drop.timestamp);
PlTerm filename = Pl_Mk_Codes(e->drop.file);
PlTerm args[] = {timestamp, filename};
*t = 0;
*o = Pl_Mk_Compound(g_atomDropFile, 2, &args[0]);
}
//--------------------------------------------------------------------
// mouse_down / mouse_up ( <timestamp>,
// <windowID>,
// <which>,
// <button>,
// pressed / released,
// <clicks>,
// <x>,
// <y> ).
//--------------------------------------------------------------------
EVWRAPPER(evMouseButton)
{
sprintf(t,
"%s(%u,%u,%i,%s,%s,%i,%i,%i)",
e->type == SDL_MOUSEBUTTONDOWN ? "mouse_down" : "mouse_up",
e->button.timestamp,
e->button.windowID,
e->button.which,
evButtonName(e->button.button),
e->button.state == SDL_PRESSED ? "pressed" : "released",
e->button.clicks,
e->button.x,
e->button.y);
}
//--------------------------------------------------------------------
// mouse_motion ( <timestamp>,
// <windowID>,
// <which>,
// <state>,
// <x>,
// <y>,
// <xrel>,
// <yrel> ).
//--------------------------------------------------------------------
EVWRAPPER(evMouseMotion)
{
sprintf(t,
"mouse_motion(%u,%u,%u,%u,%i,%i,%i,%i)",
e->motion.timestamp,
e->motion.windowID,
e->motion.which,
e->motion.state,
e->motion.x,
e->motion.y,
e->motion.xrel,
e->motion.yrel
);
}
//--------------------------------------------------------------------
// mouse_wheel ( <timestamp>,
// <windowID>,
// <which>,
// <state>,
// <x>,
// <y> ).
//--------------------------------------------------------------------
EVWRAPPER(evMouseWheel) {
sprintf(t,
"mouse_wheel(%u,%u,%u,%i,%i)",
e->motion.timestamp,
e->motion.windowID,
e->motion.which,
e->motion.x,
e->motion.y);
}
//--------------------------------------------------------------------
// user_event( <timestamp>, <windowID>, <code>, <positive>, <positive>).
//--------------------------------------------------------------------
EVWRAPPER_TERM(evUserEvent)
{
PlTerm args[] = {
Pl_Mk_Integer((PlLong)e->user.timestamp),
Pl_Mk_Integer((PlLong)e->user.windowID),
Pl_Mk_Integer(e->user.code),
Pl_Mk_Positive((PlLong)e->user.data1),
Pl_Mk_Positive((PlLong)e->user.data2)
};
*t = 0;
*o = Pl_Mk_Compound(g_atomUserEvent, 5, &args[0]);
}
//--------------------------------------------------------------------
// dollar_gesture/event( <timestamp>, <windowID>, <code>, <positive>, <positive>).
//--------------------------------------------------------------------
EVWRAPPER_TERM(evDollarEvent)
{
int dollarEventType = (e->type == SDL_DOLLARGESTURE)
? g_atomDollarGesture
: g_atomDollarRecord;
#ifdef __DEBUG__
fprintf(stdout, "DOLLARx: type: %i\n", e->type);
#endif
PlTerm args[] = {
Pl_Mk_Integer((PlLong)e->dgesture.timestamp),
Pl_Mk_Integer((PlLong)e->dgesture.touchId),
Pl_Mk_Integer((PlLong)e->dgesture.gestureId),
Pl_Mk_Positive((PlLong)e->dgesture.numFingers),
Pl_Mk_Float((double)e->dgesture.error),
Pl_Mk_Float((double)e->dgesture.x),
Pl_Mk_Float((double)e->dgesture.y)
};
*t = 0;
*o = Pl_Mk_Compound(dollarEventType, 7, &args[0]);
}
//--------------------------------------------------------------------
// Window Event -> Descriptive String for functor use.
//--------------------------------------------------------------------
const char* evWindowType(int id) {
switch(id) {
case SDL_WINDOWEVENT_SHOWN: return "shown";
case SDL_WINDOWEVENT_HIDDEN: return "hidden";
case SDL_WINDOWEVENT_EXPOSED: return "exposed";
case SDL_WINDOWEVENT_MOVED: return "moved";
case SDL_WINDOWEVENT_RESIZED: return "resized";
case SDL_WINDOWEVENT_SIZE_CHANGED: return "size_changed";
case SDL_WINDOWEVENT_MINIMIZED: return "minimized";
case SDL_WINDOWEVENT_MAXIMIZED: return "maximized";
case SDL_WINDOWEVENT_RESTORED: return "restored";
case SDL_WINDOWEVENT_ENTER: return "enter";
case SDL_WINDOWEVENT_LEAVE: return "leave";
case SDL_WINDOWEVENT_FOCUS_GAINED: return "focus_gained";
case SDL_WINDOWEVENT_FOCUS_LOST: return "focus_lost";
case SDL_WINDOWEVENT_CLOSE: return "close";
default: return "unknown";
}
}
//--------------------------------------------------------------------
// Mouse Event -> Descriptive button name for functor use.
//--------------------------------------------------------------------
const char* evButtonName(Uint32 id) {
switch(id) {
case SDL_BUTTON_LEFT: return "left";
case SDL_BUTTON_MIDDLE: return "middle";
case SDL_BUTTON_RIGHT: return "right";
case SDL_BUTTON_X1: return "x1";
case SDL_BUTTON_X2: return "x2";
default: return "unknown";
}
}
//--------------------------------------------------------------------
//
//
//
// Circle functions
//
//
//
//--------------------------------------------------------------------
/**
* Pseudo-SDL: SDL_RenderDrawCircle
*
* @param PlLong renderer a valid handle to an SDL_Renderer*
* @param PlLong x0 circle x-position of centre
* @param PlLong y0 circle y-position of centre
* @param PlLong radius pixel radius of the circle
*
* @return PL_TRUE
*/
PlBool gp_SDL_RenderDrawCircle(PlLong renderer, PlLong x0, PlLong y0, PlLong radius)
{
SDL_Renderer *rndr = (SDL_Renderer*)renderer;
int x = radius;
int y = 0;
int decisionOver2 = 1 - x;
while(x >= y)
{
SDL_RenderDrawPoint(rndr, x + x0, y + y0);
SDL_RenderDrawPoint(rndr, y + x0, x + y0);
SDL_RenderDrawPoint(rndr, -x + x0, y + y0);
SDL_RenderDrawPoint(rndr, -y + x0, x + y0);
SDL_RenderDrawPoint(rndr, -x + x0, -y + y0);
SDL_RenderDrawPoint(rndr, -y + x0, -x + y0);
SDL_RenderDrawPoint(rndr, x + x0, -y + y0);
SDL_RenderDrawPoint(rndr, y + x0, -x + y0);
y++;
if (decisionOver2>0) {
decisionOver2 += ((y - (--x))<<1)+1;
}
else {
decisionOver2 += (y<<1)+1;
}
}
return PL_TRUE;
}
/**
* Pseudo-SDL: SDL_RenderFillCircle
*
* @param PlLong renderer a valid handle to an SDL_Renderer*
* @param PlLong x0 circle x-position of centre
* @param PlLong y0 circle y-position of centre
* @param PlLong radius pixel radius of the circle
*
* @return PL_TRUE
*/
PlBool gp_SDL_RenderFillCircle(PlLong renderer, PlLong x0, PlLong y0, PlLong radius)
{
SDL_Renderer *rndr = (SDL_Renderer*)renderer;
int x = radius;
int y = 0;
int decisionOver2 = 1 - x;
while(x >= y)
{
SDL_RenderDrawLine(rndr, x + x0, y + y0, x + x0, -y + y0);
SDL_RenderDrawLine(rndr, y + x0, x + y0, y + x0, -x + y0);
SDL_RenderDrawLine(rndr, -x + x0, y + y0, -x + x0, -y + y0);
SDL_RenderDrawLine(rndr, -y + x0, x + y0, -y + x0, -x + y0);
y++;
if (decisionOver2>0) {
decisionOver2 += ((y - (--x))<<1)+1;
}
else {
decisionOver2 += (y<<1)+1;
}
}
return PL_TRUE;
}
//--------------------------------------------------------------------
//
// Texture / Surface / Renderer Functions
//
//--------------------------------------------------------------------
PlBool gp_SDL_LoadBMP(char* filename, PlLong *surface)
{
SDL_Surface *image = SDL_LoadBMP(filename);
if (!image) {
return RETURN_SDL_FAIL(SDL_LoadBMP);
}
*surface = (PlLong)image;
return PL_TRUE;
}
PlBool gp_SDL_FreeSurface(PlLong surface)
{
SDL_FreeSurface((SDL_Surface*)surface);
return PL_TRUE;
}
PlBool gp_SDL_CreateTextureFromSurface(PlLong rndr, PlLong bitmap, PlLong *texture)
{
SDL_Texture* tex = SDL_CreateTextureFromSurface(
(SDL_Renderer*)rndr,
(SDL_Surface*)bitmap);
if (!tex) {
RETURN_SDL_FAIL(SDL_CreateTextureFromSurface);
}
*texture = (PlLong)tex;
return PL_TRUE;
}
PlBool gp_SDL_RenderCopyDefaults(PlLong dstRenderer, PlLong srcTexture)
{
SDL_RenderCopy(
(SDL_Renderer*)dstRenderer,