slifer 0.2.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,100 +1,66 @@
1
- import Vector2 from "../engine/vector2";
2
- import { libsdl, libttf } from "../ffi";
3
- import { ptr } from 'bun:ffi';
4
- import Font from '../engine/font';
5
- import type Image from "../engine/image";
6
- import Render from "../engine/render";
7
- import Window from '../engine/window';
8
- import type Color from "../engine/color";
9
- import Rectangle from "../engine/rectangle";
10
- import Canvas from '../engine/canvas';
11
-
12
- /** @internal */
13
- export default class Graphics {
14
-
15
- public render() : void {
16
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Render.pointer, Render.surface);
17
- if (texture == null) throw `Render texture creation failed`;
18
- libsdl.symbols.SDL_RenderCopy(
19
- Render.pointer,
20
- texture,
21
- null,
22
- null
23
- );
24
- libsdl.symbols.SDL_RenderPresent(Render.pointer);
25
- libsdl.symbols.SDL_DestroyTexture(texture);
26
- }
27
-
28
- public draw(drawable: Image | Canvas, x: number, y: number, scaleX?: number, scaleY?: number) : void {
29
-
30
- const destArray = new Uint32Array(4);
31
- destArray[0] = x;
32
- destArray[1] = y;
33
- destArray[2] = drawable.width * (scaleX ? scaleX : 1);
34
- destArray[3] = drawable.height * (scaleY ? scaleY : 1);
35
-
36
- libsdl.symbols.SDL_UpperBlitScaled(
37
- (drawable as any).pointer,
38
- null,
39
- Render.surface,
40
- ptr(destArray)
41
- );
42
- }
43
-
44
- public setBackground(color: Color) : void {
45
- const rect = new Rectangle(
46
- new Vector2(0, 0),
47
- new Vector2(Window.size.x, Window.size.y)
48
- );
49
- this.drawRect(rect, color);
50
- }
51
-
52
- public drawRect(rectangle: Rectangle, color: Color) {
53
- const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
54
-
55
- const rect = new Uint32Array(4);
56
- rect[0] = rectangle.position.x;
57
- rect[1] = rectangle.position.y;
58
- rect[2] = rectangle.size.x;
59
- rect[3] = rectangle.size.y;
60
-
61
- libsdl.symbols.SDL_FillRect(Render.surface, ptr(rect), _col);
62
- }
63
-
64
- public print(text: string, x: number, y: number, font: Font, color: Color) {
65
- const wArr = new Uint32Array(1);
66
- const hArr = new Uint32Array(1);
67
-
68
- libttf.symbols.TTF_SizeText(
69
- (font as any).pointer,
70
- //@ts-expect-error
71
- Buffer.from(text+"\x00"),
72
- ptr(wArr),
73
- ptr(hArr)
74
- );
75
-
76
- const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
77
-
78
- const surface = libttf.symbols.TTF_RenderText_Solid(
79
- (font as any).pointer,
80
- //@ts-expect-error
81
- Buffer.from(text+'\x00'),
82
- _col
83
- );
84
- if (surface == null) throw `Rendering text failed`;
85
-
86
- const destRect = new Uint32Array(4);
87
- destRect[0] = x;
88
- destRect[1] = y;
89
- destRect[2] = wArr[0];
90
- destRect[3] = hArr[0];
91
-
92
- libsdl.symbols.SDL_UpperBlit(
93
- surface,
94
- null,
95
- Render.surface,
96
- ptr(destRect)
97
- );
98
-
99
- }
100
- }
1
+ import Vector2 from "../engine/vector2";
2
+ import { libsdl, libttf } from "../ffi";
3
+ import { ptr } from 'bun:ffi';
4
+ import Font from '../engine/font';
5
+ import type Image from "../engine/image";
6
+ import Render from "../engine/render";
7
+ import Window from '../engine/window';
8
+ import type Color from "../engine/color";
9
+ import Rectangle from "../engine/rectangle";
10
+ import Cursor from '../engine/cursor';
11
+
12
+ type Drawable = Image;
13
+
14
+ /** @internal */
15
+ export default class Graphics {
16
+ public render() {
17
+ libsdl.symbols.SDL_RenderPresent(Render.pointer);
18
+ }
19
+
20
+ public draw(drawable: Drawable, x: number, y: number) {
21
+ (drawable as any).destArray[0] = x;
22
+ (drawable as any).destArray[1] = y;
23
+
24
+ libsdl.symbols.SDL_RenderCopy(
25
+ Render.pointer,
26
+ (drawable as any).pointer,
27
+ null,
28
+ ptr((drawable as any).destArray)
29
+ )
30
+ }
31
+
32
+ public drawScaled(drawable: Drawable, x: number, y: number, xscale: number, yscale: number) {
33
+ (drawable as any).destArray[0] = x;
34
+ (drawable as any).destArray[1] = y;
35
+ (drawable as any).destArray[2] = drawable.width * xscale;
36
+ (drawable as any).destArray[3] = drawable.height * yscale;
37
+
38
+ libsdl.symbols.SDL_RenderCopy(
39
+ Render.pointer,
40
+ (drawable as any).pointer,
41
+ null,
42
+ ptr((drawable as any).destArray)
43
+ )
44
+ }
45
+
46
+ private setColor(color: Color) {
47
+ libsdl.symbols.SDL_SetRenderDrawColor(Render.pointer, color.r, color.g, color.b, color.a);
48
+ }
49
+
50
+ public setBackground(color: Color) {
51
+ this.setColor(color);
52
+ libsdl.symbols.SDL_RenderClear(Render.pointer);
53
+ }
54
+
55
+ public drawRect(rectangle: Rectangle, color: Color) {
56
+ this.setColor(color);
57
+
58
+ const rect = new Uint32Array(4);
59
+ rect[0] = rectangle.position.x;
60
+ rect[1] = rectangle.position.y;
61
+ rect[2] = rectangle.size.x;
62
+ rect[3] = rectangle.size.y;
63
+
64
+ libsdl.symbols.SDL_RenderFillRect(Render.pointer, ptr(rect));
65
+ }
66
+ }
@@ -1,96 +1,100 @@
1
- import { libsdl } from "../ffi";
2
- import { toArrayBuffer } from "bun:ffi";
3
-
4
- /** @internal */
5
- export default class Keyboard {
6
-
7
- public static keyMap = new Map<string, number>();
8
-
9
- private static convertScancodeToKey(scancode: number): string {
10
- const keyFromScancode = libsdl.symbols.SDL_GetKeyFromScancode(scancode);
11
- const keyName = libsdl.symbols
12
- .SDL_GetKeyName(keyFromScancode)
13
- .toString()
14
- .toLocaleLowerCase();
15
-
16
- return keyName;
17
- }
18
-
19
- public static handleStates() {
20
- const keyState = libsdl.symbols.SDL_GetKeyboardState(null);
21
- if (keyState == null) throw `Keyboard state returned null.`;
22
- const keyDV = new DataView(toArrayBuffer(keyState, 0, 512));
23
- for (let kn = 0; kn < 512; kn++) {
24
- if (keyDV.getUint8(kn) == 1) {
25
- const keyName = this.convertScancodeToKey(kn);
26
- const kmGet = this.keyMap.get(keyName);
27
-
28
- if (kmGet == undefined || kmGet == 0) {
29
- this.keyMap.set(keyName, 1);
30
- } else if (kmGet == 1) {
31
- this.keyMap.set(keyName, 2);
32
- }
33
- } else if (keyDV.getUint8(kn) == 0) {
34
- const keyName = this.convertScancodeToKey(kn);
35
- this.keyMap.set(keyName, 0);
36
- }
37
- }
38
- }
39
-
40
- public isPressed(key: keys) : boolean {
41
- const kmGet = Keyboard.keyMap.get(key);
42
- if (kmGet == 1) return true;
43
- return false;
44
- }
45
-
46
- public isDown(key: keys) : boolean {
47
- const kmGet = Keyboard.keyMap.get(key);
48
- if (kmGet == 1 || kmGet == 2) return true;
49
- return false;
50
- }
51
- }
52
-
53
- type keys =
54
- | "a"
55
- | "b"
56
- | "c"
57
- | "d"
58
- | "e"
59
- | "f"
60
- | "g"
61
- | "h"
62
- | "i"
63
- | "j"
64
- | "k"
65
- | "l"
66
- | "m"
67
- | "n"
68
- | "o"
69
- | "p"
70
- | "q"
71
- | "r"
72
- | "s"
73
- | "t"
74
- | "u"
75
- | "v"
76
- | "w"
77
- | "x"
78
- | "y"
79
- | "z"
80
- | "1"
81
- | "2"
82
- | "3"
83
- | "4"
84
- | "5"
85
- | "6"
86
- | "7"
87
- | "8"
88
- | "9"
89
- | "0"
90
- | "space"
91
- | "caps lock"
92
- | "tab"
93
- | "left shift"
94
- | "right shift"
95
- | "left ctrl"
96
- | "escape";
1
+ import { libsdl } from "../ffi";
2
+ import { toArrayBuffer } from "bun:ffi";
3
+
4
+ /** @internal */
5
+ export default class Keyboard {
6
+
7
+ public static keyMap = new Map<string, number>();
8
+
9
+ private static convertScancodeToKey(scancode: number): string {
10
+ const keyFromScancode = libsdl.symbols.SDL_GetKeyFromScancode(scancode);
11
+ const keyName = libsdl.symbols
12
+ .SDL_GetKeyName(keyFromScancode)
13
+ .toString()
14
+ .toLocaleLowerCase();
15
+
16
+ return keyName;
17
+ }
18
+
19
+ public static handleStates() {
20
+ const keyState = libsdl.symbols.SDL_GetKeyboardState(null);
21
+ if (keyState == null) throw `Keyboard state returned null.`;
22
+ const keyDV = new DataView(toArrayBuffer(keyState, 0, 128));
23
+ for (let kn = 0; kn < 128; kn++) {
24
+ if (keyDV.getUint8(kn) == 1) {
25
+ const keyName = this.convertScancodeToKey(kn);
26
+ const kmGet = this.keyMap.get(keyName);
27
+
28
+
29
+ if (kmGet == undefined || kmGet == 0) {
30
+ this.keyMap.set(keyName, 1);
31
+ } else if (kmGet == 1) {
32
+ this.keyMap.set(keyName, 2);
33
+ }
34
+ } else if (keyDV.getUint8(kn) == 0) {
35
+ const keyName = this.convertScancodeToKey(kn);
36
+ this.keyMap.set(keyName, 0);
37
+ }
38
+ }
39
+ }
40
+
41
+ public isPressed(key: keys) : boolean {
42
+ const kmGet = Keyboard.keyMap.get(key);
43
+ if (kmGet == 1) return true;
44
+ return false;
45
+ }
46
+
47
+ public isDown(key: keys) : boolean {
48
+ const kmGet = Keyboard.keyMap.get(key);
49
+ if (kmGet == 1 || kmGet == 2) return true;
50
+ return false;
51
+ }
52
+ }
53
+
54
+ export type keys =
55
+ | "a"
56
+ | "b"
57
+ | "c"
58
+ | "d"
59
+ | "e"
60
+ | "f"
61
+ | "g"
62
+ | "h"
63
+ | "i"
64
+ | "j"
65
+ | "k"
66
+ | "l"
67
+ | "m"
68
+ | "n"
69
+ | "o"
70
+ | "p"
71
+ | "q"
72
+ | "r"
73
+ | "s"
74
+ | "t"
75
+ | "u"
76
+ | "v"
77
+ | "w"
78
+ | "x"
79
+ | "y"
80
+ | "z"
81
+ | "1"
82
+ | "2"
83
+ | "3"
84
+ | "4"
85
+ | "5"
86
+ | "6"
87
+ | "7"
88
+ | "8"
89
+ | "9"
90
+ | "0"
91
+ | "space"
92
+ | "caps lock"
93
+ | "tab"
94
+ | "left shift"
95
+ | "right shift"
96
+ | "left ctrl"
97
+ | "escape"
98
+ | "backspace"
99
+ | "."
100
+ | "return";
@@ -1,76 +1,76 @@
1
- import { libsdl } from "../ffi";
2
- import { ptr } from "bun:ffi";
3
- import Vector2 from "../engine/vector2";
4
-
5
- /** @internal */
6
- class Mouse {
7
- private static x: number;
8
- private static y: number;
9
-
10
- private static buttonMap = new Map<number, number>();
11
-
12
- static onRelease(button: number) {
13
- this.buttonMap.set(button, 0);
14
- }
15
-
16
- static onMove(x: number, y: number) {
17
- this.x = x;
18
- this.y = y;
19
- }
20
-
21
- static handleState() {
22
- const xArr = new Uint32Array(1);
23
- const yArr = new Uint32Array(1);
24
- const down = libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
25
- const bmGet = this.buttonMap.get(down);
26
-
27
- if (bmGet == undefined || bmGet == 0) {
28
- this.buttonMap.set(down, 1);
29
- } else if (bmGet == 1) {
30
- this.buttonMap.set(down, 2);
31
- }
32
- }
33
-
34
- private static getPressMap(button: number) {
35
- const bmGet = this.buttonMap.get(button);
36
- if (bmGet == 1) return true;
37
- return false;
38
- }
39
-
40
- private static getDownMap(button: number) {
41
- const bmGet = this.buttonMap.get(button);
42
- if (bmGet == 1 || bmGet == 2) return true;
43
- return false;
44
- }
45
-
46
- isPressed(button: buttons): boolean {
47
- switch (button) {
48
- case "left":
49
- return Mouse.getPressMap(1);
50
- case "middle":
51
- return Mouse.getPressMap(2);
52
- case "right":
53
- return Mouse.getPressMap(3);
54
- }
55
- }
56
-
57
- isDown(button: buttons): boolean {
58
- switch (button) {
59
- case "left":
60
- return Mouse.getDownMap(1);
61
- case "middle":
62
- return Mouse.getDownMap(2);
63
- case "right":
64
- return Mouse.getDownMap(3);
65
- }
66
- }
67
-
68
- getPosition(): Vector2 {
69
- return new Vector2(Mouse.x, Mouse.y);
70
- }
71
- }
72
-
73
- type buttons = "left" | "middle" | "right";
74
-
75
- /** @internal */
76
- export default Mouse;
1
+ import { libsdl } from "../ffi";
2
+ import { ptr } from "bun:ffi";
3
+ import Vector2 from "../engine/vector2";
4
+
5
+ /** @internal */
6
+ class Mouse {
7
+ private static x: number;
8
+ private static y: number;
9
+
10
+ private static buttonMap = new Map<number, number>();
11
+
12
+ static onRelease(button: number) {
13
+ this.buttonMap.set(button, 0);
14
+ }
15
+
16
+ static onMove(x: number, y: number) {
17
+ this.x = x;
18
+ this.y = y;
19
+ }
20
+
21
+ static handleState() {
22
+ const xArr = new Uint32Array(1);
23
+ const yArr = new Uint32Array(1);
24
+ const down = libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
25
+ const bmGet = this.buttonMap.get(down);
26
+
27
+ if (bmGet == undefined || bmGet == 0) {
28
+ this.buttonMap.set(down, 1);
29
+ } else if (bmGet == 1) {
30
+ this.buttonMap.set(down, 2);
31
+ }
32
+ }
33
+
34
+ private static getPressMap(button: number) {
35
+ const bmGet = this.buttonMap.get(button);
36
+ if (bmGet == 1) return true;
37
+ return false;
38
+ }
39
+
40
+ private static getDownMap(button: number) {
41
+ const bmGet = this.buttonMap.get(button);
42
+ if (bmGet == 1 || bmGet == 2) return true;
43
+ return false;
44
+ }
45
+
46
+ isPressed(button: buttons): boolean {
47
+ switch (button) {
48
+ case "left":
49
+ return Mouse.getPressMap(1);
50
+ case "middle":
51
+ return Mouse.getPressMap(2);
52
+ case "right":
53
+ return Mouse.getPressMap(3);
54
+ }
55
+ }
56
+
57
+ isDown(button: buttons): boolean {
58
+ switch (button) {
59
+ case "left":
60
+ return Mouse.getDownMap(1);
61
+ case "middle":
62
+ return Mouse.getDownMap(2);
63
+ case "right":
64
+ return Mouse.getDownMap(3);
65
+ }
66
+ }
67
+
68
+ getPosition(): Vector2 {
69
+ return new Vector2(Mouse.x, Mouse.y);
70
+ }
71
+ }
72
+
73
+ type buttons = "left" | "middle" | "right";
74
+
75
+ /** @internal */
76
+ export default Mouse;
@@ -1,43 +1,43 @@
1
- import { describe, expect, it } from "bun:test";
2
- import { initSDL, initSDLImage, initSDLTypeFont, initSDLMixer } from "./engine";
3
- import Vector2 from "./engine/vector2";
4
- import Render from "./engine/render";
5
- import Window from "./engine/window";
6
-
7
- describe("Initializing SDL ", () => {
8
- it("Should initialize without error ", () => {
9
- expect(() => initSDL()).not.toThrow(Error);
10
- });
11
- });
12
-
13
- describe("Initializing SDL Image ", () => {
14
- it("Should initialize without error ", () => {
15
- expect(() => initSDLImage()).not.toThrow(Error);
16
- });
17
- });
18
-
19
- describe("Initializing SDL TTF ", () => {
20
- it("Should initialize without error ", () => {
21
- expect(() => initSDLTypeFont()).not.toThrow(Error);
22
- });
23
- });
24
-
25
- describe("Initializing SDL Mixer ", () => {
26
- it("Should initialize without error ", () => {
27
- expect(() => initSDLMixer()).not.toThrow(Error);
28
- });
29
- });
30
-
31
- describe("Window Creation ", () => {
32
- it("Should create window without error", () => {
33
- expect(() =>
34
- Window.createWindow("Game", new Vector2(1, 1))
35
- ).not.toThrow(Error);
36
- });
37
- });
38
-
39
- describe("Renderer Creation ", () => {
40
- it("Should create renderer without error", () => {
41
- expect(() => Render.createRenderer()).not.toThrow(Error);
42
- });
43
- });
1
+ import { describe, expect, it } from "bun:test";
2
+ import { initSDL, initSDLImage, initSDLTypeFont, initSDLMixer } from "./engine";
3
+ import Vector2 from "./engine/vector2";
4
+ import Render from "./engine/render";
5
+ import Window from "./engine/window";
6
+
7
+ describe("Initializing SDL ", () => {
8
+ it("Should initialize without error ", () => {
9
+ expect(() => initSDL()).not.toThrow(Error);
10
+ });
11
+ });
12
+
13
+ describe("Initializing SDL Image ", () => {
14
+ it("Should initialize without error ", () => {
15
+ expect(() => initSDLImage()).not.toThrow(Error);
16
+ });
17
+ });
18
+
19
+ describe("Initializing SDL TTF ", () => {
20
+ it("Should initialize without error ", () => {
21
+ expect(() => initSDLTypeFont()).not.toThrow(Error);
22
+ });
23
+ });
24
+
25
+ describe("Initializing SDL Mixer ", () => {
26
+ it("Should initialize without error ", () => {
27
+ expect(() => initSDLMixer()).not.toThrow(Error);
28
+ });
29
+ });
30
+
31
+ describe("Window Creation ", () => {
32
+ it("Should create window without error", () => {
33
+ expect(() =>
34
+ Window.createWindow("Game", 1, 1)
35
+ ).not.toThrow(Error);
36
+ });
37
+ });
38
+
39
+ describe("Renderer Creation ", () => {
40
+ it("Should create renderer without error", () => {
41
+ expect(() => Render.createRenderer(1, 1)).not.toThrow(Error);
42
+ });
43
+ });