@@ -42,7 +42,8 @@ class RaylibJs {
42
42
this . currentPressedKeyState = new Set ( ) ;
43
43
this . currentMouseWheelMoveState = 0 ;
44
44
this . currentMousePosition = { x : 0 , y : 0 } ;
45
- this . images = [ ] ;
45
+ this . images = { } ;
46
+ this . nextTextureId = 0 ;
46
47
this . quit = false ;
47
48
}
48
49
@@ -285,29 +286,39 @@ class RaylibJs {
285
286
const buffer = this . wasm . instance . exports . memory . buffer ;
286
287
const filename = cstr_by_ptr ( buffer , filename_ptr ) ;
287
288
288
- var result = new Uint32Array ( buffer , result_ptr , 5 )
289
289
var img = new Image ( ) ;
290
- img . src = filename ;
291
- this . images . push ( img ) ;
292
290
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 ;
299
298
299
+ var result = new Uint32Array ( buffer , result_ptr , 1 )
300
+ result [ 0 ] = id ;
300
301
return result ;
301
302
}
302
303
303
304
// RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
304
305
DrawTexture ( texture_ptr , posX , posY , color_ptr ) {
305
306
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
+
307
309
// // TODO: implement tinting for DrawTexture
308
310
// const tint = getColorFromMemory(buffer, color_ptr);
309
311
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
+ }
311
322
}
312
323
313
324
// TODO: codepoints are not implemented
0 commit comments