aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-04-08 10:59:57 +0200
committerLoïc Hoguin <[email protected]>2014-04-08 11:01:03 +0200
commit424cf7c72829a39f10f1d5e6975118e4348c0379 (patch)
tree2a6aae87f9daf2a856e87bd0a85c772ced32f48a
parentf5ecbb3b0282637423b236b2532d3e312cb8b291 (diff)
downloadesdl2-424cf7c72829a39f10f1d5e6975118e4348c0379.tar.gz
esdl2-424cf7c72829a39f10f1d5e6975118e4348c0379.tar.bz2
esdl2-424cf7c72829a39f10f1d5e6975118e4348c0379.zip
Make the sdl module C code use the main thread
All video operations should be on the main thread, including initialization. This makes no difference on my system, but might on others. There is no cost doing this as these functions are very rarely called, and usually before/after everything else.
-rw-r--r--c_src/sdl.c70
-rw-r--r--src/sdl.erl11
2 files changed, 60 insertions, 21 deletions
diff --git a/c_src/sdl.c b/c_src/sdl.c
index 6a429f3..02e8150 100644
--- a/c_src/sdl.c
+++ b/c_src/sdl.c
@@ -27,16 +27,33 @@
NIF_LIST_TO_FLAGS_FUNCTION(list_to_init_flags, Uint32, INIT_FLAGS)
+// init
+
+NIF_CALL_HANDLER(thread_init)
+{
+ if (SDL_Init((long)args[0]))
+ return sdl_error_tuple(env);
+
+ return atom_ok;
+}
+
NIF_FUNCTION(init)
{
Uint32 flags = 0;
BADARG_IF(!list_to_init_flags(env, argv[0], &flags));
- if (SDL_Init(flags) == 0)
- return atom_ok;
+ return nif_thread_call(env, thread_init, 1, flags);
+}
+
+// init_subsystem
+
+NIF_CALL_HANDLER(thread_init_subsystem)
+{
+ if (SDL_InitSubSystem((long)args[0]))
+ return sdl_error_tuple(env);
- return sdl_error_tuple(env);
+ return atom_ok;
}
NIF_FUNCTION(init_subsystem)
@@ -45,17 +62,26 @@ NIF_FUNCTION(init_subsystem)
BADARG_IF(!list_to_init_flags(env, argv[0], &flags));
- if (SDL_InitSubSystem(flags) == 0)
- return atom_ok;
+ return nif_thread_call(env, thread_init_subsystem, 1, flags);
+}
+
+// quit
- return sdl_error_tuple(env);
+NIF_CAST_HANDLER(thread_quit)
+{
+ SDL_Quit();
}
NIF_FUNCTION(quit)
{
- SDL_Quit();
+ return nif_thread_cast(env, thread_quit, 0);;
+}
- return atom_ok;
+// quit_subsystem
+
+NIF_CAST_HANDLER(thread_quit_subsystem)
+{
+ SDL_QuitSubSystem((long)args[0]);
}
NIF_FUNCTION(quit_subsystem)
@@ -64,19 +90,32 @@ NIF_FUNCTION(quit_subsystem)
BADARG_IF(!list_to_init_flags(env, argv[0], &flags));
- SDL_QuitSubSystem(flags);
-
- return atom_ok;
+ return nif_thread_cast(env, thread_quit_subsystem, 1, flags);
}
-NIF_FUNCTION(set_main_ready)
+// set_main_ready
+
+NIF_CAST_HANDLER(thread_set_main_ready)
{
SDL_SetMainReady();
+}
- return atom_ok;
+NIF_FUNCTION(set_main_ready)
+{
+ return nif_thread_cast(env, thread_set_main_ready, 0);;
}
+// was_init
// @todo Implement the case where we want to receive a list of everything init.
+
+NIF_CALL_HANDLER(thread_was_init)
+{
+ if (SDL_WasInit((long)args[0]))
+ return atom_true;
+
+ return atom_false;
+}
+
NIF_FUNCTION(was_init)
{
unsigned int length;
@@ -86,8 +125,5 @@ NIF_FUNCTION(was_init)
BADARG_IF(length == 0);
BADARG_IF(!list_to_init_flags(env, argv[0], &flags));
- if (SDL_WasInit(flags))
- return atom_true;
-
- return atom_false;
+ return nif_thread_call(env, thread_was_init, 1, flags);
}
diff --git a/src/sdl.erl b/src/sdl.erl
index b7adae6..1b8c6a6 100644
--- a/src/sdl.erl
+++ b/src/sdl.erl
@@ -23,10 +23,11 @@
-export([is_started/1]).
start() ->
- esdl2:init([]).
+ start([]).
start(Subsystems) ->
- esdl2:init(Subsystems).
+ esdl2:init(Subsystems),
+ receive {'_nif_thread_ret_', Ret} -> Ret end.
stop() ->
esdl2:quit().
@@ -42,10 +43,12 @@ stop_on_exit() ->
ok.
start_subsystems(Subsystems) ->
- esdl2:init_subsystem(Subsystems).
+ esdl2:init_subsystem(Subsystems),
+ receive {'_nif_thread_ret_', Ret} -> Ret end.
stop_subsystems(Subsystems) ->
esdl2:quit_subsystem(Subsystems).
is_started(Subsystem) ->
- esdl2:was_init([Subsystem]).
+ esdl2:was_init([Subsystem]),
+ receive {'_nif_thread_ret_', Ret} -> Ret end.