slifer 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slifer",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "A game framework made for bun and typescript",
5
5
  "module": "index.ts",
6
6
  "devDependencies": {
Binary file
@@ -2,6 +2,7 @@ import { libimage, libsdl, libttf } from "../ffi";
2
2
  import Global from "../global";
3
3
  import { type Pointer, ptr } from 'bun:ffi';
4
4
  import path from 'path';
5
+ import type { Rectangle, Vector2 } from "../slifer";
5
6
 
6
7
  /** @internal */
7
8
  class Graphics {
@@ -47,6 +48,7 @@ class Graphics {
47
48
  */
48
49
  loadImage(path: string) : Image {
49
50
  const _path = Buffer.from(path + "\x00");
51
+ //@ts-expect-error Buffer and CString differences
50
52
  const surface = libimage.symbols.IMG_Load(_path);
51
53
  if (surface == null) throw `Image failed to load`;
52
54
  const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
@@ -65,19 +67,33 @@ class Graphics {
65
67
  * @param ys (optional) scale of y axis
66
68
  * @param flip (optional) horizontal flip
67
69
  */
68
- draw(image: Image, x: number, y: number, rotation?: number, xs?: number, ys?: number, flip?: true) {
70
+ draw(image: Image, position: Vector2, clipRectangle?: Rectangle, rotation?: number, xs?: number, ys?: number, flip?: true) {
69
71
  const _dest = new Uint32Array(4);
70
72
  const wArr = new Uint32Array(1);
71
73
  const hArr = new Uint32Array(1);
72
- libsdl.symbols.SDL_QueryTexture((image as any).pointer, null, null, ptr(wArr), ptr(hArr));
73
- _dest[0] = x;
74
- _dest[1] = y;
74
+ let srcRect: null | Uint32Array = null;
75
+
76
+ if (clipRectangle == undefined) {
77
+ libsdl.symbols.SDL_QueryTexture((image as any).pointer, null, null, ptr(wArr), ptr(hArr));
78
+ } else {
79
+ srcRect = new Uint32Array(4);
80
+ srcRect[0] = clipRectangle.x;
81
+ srcRect[1] = clipRectangle.y;
82
+ srcRect[2] = clipRectangle.width;
83
+ srcRect[3] = clipRectangle.height;
84
+ wArr[0] = clipRectangle.width;
85
+ hArr[0] = clipRectangle.height;
86
+ }
87
+
88
+
89
+ _dest[0] = position.x;
90
+ _dest[1] = position.y;
75
91
  _dest[2] = wArr[0] * (xs ? xs : 1);
76
92
  _dest[3] = hArr[0] * (ys ? ys : 1);
77
93
  const _center = new Uint32Array(2);
78
94
  _center[0] = _dest[2] / 2;
79
95
  _center[1] = _dest[3] / 2;
80
- libsdl.symbols.SDL_RenderCopyEx(Global.ptrRenderer, (image as any).pointer, null, ptr(_dest), rotation ? rotation : 0, ptr(_center), flip ? Number(flip) : 0);
96
+ libsdl.symbols.SDL_RenderCopyEx(Global.ptrRenderer, (image as any).pointer, srcRect, ptr(_dest), rotation ? rotation : 0, ptr(_center), flip ? Number(flip) : 0);
81
97
  }
82
98
 
83
99
  /**
@@ -96,12 +112,14 @@ class Graphics {
96
112
  // Get width and height of text
97
113
  const wArr = new Uint32Array(1);
98
114
  const hArr = new Uint32Array(1);
115
+ //@ts-expect-error Buffer and CString differences
99
116
  libttf.symbols.TTF_SizeText(Global.ptrFont,textBuffer , ptr(wArr), ptr(hArr));
100
117
 
101
118
  // Define color
102
119
  const _col = ((color.r << 0) + (color.g << 8) + (color.b << 16));
103
120
 
104
121
  // Create texture
122
+ //@ts-expect-error Buffer and CString differences
105
123
  const surface = libttf.symbols.TTF_RenderText_Solid(Global.ptrFont, textBuffer, _col);
106
124
  if (surface == null) throw `Surface creation failed on print`;
107
125
  const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
@@ -125,6 +143,7 @@ class Graphics {
125
143
  * @param pt size of text
126
144
  */
127
145
  setFont(path: string, pt: number) {
146
+ //@ts-expect-error Buffer and CString differences
128
147
  const tempFont = libttf.symbols.TTF_OpenFont(Buffer.from(path+"\x00"), pt);
129
148
  if (tempFont == null) throw `Font loading failed`;
130
149
  Global.ptrFont = tempFont;
package/src/slifer.ts CHANGED
@@ -4,10 +4,10 @@ import { ptr } from "bun:ffi";
4
4
  import Graphics from "./modules/graphics";
5
5
  import Keyboard from "./modules/keyboard";
6
6
  import Mouse from "./modules/mouse";
7
- import { version } from '../package.json';
7
+ import { version } from "../package.json";
8
8
 
9
9
  //@ts-expect-error
10
- const fontFile = await import("../Jacquard_12/Jacquard12-Regular.ttf");
10
+ const fontFile = await import("./Jost-Bold.ttf");
11
11
 
12
12
  /** @internal */
13
13
  class Window {
@@ -21,36 +21,49 @@ class Window {
21
21
  this.title = title;
22
22
  }
23
23
 
24
- setSize(width: number, height: number) : void {
24
+ setSize(width: number, height: number): void {
25
25
  libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
26
26
  }
27
27
 
28
- setTitle(title: string) : void {
29
- libsdl.symbols.SDL_SetWindowTitle(Global.ptrWindow, Buffer.from(title+'\x00'));
28
+ setTitle(title: string): void {
29
+ libsdl.symbols.SDL_SetWindowTitle(
30
+ Global.ptrWindow,
31
+ //@ts-expect-error Buffer and CString differences
32
+ Buffer.from(title + "\x00")
33
+ );
30
34
  }
31
35
 
32
- setFullscreen(flag: boolean) : void {
36
+ setFullscreen(flag: boolean): void {
33
37
  libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
34
38
  }
35
39
 
36
- centerWindow() : void {
37
- libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, 0x2FFF0000, 0x2FFF0000);
40
+ centerWindow(): void {
41
+ libsdl.symbols.SDL_SetWindowPosition(
42
+ Global.ptrWindow,
43
+ 0x2fff0000,
44
+ 0x2fff0000
45
+ );
38
46
  }
39
47
 
40
- setPosition(x: number, y: number) : void {
48
+ setPosition(x: number, y: number): void {
41
49
  libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
42
50
  }
43
51
  }
44
52
 
45
53
  /** @interal */
46
54
  export class SliferClass {
47
- isRunning: boolean = true;
55
+ private isRunning: boolean = true;
56
+ private lastFrame: number = 0;
57
+ private firstFrame: number = 0;
48
58
 
49
59
  // Modules
50
60
  Graphics = new Graphics();
51
61
  Keyboard = new Keyboard();
52
62
  Mouse = new Mouse();
53
63
 
64
+ // Public Variables
65
+ public dt: number = 0;
66
+
54
67
  constructor() {
55
68
  const baseInit = libsdl.symbols.SDL_Init(0x00000020);
56
69
  if (baseInit != 0) throw `SDL failed to initialize`;
@@ -79,12 +92,13 @@ export class SliferClass {
79
92
  }
80
93
  */
81
94
 
82
-
83
-
84
95
  const tempFont = libttf.symbols.TTF_OpenFont(
85
- Buffer.from(fontFile.default), 24);
86
- if (tempFont == null) throw `Default font loading failed`;
87
- Global.ptrFont = tempFont;
96
+ //@ts-expect-error Buffer and CString differences
97
+ Buffer.from(fontFile.default),
98
+ 24
99
+ );
100
+ if (tempFont == null) throw `Default font loading failed`;
101
+ Global.ptrFont = tempFont;
88
102
  }
89
103
 
90
104
  /**
@@ -98,12 +112,13 @@ export class SliferClass {
98
112
 
99
113
  // Creating window pointer
100
114
  const _win = libsdl.symbols.SDL_CreateWindow(
115
+ //@ts-expect-error Buffer and CString differences
101
116
  _title,
102
- 0x2FFF0000,
103
- 0x2FFF0000,
117
+ 0x2fff0000,
118
+ 0x2fff0000,
104
119
  width,
105
120
  height,
106
- 0,
121
+ 0
107
122
  );
108
123
  if (_win == null) throw `Window creation failed`;
109
124
  Global.ptrWindow = _win;
@@ -113,6 +128,8 @@ export class SliferClass {
113
128
  if (_ren == null) throw `Renderer Creation failed`;
114
129
  Global.ptrRenderer = _ren;
115
130
 
131
+ this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
132
+
116
133
  return new Window(title, width, height);
117
134
  }
118
135
 
@@ -123,6 +140,13 @@ export class SliferClass {
123
140
  // Clear the renderer
124
141
  libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
125
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
+
126
150
  // Poll Events
127
151
  const eventArray = new Uint16Array(32);
128
152
  const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
@@ -177,3 +201,34 @@ export class SliferClass {
177
201
  return version;
178
202
  }
179
203
  }
204
+
205
+ export class Vector2 {
206
+ x;
207
+ y;
208
+
209
+ constructor(x: number, y: number) {
210
+ this.x = x;
211
+ this.y = y;
212
+ }
213
+ }
214
+
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
+ }
Binary file
@@ -1,93 +0,0 @@
1
- Copyright 2023 The Soft Type Project Authors (https://github.com/scfried/soft-type-jacquard)
2
-
3
- This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
- This license is copied below, and is also available with a FAQ at:
5
- https://openfontlicense.org
6
-
7
-
8
- -----------------------------------------------------------
9
- SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
- -----------------------------------------------------------
11
-
12
- PREAMBLE
13
- The goals of the Open Font License (OFL) are to stimulate worldwide
14
- development of collaborative font projects, to support the font creation
15
- efforts of academic and linguistic communities, and to provide a free and
16
- open framework in which fonts may be shared and improved in partnership
17
- with others.
18
-
19
- The OFL allows the licensed fonts to be used, studied, modified and
20
- redistributed freely as long as they are not sold by themselves. The
21
- fonts, including any derivative works, can be bundled, embedded,
22
- redistributed and/or sold with any software provided that any reserved
23
- names are not used by derivative works. The fonts and derivatives,
24
- however, cannot be released under any other type of license. The
25
- requirement for fonts to remain under this license does not apply
26
- to any document created using the fonts or their derivatives.
27
-
28
- DEFINITIONS
29
- "Font Software" refers to the set of files released by the Copyright
30
- Holder(s) under this license and clearly marked as such. This may
31
- include source files, build scripts and documentation.
32
-
33
- "Reserved Font Name" refers to any names specified as such after the
34
- copyright statement(s).
35
-
36
- "Original Version" refers to the collection of Font Software components as
37
- distributed by the Copyright Holder(s).
38
-
39
- "Modified Version" refers to any derivative made by adding to, deleting,
40
- or substituting -- in part or in whole -- any of the components of the
41
- Original Version, by changing formats or by porting the Font Software to a
42
- new environment.
43
-
44
- "Author" refers to any designer, engineer, programmer, technical
45
- writer or other person who contributed to the Font Software.
46
-
47
- PERMISSION & CONDITIONS
48
- Permission is hereby granted, free of charge, to any person obtaining
49
- a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
- redistribute, and sell modified and unmodified copies of the Font
51
- Software, subject to the following conditions:
52
-
53
- 1) Neither the Font Software nor any of its individual components,
54
- in Original or Modified Versions, may be sold by itself.
55
-
56
- 2) Original or Modified Versions of the Font Software may be bundled,
57
- redistributed and/or sold with any software, provided that each copy
58
- contains the above copyright notice and this license. These can be
59
- included either as stand-alone text files, human-readable headers or
60
- in the appropriate machine-readable metadata fields within text or
61
- binary files as long as those fields can be easily viewed by the user.
62
-
63
- 3) No Modified Version of the Font Software may use the Reserved Font
64
- Name(s) unless explicit written permission is granted by the corresponding
65
- Copyright Holder. This restriction only applies to the primary font name as
66
- presented to the users.
67
-
68
- 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
- Software shall not be used to promote, endorse or advertise any
70
- Modified Version, except to acknowledge the contribution(s) of the
71
- Copyright Holder(s) and the Author(s) or with their explicit written
72
- permission.
73
-
74
- 5) The Font Software, modified or unmodified, in part or in whole,
75
- must be distributed entirely under this license, and must not be
76
- distributed under any other license. The requirement for fonts to
77
- remain under this license does not apply to any document created
78
- using the Font Software.
79
-
80
- TERMINATION
81
- This license becomes null and void if any of the above conditions are
82
- not met.
83
-
84
- DISCLAIMER
85
- THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
- OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
- COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
- INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
- DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
- FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
- OTHER DEALINGS IN THE FONT SOFTWARE.