Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image width and height #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class RaylibJs {
this.currentPressedKeyState = new Set();
this.currentMouseWheelMoveState = 0;
this.currentMousePosition = {x: 0, y: 0};
this.images = [];
this.images = {};
this.nextTextureId = 0;
this.quit = false;
}

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

var result = new Uint32Array(buffer, result_ptr, 5)
var img = new Image();
img.src = filename;
this.images.push(img);

result[0] = this.images.indexOf(img);
// TODO: get the true width and height of the image
result[1] = 256; // width
result[2] = 256; // height
result[3] = 1; // mipmaps
result[4] = 7; // format PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
const id = this.nextTextureId++;
img.dataset.textureId = id;

img.addEventListener("load", () => {
this.images[img.dataset.textureId] = img;
});
img.src = filename;

var result = new Uint32Array(buffer, result_ptr, 1)
result[0] = id;
return result;
}

// RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
DrawTexture(texture_ptr, posX, posY, color_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr, 5);
const [id] = new Uint32Array(buffer, texture_ptr, 1);

// // TODO: implement tinting for DrawTexture
// const tint = getColorFromMemory(buffer, color_ptr);

this.ctx.drawImage(this.images[id], posX, posY);
const image = this.images[id];
if(image) {
this.ctx.drawImage(
image,
posX - (image.naturalWidth / 2),
posY - (image.naturalHeight / 2),
image.naturalWidth,
image.naturalHeight
);
}
}

// TODO: codepoints are not implemented
Expand Down