bloody-engine 1.0.5 → 1.0.7

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.
Files changed (105) hide show
  1. package/README.md +85 -37
  2. package/dist/node/core/interpolation.d.ts +85 -0
  3. package/dist/node/core/interpolation.d.ts.map +1 -0
  4. package/dist/node/core/resource-loader-factory.d.ts +58 -0
  5. package/dist/node/core/resource-loader-factory.d.ts.map +1 -0
  6. package/dist/node/core/resource-pipeline.d.ts.map +1 -0
  7. package/dist/node/core/sprite-atlas.d.ts +356 -0
  8. package/dist/node/core/sprite-atlas.d.ts.map +1 -0
  9. package/dist/{web → node}/core/texture.d.ts +32 -1
  10. package/dist/node/core/texture.d.ts.map +1 -0
  11. package/dist/node/core/ticker-config.d.ts +60 -0
  12. package/dist/node/core/ticker-config.d.ts.map +1 -0
  13. package/dist/node/core/ticker.d.ts +58 -0
  14. package/dist/node/core/ticker.d.ts.map +1 -0
  15. package/dist/node/examples/node-texture-loader-demo.d.ts +20 -0
  16. package/dist/node/examples/node-texture-loader-demo.d.ts.map +1 -0
  17. package/dist/{web → node}/examples/resource-loader-demo.d.ts.map +1 -1
  18. package/dist/node/examples/sprite-atlas-demo.d.ts +47 -0
  19. package/dist/node/examples/sprite-atlas-demo.d.ts.map +1 -0
  20. package/dist/node/index.js +3457 -1859
  21. package/dist/node/input/command-queue.d.ts +83 -0
  22. package/dist/node/input/command-queue.d.ts.map +1 -0
  23. package/dist/node/input/command-types.d.ts +75 -0
  24. package/dist/node/input/command-types.d.ts.map +1 -0
  25. package/dist/node/input/index.d.ts +10 -0
  26. package/dist/node/input/index.d.ts.map +1 -0
  27. package/dist/node/input/input-source.d.ts +53 -0
  28. package/dist/node/input/input-source.d.ts.map +1 -0
  29. package/dist/node/input/network-input-source.d.ts +112 -0
  30. package/dist/node/input/network-input-source.d.ts.map +1 -0
  31. package/dist/node/input/sdl-input-source.d.ts +107 -0
  32. package/dist/node/input/sdl-input-source.d.ts.map +1 -0
  33. package/dist/node/platforms/node/node-texture-loader.d.ts +102 -0
  34. package/dist/node/platforms/node/node-texture-loader.d.ts.map +1 -0
  35. package/dist/{web → node}/public-api.d.ts +12 -3
  36. package/dist/node/public-api.d.ts.map +1 -0
  37. package/dist/{web → node}/rendering/batch-renderer.d.ts +26 -2
  38. package/dist/{web → node}/rendering/batch-renderer.d.ts.map +1 -1
  39. package/dist/{web → node}/rendering/rendering-context-factory.d.ts +3 -7
  40. package/dist/node/rendering/rendering-context-factory.d.ts.map +1 -0
  41. package/dist/node/rendering/spatial-hash.d.ts +142 -0
  42. package/dist/node/rendering/spatial-hash.d.ts.map +1 -0
  43. package/dist/node/simulation/entity-manager.d.ts +107 -0
  44. package/dist/node/simulation/entity-manager.d.ts.map +1 -0
  45. package/dist/node/simulation/entity.d.ts +77 -0
  46. package/dist/node/simulation/entity.d.ts.map +1 -0
  47. package/dist/node/simulation/index.d.ts +20 -0
  48. package/dist/node/simulation/index.d.ts.map +1 -0
  49. package/dist/node/simulation/simulation-loop.d.ts +123 -0
  50. package/dist/node/simulation/simulation-loop.d.ts.map +1 -0
  51. package/package.json +8 -10
  52. package/dist/web/core/resource-loader-factory.d.ts +0 -90
  53. package/dist/web/core/resource-loader-factory.d.ts.map +0 -1
  54. package/dist/web/core/resource-pipeline.d.ts.map +0 -1
  55. package/dist/web/core/texture.d.ts.map +0 -1
  56. package/dist/web/index.js +0 -2223
  57. package/dist/web/index.umd.js +0 -56
  58. package/dist/web/platforms/browser/browser-context.d.ts +0 -31
  59. package/dist/web/platforms/browser/browser-context.d.ts.map +0 -1
  60. package/dist/web/platforms/browser/browser-resource-loader.d.ts +0 -67
  61. package/dist/web/platforms/browser/browser-resource-loader.d.ts.map +0 -1
  62. package/dist/web/public-api.d.ts.map +0 -1
  63. package/dist/web/rendering/rendering-context-factory.d.ts.map +0 -1
  64. /package/dist/{web → node}/core/buffer.d.ts +0 -0
  65. /package/dist/{web → node}/core/buffer.d.ts.map +0 -0
  66. /package/dist/{web → node}/core/grahpic-device.d.ts +0 -0
  67. /package/dist/{web → node}/core/grahpic-device.d.ts.map +0 -0
  68. /package/dist/{web → node}/core/index.d.ts +0 -0
  69. /package/dist/{web → node}/core/index.d.ts.map +0 -0
  70. /package/dist/{web → node}/core/resource-loader.d.ts +0 -0
  71. /package/dist/{web → node}/core/resource-loader.d.ts.map +0 -0
  72. /package/dist/{web → node}/core/resource-pipeline.d.ts +0 -0
  73. /package/dist/{web → node}/core/shader.d.ts +0 -0
  74. /package/dist/{web → node}/core/shader.d.ts.map +0 -0
  75. /package/dist/{web → node}/demo-node.d.ts +0 -0
  76. /package/dist/{web → node}/demo-node.d.ts.map +0 -0
  77. /package/dist/{web → node}/examples/batch-renderer-demo.d.ts +0 -0
  78. /package/dist/{web → node}/examples/batch-renderer-demo.d.ts.map +0 -0
  79. /package/dist/{web → node}/examples/projection-examples.d.ts +0 -0
  80. /package/dist/{web → node}/examples/projection-examples.d.ts.map +0 -0
  81. /package/dist/{web → node}/examples/resource-loader-demo.d.ts +0 -0
  82. /package/dist/{web → node}/examples/shader-examples.d.ts +0 -0
  83. /package/dist/{web → node}/examples/shader-examples.d.ts.map +0 -0
  84. /package/dist/{web → node}/examples/sprite-batch-renderer-demo.d.ts +0 -0
  85. /package/dist/{web → node}/examples/sprite-batch-renderer-demo.d.ts.map +0 -0
  86. /package/dist/{web → node}/index.d.ts +0 -0
  87. /package/dist/{web → node}/index.d.ts.map +0 -0
  88. /package/dist/{web → node}/platforms/node/node-context.d.ts +0 -0
  89. /package/dist/{web → node}/platforms/node/node-context.d.ts.map +0 -0
  90. /package/dist/{web → node}/platforms/node/node-resource-loader.d.ts +0 -0
  91. /package/dist/{web → node}/platforms/node/node-resource-loader.d.ts.map +0 -0
  92. /package/dist/{web → node}/platforms/node/sdl-window.d.ts +0 -0
  93. /package/dist/{web → node}/platforms/node/sdl-window.d.ts.map +0 -0
  94. /package/dist/{web → node}/projection.test.d.ts +0 -0
  95. /package/dist/{web → node}/projection.test.d.ts.map +0 -0
  96. /package/dist/{web → node}/rendering/camera.d.ts +0 -0
  97. /package/dist/{web → node}/rendering/camera.d.ts.map +0 -0
  98. /package/dist/{web → node}/rendering/projection.d.ts +0 -0
  99. /package/dist/{web → node}/rendering/projection.d.ts.map +0 -0
  100. /package/dist/{web → node}/rendering/rendering-context.d.ts +0 -0
  101. /package/dist/{web → node}/rendering/rendering-context.d.ts.map +0 -0
  102. /package/dist/{web → node}/rendering/vertex.d.ts +0 -0
  103. /package/dist/{web → node}/rendering/vertex.d.ts.map +0 -0
  104. /package/dist/{web → node}/scene/scene.d.ts +0 -0
  105. /package/dist/{web → node}/scene/scene.d.ts.map +0 -0
package/README.md CHANGED
@@ -1,15 +1,16 @@
1
1
  # Bloody Engine
2
2
 
3
- A WebGL-based 2.5D graphics engine for isometric rendering, written in TypeScript. Designed for both browser and Node.js environments with full isomorphic support.
3
+ A WebGL-based 2.5D graphics engine for isometric rendering on Node.js, written in TypeScript. Designed for server-side rendering and headless graphics processing.
4
4
 
5
5
  ## Features
6
6
 
7
7
  - **2.5D Rendering** - Optimized for isometric and dimetric projections
8
- - **Cross-Platform** - Works in browsers and Node.js (headless rendering)
8
+ - **Server-Side Rendering** - Headless WebGL rendering on Node.js using `gl` and `@kmamal/sdl`
9
9
  - **Batch Rendering** - Efficient sprite batching with GPU-accelerated transformations
10
10
  - **Resource Management** - Unified asset loading pipeline for textures and resources
11
11
  - **TypeScript** - Fully typed for excellent developer experience
12
12
  - **Depth Sorting** - Proper 2.5D occlusion handling
13
+ - **Window Management** - SDL-based window creation for interactive applications
13
14
 
14
15
  ## Installation
15
16
 
@@ -19,49 +20,59 @@ npm install bloody-engine
19
20
 
20
21
  ## Quick Start
21
22
 
22
- ### Browser
23
-
24
23
  ```typescript
25
- import { BloodyEngine } from 'bloody-engine';
26
-
27
- // Initialize engine
28
- const engine = new BloodyEngine({
29
- width: 800,
30
- height: 600
31
- });
32
-
33
- // Start rendering loop
34
- engine.start();
35
- ```
24
+ import { GraphicsDevice, Shader, Texture } from 'bloody-engine';
25
+
26
+ // Create a graphics device with SDL window
27
+ const device = new GraphicsDevice(800, 600);
28
+
29
+ // Get the WebGL context
30
+ const gl = device.getGLContext();
31
+
32
+ // Create a shader
33
+ const shader = device.createShader(`
34
+ attribute vec3 aPosition;
35
+ uniform mat4 uMatrix;
36
+
37
+ void main() {
38
+ gl_Position = uMatrix * vec4(aPosition, 1.0);
39
+ }
40
+ `, `
41
+ precision mediump float;
42
+ uniform vec3 uColor;
43
+
44
+ void main() {
45
+ gl_FragColor = vec4(uColor, 1.0);
46
+ }
47
+ `);
48
+
49
+ // Create a texture from PNG
50
+ const { PNG } = require('pngjs');
51
+ const fs = require('fs/promises');
52
+ const pngData = await fs.readFile('texture.png');
53
+ const png = PNG.sync.read(pngData);
54
+ const texture = Texture.createFromPNG(gl, png);
36
55
 
37
- ### Node.js
38
-
39
- ```typescript
40
- import { BloodyEngine } from 'bloody-engine';
41
-
42
- // Initialize engine for headless rendering
43
- const engine = new BloodyEngine({
44
- width: 800,
45
- height: 600,
46
- headless: true
47
- });
48
-
49
- // Render and capture output
50
- engine.renderFrame();
51
- const pixels = engine.getPixels();
56
+ // Render
57
+ device.clear({ r: 0.1, g: 0.1, b: 0.1, a: 1.0 });
58
+ shader.use();
59
+ // ... rendering code ...
60
+ device.present();
61
+
62
+ // For headless rendering, capture the output
63
+ const context = device.getRenderingContext();
64
+ const pixels = context.readPixels();
52
65
  ```
53
66
 
54
- ## Documentation
55
-
56
- For detailed documentation and architecture, see [docs/README.MD](docs/README.MD).
57
-
58
67
  ## Examples
59
68
 
69
+ ### Sprite Batch Rendering
70
+
60
71
  ```typescript
61
- // Create a sprite batch renderer
62
- import { SpriteBatchRenderer, Texture } from 'bloody-engine';
72
+ import { SpriteBatchRenderer, Camera, Texture } from 'bloody-engine';
63
73
 
64
- const batchRenderer = new SpriteBatchRenderer(gl, shader);
74
+ const batchRenderer = new SpriteBatchRenderer(gl, shader, 1000);
75
+ const camera = new Camera(0, 0, 1.0);
65
76
 
66
77
  // Add sprites
67
78
  batchRenderer.addQuad({
@@ -79,6 +90,43 @@ batchRenderer.addQuad({
79
90
  batchRenderer.render(camera);
80
91
  ```
81
92
 
93
+ ### Resource Loading
94
+
95
+ ```typescript
96
+ import { NodeResourceLoader } from 'bloody-engine';
97
+
98
+ const loader = new NodeResourceLoader('./assets');
99
+
100
+ // Load a shader
101
+ const vertexSource = await loader.load('shaders/basic.vert');
102
+ const fragmentSource = await loader.load('shaders/basic.frag');
103
+
104
+ // Batch load multiple resources
105
+ const { succeeded, failed } = await loader.loadMultiple([
106
+ 'textures/sprite1.png',
107
+ 'textures/sprite2.png',
108
+ 'shaders/shader.vert'
109
+ ]);
110
+ ```
111
+
112
+ ## Dependencies
113
+
114
+ - **gl** - Headless WebGL for Node.js
115
+ - **@kmamal/sdl** - SDL2 bindings for window and input management
116
+ - **pngjs** - PNG image decoding
117
+
118
+ ## Documentation
119
+
120
+ For detailed documentation and architecture, see [docs/README.MD](docs/README.MD).
121
+
122
+ ## Building
123
+
124
+ ```bash
125
+ npm run build
126
+ ```
127
+
128
+ This will generate the distribution files in `dist/node/`.
129
+
82
130
  ## License
83
131
 
84
132
  MIT License - see [LICENSE](LICENSE) for details.
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Linear interpolation between two values
3
+ * @param a - Start value
4
+ * @param b - End value
5
+ * @param t - Interpolation factor (0-1)
6
+ * @returns Interpolated value
7
+ */
8
+ export declare function lerp(a: number, b: number, t: number): number;
9
+ /**
10
+ * Interpolate between two 2D vectors
11
+ * @param a - Start vector
12
+ * @param b - End vector
13
+ * @param t - Interpolation factor (0-1)
14
+ * @returns Interpolated vector
15
+ */
16
+ export declare function lerpVec2(a: {
17
+ x: number;
18
+ y: number;
19
+ }, b: {
20
+ x: number;
21
+ y: number;
22
+ }, t: number): {
23
+ x: number;
24
+ y: number;
25
+ };
26
+ /**
27
+ * Interpolate between two 3D vectors
28
+ * @param a - Start vector
29
+ * @param b - End vector
30
+ * @param t - Interpolation factor (0-1)
31
+ * @returns Interpolated vector
32
+ */
33
+ export declare function lerpVec3(a: {
34
+ x: number;
35
+ y: number;
36
+ z: number;
37
+ }, b: {
38
+ x: number;
39
+ y: number;
40
+ z: number;
41
+ }, t: number): {
42
+ x: number;
43
+ y: number;
44
+ z: number;
45
+ };
46
+ /**
47
+ * Interpolate rotation (handles angle wrapping)
48
+ * @param a - Start angle in radians
49
+ * @param b - End angle in radians
50
+ * @param t - Interpolation factor (0-1)
51
+ * @returns Interpolated angle
52
+ */
53
+ export declare function lerpAngle(a: number, b: number, t: number): number;
54
+ /**
55
+ * State buffer for interpolation
56
+ * Stores previous and current states for smooth interpolation
57
+ */
58
+ export declare class StateBuffer<T> {
59
+ private previous;
60
+ private current;
61
+ constructor(initialState: T);
62
+ /**
63
+ * Update to new state (shifts current to previous)
64
+ * @param newState - The new state to set as current
65
+ */
66
+ update(newState: T): void;
67
+ /**
68
+ * Get interpolated state
69
+ * @param alpha - Interpolation factor (0-1)
70
+ * @param lerpFn - Interpolation function for the state type
71
+ * @returns Interpolated state
72
+ */
73
+ interpolate(alpha: number, lerpFn: (a: T, b: T, t: number) => T): T;
74
+ /**
75
+ * Get current state without interpolation
76
+ * @returns Current state
77
+ */
78
+ getCurrent(): T;
79
+ /**
80
+ * Get previous state
81
+ * @returns Previous state
82
+ */
83
+ getPrevious(): T;
84
+ }
85
+ //# sourceMappingURL=interpolation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolation.d.ts","sourceRoot":"","sources":["../../../src/core/interpolation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACtB,CAAC,EAAE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,C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Environment type enumeration
4
+ */
5
+ export declare enum Environment {
6
+ /** Node.js environment (file system APIs available) */
7
+ NODE = "node"
8
+ }
9
+ /**
10
+ * Resource loader factory configuration
11
+ */
12
+ export interface ResourceLoaderFactoryOptions {
13
+ /**
14
+ * Base directory for Node.js resource loader
15
+ */
16
+ baseDir?: string;
17
+ }
18
+ /**
19
+ * Factory class for creating resource loaders
20
+ * Creates Node.js resource loaders
21
+ */
22
+ export declare class ResourceLoaderFactory {
23
+ /**
24
+ * Detect the current runtime environment
25
+ * @returns The detected environment type (always NODE)
26
+ */
27
+ static detectEnvironment(): Environment;
28
+ /**
29
+ * Check if the current environment is a browser
30
+ * @returns false (not browser)
31
+ */
32
+ static isBrowser(): boolean;
33
+ /**
34
+ * Check if the current environment is Node.js
35
+ * @returns true
36
+ */
37
+ static isNode(): boolean;
38
+ /**
39
+ * Create a Node.js resource loader
40
+ * @param options Optional factory configuration
41
+ * @returns A Node.js resource loader instance
42
+ */
43
+ static create(options?: ResourceLoaderFactoryOptions): Promise<IResourceLoader>;
44
+ /**
45
+ * Create a Node.js resource loader
46
+ * @param options Optional factory configuration
47
+ * @returns A Node.js resource loader instance
48
+ */
49
+ static createNodeLoader(options?: ResourceLoaderFactoryOptions): Promise<IResourceLoader>;
50
+ }
51
+ /**
52
+ * Convenience function to create a resource loader
53
+ * Shortcut for ResourceLoaderFactory.create()
54
+ * @param options Optional factory configuration
55
+ * @returns A resource loader instance
56
+ */
57
+ export declare function createResourceLoader(options?: ResourceLoaderFactoryOptions): Promise<IResourceLoader>;
58
+ //# sourceMappingURL=resource-loader-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-loader-factory.d.ts","sourceRoot":"","sources":["../../../src/core/resource-loader-factory.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGzD;;GAEG;AACH,oBAAY,WAAW;IACrB,uDAAuD;IACvD,IAAI,SAAS;CACd;AAED;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,qBAAa,qBAAqB;IAChC;;;OAGG;IACH,MAAM,CAAC,iBAAiB,IAAI,WAAW;IAIvC;;;OAGG;IACH,MAAM,CAAC,SAAS,IAAI,OAAO;IAI3B;;;OAGG;IACH,MAAM,CAAC,MAAM,IAAI,OAAO;IAIxB;;;;OAIG;WACU,MAAM,CAAC,OAAO,CAAC,EAAE,4BAA4B,GAAG,OAAO,CAAC,eAAe,CAAC;IAIrF;;;;OAIG;WACU,gBAAgB,CAC3B,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,eAAe,CAAC;CAG5B;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,eAAe,CAAC,CAE1B"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-pipeline.d.ts","sourceRoot":"","sources":["../../../src/core/resource-pipeline.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EAEf,mBAAmB,EACpB,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,gCAAgC;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,kCAAkC;IAClC,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,YAAY;IACrD,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,uBAAwB,SAAQ,mBAAmB;IAClE,uDAAuD;IACvD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,wDAAwD;IACxD,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AA8CD;;;;GAIG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC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,WAAW,IAAI,IAAI;IAInB;;OAEG;IACH,YAAY,IAAI,IAAI;IAIpB;;;OAGG;IACH,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAIzC;;;OAGG;IACH,SAAS,IAAI,eAAe;CAG7B;AAED;;;;GAIG;AACH,wBAAsB,sBAAsB,CAC1C,OAAO,CAAC,EAAE,uBAAuB,GAAG;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GACA,OAAO,CAAC,gBAAgB,CAAC,CAS3B"}
@@ -0,0 +1,356 @@
1
+ import { Texture } from './texture';
2
+ /**
3
+ * UV rectangle defining a region in texture space (0-1 range)
4
+ */
5
+ export interface UVRect {
6
+ /** Minimum U coordinate (left edge) */
7
+ uMin: number;
8
+ /** Minimum V coordinate (bottom edge) */
9
+ vMin: number;
10
+ /** Maximum U coordinate (right edge) */
11
+ uMax: number;
12
+ /** Maximum V coordinate (top edge) */
13
+ vMax: number;
14
+ }
15
+ /**
16
+ * Pixel rectangle defining a region in pixel coordinates
17
+ */
18
+ export interface PixelRect {
19
+ /** Left edge in pixels */
20
+ x: number;
21
+ /** Bottom edge in pixels (Y-up coordinate system) */
22
+ y: number;
23
+ /** Width in pixels */
24
+ width: number;
25
+ /** Height in pixels */
26
+ height: number;
27
+ }
28
+ /**
29
+ * Sprite metadata stored in the atlas
30
+ */
31
+ export interface SpriteInfo {
32
+ /** Unique identifier for this sprite */
33
+ name: string;
34
+ /** UV rectangle in texture space */
35
+ uvRect: UVRect;
36
+ /** Pixel rectangle in the source texture */
37
+ pixelRect: PixelRect;
38
+ /** Width of the sprite in pixels */
39
+ width: number;
40
+ /** Height of the sprite in pixels */
41
+ height: number;
42
+ }
43
+ /**
44
+ * Represents a single sprite within a texture atlas.
45
+ * Provides convenient access to UV coordinates and sprite dimensions.
46
+ */
47
+ export declare class Sprite {
48
+ private readonly info;
49
+ /**
50
+ * Create a new sprite instance
51
+ * @param info Sprite information including UV coordinates and dimensions
52
+ */
53
+ constructor(info: SpriteInfo);
54
+ /**
55
+ * Get the sprite's unique name/identifier
56
+ */
57
+ getName(): string;
58
+ /**
59
+ * Get the UV rectangle for this sprite
60
+ * @returns UV coordinates in 0-1 range
61
+ */
62
+ getUVRect(): UVRect;
63
+ /**
64
+ * Get the pixel rectangle for this sprite
65
+ * @returns Pixel coordinates in the source texture
66
+ */
67
+ getPixelRect(): PixelRect;
68
+ /**
69
+ * Get the sprite width in pixels
70
+ */
71
+ getWidth(): number;
72
+ /**
73
+ * Get the sprite height in pixels
74
+ */
75
+ getHeight(): number;
76
+ /**
77
+ * Get the aspect ratio (width / height)
78
+ */
79
+ getAspectRatio(): number;
80
+ /**
81
+ * Create a UV rect object suitable for SpriteQuadInstance
82
+ * @returns UV rect as expected by batch renderer
83
+ */
84
+ toQuadUVRect(): {
85
+ uMin: number;
86
+ vMin: number;
87
+ uMax: number;
88
+ vMax: number;
89
+ };
90
+ /**
91
+ * Get a string representation of this sprite
92
+ */
93
+ toString(): string;
94
+ }
95
+ /**
96
+ * Options for creating a texture atlas
97
+ */
98
+ export interface AtlasOptions {
99
+ /** Texture containing the atlas image */
100
+ texture: Texture;
101
+ /** Width of the texture in pixels (auto-detected from texture if not provided) */
102
+ textureWidth?: number;
103
+ /** Height of the texture in pixels (auto-detected from texture if not provided) */
104
+ textureHeight?: number;
105
+ }
106
+ /**
107
+ * Texture Atlas Manager
108
+ *
109
+ * Manages a collection of sprites within a single texture.
110
+ * Provides methods to define sprites by pixel or normalized coordinates,
111
+ * and retrieve sprite information by name.
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * // Create an atlas from a loaded texture
116
+ * const atlas = new TextureAtlas({ texture: myTexture });
117
+ *
118
+ * // Define sprites by pixel coordinates
119
+ * atlas.defineSprite("player_idle_1", { x: 0, y: 0, width: 32, height: 32 });
120
+ * atlas.defineSprite("player_idle_2", { x: 32, y: 0, width: 32, height: 32 });
121
+ *
122
+ * // Or define sprites by normalized UV coordinates
123
+ * atlas.defineSpriteUV("background", { uMin: 0, vMin: 0, uMax: 1, vMax: 1 }, 512, 512);
124
+ *
125
+ * // Get a sprite for rendering
126
+ * const sprite = atlas.getSprite("player_idle_1");
127
+ * renderer.addQuad({
128
+ * x: 100, y: 100, z: 0,
129
+ * width: sprite.getWidth(),
130
+ * height: sprite.getHeight(),
131
+ * rotation: 0,
132
+ * uvRect: sprite.toQuadUVRect(),
133
+ * texIndex: 0
134
+ * });
135
+ * ```
136
+ */
137
+ export declare class TextureAtlas {
138
+ private readonly texture;
139
+ private readonly textureWidth;
140
+ private readonly textureHeight;
141
+ private readonly sprites;
142
+ /**
143
+ * Create a new texture atlas
144
+ * @param options Atlas configuration options
145
+ */
146
+ constructor(options: AtlasOptions);
147
+ /**
148
+ * Get the underlying texture
149
+ */
150
+ getTexture(): Texture;
151
+ /**
152
+ * Get the texture dimensions
153
+ */
154
+ getTextureDimensions(): {
155
+ width: number;
156
+ height: number;
157
+ };
158
+ /**
159
+ * Define a sprite by pixel coordinates.
160
+ * Pixels are in Y-up coordinate system (bottom-left origin).
161
+ *
162
+ * @param name Unique identifier for this sprite
163
+ * @param rect Pixel rectangle defining the sprite region
164
+ * @returns The created Sprite instance
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * // Define a 32x32 sprite at the bottom-left corner
169
+ * atlas.defineSprite("player", { x: 0, y: 0, width: 32, height: 32 });
170
+ *
171
+ * // Define a sprite starting at column 2, row 1 (assuming 32px tiles)
172
+ * atlas.defineSprite("enemy", { x: 64, y: 32, width: 32, height: 32 });
173
+ * ```
174
+ */
175
+ defineSprite(name: string, rect: PixelRect): Sprite;
176
+ /**
177
+ * Define a sprite grid (regular grid of same-sized sprites).
178
+ * Useful for sprite sheets and tilesets.
179
+ *
180
+ * @param prefix Name prefix for each sprite (e.g., "tile_")
181
+ * @param startX Starting X position in pixels
182
+ * @param startY Starting Y position in pixels
183
+ * @param spriteWidth Width of each sprite in pixels
184
+ * @param spriteHeight Height of each sprite in pixels
185
+ * @param columns Number of columns in the grid
186
+ * @param rows Number of rows in the grid
187
+ * @param columnSpacing Optional horizontal gap between sprites (default 0)
188
+ * @param rowSpacing Optional vertical gap between sprites (default 0)
189
+ * @returns Array of created sprite names
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * // Define a 4x4 grid of 32x32 tiles
194
+ * const tiles = atlas.defineGrid(
195
+ * "tile_", 0, 0, 32, 32, 4, 4
196
+ * );
197
+ * // tiles = ["tile_0_0", "tile_1_0", ..., "tile_3_3"]
198
+ * ```
199
+ */
200
+ defineGrid(prefix: string, startX: number, startY: number, spriteWidth: number, spriteHeight: number, columns: number, rows: number, columnSpacing?: number, rowSpacing?: number): string[];
201
+ /**
202
+ * Define a sprite by normalized UV coordinates.
203
+ * Useful when you have pre-computed UV values.
204
+ *
205
+ * @param name Unique identifier for this sprite
206
+ * @param uvRect UV rectangle (0-1 range)
207
+ * @param pixelWidth Width in pixels (for reference, optional)
208
+ * @param pixelHeight Height in pixels (for reference, optional)
209
+ * @returns The created Sprite instance
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * // Define a sprite covering the entire texture
214
+ * atlas.defineSpriteUV("full", {
215
+ * uMin: 0, vMin: 0, uMax: 1, vMax: 1
216
+ * }, 512, 512);
217
+ * ```
218
+ */
219
+ defineSpriteUV(name: string, uvRect: UVRect, pixelWidth?: number, pixelHeight?: number): Sprite;
220
+ /**
221
+ * Get a sprite by name
222
+ * @param name Sprite identifier
223
+ * @returns Sprite instance or undefined if not found
224
+ */
225
+ getSprite(name: string): Sprite | undefined;
226
+ /**
227
+ * Check if a sprite exists in the atlas
228
+ * @param name Sprite identifier
229
+ */
230
+ hasSprite(name: string): boolean;
231
+ /**
232
+ * Get all sprite names in the atlas
233
+ */
234
+ getSpriteNames(): string[];
235
+ /**
236
+ * Get the number of sprites defined in the atlas
237
+ */
238
+ getSpriteCount(): number;
239
+ /**
240
+ * Remove a sprite from the atlas
241
+ * @param name Sprite identifier
242
+ * @returns true if sprite was removed, false if it didn't exist
243
+ */
244
+ removeSprite(name: string): boolean;
245
+ /**
246
+ * Clear all sprites from the atlas
247
+ */
248
+ clear(): void;
249
+ /**
250
+ * Create a sprite definition JSON from all sprites in the atlas.
251
+ * Useful for serializing atlas data to disk.
252
+ *
253
+ * @returns JSON-serializable object containing all sprite data
254
+ */
255
+ toJSON(): object;
256
+ /**
257
+ * Load sprite definitions from a JSON object.
258
+ * Useful for loading atlas data from disk.
259
+ *
260
+ * @param data JSON object containing sprite definitions
261
+ * @example
262
+ * ```typescript
263
+ * // Load from a parsed JSON file
264
+ * const data = JSON.parse(fs.readFileSync("atlas.json", "utf8"));
265
+ * atlas.fromJSON(data);
266
+ * ```
267
+ */
268
+ fromJSON(data: {
269
+ textureWidth?: number;
270
+ textureHeight?: number;
271
+ sprites: Record<string, {
272
+ pixelRect: PixelRect;
273
+ width?: number;
274
+ height?: number;
275
+ }>;
276
+ }): void;
277
+ /**
278
+ * Bind the atlas texture to a texture unit
279
+ * @param unit Texture unit (0-7 typically, default 0)
280
+ */
281
+ bind(unit?: number): void;
282
+ /**
283
+ * Unbind the atlas texture
284
+ */
285
+ unbind(): void;
286
+ /**
287
+ * Get a string representation of the atlas
288
+ */
289
+ toString(): string;
290
+ }
291
+ /**
292
+ * Utility function to create a TextureAtlas from common sprite sheet formats.
293
+ */
294
+ export declare class AtlasLoader {
295
+ /**
296
+ * Create an atlas from a regular grid sprite sheet.
297
+ * All sprites must have the same dimensions and be arranged in rows and columns.
298
+ *
299
+ * @param texture The texture containing the sprite sheet
300
+ * @param options Grid configuration options
301
+ * @returns A configured TextureAtlas with all sprites defined
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * const atlas = AtlasLoader.loadFromGrid(myTexture, {
306
+ * spriteWidth: 32,
307
+ * spriteHeight: 32,
308
+ * columns: 4,
309
+ * rows: 4,
310
+ * prefix: "character_"
311
+ * });
312
+ * ```
313
+ */
314
+ static loadFromGrid(texture: Texture, options: {
315
+ spriteWidth: number;
316
+ spriteHeight: number;
317
+ columns: number;
318
+ rows: number;
319
+ prefix?: string;
320
+ columnSpacing?: number;
321
+ rowSpacing?: number;
322
+ startX?: number;
323
+ startY?: number;
324
+ }): TextureAtlas;
325
+ /**
326
+ * Create an atlas from a JSON definition file.
327
+ * The JSON should contain sprite names and their pixel rectangles.
328
+ *
329
+ * @param texture The texture containing the atlas
330
+ * @param jsonData Parsed JSON object with sprite definitions
331
+ * @returns A configured TextureAtlas
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * // JSON format:
336
+ * // {
337
+ * // "textureWidth": 512,
338
+ * // "textureHeight": 512,
339
+ * // "sprites": {
340
+ * // "player": { "pixelRect": { "x": 0, "y": 0, "width": 32, "height": 32 } },
341
+ * // "enemy": { "pixelRect": { "x": 32, "y": 0, "width": 32, "height": 32 } }
342
+ * // }
343
+ * // }
344
+ *
345
+ * const atlas = AtlasLoader.loadFromJSON(myTexture, jsonData);
346
+ * ```
347
+ */
348
+ static loadFromJSON(texture: Texture, jsonData: {
349
+ textureWidth?: number;
350
+ textureHeight?: number;
351
+ sprites: Record<string, {
352
+ pixelRect: PixelRect;
353
+ }>;
354
+ }): TextureAtlas;
355
+ }
356
+ //# sourceMappingURL=sprite-atlas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sprite-atlas.d.ts","sourceRoot":"","sources":["../../../src/core/sprite-atlas.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,yCAAyC;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,S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