slifer 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +12 -12
- package/src/color.ts +15 -0
- package/src/engine/rectangle.ts +25 -0
- package/src/engine/vector.ts +49 -0
- package/src/engine.ts +24 -0
- package/src/modules/graphics.ts +83 -50
- package/src/slifer.test.ts +8 -0
- package/src/slifer.ts +154 -213
package/package.json
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
2
|
+
"name": "slifer",
|
3
|
+
"version": "0.2.0",
|
4
|
+
"description": "A game framework made for bun and typescript",
|
5
|
+
"module": "index.ts",
|
6
|
+
"devDependencies": {
|
7
|
+
"@types/bun": "latest",
|
8
|
+
"typedoc": "^0.26.7"
|
9
|
+
},
|
10
|
+
"peerDependencies": {
|
11
|
+
"typescript": "^5.0.0"
|
12
|
+
},
|
13
|
+
"type": "module"
|
14
14
|
}
|
package/src/color.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { ptr } from "bun:ffi";
|
2
|
+
import Vector2 from "./vector";
|
3
|
+
|
4
|
+
class Rectangle {
|
5
|
+
private readonly pointer;
|
6
|
+
public position: Vector2;
|
7
|
+
public size: Vector2;
|
8
|
+
|
9
|
+
constructor(position: Vector2, size: Vector2) {
|
10
|
+
const arr = new Uint32Array(4);
|
11
|
+
arr[0] = position.x;
|
12
|
+
arr[1] = position.y;
|
13
|
+
arr[2] = size.x;
|
14
|
+
arr[3] = size.y;
|
15
|
+
this.pointer = ptr(arr);
|
16
|
+
this.position = position;
|
17
|
+
this.size = size;
|
18
|
+
}
|
19
|
+
|
20
|
+
static empty() {
|
21
|
+
return new Rectangle(new Vector2(0, 0), new Vector2(0, 0));
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
export default Rectangle;
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class Vector2 {
|
2
|
+
public x;
|
3
|
+
public y;
|
4
|
+
|
5
|
+
constructor(x: number, y: number) {
|
6
|
+
this.x = x;
|
7
|
+
this.y = y;
|
8
|
+
}
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Creates a Vector2 with the components 1,1
|
12
|
+
*/
|
13
|
+
static one() {
|
14
|
+
return new Vector2(1, 1);
|
15
|
+
}
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Creates a Vector2 with the components 1,0
|
19
|
+
*/
|
20
|
+
static unitX() {
|
21
|
+
return new Vector2(1, 0);
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Creates a Vector2 with the components 0,1
|
26
|
+
*/
|
27
|
+
static unitY() {
|
28
|
+
return new Vector2(0, 1);
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Creates a vector2 with component 0,0
|
33
|
+
*/
|
34
|
+
static zero() {
|
35
|
+
return new Vector2(0, 0);
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Adds two vectors together
|
40
|
+
*
|
41
|
+
* @param vec1 Vector2 object
|
42
|
+
* @param vec2 Vector2 object
|
43
|
+
*/
|
44
|
+
static add(vec1: Vector2, vec2: Vector2) {
|
45
|
+
return new Vector2(vec1.x + vec2.x, vec1.y + vec2.y);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
export default Vector2;
|
package/src/engine.ts
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
import { libsdl, libimage, libttf } from "./ffi";
|
2
|
+
import Global from "./global";
|
3
|
+
|
4
|
+
//@ts-expect-error
|
5
|
+
const fontFile = await import("./Jost-Bold.ttf");
|
6
|
+
|
7
|
+
export function initLibraries(): void {
|
8
|
+
const baseInit = libsdl.symbols.SDL_Init(0x00000020);
|
9
|
+
if (baseInit != 0) throw `SDL failed to initialize`;
|
10
|
+
|
11
|
+
const imageInit = libimage.symbols.IMG_Init(3);
|
12
|
+
if (imageInit != 3) throw `SDL Image failed to initialize`;
|
13
|
+
|
14
|
+
const ttfInit = libttf.symbols.TTF_Init();
|
15
|
+
if (ttfInit != 0) throw `SDL TTF failed to initialize`;
|
16
|
+
|
17
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
18
|
+
Buffer.from(fontFile.default),
|
19
|
+
24
|
20
|
+
);
|
21
|
+
|
22
|
+
if (tempFont == null) throw `Default font loading failed`;
|
23
|
+
Global.ptrFont = tempFont;
|
24
|
+
}
|
package/src/modules/graphics.ts
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
import { libimage, libsdl, libttf } from "../ffi";
|
2
2
|
import Global from "../global";
|
3
|
-
import { type Pointer, ptr } from
|
4
|
-
import
|
5
|
-
import
|
3
|
+
import { type Pointer, ptr } from "bun:ffi";
|
4
|
+
import Rectangle from "../engine/rectangle";
|
5
|
+
import Color from "../color";
|
6
|
+
import Vector2 from "../engine/vector";
|
6
7
|
|
7
8
|
/** @internal */
|
8
9
|
class Graphics {
|
@@ -15,8 +16,8 @@ class Graphics {
|
|
15
16
|
|
16
17
|
/**
|
17
18
|
* Create a new color. All values are from 0-255
|
18
|
-
*
|
19
|
-
* @param r red value
|
19
|
+
*
|
20
|
+
* @param r red value
|
20
21
|
* @param g green value
|
21
22
|
* @param b blue value
|
22
23
|
* @param a alpha value
|
@@ -28,37 +29,47 @@ class Graphics {
|
|
28
29
|
}
|
29
30
|
|
30
31
|
/**
|
31
|
-
* Sets the background of the window to a color of choice.
|
32
|
-
*
|
32
|
+
* Sets the background of the window to a color of choice.
|
33
|
+
*
|
33
34
|
* Make sure this is put in the top level of the while loop
|
34
35
|
* as it will clear the renderer.
|
35
|
-
*
|
36
|
+
*
|
36
37
|
* @param color Color object. Make using Slifer.Graphics.makeColor
|
37
38
|
*/
|
38
39
|
setBackground(color: Color) {
|
39
|
-
libsdl.symbols.SDL_SetRenderDrawColor(
|
40
|
+
libsdl.symbols.SDL_SetRenderDrawColor(
|
41
|
+
Global.ptrRenderer,
|
42
|
+
color.r,
|
43
|
+
color.g,
|
44
|
+
color.b,
|
45
|
+
color.a
|
46
|
+
);
|
40
47
|
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
41
48
|
}
|
42
49
|
|
43
50
|
/**
|
44
51
|
* Loads a new image
|
45
|
-
*
|
52
|
+
*
|
46
53
|
* @param path string path to image
|
47
54
|
* @returns Image ready to draw
|
48
55
|
*/
|
49
|
-
loadImage(path: string)
|
56
|
+
loadImage(path: string): Image {
|
50
57
|
const _path = Buffer.from(path + "\x00");
|
51
|
-
//@ts-expect-error Buffer and CString differences
|
52
58
|
const surface = libimage.symbols.IMG_Load(_path);
|
53
59
|
if (surface == null) throw `Image failed to load`;
|
54
|
-
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
|
60
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
|
61
|
+
Global.ptrRenderer,
|
62
|
+
surface
|
63
|
+
);
|
55
64
|
if (texture == null) throw `Image failed to be created`;
|
56
65
|
return new Image(texture);
|
57
66
|
}
|
58
67
|
|
68
|
+
draw(image: Image, position: Vector2) {}
|
69
|
+
|
59
70
|
/**
|
60
71
|
* Method to draw the image to the screen
|
61
|
-
*
|
72
|
+
*
|
62
73
|
* @param image Image object to draw. Made using Slifer.Graphics.loadImage
|
63
74
|
* @param x x position to draw image
|
64
75
|
* @param y y position to draw image
|
@@ -67,14 +78,28 @@ class Graphics {
|
|
67
78
|
* @param ys (optional) scale of y axis
|
68
79
|
* @param flip (optional) horizontal flip
|
69
80
|
*/
|
70
|
-
|
81
|
+
drawEx(
|
82
|
+
image: Image,
|
83
|
+
position: Vector2,
|
84
|
+
clipRectangle?: Rectangle,
|
85
|
+
rotation?: number,
|
86
|
+
xs?: number,
|
87
|
+
ys?: number,
|
88
|
+
flip?: true
|
89
|
+
) {
|
71
90
|
const _dest = new Uint32Array(4);
|
72
91
|
const wArr = new Uint32Array(1);
|
73
92
|
const hArr = new Uint32Array(1);
|
74
93
|
let srcRect: null | Uint32Array = null;
|
75
|
-
|
94
|
+
|
76
95
|
if (clipRectangle == undefined) {
|
77
|
-
libsdl.symbols.SDL_QueryTexture(
|
96
|
+
libsdl.symbols.SDL_QueryTexture(
|
97
|
+
(image as any).pointer,
|
98
|
+
null,
|
99
|
+
null,
|
100
|
+
ptr(wArr),
|
101
|
+
ptr(hArr)
|
102
|
+
);
|
78
103
|
} else {
|
79
104
|
srcRect = new Uint32Array(4);
|
80
105
|
srcRect[0] = clipRectangle.x;
|
@@ -85,7 +110,6 @@ class Graphics {
|
|
85
110
|
hArr[0] = clipRectangle.height;
|
86
111
|
}
|
87
112
|
|
88
|
-
|
89
113
|
_dest[0] = position.x;
|
90
114
|
_dest[1] = position.y;
|
91
115
|
_dest[2] = wArr[0] * (xs ? xs : 1);
|
@@ -93,36 +117,53 @@ class Graphics {
|
|
93
117
|
const _center = new Uint32Array(2);
|
94
118
|
_center[0] = _dest[2] / 2;
|
95
119
|
_center[1] = _dest[3] / 2;
|
96
|
-
libsdl.symbols.SDL_RenderCopyEx(
|
120
|
+
libsdl.symbols.SDL_RenderCopyEx(
|
121
|
+
Global.ptrRenderer,
|
122
|
+
(image as any).pointer,
|
123
|
+
srcRect,
|
124
|
+
ptr(_dest),
|
125
|
+
rotation ? rotation : 0,
|
126
|
+
ptr(_center),
|
127
|
+
flip ? Number(flip) : 0
|
128
|
+
);
|
97
129
|
}
|
98
130
|
|
99
131
|
/**
|
100
132
|
* Method to draw text to the screen
|
101
|
-
*
|
133
|
+
*
|
102
134
|
* @param text the string of text to print
|
103
135
|
* @param x x position
|
104
136
|
* @param y y position
|
105
137
|
* @param color color of text. Made using Slifer.Graphics.makeColor.
|
106
138
|
*/
|
107
139
|
print(text: string, x: number, y: number, color: Color) {
|
108
|
-
|
109
140
|
// Create text buffer
|
110
|
-
const textBuffer = Buffer.from(text+"\x00");
|
141
|
+
const textBuffer = Buffer.from(text + "\x00");
|
111
142
|
|
112
|
-
// Get width and height of text
|
143
|
+
// Get width and height of text
|
113
144
|
const wArr = new Uint32Array(1);
|
114
145
|
const hArr = new Uint32Array(1);
|
115
|
-
|
116
|
-
|
146
|
+
libttf.symbols.TTF_SizeText(
|
147
|
+
Global.ptrFont,
|
148
|
+
textBuffer,
|
149
|
+
ptr(wArr),
|
150
|
+
ptr(hArr)
|
151
|
+
);
|
117
152
|
|
118
153
|
// Define color
|
119
|
-
const _col = (
|
154
|
+
const _col = (color.r << 0) + (color.g << 8) + (color.b << 16);
|
120
155
|
|
121
156
|
// Create texture
|
122
|
-
|
123
|
-
|
157
|
+
const surface = libttf.symbols.TTF_RenderText_Solid(
|
158
|
+
Global.ptrFont,
|
159
|
+
textBuffer,
|
160
|
+
_col
|
161
|
+
);
|
124
162
|
if (surface == null) throw `Surface creation failed on print`;
|
125
|
-
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
|
163
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
|
164
|
+
Global.ptrRenderer,
|
165
|
+
surface
|
166
|
+
);
|
126
167
|
if (texture == null) throw `Texture creation failed on print`;
|
127
168
|
|
128
169
|
// Create destination
|
@@ -133,45 +174,37 @@ class Graphics {
|
|
133
174
|
destArr[3] = hArr[0];
|
134
175
|
|
135
176
|
// Draw text
|
136
|
-
libsdl.symbols.SDL_RenderCopy(
|
177
|
+
libsdl.symbols.SDL_RenderCopy(
|
178
|
+
Global.ptrRenderer,
|
179
|
+
texture,
|
180
|
+
null,
|
181
|
+
ptr(destArr)
|
182
|
+
);
|
137
183
|
}
|
138
184
|
|
139
185
|
/**
|
140
186
|
* Sets the font to a ttf file in your project
|
141
|
-
*
|
187
|
+
*
|
142
188
|
* @param path relative path to font
|
143
189
|
* @param pt size of text
|
144
190
|
*/
|
145
191
|
setFont(path: string, pt: number) {
|
146
|
-
|
147
|
-
|
192
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
193
|
+
Buffer.from(path + "\x00"),
|
194
|
+
pt
|
195
|
+
);
|
148
196
|
if (tempFont == null) throw `Font loading failed`;
|
149
197
|
Global.ptrFont = tempFont;
|
150
198
|
}
|
151
199
|
}
|
152
200
|
|
153
201
|
class Image {
|
154
|
-
|
155
202
|
private pointer;
|
156
|
-
|
203
|
+
|
157
204
|
constructor(texture: Pointer) {
|
158
205
|
this.pointer = texture;
|
159
206
|
}
|
160
207
|
}
|
161
208
|
|
162
|
-
class Color {
|
163
|
-
readonly r;
|
164
|
-
readonly g;
|
165
|
-
readonly b;
|
166
|
-
readonly a;
|
167
|
-
|
168
|
-
constructor(r: number, g: number, b: number, a: number) {
|
169
|
-
this.r = r;
|
170
|
-
this.g = g;
|
171
|
-
this.b = b;
|
172
|
-
this.a = a;
|
173
|
-
}
|
174
|
-
}
|
175
|
-
|
176
209
|
/** @internal */
|
177
|
-
export default Graphics;
|
210
|
+
export default Graphics;
|
package/src/slifer.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { libimage, libsdl, libttf } from "./ffi";
|
2
|
+
import { initLibraries } from "./engine";
|
2
3
|
import Global from "./global";
|
3
4
|
import { ptr } from "bun:ffi";
|
4
5
|
import Graphics from "./modules/graphics";
|
@@ -6,229 +7,169 @@ import Keyboard from "./modules/keyboard";
|
|
6
7
|
import Mouse from "./modules/mouse";
|
7
8
|
import { version } from "../package.json";
|
8
9
|
|
9
|
-
//@ts-expect-error
|
10
|
-
const fontFile = await import("./Jost-Bold.ttf");
|
11
|
-
|
12
10
|
/** @internal */
|
13
11
|
class Window {
|
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
|
-
}
|
12
|
+
public width: number;
|
13
|
+
public height: number;
|
14
|
+
public title: string;
|
15
|
+
|
16
|
+
constructor(title: string, width: number, height: number) {
|
17
|
+
this.width = width;
|
18
|
+
this.height = height;
|
19
|
+
this.title = title;
|
20
|
+
}
|
21
|
+
|
22
|
+
setSize(width: number, height: number): void {
|
23
|
+
libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
|
24
|
+
}
|
25
|
+
|
26
|
+
setTitle(title: string): void {
|
27
|
+
libsdl.symbols.SDL_SetWindowTitle(
|
28
|
+
Global.ptrWindow,
|
29
|
+
Buffer.from(title + "\x00")
|
30
|
+
);
|
31
|
+
}
|
32
|
+
|
33
|
+
setFullscreen(flag: boolean): void {
|
34
|
+
libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
|
35
|
+
}
|
36
|
+
|
37
|
+
centerWindow(): void {
|
38
|
+
libsdl.symbols.SDL_SetWindowPosition(
|
39
|
+
Global.ptrWindow,
|
40
|
+
0x2fff0000,
|
41
|
+
0x2fff0000
|
42
|
+
);
|
43
|
+
}
|
44
|
+
|
45
|
+
setPosition(x: number, y: number): void {
|
46
|
+
libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
|
47
|
+
}
|
51
48
|
}
|
52
49
|
|
53
50
|
/** @interal */
|
54
51
|
export class SliferClass {
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
if (baseInit != 0) throw `SDL failed to initialize`;
|
70
|
-
|
71
|
-
const imageInit = libimage.symbols.IMG_Init(3);
|
72
|
-
if (imageInit != 3) throw `SDL Image failed to initialize`;
|
73
|
-
|
74
|
-
const ttfInit = libttf.symbols.TTF_Init();
|
75
|
-
if (ttfInit != 0) throw `SDL TTF failed to initialize`;
|
76
|
-
|
77
|
-
/*
|
78
|
-
if (process.platform == "darwin") {
|
79
|
-
const tempFont = libttf.symbols.TTF_OpenFont(
|
80
|
-
Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
|
81
|
-
12,
|
82
|
-
);
|
83
|
-
if (tempFont == null) throw `Default font loading failed`;
|
84
|
-
Global.ptrFont = tempFont;
|
85
|
-
} else if (process.platform == "linux") {
|
86
|
-
const tempFont = libttf.symbols.TTF_OpenFont(
|
87
|
-
Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
|
88
|
-
12,
|
89
|
-
);
|
90
|
-
if (tempFont == null) throw `Default font loading failed`;
|
91
|
-
Global.ptrFont = tempFont;
|
52
|
+
private isRunning: boolean = true;
|
53
|
+
private lastFrame: number = 0;
|
54
|
+
private firstFrame: number = 0;
|
55
|
+
|
56
|
+
// Modules
|
57
|
+
Graphics = new Graphics();
|
58
|
+
Keyboard = new Keyboard();
|
59
|
+
Mouse = new Mouse();
|
60
|
+
|
61
|
+
// Public Variables
|
62
|
+
public dt: number = 0;
|
63
|
+
|
64
|
+
constructor() {
|
65
|
+
initLibraries();
|
92
66
|
}
|
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
|
-
if (_ren == null) throw `Renderer Creation failed`;
|
129
|
-
Global.ptrRenderer = _ren;
|
130
|
-
|
131
|
-
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
132
|
-
|
133
|
-
return new Window(title, width, height);
|
134
|
-
}
|
135
|
-
|
136
|
-
/**
|
137
|
-
* @returns if the window should close
|
138
|
-
*/
|
139
|
-
shouldClose(): boolean {
|
140
|
-
// Clear the renderer
|
141
|
-
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
142
|
-
|
143
|
-
// Setup deltatime
|
144
|
-
this.lastFrame = this.firstFrame;
|
145
|
-
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
146
|
-
|
147
|
-
this.dt = ((this.firstFrame - this.lastFrame) * 1000 / Number(libsdl.symbols.SDL_GetPerformanceFrequency()));
|
148
|
-
|
149
|
-
|
150
|
-
// Poll Events
|
151
|
-
const eventArray = new Uint16Array(32);
|
152
|
-
const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
|
153
|
-
|
154
|
-
if (isEvent) {
|
155
|
-
switch (eventArray[0]) {
|
156
|
-
// Quit event
|
157
|
-
case 256:
|
158
|
-
this.isRunning = false;
|
159
|
-
break;
|
160
|
-
// Keydown event
|
161
|
-
case 768:
|
162
|
-
const _dscancode = eventArray[8];
|
163
|
-
const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
|
164
|
-
const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
|
165
|
-
Keyboard.setKeyDown(_dname.toString().toLowerCase());
|
166
|
-
break;
|
167
|
-
// Keyup event
|
168
|
-
case 769:
|
169
|
-
const _uscancode = eventArray[8];
|
170
|
-
const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
|
171
|
-
const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
|
172
|
-
Keyboard.setKeyUp(_uname.toString().toLowerCase());
|
173
|
-
break;
|
174
|
-
// Mouse down event
|
175
|
-
case 1025:
|
176
|
-
const _dbtn = eventArray[8] - 256;
|
177
|
-
Mouse.setButtonDown(_dbtn);
|
178
|
-
break;
|
179
|
-
// Mouse up event
|
180
|
-
case 1026:
|
181
|
-
const _ubtn = eventArray[8];
|
182
|
-
Mouse.setButtonUp(_ubtn);
|
183
|
-
break;
|
184
|
-
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* @param title Title of window
|
70
|
+
* @param width Width of window
|
71
|
+
* @param height Height of window
|
72
|
+
*/
|
73
|
+
createWindow(title: string, width: number, height: number): Window {
|
74
|
+
// Creating cstring buffer from string
|
75
|
+
const _title = Buffer.from(title + "\x00");
|
76
|
+
|
77
|
+
// Creating window pointer
|
78
|
+
const _win = libsdl.symbols.SDL_CreateWindow(
|
79
|
+
_title,
|
80
|
+
0x2fff0000,
|
81
|
+
0x2fff0000,
|
82
|
+
width,
|
83
|
+
height,
|
84
|
+
0
|
85
|
+
);
|
86
|
+
if (_win == null) throw `Window creation failed`;
|
87
|
+
Global.ptrWindow = _win;
|
88
|
+
|
89
|
+
// Creating renderer pointer
|
90
|
+
const vsyncHint = 0x00000004;
|
91
|
+
const _ren = libsdl.symbols.SDL_CreateRenderer(
|
92
|
+
Global.ptrWindow,
|
93
|
+
-1,
|
94
|
+
vsyncHint
|
95
|
+
);
|
96
|
+
if (_ren == null) throw `Renderer Creation failed`;
|
97
|
+
Global.ptrRenderer = _ren;
|
98
|
+
|
99
|
+
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
100
|
+
|
101
|
+
return new Window(title, width, height);
|
185
102
|
}
|
186
103
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
104
|
+
/**
|
105
|
+
* @returns if the window should close
|
106
|
+
*/
|
107
|
+
shouldClose(): boolean {
|
108
|
+
// Clear the renderer
|
109
|
+
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
110
|
+
|
111
|
+
// Setup deltatime
|
112
|
+
this.lastFrame = this.firstFrame;
|
113
|
+
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
114
|
+
|
115
|
+
this.dt =
|
116
|
+
((this.firstFrame - this.lastFrame) * 1000) /
|
117
|
+
Number(libsdl.symbols.SDL_GetPerformanceFrequency());
|
118
|
+
|
119
|
+
// Poll Events
|
120
|
+
const eventArray = new Uint16Array(32);
|
121
|
+
const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
|
122
|
+
|
123
|
+
if (isEvent) {
|
124
|
+
switch (eventArray[0]) {
|
125
|
+
// Quit event
|
126
|
+
case 256:
|
127
|
+
this.isRunning = false;
|
128
|
+
break;
|
129
|
+
// Keydown event
|
130
|
+
case 768:
|
131
|
+
const _dscancode = eventArray[8];
|
132
|
+
const _dkey =
|
133
|
+
libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
|
134
|
+
const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
|
135
|
+
Keyboard.setKeyDown(_dname.toString().toLowerCase());
|
136
|
+
break;
|
137
|
+
// Keyup event
|
138
|
+
case 769:
|
139
|
+
const _uscancode = eventArray[8];
|
140
|
+
const _ukey =
|
141
|
+
libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
|
142
|
+
const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
|
143
|
+
Keyboard.setKeyUp(_uname.toString().toLowerCase());
|
144
|
+
break;
|
145
|
+
// Mouse down event
|
146
|
+
case 1025:
|
147
|
+
const _dbtn = eventArray[8] - 256;
|
148
|
+
Mouse.setButtonDown(_dbtn);
|
149
|
+
break;
|
150
|
+
// Mouse up event
|
151
|
+
case 1026:
|
152
|
+
const _ubtn = eventArray[8];
|
153
|
+
Mouse.setButtonUp(_ubtn);
|
154
|
+
break;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
return !this.isRunning;
|
159
|
+
}
|
204
160
|
|
205
|
-
|
206
|
-
|
207
|
-
|
161
|
+
/**
|
162
|
+
* Slifers quit method
|
163
|
+
*/
|
164
|
+
quit() {
|
165
|
+
libttf.symbols.TTF_CloseFont(Global.ptrFont);
|
166
|
+
libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
|
167
|
+
libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
|
168
|
+
libsdl.symbols.SDL_Quit();
|
169
|
+
}
|
208
170
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
}
|
171
|
+
getVersion() {
|
172
|
+
return version;
|
173
|
+
}
|
213
174
|
}
|
214
175
|
|
215
|
-
export class Rectangle {
|
216
|
-
private readonly pointer;
|
217
|
-
readonly x;
|
218
|
-
readonly y;
|
219
|
-
readonly width;
|
220
|
-
readonly height;
|
221
|
-
|
222
|
-
constructor(x: number, y: number, width: number, height: number) {
|
223
|
-
const arr = new Uint32Array(4);
|
224
|
-
arr[0] = x;
|
225
|
-
arr[1] = y;
|
226
|
-
arr[2] = width;
|
227
|
-
arr[3] = height;
|
228
|
-
this.pointer = ptr(arr);
|
229
|
-
this.x = x;
|
230
|
-
this.y = y;
|
231
|
-
this.width = width;
|
232
|
-
this.height = height;
|
233
|
-
}
|
234
|
-
}
|