- Hands-On Game Development with WebAssembly
- Rick Battagline
- 281字
- 2021-06-24 13:41:07
Rendering a texture to the HTML5 canvas
After we load a font from the virtual filesystem and then render that font to the texture, we need to take that texture and copy it to a location in our renderer object. After we have done that, we will need to take that renderer and present its contents to the HTML5 canvas element.
The following is the source code that renders the texture to the canvas:
SDL_QueryTexture( texture,
NULL, NULL,
&dest.w, &dest.h ); // query the width and height
dest.x -= dest.w / 2;
dest.y -= dest.h / 2;
SDL_RenderCopy( renderer, texture, NULL, &dest );
SDL_RenderPresent( renderer );
The call to the SDL_QueryTexture function is used to retrieve the width and height of the texture. We need to use these values in the destination rectangle so that we render our texture to the canvas without changing its dimensions. After that call, the program knows the width and height of the texture, so it can use those values to modify the x and y variables of the destination rectangle so that it can center our text on the canvas. Because the x and y values of the dest (destination) rectangle specify the top-left corner of that rectangle, we need to subtract half the width and half the height of the rectangle to make sure that it is centered. The SDL_RenderCopy function then renders this texture to our rendering buffer and SDL_RenderPresent moves that entire buffer to the HTML5 canvas.
At this point, all that is left to do in the code is return:
return EXIT_SUCCESS;
Returning with a value of EXIT_SUCCESS tells our JavaScript glue code that everything went well when running this module.
- Instant uTorrent
- 電腦常見問題與故障排除
- 基于Proteus和Keil的C51程序設計項目教程(第2版):理論、仿真、實踐相融合
- Mastering Delphi Programming:A Complete Reference Guide
- 電腦常見故障現(xiàn)場處理
- AMD FPGA設計優(yōu)化寶典:面向Vivado/SystemVerilog
- OpenGL Game Development By Example
- 固態(tài)存儲:原理、架構(gòu)與數(shù)據(jù)安全
- 筆記本電腦使用、維護與故障排除從入門到精通(第5版)
- LPC1100系列處理器原理及應用
- 無蘋果不生活:OS X Mountain Lion 隨身寶典
- 單片機原理及應用:基于C51+Proteus仿真
- Blender Game Engine:Beginner's Guide
- STM32自學筆記
- 基于網(wǎng)絡化教學的項目化單片機應用技術(shù)