This simple sample shows how to use emscripten to build an app using OpenGL and then deploy it as a native application on Android and IOS
This uses the following technologies:
- emscripten with OpenGL - This is used to convert the C++ code to WebAssembly
- Capacitor - Cross platform native runtime for web apps.
-
(On Windows and Mac OS/X) Install Android studio
-
(On Mac OS/X) Install XCode from the Mac App Store
-
Set up an emscripten app so that it builds into the
dist/directory with a main entry pointindex.html. Note that you can modify this in thecapacitor.config.ts, but it is easiest to start with the defaults. -
Initialize
npmin the project root directory if you haven't already done so withnpm init -
Install local standalone web server (serve)[https://www.npmjs.com/package/serve]
npm install serve -
Set up a default build command and possibly a serve command in
package.jsonin thescriptssection. Changeappnameto match your source files:"scripts": { "build": "emcc appname.cpp -o dist/index.html -s USE_WEBGL2=1 -s FULL_ES2=1 -O2 --shell-file appname.html", "serve": "serve dist" },
-
npm install @capacitor/core @capacitor/cli -
Initialize Capacitor. You only need to do this once.
npx cap init -
Add the Platforms you want to build for. Note that while you can do both on a Mac, you can only do Android on a windows platform.
npx cap add ios npx cap add android -
Build your project so that the web pages are in the
dist/directory.npm run build -
If you want to test it locally, you can use serve:
npm run serveand then open up the link (typically
http://localhost:3000) to see the application run. -
Synchronize the built code with the native project. Basically it copies the compiled code into the target directories. You have to do this each time after you build the code.
npx cap sync -
Test it on the target device or emulator
npx cap open ios npx cap open android