Skip to content

Commit

Permalink
Add example to wasm paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jkisor committed Feb 14, 2024
1 parent 9b778b6 commit bb2e77d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
const wasmPaths = {
"tsoding": ["game",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
"shapes": ["shapes_colors_palette"]
"shapes": ["shapes_colors_palette"],
"textures": ["textures_logo_raylib"]
}

const raylibExampleSelect = document.getElementById("raylib-example-select");
Expand Down
31 changes: 31 additions & 0 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RaylibJs {
this.currentPressedKeyState = new Set();
this.currentMouseWheelMoveState = 0;
this.currentMousePosition = {x: 0, y: 0};
this.images = [];
this.quit = false;
}

Expand Down Expand Up @@ -235,6 +236,36 @@ class RaylibJs {
return this.ctx.measureText(text).width;
}

// RLAPI Texture2D LoadTexture(const char *fileName);
async LoadTexture(result_ptr, filename_ptr) {
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);
result[1] = 256; // width
result[2] = 256; // height
result[3] = 1; // mipmaps
result[4] = 7; // format PIXELFORMAT_UNCOMPRESSED_R8G8B8A8

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 tint = getColorFromMemory(buffer, color_ptr);

if(this.images[id]) {
this.ctx.drawImage(this.images[id], posX, posY);
}
}

raylib_js_set_entry(entry) {
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
}
Expand Down

0 comments on commit bb2e77d

Please sign in to comment.