cubeforge 0.3.1 → 0.3.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.
Files changed (2) hide show
  1. package/dist/index.js +49 -17
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1500,17 +1500,33 @@ var RenderSystem = class {
1500
1500
  loadTexture(src) {
1501
1501
  const cached = this.textures.get(src);
1502
1502
  if (cached) return cached;
1503
- let img = this.imageCache.get(src);
1504
- if (!img) {
1505
- img = new Image();
1503
+ const existing = this.imageCache.get(src);
1504
+ if (existing && existing.complete && existing.naturalWidth > 0) {
1505
+ const gl = this.gl;
1506
+ const tex = gl.createTexture();
1507
+ gl.bindTexture(gl.TEXTURE_2D, tex);
1508
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, existing);
1509
+ gl.generateMipmap(gl.TEXTURE_2D);
1510
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
1511
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1512
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1513
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1514
+ this.textures.set(src, tex);
1515
+ return tex;
1516
+ }
1517
+ if (!existing) {
1518
+ const img = new Image();
1506
1519
  img.src = src;
1507
1520
  img.onload = () => {
1508
- const tex = this.gl.createTexture();
1509
- this.gl.bindTexture(this.gl.TEXTURE_2D, tex);
1510
- this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, img);
1511
- this.gl.generateMipmap(this.gl.TEXTURE_2D);
1512
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_LINEAR);
1513
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
1521
+ const gl = this.gl;
1522
+ const tex = gl.createTexture();
1523
+ gl.bindTexture(gl.TEXTURE_2D, tex);
1524
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
1525
+ gl.generateMipmap(gl.TEXTURE_2D);
1526
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
1527
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1528
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1529
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1514
1530
  this.textures.set(src, tex);
1515
1531
  };
1516
1532
  this.imageCache.set(src, img);
@@ -1773,20 +1789,35 @@ var RenderSystem = class {
1773
1789
  const transform = world.getComponent(id, "Transform");
1774
1790
  const sprite = world.getComponent(id, "Sprite");
1775
1791
  if (!sprite.visible) continue;
1776
- if (sprite.src && !sprite.image) {
1792
+ if (sprite.image && sprite.image.complete && sprite.image.naturalWidth > 0) {
1793
+ const src = sprite.image.src;
1794
+ if (src && !this.textures.has(src)) {
1795
+ const gl2 = this.gl;
1796
+ const tex = gl2.createTexture();
1797
+ gl2.bindTexture(gl2.TEXTURE_2D, tex);
1798
+ gl2.texImage2D(gl2.TEXTURE_2D, 0, gl2.RGBA, gl2.RGBA, gl2.UNSIGNED_BYTE, sprite.image);
1799
+ gl2.generateMipmap(gl2.TEXTURE_2D);
1800
+ gl2.texParameteri(gl2.TEXTURE_2D, gl2.TEXTURE_MIN_FILTER, gl2.LINEAR_MIPMAP_LINEAR);
1801
+ gl2.texParameteri(gl2.TEXTURE_2D, gl2.TEXTURE_MAG_FILTER, gl2.LINEAR);
1802
+ gl2.texParameteri(gl2.TEXTURE_2D, gl2.TEXTURE_WRAP_S, gl2.CLAMP_TO_EDGE);
1803
+ gl2.texParameteri(gl2.TEXTURE_2D, gl2.TEXTURE_WRAP_T, gl2.CLAMP_TO_EDGE);
1804
+ this.textures.set(src, tex);
1805
+ }
1806
+ } else if (sprite.src && !sprite.image) {
1777
1807
  let img = this.imageCache.get(sprite.src);
1778
1808
  if (!img) {
1779
1809
  img = new Image();
1780
1810
  img.src = sprite.src;
1781
1811
  this.imageCache.set(sprite.src, img);
1782
1812
  img.onload = () => {
1783
- const tex = this.gl.createTexture();
1784
- this.gl.bindTexture(this.gl.TEXTURE_2D, tex);
1785
- this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, img);
1786
- this.gl.generateMipmap(this.gl.TEXTURE_2D);
1787
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_LINEAR);
1788
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
1789
- this.textures.set(sprite.src, tex);
1813
+ const gl2 = this.gl;
1814
+ const tex = gl2.createTexture();
1815
+ gl2.bindTexture(gl2.TEXTURE_2D, tex);
1816
+ gl2.texImage2D(gl2.TEXTURE_2D, 0, gl2.RGBA, gl2.RGBA, gl2.UNSIGNED_BYTE, img);
1817
+ gl2.generateMipmap(gl2.TEXTURE_2D);
1818
+ gl2.texParameteri(gl2.TEXTURE_2D, gl2.TEXTURE_MIN_FILTER, gl2.LINEAR_MIPMAP_LINEAR);
1819
+ gl2.texParameteri(gl2.TEXTURE_2D, gl2.TEXTURE_MAG_FILTER, gl2.LINEAR);
1820
+ this.textures.set(img.src, tex);
1790
1821
  };
1791
1822
  }
1792
1823
  sprite.image = img;
@@ -3687,6 +3718,7 @@ function Sprite({
3687
3718
  const viteEnv = import.meta.env;
3688
3719
  const base = (viteEnv?.BASE_URL ?? "/").replace(/\/$/, "");
3689
3720
  const resolvedSrc = base && src.startsWith("/") ? base + src : src;
3721
+ comp.src = resolvedSrc;
3690
3722
  engine.assets.loadImage(resolvedSrc).then((img) => {
3691
3723
  const c = engine.ecs.getComponent(entityId, "Sprite");
3692
3724
  if (c) c.image = img;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cubeforge",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "React-first 2D browser game engine",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {