Skip to content

redblobgames/helloworld-sdl2-opengl-emscripten

Repository files navigation

http://unmaintained.tech/badge.svg

I wanted a bare-bones program for SDL2 + OpenGL 2D rendering that also compiled to JS+WebGL with Emscripten+Wasm. There are some SDL2 + OpenGL tutorials out there but most of them don’t target the WebGL subset of OpenGL. I can’t use the fixed function pipeline, so older OpenGL code won’t work, and I can’t use newer things like vertex array objects, instancing, or geometry shaders, so newer OpenGL code often won’t work.

Installation

The Makefile should work on Mac OS X and Linux. The code should work on Windows and other SDL+OpenGL platforms but I haven’t tested that.

Install SDL2

On Mac using Homebrew:

brew install SDL2 SDL2_image

On RedHat/Fedora flavors of Linux:

sudo yum install SDL2-devel SDL2_image-devel

On Debian/Ubuntu flavors of Linux:

sudo apt install libsdl2-dev libsdl2-image-dev

On Arch flavors of Linux:

sudo pacman -S sdl2 sdl2_image

Install STB, Dear Imgui

git submodule init
git submodule update

Compiling and running

To run the native code:

make && bin/main

To run the emscripten code: (brew install emscripten on Mac, pacman -S emscripten on Arch Linux):

make www

and then tell the webserver to serve the files from www/.

My Emscripten-ed wasm output is on my web site.

I’ve also written a partial CMakeLists.txt file but am not using it yet.

The font included in this project is Droid Sans, under the Apache License v2. The ImGui library includes the Proggy font.

About

Basic program that uses SDL2+OpenGL, compiling both locally and via emscripten

Topics

Resources

License

Stars

Watchers

Forks