Skip to content

Commit 66bbf8f

Browse files
authored
SDL2 exporting fix (#5945)
* add a test for sdl2 title changing * export things for SDL2 in the get() method, which runs at link time, as opposed to process_args which is just for compiling to bitcode. add a test for sdl2 usage with intermediate bitcode
1 parent 24b73f5 commit 66bbf8f

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

tests/sdl2_misc.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "SDL2/SDL.h"
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <assert.h>
6+
7+
#include <emscripten.h>
8+
9+
int main(int argc, char *argv[])
10+
{
11+
SDL_Window *window;
12+
SDL_Surface *surface;
13+
SDL_Cursor *cursor;
14+
15+
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
16+
printf("Unable to initialize SDL: %s\n", SDL_GetError());
17+
return 1;
18+
}
19+
20+
window = SDL_CreateWindow(
21+
"sdl2_misc",
22+
SDL_WINDOWPOS_UNDEFINED,
23+
SDL_WINDOWPOS_UNDEFINED,
24+
100,
25+
100,
26+
0
27+
);
28+
29+
EM_ASM({
30+
assert(document.title === 'sdl2_misc');
31+
});
32+
const char* intended = "a custom window title";
33+
SDL_SetWindowTitle(window, intended);
34+
const char* seen = SDL_GetWindowTitle(window);
35+
if (strcmp(intended, seen) != 0) {
36+
printf("Got a weird title back: %s\n", seen);
37+
return 1;
38+
}
39+
EM_ASM({
40+
assert(document.title === 'a custom window title');
41+
});
42+
43+
SDL_DestroyWindow(window);
44+
SDL_Quit();
45+
REPORT_RESULT(1);
46+
47+
return 0;
48+
}

tests/test_browser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,15 @@ def test_sdl2_custom_cursor(self):
28352835
shutil.copyfile(path_from_root('tests', 'cursor.bmp'), os.path.join(self.get_dir(), 'cursor.bmp'))
28362836
self.btest('sdl2_custom_cursor.c', expected='1', args=['--preload-file', 'cursor.bmp', '-s', 'USE_SDL=2'])
28372837

2838+
def test_sdl2_misc(self):
2839+
self.btest('sdl2_misc.c', expected='1', args=['-s', 'USE_SDL=2'])
2840+
print('also test building to object files first')
2841+
src = open(path_from_root('tests', 'sdl2_misc.c')).read()
2842+
open('test.c', 'w').write(self.with_report_result(src))
2843+
Popen([PYTHON, EMCC, 'test.c', '-s', 'USE_SDL=2', '-o', 'test.o']).communicate()
2844+
Popen([PYTHON, EMCC, 'test.o', '-s', 'USE_SDL=2', '-o', 'test.html']).communicate()
2845+
self.run_browser('test.html', '...', '/report_result?1')
2846+
28382847
def test_cocos2d_hello(self):
28392848
from tools import system_libs
28402849
cocos2d_root = os.path.join(system_libs.Ports.get_build_dir(), 'Cocos2d')

tools/ports/sdl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def setup_includes():
2020

2121
def get(ports, settings, shared):
2222
if settings.USE_SDL == 2:
23+
# SDL2 uses some things on the Module object, make sure they are exported
24+
settings.EXPORTED_RUNTIME_METHODS.append('Pointer_stringify')
25+
# get the port
2326
ports.fetch_project('sdl2', 'https://github.com/emscripten-ports/SDL2/archive/' + TAG + '.zip', 'SDL2-' + TAG)
2427
def create():
2528
# we are rebuilding SDL, clear dependant projects so they copy in their includes to ours properly
@@ -56,7 +59,6 @@ def process_args(ports, args, settings, shared):
5659
elif settings.USE_SDL == 2:
5760
get(ports, settings, shared)
5861
args += ['-Xclang', '-isystem' + os.path.join(shared.Cache.get_path('ports-builds'), 'sdl2', 'include')]
59-
settings.EXPORTED_RUNTIME_METHODS.append('Pointer_stringify')
6062
return args
6163

6264
def show():

0 commit comments

Comments
 (0)