Skip to content

Commit 4e8efe3

Browse files
author
Orwa Diraneyya
committed
Initial commit (SDL1.2)
0 parents  commit 4e8efe3

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index.wasm
2+
index.mjs

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5502
3+
}

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
wasm:
2+
emcc -g index.c -s MODULARIZE -o index.mjs

index.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <SDL.h>
2+
#include <emscripten.h>
3+
#include <stdlib.h>
4+
5+
SDL_Surface *screen;
6+
7+
void drawRandomPixels() {
8+
if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen);
9+
10+
Uint8 * pixels = screen->pixels;
11+
12+
for (int i=0; i < 32 * 32 * 4; i++) {
13+
char randomByte = rand() % 255;
14+
pixels[i] = randomByte;
15+
}
16+
17+
if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
18+
19+
SDL_Flip(screen);
20+
}
21+
22+
int main(int argc, char* argv[]) {
23+
24+
SDL_Init(SDL_INIT_VIDEO);
25+
26+
EM_ASM(
27+
SDL.defaults.copyOnLock = false;
28+
SDL.defaults.discardOnLock = true;
29+
SDL.defaults.opaqueFrontBuffer = false;
30+
);
31+
32+
screen = SDL_SetVideoMode(32, 32, 32, SDL_SWSURFACE);
33+
34+
emscripten_set_main_loop(drawRandomPixels, 0, 1);
35+
}

index.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
<style>
9+
#canvas {
10+
image-rendering: pixelated;
11+
width: 100%;
12+
aspect-ratio: 1 / 1;
13+
}
14+
</style>
15+
</head>
16+
<body>
17+
<h1>SDL2 WASM Game Loader</h1>
18+
<canvas id="canvas"></canvas>
19+
<script type="module">
20+
import initModule from './index.mjs';
21+
22+
var Module = await initModule({
23+
canvas: document.getElementById('canvas')
24+
});
25+
</script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)