-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
mod.ts
1660 lines (1530 loc) · 39.9 KB
/
mod.ts
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
// deno-lint-ignore-file no-unused-vars
import {
cstring,
i32,
type SizedFFIType,
Struct,
u16,
u32,
u64,
u8,
} from "./byte_type.ts";
let DENO_SDL2_PATH: string | undefined;
try {
DENO_SDL2_PATH = Deno.env.get("DENO_SDL2_PATH");
} catch (_) {
// ignore, this can only fail if permission is not given
}
function isMacos() {
return Deno.build.os === "darwin";
}
function isLinux() {
return Deno.build.os === "linux";
}
function isWindows() {
return Deno.build.os === "windows";
}
const OS_PREFIX = Deno.build.os === "windows" ? "" : "lib";
const OS_SUFFIX = Deno.build.os === "windows"
? ".dll"
: Deno.build.os === "darwin"
? ".dylib"
: ".so";
function getLibraryPath(lib: string): string {
lib = `${OS_PREFIX}${lib}${OS_SUFFIX}`;
if (DENO_SDL2_PATH) {
return `${DENO_SDL2_PATH}/${lib}`;
} else {
return lib;
}
}
const sdl2 = Deno.dlopen(getLibraryPath("SDL2"), {
"SDL_Init": {
"parameters": ["u32"],
"result": "i32",
},
"SDL_InitSubSystem": {
"parameters": ["u32"],
"result": "i32",
},
"SDL_QuitSubSystem": {
"parameters": ["u32"],
"result": "i32",
},
"SDL_GetPlatform": {
"parameters": [],
"result": "pointer",
},
"SDL_GetError": {
"parameters": [],
"result": "pointer",
},
"SDL_PollEvent": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_WaitEvent": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_GetCurrentVideoDriver": {
"parameters": [],
"result": "pointer",
},
"SDL_CreateWindow": {
"parameters": [
"buffer",
"i32",
"i32",
"i32",
"i32",
"u32",
],
"result": "pointer",
},
"SDL_DestroyWindow": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_GetWindowSize": {
"parameters": ["pointer", "pointer", "pointer"],
"result": "i32",
},
"SDL_GetWindowPosition": {
"parameters": ["pointer", "pointer", "pointer"],
"result": "i32",
},
"SDL_GetWindowFlags": {
"parameters": ["pointer"],
"result": "u32",
},
"SDL_SetWindowTitle": {
"parameters": ["pointer", "pointer"],
"result": "i32",
},
"SDL_SetWindowIcon": {
"parameters": ["pointer", "pointer"],
"result": "i32",
},
"SDL_SetWindowPosition": {
"parameters": ["pointer", "i32", "i32"],
"result": "i32",
},
"SDL_SetWindowSize": {
"parameters": ["pointer", "i32", "i32"],
"result": "i32",
},
"SDL_SetWindowFullscreen": {
"parameters": ["pointer", "u32"],
"result": "i32",
},
"SDL_SetWindowMinimumSize": {
"parameters": ["pointer", "i32", "i32"],
"result": "i32",
},
"SDL_SetWindowMaximumSize": {
"parameters": ["pointer", "i32", "i32"],
"result": "i32",
},
"SDL_SetWindowBordered": {
"parameters": ["pointer", "i32"],
"result": "i32",
},
"SDL_SetWindowResizable": {
"parameters": ["pointer", "i32"],
"result": "i32",
},
"SDL_SetWindowInputFocus": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_SetWindowGrab": {
"parameters": ["pointer", "i32"],
"result": "i32",
},
"SDL_CreateRenderer": {
"parameters": ["pointer", "i32", "u32"],
"result": "pointer",
},
"SDL_SetRenderDrawColor": {
"parameters": ["pointer", "u8", "u8", "u8", "u8"],
"result": "i32",
},
"SDL_RenderClear": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_SetRenderDrawBlendMode": {
"parameters": ["pointer", "u32"],
"result": "i32",
},
"SDL_RenderPresent": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_RenderDrawPoint": {
"parameters": ["pointer", "i32", "i32"],
"result": "i32",
},
"SDL_RenderDrawPoints": {
"parameters": ["pointer", "pointer", "i32"],
"result": "i32",
},
"SDL_RenderDrawLine": {
"parameters": ["pointer", "i32", "i32", "i32", "i32"],
"result": "i32",
},
"SDL_RenderDrawLines": {
"parameters": ["pointer", "pointer", "i32"],
"result": "i32",
},
"SDL_RenderDrawRect": {
"parameters": ["pointer", "pointer"],
"result": "i32",
},
"SDL_RenderDrawRects": {
"parameters": ["pointer", "pointer", "i32"],
"result": "i32",
},
"SDL_RenderFillRect": {
"parameters": ["pointer", "pointer"],
"result": "i32",
},
"SDL_RenderFillRects": {
"parameters": ["pointer", "pointer", "i32"],
"result": "i32",
},
"SDL_RenderCopy": {
"parameters": ["pointer", "pointer", "pointer", "pointer"],
"result": "i32",
},
"SDL_RenderCopyEx": {
"parameters": [
"pointer",
"pointer",
"pointer",
"pointer",
"f32",
"pointer",
"u32",
],
"result": "i32",
},
"SDL_RenderReadPixels": {
"parameters": ["pointer", "pointer", "u32", "pointer", "i32"],
"result": "i32",
},
"SDL_CreateTexture": {
"parameters": ["pointer", "u32", "i32", "i32", "i32"],
"result": "pointer",
},
"SDL_DestroyTexture": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_QueryTexture": {
"parameters": [
"pointer",
"buffer",
"buffer",
"buffer",
"buffer",
],
"result": "i32",
},
"SDL_SetTextureColorMod": {
"parameters": ["pointer", "u8", "u8", "u8"],
"result": "i32",
},
"SDL_SetTextureAlphaMod": {
"parameters": ["pointer", "u8"],
"result": "i32",
},
"SDL_UpdateTexture": {
"parameters": ["pointer", "buffer", "buffer", "i32"],
"result": "i32",
},
"SDL_LoadBMP_RW": {
"parameters": ["buffer"],
"result": "pointer",
},
"SDL_CreateTextureFromSurface": {
"parameters": ["pointer", "pointer"],
"result": "pointer",
},
"SDL_GetWindowWMInfo": {
"parameters": ["pointer", "pointer"],
"result": "i32",
},
"SDL_GetVersion": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_Metal_CreateView": {
"parameters": ["pointer"],
"result": "pointer",
},
"SDL_RaiseWindow": {
"parameters": ["pointer"],
"result": "i32",
},
"SDL_GetKeyName": {
"parameters": ["i32"],
"result": "pointer",
},
"SDL_StartTextInput": {
"parameters": [],
"result": "i32",
},
"SDL_StopTextInput": {
"parameters": [],
"result": "i32",
},
"SDL_RWFromMem": {
"parameters": ["buffer", "i32"],
"result": "pointer",
},
});
const SDL2_Image_symbols = {
"IMG_Init": {
"parameters": ["u32"],
"result": "u32",
},
"IMG_Load": {
"parameters": ["buffer"],
"result": "pointer",
},
"IMG_Load_RW": {
"parameters": ["pointer"],
"result": "pointer",
},
} as const;
const SDL2_TTF_symbols = {
"TTF_Init": {
"parameters": [],
"result": "u32",
},
"TTF_OpenFont": {
"parameters": ["buffer", "i32"],
"result": "pointer",
},
"TTF_OpenFontRW": {
"parameters": ["pointer", "i32", "i32"],
"result": "pointer",
},
"TTF_RenderText_Solid": {
"parameters": ["pointer", "buffer", "pointer"],
"result": "pointer",
},
"TTF_RenderText_Shaded": {
"parameters": ["pointer", "pointer", "pointer", "pointer"],
"result": "pointer",
},
"TTF_RenderText_Blended": {
"parameters": ["pointer", "buffer", "pointer"],
"result": "pointer",
},
"TTF_CloseFont": {
"parameters": ["pointer"],
"result": "i32",
},
"TTF_Quit": {
"parameters": [],
"result": "i32",
},
} as const;
let sdl2Image: Deno.DynamicLibrary<typeof SDL2_Image_symbols>,
sdl2Font: Deno.DynamicLibrary<typeof SDL2_TTF_symbols>;
try {
sdl2Image = Deno.dlopen(getLibraryPath("SDL2_image"), SDL2_Image_symbols);
} catch (_e) {
console.log("SDL2_image not loaded. Some features will not be available.");
}
try {
sdl2Font = Deno.dlopen(getLibraryPath("SDL2_ttf"), SDL2_TTF_symbols);
} catch (_e) {
console.log("SDL2_ttf not loaded. Some features will not be available.");
}
let context_alive = false;
function init() {
if (context_alive) {
return;
}
context_alive = true;
const result = sdl2.symbols.SDL_Init(0);
if (result != 0) {
const errPtr = sdl2.symbols.SDL_GetError();
const view = new Deno.UnsafePointerView(errPtr!);
throw new Error(`SDL_Init failed: ${view.getCString()}`);
}
const platform = sdl2.symbols.SDL_GetPlatform();
const view = new Deno.UnsafePointerView(platform!);
console.log(`SDL2 initialized on ${view.getCString()}`);
// Initialize subsystems
// SDL_INIT_EVENTS
{
const result = sdl2.symbols.SDL_InitSubSystem(0x00000001);
if (result != 0) {
const errPtr = sdl2.symbols.SDL_GetError();
const view = new Deno.UnsafePointerView(errPtr!);
throw new Error(`SDL_InitSubSystem failed: ${view.getCString()}`);
}
}
// SDL_INIT_VIDEO
{
const result = sdl2.symbols.SDL_InitSubSystem(0x00000010);
if (result != 0) {
const errPtr = sdl2.symbols.SDL_GetError();
const view = new Deno.UnsafePointerView(errPtr!);
throw new Error(`SDL_InitSubSystem failed: ${view.getCString()}`);
}
}
// SDL_INIT_IMAGE
{
const result = sdl2.symbols.SDL_InitSubSystem(0x00000004);
if (result != 0) {
const errPtr = sdl2.symbols.SDL_GetError();
const view = new Deno.UnsafePointerView(errPtr!);
throw new Error(`SDL_InitSubSystem failed: ${view.getCString()}`);
}
}
// IMG_Init
{
// TIF = 4, WEBP = 8
sdl2Image?.symbols.IMG_Init(1 | 2); // png and jpg
}
// SDL_INIT_TTF
{
const result = sdl2.symbols.SDL_InitSubSystem(0x00000100);
if (result != 0) {
const errPtr = sdl2.symbols.SDL_GetError();
const view = new Deno.UnsafePointerView(errPtr!);
throw new Error(`SDL_InitSubSystem failed: ${view.getCString()}`);
}
}
// TTF_Init
{
sdl2Font?.symbols.TTF_Init();
}
}
init();
/**
* An enum that contains structures for the different event types.
*/
export enum EventType {
First = 0,
Quit = 0x100,
AppTerminating = 0x101,
AppLowMemory = 0x102,
AppWillEnterBackground = 0x103,
AppDidEnterBackground = 0x104,
AppWillEnterForeground = 0x105,
AppDidEnterForeground = 0x106,
WindowEvent = 0x200,
KeyDown = 0x300,
KeyUp = 0x301,
TextEditing = 0x302,
TextInput = 0x303,
MouseMotion = 0x400,
MouseButtonDown = 0x401,
MouseButtonUp = 0x402,
MouseWheel = 0x403,
// JoyAxisMotion = 0x600,
// JoyBallMotion = 0x601,
// JoyHatMotion = 0x602,
// JoyButtonDown = 0x603,
// JoyButtonUp = 0x604,
// JoyDeviceAdded = 0x605,
// JoyDeviceRemoved = 0x606,
// ControllerAxisMotion = 0x650,
// ControllerButtonDown = 0x651,
// ControllerButtonUp = 0x652,
// ControllerDeviceAdded = 0x653,
// ControllerDeviceRemoved = 0x654,
// ControllerDeviceRemapped = 0x655,
// FingerDown = 0x700,
// FingerUp = 0x701,
// FingerMotion = 0x702,
// DollarGesture = 0x800,
// DollarRecord = 0x801,
// MultiGesture = 0x802,
// ClipboardUpdate = 0x900,
// DropFile = 0x1000,
// DropText = 0x1001,
// DropBegin = 0x1002,
// DropComplete = 0x1003,
AudioDeviceAdded = 0x1100,
AudioDeviceRemoved = 0x1101,
// RenderTargetsReset = 0x2000,
// RenderDeviceReset = 0x2001,
User = 0x8000,
Last = 0xFFFF,
Draw,
}
const _raw = Symbol("raw");
const enc = new TextEncoder();
function asCString(str: string): Uint8Array {
return enc.encode(`${str}\0`);
}
function throwSDLError(): never {
const error = sdl2.symbols.SDL_GetError();
const view = Deno.UnsafePointerView.getCString(error!);
throw new Error(`SDL Error: ${view}`);
}
/**
* SDL2 canvas.
*/
export class Canvas {
constructor(
private window: Deno.PointerValue,
private target: Deno.PointerValue,
) {}
serialize(): ArrayBuffer {
return new BigUint64Array([Deno.UnsafePointer.value(this.target)]).buffer;
}
static deserialize(data: ArrayBuffer): Canvas {
const [ptr] = new BigUint64Array(data);
return new Canvas(null!, Deno.UnsafePointer.create(ptr));
}
/**
* Set the color used for drawing operations (Rect, Line and Clear).
* @param r the red value used to draw on the rendering target
* @param g the green value used to draw on the rendering target
* @param b the blue value used to draw on the rendering target
* @param a the alpha value used to draw on the rendering target; usually SDL_ALPHA_OPAQUE (255).
*/
setDrawColor(r: number, g: number, b: number, a: number) {
const ret = sdl2.symbols.SDL_SetRenderDrawColor(this.target, r, g, b, a);
if (ret < 0) {
throwSDLError();
}
}
/**
* Clear the current rendering target with the drawing color.
*/
clear() {
const ret = sdl2.symbols.SDL_RenderClear(this.target);
if (ret < 0) {
throwSDLError();
}
}
/**
* Update the screen with any rendering performed since the previous call.
*/
present() {
sdl2.symbols.SDL_RenderPresent(this.target);
}
/**
* Draw a point on the current rendering target.
* @param x the x coordinate of the point
* @param y the y coordinate of the point
*/
drawPoint(x: number, y: number) {
const ret = sdl2.symbols.SDL_RenderDrawPoint(this.target, x, y);
if (ret < 0) {
throwSDLError();
}
}
/**
* Draw multiple points on the current rendering target.
* @param points an array of Points (x, y) structures that represent the points to draw
*/
drawPoints(points: [number, number][]) {
const intArray = new Int32Array(points.flat());
const ret = sdl2.symbols.SDL_RenderDrawPoints(
this.target,
Deno.UnsafePointer.of(intArray),
intArray.length,
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Draw a line on the current rendering target.
* @param x1 the x coordinate of the start point
* @param y1 the y coordinate of the start point
* @param x2 the x coordinate of the end point
* @param y2 the y coordinate of the end point
*/
drawLine(x1: number, y1: number, x2: number, y2: number) {
const ret = sdl2.symbols.SDL_RenderDrawLine(this.target, x1, y1, x2, y2);
if (ret < 0) {
throwSDLError();
}
}
/**
* Draw a series of connected lines on the current rendering target.
* @param points an array of Points (x, y) structures representing points along the lines
*/
drawLines(points: [number, number][]) {
const intArray = new Int32Array(points.flat());
const ret = sdl2.symbols.SDL_RenderDrawLines(
this.target,
Deno.UnsafePointer.of(intArray),
intArray.length,
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Draw a rectangle on the current rendering target.
* @param x the x coordinate of the rectangle
* @param y the y coordinate of the rectangle
* @param w the width of the rectangle
* @param h the height of the rectangle
*/
drawRect(x: number, y: number, w: number, h: number) {
const intArray = new Int32Array([x, y, w, h]);
const ret = sdl2.symbols.SDL_RenderDrawRect(
this.target,
Deno.UnsafePointer.of(intArray),
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Draw some number of rectangles on the current rendering target.
* @param rects an array of Rect (x, y, w, h) structures representing the rectangles to draw
*/
drawRects(rects: [number, number, number, number][]) {
const intArray = new Int32Array(rects.flat());
const ret = sdl2.symbols.SDL_RenderDrawRects(
this.target,
Deno.UnsafePointer.of(intArray),
intArray.length,
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Fill a rectangle on the current rendering target with the drawing color.
* @param x the x coordinate of the rectangle
* @param y the y coordinate of the rectangle
* @param w the width of the rectangle
* @param h the height of the rectangle
*/
fillRect(x: number, y: number, w: number, h: number) {
const intArray = new Int32Array([x, y, w, h]);
const ret = sdl2.symbols.SDL_RenderFillRect(
this.target,
Deno.UnsafePointer.of(intArray),
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Fill some number of rectangles on the current rendering target with the drawing color.
* @param rects an array of Rect (x, y, w, h) structures representing the rectangles to fill
*/
fillRects(rects: [number, number, number, number][]) {
const intArray = new Int32Array(rects.flat());
const ret = sdl2.symbols.SDL_RenderFillRects(
this.target,
Deno.UnsafePointer.of(intArray),
intArray.length,
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Copy a portion of the texture to the current rendering target.
* @param texture the source texture
* @param source the source rectangle, or null to copy the entire texture
* @param dest the destination rectangle, or null for the entire rendering target; the texture will be stretched to fill the given rectangle
*/
copy(texture: Texture, source?: Rect, dest?: Rect) {
const ret = sdl2.symbols.SDL_RenderCopy(
this.target,
texture[_raw],
source ? Deno.UnsafePointer.of(source[_raw]) : null,
dest ? Deno.UnsafePointer.of(dest[_raw]) : null,
);
if (ret < 0) {
throwSDLError();
}
}
/**
* TextureCreator is a helper class for creating textures.
* @returns a TextureCreator object for use with creating textures
*/
textureCreator(): TextureCreator {
return new TextureCreator(this.target);
}
/**
* Create a font from a file, using a specified point size.
* @param path the path to the font file
* @param size point size to use for the newly-opened font
* @returns a Font object for use with rendering text
*/
loadFont(path: string, size: number): Font {
const raw = sdl2Font.symbols.TTF_OpenFont(asCString(path), size);
return new Font(raw);
}
loadFontRaw(data: Uint8Array, size: number): Font {
const rwops = sdl2.symbols.SDL_RWFromMem(data, data.byteLength);
if (rwops === null) {
throwSDLError();
}
const raw = sdl2Font.symbols.TTF_OpenFontRW(rwops, 1, size);
if (raw === null) {
throwSDLError();
}
return new Font(raw);
}
}
/**
* Font is a helper class for rendering text.
*/
export class Font {
[_raw]: Deno.PointerValue;
constructor(raw: Deno.PointerValue) {
this[_raw] = raw;
}
/**
* Render a solid color version of the text.
* @param text text to render, in Latin1 encoding.
* @param color the foreground color of the text
* @returns a Texture object
*/
renderSolid(text: string, color: Color): Surface {
const raw = sdl2Font.symbols.TTF_RenderText_Solid(
this[_raw],
asCString(text),
color[_raw],
);
return new Surface(raw);
}
/**
* Render text at high quality to a new ARGB surface.
* @param text text to render, in Latin1 encoding.
* @param color the foreground color of the text
* @returns a Texture object
*/
renderBlended(text: string, color: Color): Surface {
const raw = sdl2Font.symbols.TTF_RenderText_Blended(
this[_raw],
asCString(text),
color[_raw],
);
return new Surface(raw);
}
}
/**
* Color is a helper class for representing colors.
*/
export class Color {
[_raw]: Deno.PointerValue;
constructor(r: number, g: number, b: number, a: number = 0xff) {
const raw = new Uint8Array([r, g, b, a]);
this[_raw] = Deno.UnsafePointer.of(raw);
}
}
/**
* A structure that contains pixel format information.
* @see https://wiki.libsdl.org/SDL2/SDL_PixelFormat
*/
export enum PixelFormat {
Unknown = 0,
Index1LSB = 286261504,
Index1MSB = 287310080,
Index4LSB = 303039488,
Index4MSB = 304088064,
Index8 = 318769153,
RGB332 = 336660481,
XRGB4444 = 353504258,
XBGR4444 = 357698562,
XRGB1555 = 353570562,
XBGR1555 = 357764866,
ARGB4444 = 355602434,
RGBA4444 = 356651010,
ABGR4444 = 359796738,
BGRA4444 = 360845314,
ARGB1555 = 355667970,
RGBA5551 = 356782082,
ABGR1555 = 359862274,
BGRA5551 = 360976386,
RGB565 = 353701890,
BGR565 = 357896194,
RGB24 = 386930691,
BGR24 = 390076419,
XRGB8888 = 370546692,
RGBX8888 = 371595268,
XBGR8888 = 374740996,
BGRX8888 = 375789572,
ARGB8888 = 372645892,
RGBA8888 = 373694468,
ABGR8888 = 376840196,
BGRA8888 = 377888772,
ARGB2101010 = 372711428,
YV12 = 842094169,
IYUV = 1448433993,
YUY2 = 844715353,
UYVY = 1498831189,
YVYU = 1431918169,
}
/**
* An enumeration of texture access patterns.
* @see https://wiki.libsdl.org/SDL2/SDL_TextureAccess
*/
export enum TextureAccess {
Static = 0,
Streaming = 1,
Target = 2,
}
/**
* A class used to create textures.
*/
export class TextureCreator {
constructor(private raw: Deno.PointerValue) {}
/**
* Create a texture for a rendering context.
* @param format the format of the texture
* @param access one of the enumerated values in TextureAccess or a number
* @param w the width of the texture in pixels
* @param h the height of the texture in pixels
* @returns a Texture object
*
* @example
* ```ts
* const creator = canvas.textureCreator();
* const texture = creator.createTexture(
* PixelFormat.RGBA8888,
* TextureAccess.Static,
* 640,
* 480,
* );
* ```
*/
createTexture(
format: number,
access: number,
w: number,
h: number,
): Texture {
const raw = sdl2.symbols.SDL_CreateTexture(
this.raw,
format,
access,
w,
h,
);
if (raw === null) {
throwSDLError();
}
return new Texture(raw);
}
/**
* Create a texture from a surface.
* @param surface the surface used to create the texture
* @returns a Texture object
*/
createTextureFromSurface(surface: Surface): Texture {
const raw = sdl2.symbols.SDL_CreateTextureFromSurface(
this.raw,
surface[_raw],
);
if (raw === null) {
throwSDLError();
}
return new Texture(raw);
}
}
/**
* An interface that contains information about a texture.
*/
export interface TextureQuery {
format: number;
access: TextureAccess;
w: number;
h: number;
}
/**
* A structure that contains an efficient, driver-specific representation of pixel data.
* @see https://wiki.libsdl.org/SDL2/SDL_Texture
*/
export class Texture {
[_raw]: Deno.PointerValue;
constructor(private raw: Deno.PointerValue) {
this[_raw] = raw;
}
/**
* Query the attributes of a texture.
* @returns a TextureQuery
*/
query(): TextureQuery {
const format = new Uint32Array(1);
const access = new Uint32Array(1);
const w = new Uint32Array(1);
const h = new Uint32Array(1);
const ret = sdl2.symbols.SDL_QueryTexture(
this.raw,
format,
access,
w,
h,
);
if (ret < 0) {
throwSDLError();
}
return {
format: format[0],
access: access[0],
w: w[0],
h: h[0],
};
}
/**
* Set an additional color value multiplied into render copy operations.
* @param r the red color value
* @param g the green color value
* @param b the blue color value
*/
setColorMod(r: number, g: number, b: number) {
const ret = sdl2.symbols.SDL_SetTextureColorMod(
this.raw,
r,
g,
b,
);
if (ret < 0) {
throwSDLError();
}
}
/**
* Set an additional alpha value multiplied into render copy operations.
* @param a the source alpha value multiplied into copy operations
*/
setAlphaMod(a: number) {
const ret = sdl2.symbols.SDL_SetTextureAlphaMod(this.raw, a);
if (ret < 0) {
throwSDLError();
}
}
/**
* Update the given texture rectangle with new pixel data.
* @param pixels the raw pixel data in the format of the texture
* @param pitch the number of bytes in a row of pixel data, including padding between lines
* @param rect an Rect representing the area to update, or null to update the entire texture
*/
update(pixels: Uint8Array, pitch: number, rect?: Rect) {
const ret = sdl2.symbols.SDL_UpdateTexture(
this.raw,
rect ? rect[_raw] : null,
pixels,
pitch,
);
if (ret < 0) {
throwSDLError();
}
}
}
/**
* A structure that contains the definition of a rectangle, with the origin at the upper left.
* @see https://wiki.libsdl.org/SDL2/SDL_Rect
*/
export class Rect {
[_raw]: Uint32Array;
constructor(x: number, y: number, w: number, h: number) {
this[_raw] = new Uint32Array([x, y, w, h]);
}
/**
* The x coordinate of the rectangle.
*/
get x(): number {
return this[_raw][0];
}
/**
* The y coordinate of the rectangle.
*/
get y(): number {
return this[_raw][1];
}
/**