Skip to content

Commit bb2e77d

Browse files
committed
Add example to wasm paths
1 parent 9b778b6 commit bb2e77d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
const wasmPaths = {
4848
"tsoding": ["game",],
4949
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
50-
"shapes": ["shapes_colors_palette"]
50+
"shapes": ["shapes_colors_palette"],
51+
"textures": ["textures_logo_raylib"]
5152
}
5253

5354
const raylibExampleSelect = document.getElementById("raylib-example-select");

raylib.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class RaylibJs {
3232
this.currentPressedKeyState = new Set();
3333
this.currentMouseWheelMoveState = 0;
3434
this.currentMousePosition = {x: 0, y: 0};
35+
this.images = [];
3536
this.quit = false;
3637
}
3738

@@ -235,6 +236,36 @@ class RaylibJs {
235236
return this.ctx.measureText(text).width;
236237
}
237238

239+
// RLAPI Texture2D LoadTexture(const char *fileName);
240+
async LoadTexture(result_ptr, filename_ptr) {
241+
const buffer = this.wasm.instance.exports.memory.buffer;
242+
const filename = cstr_by_ptr(buffer, filename_ptr);
243+
244+
var result = new Uint32Array(buffer, result_ptr, 5)
245+
var img = new Image();
246+
img.src = filename;
247+
this.images.push(img);
248+
249+
result[0] = this.images.indexOf(img);
250+
result[1] = 256; // width
251+
result[2] = 256; // height
252+
result[3] = 1; // mipmaps
253+
result[4] = 7; // format PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
254+
255+
return result;
256+
}
257+
258+
// RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
259+
DrawTexture(texture_ptr, posX, posY, color_ptr) {
260+
const buffer = this.wasm.instance.exports.memory.buffer;
261+
const [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr, 5);
262+
// const tint = getColorFromMemory(buffer, color_ptr);
263+
264+
if(this.images[id]) {
265+
this.ctx.drawImage(this.images[id], posX, posY);
266+
}
267+
}
268+
238269
raylib_js_set_entry(entry) {
239270
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
240271
}

0 commit comments

Comments
 (0)