-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.c
71 lines (52 loc) · 1.6 KB
/
index.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <SDL2/SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include <stdlib.h>
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
void drawRandomPixels() {
if (SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
Uint8 * pixels = surface->pixels;
for (int i=0; i < 32 * 32 * 4; i++) {
char randomByte = rand() % 255;
pixels[i] = randomByte;
}
if (SDL_MUSTLOCK(surface))
SDL_UnlockSurface(surface);
SDL_Texture *screenTexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screenTexture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(screenTexture);
}
#define FPS 30
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(512, 512, 0, &window, &renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
SDL_RenderSetLogicalSize(renderer, 32, 32);
surface = SDL_CreateRGBSurface(0, 32, 32, 32, 0, 0, 0, 0);
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(drawRandomPixels, FPS, 1);
#else
while (1) {
// Variable to store GUI events
SDL_Event event;
Uint64 timeout = SDL_GetTicks64() + (Uint8)(1000.0f / FPS);
drawRandomPixels();
if (SDL_WaitEventTimeout(&event, timeout - SDL_GetTicks64()))
{
if (event.type == SDL_QUIT)
// Break out of the loop on quit
break;
}
}
SDL_FreeSurface(surface);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
#endif
}