forked from timhutton/sdl-canvas-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.cpp
56 lines (44 loc) · 1.31 KB
/
core.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <SDL2/SDL.h>
#include <emscripten.h>
#include <cstdlib>
struct context
{
SDL_Renderer *renderer;
int iteration;
};
void mainloop(void *arg)
{
context *ctx = static_cast<context*>(arg);
SDL_Renderer *renderer = ctx->renderer;
// example: draw a moving rectangle
// red background
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer);
// moving blue rectangle
SDL_Rect r;
r.x = ctx->iteration % 255;
r.y = 50;
r.w = 50;
r.h = 50;
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255 );
SDL_RenderFillRect(renderer, &r );
SDL_RenderPresent(renderer);
ctx->iteration++;
}
int main()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(255, 255, 0, &window, &renderer);
context ctx;
ctx.renderer = renderer;
ctx.iteration = 0;
const int simulate_infinite_loop = 1; // call the function repeatedly
const int fps = -1; // call the function as fast as the browser wants to render (typically 60fps)
emscripten_set_main_loop_arg(mainloop, &ctx, fps, simulate_infinite_loop);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}