Skip to content

Commit 0eaa8c6

Browse files
committed
Added INTRO-emscripten.md
1 parent 21b4335 commit 0eaa8c6

File tree

3 files changed

+85
-39
lines changed

3 files changed

+85
-39
lines changed

docs/INTRO-emscripten.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# Introduction to SDL with Emscripten
3+
4+
First, you should have the Emscripten SDK installed from:
5+
6+
https://emscripten.org/docs/getting_started/downloads.html
7+
8+
We'll start by creating a simple project to build and run [hello.c](hello.c)
9+
10+
## Building SDL
11+
12+
Once you have a command line interface with the Emscripten SDK set up and you've changed directory to the SDL directory, you can build SDL like this:
13+
14+
```sh
15+
mkdir hello
16+
cd hello
17+
emcmake cmake ..
18+
emmake make
19+
```
20+
21+
## Building your app
22+
23+
In this case we'll just run a simple command to compile our source with the SDL library we just built:
24+
```sh
25+
emcc -o index.html ../docs/hello.c -I../include -L. -lSDL3
26+
```
27+
28+
## Running your app
29+
30+
You can now run your app by pointing a webserver at your build directory and connecting a web browser to it.
31+
32+
## More information
33+
34+
A more complete example is available at:
35+
36+
https://github.com/Ravbug/sdl3-sample
37+
38+
Additional information and troubleshooting is available in [README-emscripten.md](README-emscripten.md)

docs/README-emscripten.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ If you want to build with thread support, something like this works:
239239
```bash
240240
mkdir build
241241
cd build
242-
emcmake cmake -DSDL_THREADS=On ..
242+
emcmake cmake -DSDL_THREADS=ON ..
243243
# you can also do `emcmake cmake -G Ninja ..` and then use `ninja` instead of this command.
244244
emmake make -j4
245245
```
246246

247-
To build the tests, add `-DSDL_TESTS=On` to the `emcmake cmake` command line.
248-
To build the examples, add `-DSDL_EXAMPLES=On` to the `emcmake cmake` command line.
247+
To build the tests, add `-DSDL_TESTS=ON` to the `emcmake cmake` command line.
248+
To build the examples, add `-DSDL_EXAMPLES=ON` to the `emcmake cmake` command line.
249249

250250

251251
## Building your app

docs/hello.c

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,60 @@
99
including commercial applications, and to alter it and redistribute it
1010
freely.
1111
*/
12+
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
1213
#include <SDL3/SDL.h>
1314
#include <SDL3/SDL_main.h>
1415

15-
int main(int argc, char *argv[])
16+
static SDL_Window *window = NULL;
17+
static SDL_Renderer *renderer = NULL;
18+
19+
/* This function runs once at startup. */
20+
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
21+
{
22+
/* Create the window */
23+
if (!SDL_CreateWindowAndRenderer("Hello World", 800, 600, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
24+
SDL_Log("Couldn't create window and renderer: %s\n", SDL_GetError());
25+
return SDL_APP_FAILURE;
26+
}
27+
return SDL_APP_CONTINUE;
28+
}
29+
30+
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
31+
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
32+
{
33+
if (event->type == SDL_EVENT_KEY_DOWN ||
34+
event->type == SDL_EVENT_QUIT) {
35+
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
36+
}
37+
return SDL_APP_CONTINUE;
38+
}
39+
40+
/* This function runs once per frame, and is the heart of the program. */
41+
SDL_AppResult SDL_AppIterate(void *appstate)
1642
{
17-
SDL_Window *window = NULL;
18-
SDL_Renderer *renderer = NULL;
1943
const char *message = "Hello World!";
2044
int w = 0, h = 0;
2145
float x, y;
2246
const float scale = 4.0f;
23-
bool done = false;
2447

25-
/* Create the window */
26-
if (!SDL_CreateWindowAndRenderer("Hello World", 0, 0, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
27-
SDL_Log("Couldn't create window and renderer: %s\n", SDL_GetError());
28-
return 1;
29-
}
48+
/* Center the message and scale it up */
49+
SDL_GetRenderOutputSize(renderer, &w, &h);
50+
SDL_SetRenderScale(renderer, scale, scale);
51+
x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2;
52+
y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
3053

31-
while (!done) {
32-
SDL_Event event;
33-
34-
/* Handle events */
35-
while (SDL_PollEvent(&event)) {
36-
if (event.type == SDL_EVENT_KEY_DOWN ||
37-
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
38-
event.type == SDL_EVENT_QUIT) {
39-
done = true;
40-
}
41-
}
42-
43-
/* Center the message and scale it up */
44-
SDL_GetRenderOutputSize(renderer, &w, &h);
45-
SDL_SetRenderScale(renderer, scale, scale);
46-
x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2;
47-
y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
48-
49-
/* Draw the message */
50-
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
51-
SDL_RenderClear(renderer);
52-
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
53-
SDL_RenderDebugText(renderer, x, y, message);
54-
SDL_RenderPresent(renderer);
55-
}
56-
SDL_Quit();
54+
/* Draw the message */
55+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
56+
SDL_RenderClear(renderer);
57+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
58+
SDL_RenderDebugText(renderer, x, y, message);
59+
SDL_RenderPresent(renderer);
60+
61+
return SDL_APP_CONTINUE;
62+
}
5763

58-
return 0;
64+
/* This function runs once at shutdown. */
65+
void SDL_AppQuit(void *appstate, SDL_AppResult result)
66+
{
5967
}
6068

0 commit comments

Comments
 (0)