slifer 0.2.2 → 0.2.3

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.
package/bun.lockb ADDED
Binary file
package/index.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  import { SliferClass } from "./src/slifer";
2
2
 
3
+ export { Image } from './src/engine/image';
4
+ export { Vector2 } from './src/engine/vector';
5
+ export { Rectangle } from './src/engine/rectangle';
6
+ export { Timer } from './src/engine/time';
7
+ export { AudioSource } from './src/modules/audio'
8
+
3
9
  const Slifer = new SliferClass();
4
- export default Slifer;
10
+ export default Slifer;
11
+
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slifer",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "A game framework made for bun and typescript",
5
5
  "module": "index.ts",
6
6
  "devDependencies": {
@@ -0,0 +1,28 @@
1
+ import { type Pointer, ptr } from 'bun:ffi';
2
+ import { libsdl } from '../ffi';
3
+ import { Vector2 } from './vector';
4
+
5
+ export class Image {
6
+ public readonly pointer: Pointer;
7
+ public readonly size: Vector2;
8
+ public flipH: boolean = false;
9
+
10
+ constructor(texture: Pointer) {
11
+ this.pointer = texture;
12
+
13
+ const _wArr = new Uint32Array(1);
14
+ const _hArr = new Uint32Array(1);
15
+
16
+ libsdl.symbols.SDL_QueryTexture(
17
+ texture,
18
+ null,
19
+ null,
20
+ ptr(_wArr),
21
+ ptr(_hArr)
22
+ );
23
+
24
+ this.size = new Vector2(_wArr[0], _hArr[0]);
25
+ }
26
+
27
+ }
28
+
@@ -1,8 +1,8 @@
1
1
  import { ptr } from "bun:ffi";
2
- import Vector2 from "./vector";
2
+ import { Vector2 } from "./vector";
3
3
 
4
- class Rectangle {
5
- private readonly pointer;
4
+ export class Rectangle {
5
+ public readonly pointer;
6
6
  public position: Vector2;
7
7
  public size: Vector2;
8
8
 
@@ -20,6 +20,18 @@ class Rectangle {
20
20
  static empty() {
21
21
  return new Rectangle(new Vector2(0, 0), new Vector2(0, 0));
22
22
  }
23
+
24
+ public isColliding(rectangle: Rectangle) : boolean {
25
+ if (
26
+ this.position.x < rectangle.position.x + rectangle.size.x &&
27
+ this.position.x + this.size.x > rectangle.position.x &&
28
+ this.position.y < rectangle.position.y + rectangle.size.y &&
29
+ this.position.y + this.size.y > rectangle.position.y
30
+ ) {
31
+ return true;
32
+ }
33
+
34
+ return false;
35
+ }
23
36
  }
24
37
 
25
- export default Rectangle;
@@ -1,34 +1,79 @@
1
1
  import { libsdl } from "../ffi";
2
2
 
3
- class Time {
4
- static #instance: Time;
3
+ export class Timer {
5
4
 
6
- private lastframe: number = 0;
7
- private firstFrame: number = 0;
5
+ private mStartTicks;
6
+ private mPausedTicks;
7
+ private mPaused;
8
+ private mStarted;
9
+
10
+
11
+ constructor() {
12
+ this.mStartTicks = 0;
13
+ this.mPausedTicks = 0;
14
+ this.mPaused = false;
15
+ this.mStarted = false;
16
+ }
17
+
18
+ public start() {
19
+ this.mStarted = true;
8
20
 
9
- private constructor() {}
21
+ this.mPaused = false;
10
22
 
11
- public static get instance() {
12
- if (!Time.#instance) {
13
- Time.#instance = new Time();
14
- }
23
+ this.mStartTicks = libsdl.symbols.SDL_GetTicks();
24
+ this.mPausedTicks = 0;
25
+ }
15
26
 
16
- return Time.#instance;
17
- }
27
+ public stop() {
28
+ this.mStarted = false;
29
+ this.mPaused = true;
18
30
 
19
- public init() {
20
- this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
21
- }
31
+ this.mStartTicks = 0;
32
+ this.mPausedTicks = 0;
33
+ }
22
34
 
23
- public calcDelta(): number {
24
- this.lastframe = this.firstFrame;
25
- this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
26
- const deltaTime =
27
- ((this.firstFrame - this.lastframe) * 1000) /
28
- Number(libsdl.symbols.SDL_GetPerformanceFrequency());
35
+ public pause() {
36
+ if (this.mStarted && !this.mPaused) {
37
+ this.mPaused = true;
29
38
 
30
- return deltaTime;
31
- }
39
+ this.mPausedTicks = libsdl.symbols.SDL_GetTicks() - this.mStartTicks;
40
+ this.mStartTicks = 0;
41
+ }
42
+ }
43
+
44
+ public unpause() {
45
+ if (this.mStarted && this.mPaused) {
46
+ this.mPaused = false;
47
+
48
+ this.mStartTicks = libsdl.symbols.SDL_GetTicks() - this.mPausedTicks;
49
+ this.mPausedTicks = 0;
50
+ }
51
+ }
52
+
53
+ public getTicks() {
54
+ let time = 0;
55
+
56
+ if (this.mStarted) {
57
+ if (this.mPaused) {
58
+ time = this.mPausedTicks;
59
+ } else {
60
+ time = libsdl.symbols.SDL_GetTicks() - this.mStartTicks;
61
+ }
62
+ }
63
+
64
+ return time;
65
+ }
66
+
67
+ public isStarted() {
68
+ return this.mStarted;
69
+ }
70
+
71
+ public isPaused() {
72
+ return (this.mPaused && this.mStarted);
73
+ }
74
+
75
+
76
+
77
+
32
78
  }
33
79
 
34
- export default Time;
@@ -1,4 +1,4 @@
1
- class Vector2 {
1
+ export class Vector2 {
2
2
  public x;
3
3
  public y;
4
4
 
@@ -45,5 +45,3 @@ class Vector2 {
45
45
  return new Vector2(vec1.x + vec2.x, vec1.y + vec2.y);
46
46
  }
47
47
  }
48
-
49
- export default Vector2;
@@ -1,5 +1,5 @@
1
1
  import { type Pointer } from "bun:ffi";
2
- import Vector2 from "./vector";
2
+ import { Vector2 } from "./vector";
3
3
  import { libsdl } from "../ffi";
4
4
 
5
5
  class Window {
package/src/ffi.ts CHANGED
@@ -12,6 +12,8 @@ if (process.platform == "win32") {
12
12
  libImageImport = await import("../libs/libSDL2_image.dll");
13
13
  //@ts-expect-error
14
14
  libTTFImport = await import("../libs/libSDL2_ttf.dll");
15
+ //@ts-expect-error
16
+ libMixerImport = await import("../libs/libSDL2_mixer.dll");
15
17
  } else if (process.platform == "darwin") {
16
18
  //@ts-expect-error
17
19
  libSDLImport = await import("../libs/libSDL2.dylib");
@@ -30,7 +32,6 @@ if (process.platform == "win32") {
30
32
  libTTFImport = await import("../libs/libSDL2_ttf.so");
31
33
  //@ts-expect-error
32
34
  libMixerImport = await import("../libs/libSDL2_mixer.so");
33
-
34
35
  }
35
36
 
36
37
  /** @internal */
@@ -206,10 +207,20 @@ export const libsdl = dlopen(libSDLImport.default, {
206
207
  args: ["pointer", "cstring"],
207
208
  returns: "void",
208
209
  },
209
- SDL_LoadWAV_RW: {
210
- args: ["cstring", "pointer", "pointer", "pointer"],
210
+ SDL_GetTicks: {
211
+ returns: "uint32_t",
212
+ },
213
+ SDL_Delay: {
214
+ args: ["uint32_t"],
215
+ },
216
+ SDL_GetKeyboardState: {
217
+ args: ["pointer"],
211
218
  returns: "pointer",
212
219
  },
220
+ SDL_GetScancodeFromName: {
221
+ args: ["cstring"],
222
+ returns: "int",
223
+ },
213
224
  });
214
225
 
215
226
  /** @internal */
@@ -17,7 +17,7 @@ class Audio {
17
17
  }
18
18
  }
19
19
 
20
- class AudioSource {
20
+ export class AudioSource {
21
21
  public readonly pointer;
22
22
 
23
23
  constructor(path: string) {
@@ -1,8 +1,9 @@
1
- import { libimage, libsdl, libttf } from "../ffi";
1
+ import { libimage, libsdl } from "../ffi";
2
2
  import { type Pointer, ptr } from "bun:ffi";
3
- import Rectangle from "../engine/rectangle";
3
+ import { Image } from "../engine/image";
4
+ import { Rectangle } from "../engine/rectangle";
4
5
  import Color from "../color";
5
- import Vector2 from "../engine/vector";
6
+ import { Vector2 } from "../engine/vector";
6
7
  import Renderer from "../engine/renderer";
7
8
 
8
9
  class Graphics {
@@ -109,7 +110,8 @@ class Graphics {
109
110
  image: Image,
110
111
  position: Vector2,
111
112
  rotation?: number,
112
- scale?: Vector2
113
+ scale?: Vector2,
114
+ flipH?: boolean
113
115
  ) {
114
116
  // Define destination rect
115
117
  const dstRect = new Uint32Array(4);
@@ -126,30 +128,19 @@ class Graphics {
126
128
  ptr(dstRect),
127
129
  rotation ? rotation : 0,
128
130
  null,
129
- null
131
+ flipH ? Number(flipH) : 0
130
132
  );
131
133
  }
132
- }
133
-
134
- class Image {
135
- public readonly pointer: Pointer;
136
- public readonly size: Vector2;
137
-
138
- constructor(texture: Pointer) {
139
- this.pointer = texture;
140
-
141
- const _wArr = new Uint32Array(1);
142
- const _hArr = new Uint32Array(1);
143
134
 
144
- libsdl.symbols.SDL_QueryTexture(
145
- texture,
146
- null,
147
- null,
148
- ptr(_wArr),
149
- ptr(_hArr)
135
+ public drawRect(rectangle: Rectangle, color: Color) {
136
+ libsdl.symbols.SDL_SetRenderDrawColor(
137
+ Renderer.pointer,
138
+ color.r,
139
+ color.g,
140
+ color.b,
141
+ color.a
150
142
  );
151
-
152
- this.size = new Vector2(_wArr[0], _hArr[0]);
143
+ libsdl.symbols.SDL_RenderFillRect(Renderer.pointer, rectangle.pointer);
153
144
  }
154
145
  }
155
146
 
@@ -1,12 +1,12 @@
1
1
  import { libsdl } from "../ffi";
2
+ import { toArrayBuffer } from "bun:ffi";
2
3
 
3
4
  /** @internal */
4
5
  class Keyboard {
5
6
  static #instance: Keyboard;
6
7
 
7
- static downKeyMap = new Map<string, boolean>();
8
- static pressedKeyMap = new Map<string, boolean>();
9
- static releasedKeyMap = new Map<string, boolean>();
8
+ static keyState: DataView;
9
+ static pressMap = new Map<string, number>();
10
10
 
11
11
  private constructor() {}
12
12
 
@@ -27,63 +27,49 @@ class Keyboard {
27
27
  return keyName;
28
28
  }
29
29
 
30
- static setKeyDown(key: number): void {
31
- const keyName = Keyboard.convertScancodeToKey(key);
32
- this.downKeyMap.set(keyName.toLowerCase(), true);
33
- this.releasedKeyMap.set(keyName.toLowerCase(), false);
34
- }
35
-
36
- static setKeyUp(key: number) {
37
- const keyName = Keyboard.convertScancodeToKey(key);
38
- this.downKeyMap.set(keyName.toLowerCase(), false);
39
- this.pressedKeyMap.set(keyName.toLowerCase(), false);
40
- }
41
-
42
- /**
43
- *
44
- * @param key string of key
45
- * @returns if the key is being held down
46
- */
47
- isDown(key: keys) {
48
- const _state = Keyboard.downKeyMap.get(key);
49
- if (_state == undefined) return false;
50
-
51
- return _state;
30
+ static getStates() {
31
+ const state = libsdl.symbols.SDL_GetKeyboardState(null);
32
+ if (state == null) return;
33
+ const myArr = new DataView(toArrayBuffer(state, 0, 512));
34
+
35
+ for (let i = 0; i < 512; ++i) {
36
+ if (myArr.getUint8(i) == 1) {
37
+ const keyName = this.convertScancodeToKey(i).toLowerCase();
38
+ const kmGet = this.pressMap.get(keyName);
39
+ if (kmGet == undefined || kmGet == 0) {
40
+ this.pressMap.set(keyName, 1);
41
+ } else if (kmGet == 1) {
42
+ this.pressMap.set(keyName, 2);
43
+ }
44
+ } else if (myArr.getUint8(i) == 0) {
45
+ const keyName = this.convertScancodeToKey(i).toLowerCase();
46
+ this.pressMap.set(keyName, 0);
47
+ }
48
+ }
52
49
  }
53
50
 
54
- /**
55
- *
56
- * @param key string of key
57
- * @returns if key is pressed. Returns only once
58
- */
59
- isPressed(key: keys) {
60
- const _pressedState = Keyboard.pressedKeyMap.get(key);
61
- const _downState = Keyboard.downKeyMap.get(key);
62
-
63
- if (_downState == true) {
64
- if (_pressedState == false || _pressedState == undefined) {
65
- Keyboard.pressedKeyMap.set(key, true);
66
- return true;
67
- }
51
+ public isPressed(key: keys) {
52
+ /*
53
+ const scancode = libsdl.symbols.SDL_GetScancodeFromName(
54
+ Buffer.from(key + "\x00")
55
+ );
56
+ const thisval = Keyboard.keyState.getUint8(scancode);
57
+ */
58
+
59
+ const kmGet = Keyboard.pressMap.get(key);
60
+ if (kmGet == 1) {
61
+ return true;
68
62
  }
69
63
 
64
+
70
65
  return false;
71
66
  }
72
67
 
73
- /**
74
- *
75
- * @param key string of key
76
- * @returns if key is released. Returns only once
77
- */
78
- isReleased(key: keys) {
79
- const _releasedState = Keyboard.releasedKeyMap.get(key);
80
- const _downState = Keyboard.downKeyMap.get(key);
81
-
82
- if (_downState == false) {
83
- if (_releasedState == false || undefined) {
84
- Keyboard.releasedKeyMap.set(key, true);
85
- return true;
86
- }
68
+
69
+ public isDown(key: keys) {
70
+ const kmGet = Keyboard.pressMap.get(key);
71
+ if (kmGet == 1 || kmGet == 2) {
72
+ return true;
87
73
  }
88
74
 
89
75
  return false;
@@ -1,6 +1,6 @@
1
1
  import { libsdl } from "../ffi";
2
2
  import { ptr } from "bun:ffi";
3
- import Vector2 from "../engine/vector";
3
+ import {Vector2} from "../engine/vector";
4
4
 
5
5
  /** @internal */
6
6
  class Mouse {
package/src/slifer.ts CHANGED
@@ -5,9 +5,9 @@ import Mouse from "./modules/mouse";
5
5
  import Audio from "./modules/audio";
6
6
  import Window from "./engine/window";
7
7
  import Renderer from "./engine/renderer";
8
- import Vector2 from "./engine/vector";
9
- import Time from "./engine/time";
10
- import { ptr } from "bun:ffi";
8
+ import { Vector2 } from "./engine/vector";
9
+ import { Timer } from "./engine/time";
10
+ import { ptr, toArrayBuffer } from "bun:ffi";
11
11
  import { initLibraries } from "./engine";
12
12
  import { version } from "../package.json";
13
13
 
@@ -23,8 +23,13 @@ export class SliferClass {
23
23
  public dt: number = 0;
24
24
  public isRunning: boolean = true;
25
25
 
26
+ private fps = 60;
27
+ private ticksPerFrame = 1000 / this.fps;
28
+ private capTimer: Timer;
29
+
26
30
  constructor() {
27
31
  initLibraries();
32
+ this.capTimer = new Timer();
28
33
  }
29
34
 
30
35
  /**
@@ -40,9 +45,6 @@ export class SliferClass {
40
45
  // Create the renderer
41
46
  Renderer.createRenderer();
42
47
 
43
- // Start delta time calculations
44
- Time.instance.init();
45
-
46
48
  // Return the window object
47
49
  return window;
48
50
  }
@@ -51,11 +53,13 @@ export class SliferClass {
51
53
  * @returns if the window should close
52
54
  */
53
55
  shouldClose(): boolean {
56
+ this.capTimer.start();
57
+
54
58
  // Clear the renderer
55
59
  Renderer.clear();
56
60
 
57
61
  // Calculate delta time
58
- this.dt = Time.instance.calcDelta();
62
+ // this.dt = Time.instance.calcDelta();
59
63
 
60
64
  // Poll Events
61
65
  const eventArray = new Uint16Array(32);
@@ -67,16 +71,6 @@ export class SliferClass {
67
71
  case 256:
68
72
  this.isRunning = false;
69
73
  break;
70
- // Keydown event
71
- case 768:
72
- var scancode = eventArray[8];
73
- Keyboard.setKeyDown(scancode);
74
- break;
75
- // Keyup event
76
- case 769:
77
- var scancode = eventArray[8];
78
- Keyboard.setKeyUp(scancode);
79
- break;
80
74
  // Mouse down event
81
75
  case 1025:
82
76
  const _dbtn = eventArray[8] - 256;
@@ -90,6 +84,13 @@ export class SliferClass {
90
84
  }
91
85
  }
92
86
 
87
+ Keyboard.getStates();
88
+
89
+ const frameTicks = this.capTimer.getTicks();
90
+ if (frameTicks < this.ticksPerFrame) {
91
+ libsdl.symbols.SDL_Delay(this.ticksPerFrame - frameTicks);
92
+ }
93
+
93
94
  return !this.isRunning;
94
95
  }
95
96
 
package/Sir-BC_stop.png DELETED
Binary file
package/sample.wav DELETED
Binary file