Skip to content

Commit bc89b2b

Browse files
committed
True image width and height
1 parent a71e743 commit bc89b2b

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

raylib.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class RaylibJs {
4242
this.currentPressedKeyState = new Set();
4343
this.currentMouseWheelMoveState = 0;
4444
this.currentMousePosition = {x: 0, y: 0};
45-
this.images = [];
45+
this.images = {};
46+
this.nextTextureId = 0;
4647
this.quit = false;
4748
}
4849

@@ -285,29 +286,39 @@ class RaylibJs {
285286
const buffer = this.wasm.instance.exports.memory.buffer;
286287
const filename = cstr_by_ptr(buffer, filename_ptr);
287288

288-
var result = new Uint32Array(buffer, result_ptr, 5)
289289
var img = new Image();
290-
img.src = filename;
291-
this.images.push(img);
292290

293-
result[0] = this.images.indexOf(img);
294-
// TODO: get the true width and height of the image
295-
result[1] = 256; // width
296-
result[2] = 256; // height
297-
result[3] = 1; // mipmaps
298-
result[4] = 7; // format PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
291+
const id = this.nextTextureId++;
292+
img.dataset.textureId = id;
293+
294+
img.addEventListener("load", () => {
295+
this.images[img.dataset.textureId] = img;
296+
});
297+
img.src = filename;
299298

299+
var result = new Uint32Array(buffer, result_ptr, 1)
300+
result[0] = id;
300301
return result;
301302
}
302303

303304
// RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
304305
DrawTexture(texture_ptr, posX, posY, color_ptr) {
305306
const buffer = this.wasm.instance.exports.memory.buffer;
306-
const [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr, 5);
307+
const [id] = new Uint32Array(buffer, texture_ptr, 1);
308+
307309
// // TODO: implement tinting for DrawTexture
308310
// const tint = getColorFromMemory(buffer, color_ptr);
309311

310-
this.ctx.drawImage(this.images[id], posX, posY);
312+
const image = this.images[id];
313+
if(image) {
314+
this.ctx.drawImage(
315+
image,
316+
posX - (image.naturalWidth / 2),
317+
posY - (image.naturalHeight / 2),
318+
image.naturalWidth,
319+
image.naturalHeight
320+
);
321+
}
311322
}
312323

313324
// TODO: codepoints are not implemented

0 commit comments

Comments
 (0)