Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Hands-On Game Development with WebAssembly
Hands-On Game Development with WebAssembly

Hands-On Game Development with WebAssembly: Learn WebAssembly C++ programming by building a retro space game

Arrow left icon
Profile Icon Rick Battagline
Arrow right icon
₱1256.99 ₱1796.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7 (9 Ratings)
eBook May 2019 596 pages 1st Edition
eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial
Arrow left icon
Profile Icon Rick Battagline
Arrow right icon
₱1256.99 ₱1796.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7 (9 Ratings)
eBook May 2019 596 pages 1st Edition
eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial
eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Hands-On Game Development with WebAssembly

HTML5 and WebAssembly

In this chapter, we will show you how the C code we write to target WebAssembly comes together with HTML5, JavaScript, and CSS to create a web page. We will teach you how to create a new HTML shell file to be used by Emscripten in the creation of our WebAssembly app. We will discuss the Module object and how Emscripten uses it as an interface between our JavaScript and the WebAssembly module. We will show you how to call WebAssembly functions written in C from within JavaScript on our HTML page. We will also show you how to call JavaScript functions from our C code. We will discuss how to use CSS to improve the look of our web page. We will introduce you to the HTML5 Canvas element and show how it is possible to display images to the canvas from within JavaScript. We will briefly discuss moving those images around the canvas from our WebAssembly module. This...

The Emscripten minimal shell file

The first build we created with Emscripten used a default HTML shell file. If you have a website, this is probably not the way you would prefer your web page to look. You would probably prefer to design your look and feel using CSS and HTML5 specific to your design or business needs. For instance, the templates I use for my websites typically include advertisements to the left and right of the game's canvas. That is how traffic to these sites is monetized. You may choose to add a logo for your website above your game's canvas. There is also a text area where Emscripten logs output from printf or other standard IO calls. You may choose to remove this textarea element altogether, or you may keep it, but keep it hidden because it is useful for debugging later.

To build the HTML file based on a new shell file that is not the default Emscripten...

Creating a new HTML shell and C file

In this section, we are going to create a new shell.c file that exposes several functions called from our JavaScript. We will also use EM_ASM to call the InitWrappers function that we will define inside the new HTML shell file that we will be creating. This function will create wrappers inside JavaScript that can call functions defined in the WebAssembly module. Before creating the new HTML shell file, we need to create the C code that will be called by the JavaScript wrappers inside the HTML shell:

  1. Create the new shell.c file as follows:
#include <emscripten.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
printf("Hello World\n");
EM_ASM( InitWrappers() );
printf("Initialization Complete\n");
}

void test() {
printf("button test\n");
}

void int_test( int num ) {
printf("int...

Defining the CSS

Now that we have some basic HTML, we need to create a new shell.css file. Without any CSS styling, our page looks pretty terrible.

A page without styling will be similar to the one shown as follows:

Figure 2.1: The Hello WebAssembly app without a CSS style

Luckily for us, a little bit of CSS goes a long way to make our web page look presentable. Here is what the new shell.css file we are creating looks like:

body {
margin-top: 20px;
}

.input_box {
width: 20%;
display: inline-block;
}
.em_button {
width: 45%;
height: 40px;
background-color: orangered;
color: white;
border: 2px solid white;
font-size: 20px;
border-radius: 8px;
transition-duration: 0.5s;
}

.em_button:hover {
background-color: orange;
color: white;
border: 2px solid white;
}

.em_input {
width: 45%;
height: 20px;
font-size: 20px;
background-color: darkslategray...

HTML5 and game development

Most HTML rendering is done through the HTML Document Object Model (DOM). The DOM is what is known as a retained mode graphical library. Retained mode graphics retain a tree known as a scene graph. This scene graph keeps track of all the graphical elements in our model and how to render them. The nice thing about retained mode graphics is that they are straightforward for a developer to manage. The graphical library does all the heavy lifting and keeps track of our objects for us as well as where they render. The downside is that a retained mode system takes up a lot more memory and provides a lot less control to the developer. When we write HTML5 games, we could take images rendered in the DOM using <IMG> HTML elements and move those elements around using JavaScript or CSS animations to manipulate the positions of those images within the DOM directly...

Adding a canvas to the Emscripten template

In an earlier part of this chapter, we discussed making calls to the Emscripten WebAssembly app from a shell template. Now that you know how to make the interaction work between JavaScript and WebAssembly, we can add a canvas element back into the template and start to manipulate that canvas using the WebAssembly module. We are going to create a new .c file that will call a JavaScript function passing it an x and y coordinate. The JavaScript function will manipulate a spaceship image, moving it around the canvas. We will also create a brand new shell file called canvas_shell.html.

As we did for the previous version of our shell, we will start by breaking this file down into four sections to discuss it at a high level. We will then discuss the essential parts of this file a piece at a time.

  1. The beginning of the HTML file starts with...

Summary

In this chapter, we discussed the Emscripten minimal shell HTML file, what its various components are, and how they work. We also wrote about what parts of the file we can do without, if we are not using our shell to generate canvas code. You learned about the Module object, and how it is the interface that uses the JavaScript glue code to tie the JavaScript in our HTML and our WebAssembly together. We then created a new WebAssembly module that contained functions we exported to allow JavaScript to use Module.cwrap to create JavaScript functions we could then call from our DOM that executes our WebAssembly functions.

We created a brand new HTML shell file that used some of the Module code from the Emscripten minimal shell, but rewrote the HTML and CSS of the original shell almost entirely. We were then able to compile that new C code and HTML shell file into a working...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Create a WebAssembly game that implements sprites, animations, physics, particle systems, and other game development fundamentals
  • Get to grips with advanced game mechanics in WebAssembly
  • Learn to use WebAssembly and WebGL to render to the HTML5 canvas element

Description

Within the next few years, WebAssembly will change the web as we know it. It promises a world where you can write an application for the web in any language, and compile it for native platforms as well as the web. This book is designed to introduce web developers and game developers to the world of WebAssembly by walking through the development of a retro arcade game. You will learn how to build a WebAssembly application using C++, Emscripten, JavaScript, WebGL, SDL, and HTML5. This book covers a lot of ground in both game development and web application development. When creating a game or application that targets WebAssembly, developers need to learn a plethora of skills and tools. This book is a sample platter of those tools and skills. It covers topics including Emscripten, C/C++, WebGL, OpenGL, JavaScript, HTML5, and CSS. The reader will also learn basic techniques for game development, including 2D sprite animation, particle systems, 2D camera design, sound effects, 2D game physics, user interface design, shaders, debugging, and optimization. By the end of the book, you will be able to create simple web games and web applications targeting WebAssembly.

Who is this book for?

Web developers and game developers interested in creating applications for the web using WebAssembly. Game developers interested in deploying their games to the web Web developers interested in creating applications that are potentially orders of magnitude faster than their existing JavaScript web apps C/C++ developers interested in using their existing skills to deploy applications to the web

What you will learn

  • Build web applications with near-native performance using WebAssembly
  • Become familiar with how web applications can be used to create games using HTML5 Canvas, WebGL, and SDL
  • Become well versed with game development concepts such as sprites, animation, particle systems, AI, physics, camera design, sound effects, and shaders
  • Deploy C/C++ applications to the browser using WebAssembly and Emscripten
  • Understand how Emscripten HTML shell templates, JavaScript glue code, and a WebAssembly module interact
  • Debug and performance tune your WebAssembly application

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 31, 2019
Length: 596 pages
Edition : 1st
Language : English
ISBN-13 : 9781838646837
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 31, 2019
Length: 596 pages
Edition : 1st
Language : English
ISBN-13 : 9781838646837
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 6,736.97
Hands-On Game Development with WebAssembly
₱2245.99
C++ Game Development By Example
₱1989.99
Learn WebAssembly
₱2500.99
Total 6,736.97 Stars icon

Table of Contents

17 Chapters
Introduction to WebAssembly and Emscripten Chevron down icon Chevron up icon
HTML5 and WebAssembly Chevron down icon Chevron up icon
Introduction to WebGL Chevron down icon Chevron up icon
Sprite Animations in WebAssembly with SDL Chevron down icon Chevron up icon
Keyboard Input Chevron down icon Chevron up icon
Game Objects and the Game Loop Chevron down icon Chevron up icon
Collision Detection Chevron down icon Chevron up icon
Basic Particle System Chevron down icon Chevron up icon
Improved Particle Systems Chevron down icon Chevron up icon
AI and Steering Behaviors Chevron down icon Chevron up icon
Designing a 2D Camera Chevron down icon Chevron up icon
Sound FX Chevron down icon Chevron up icon
Game Physics Chevron down icon Chevron up icon
UI and Mouse Input Chevron down icon Chevron up icon
Shaders and 2D Lighting Chevron down icon Chevron up icon
Debugging and Optimization Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(9 Ratings)
5 star 77.8%
4 star 11.1%
3 star 11.1%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Tim in Colorado Jul 09, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Covers the basics really well, and goes in-depth into how to use the WebAssembly toolchain to write a game.
Amazon Verified review Amazon
Jason A Tucker Jul 09, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I’m primarily a business application developer, so I was not familiar with the technology or the platforms covered in this book. The author’s writing style and methodical approach helped overcome my own knowledge gaps. Kudos to both the author and editor for making a daunting task much less intimidating.
Amazon Verified review Amazon
James Barron Jul 02, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a good review of both the burgeoning technology of web assembly and some basics of 2D game development. While this book covers a wide array of subjects from writing C++ and Javascript, Web Assembly, game development, and more, it does not assume a lot of prior knowledge of these topics and does a good job of explaining step-by-step and topic-by-topic everything needed for the book. There is also a nice smattering of enriching notes on the history of web technology and game development to give context to what is presented.Since web assembly is so new, this book explains well where it is rapidly changing, where things are still not developed or is buggy, and how to work around some common issues. It covers how C++ and Javascript interact, how to properly compile and run, and how to debug. All of these have their issues and details which are nicely explained so the reader won't need to struggle with it.All in all, it is a clear, methodical guide for both those with little knowledge and those with more experienced.
Amazon Verified review Amazon
Vivace May 31, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book is a great guide into wasm. The accompanying code works well, and i appreciated there being both C and C++ versions of things. The nature of the toolchain, multiple languages, none of which are particularly concise, doesnt especially lend itself to print, yet the authors style of dissecting what is going on piece by piece, before merging it all back together works well. I have been more than a little surprised by what a useful resource this book has been.
Amazon Verified review Amazon
L. G. Sep 23, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Little slow at the beginning then it started a fire in me like when you mix gasoline and styrofoam, light it and throw it but it ends up on your friends shirt only to burn a massive hole in it. Overall a great read and an awesome amount of knowledge included.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.