官术网_书友最值得收藏!

Compiling game_objects.html

Now that we have built all of these game objects, we no longer have everything inside a single file. We will need to include several CPP files and compile them all into a single output file we will call game_objects.html. Because we have moved from the world of C to C++, we will be using em++ to indicate that the files we are compiling are C++ files and not C files. That is not strictly necessary, because Emscripten will figure out that we are compiling with C++ when it receives files with the .cpp extension as input. We are also telling the compiler explicitly the version of C++ we are using when we pass in the -std=c++17 flag. Go ahead and compile the game_objects.html file with the following em++ command:

em++ main.cpp enemy_ship.cpp player_ship.cpp projectile.cpp projectile_pool.cpp -std=c++17 --preload-file sprites -s USE_WEBGL2=1 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS=["png"] -o game_objects.html

Now that we have our game_objects.html file compiled use a web server to serve the files and open it in a browser, it should look like this:

A screenshot of game_objects.html

Do not forget that you must run WebAssembly apps using a web server, or with emrun.  If you would like to run your WebAssembly app using emrun, you must compile it with the  --emrun  flag.  The web browser requires a web server to stream the WebAssembly module.  If you attempt to open an HTML page that uses WebAssembly in a browser directly from your hard drive, that WebAssembly module will not load.

You can move your spaceship around the canvas with the arrow keys, and fire a projectile with the spacebar. The enemy ship will move around the canvas shooting randomly.

If you are having problems building this app, or any of the other apps in this book, please remember you can contact me on Twitter,  https://twitter.com/battagline/, using the Twitter handle @battagline to ask questions. I am happy to help.
主站蜘蛛池模板: 威远县| 锦屏县| 金坛市| 连平县| 边坝县| 青冈县| 喀喇沁旗| 临西县| 洱源县| 镇远县| 新巴尔虎左旗| 上蔡县| 黑河市| 永登县| 梧州市| 南宫市| 屯昌县| 武邑县| 巴中市| 黔南| 峨眉山市| 马鞍山市| 富源县| 赤城县| 沙洋县| 柏乡县| 秦皇岛市| 张家港市| 炉霍县| 山丹县| 北辰区| 阜南县| 福建省| 南京市| 泰和县| 南靖县| 洪江市| 广宁县| 和平县| 淮北市| 长岭县|