aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-04-03 19:13:04 +0200
committerLoïc Hoguin <[email protected]>2014-04-03 19:21:31 +0200
commit3026b4860fd3a1436a59bf2d26792fbbf6b60917 (patch)
tree8b8e3b8aafcc3968ee8031293fa63f65fbac4f66
parentafc06a3ef6689bd86780c3df065d3ec8efa4bce8 (diff)
downloadesdl2-3026b4860fd3a1436a59bf2d26792fbbf6b60917.tar.gz
esdl2-3026b4860fd3a1436a59bf2d26792fbbf6b60917.tar.bz2
esdl2-3026b4860fd3a1436a59bf2d26792fbbf6b60917.zip
Add sdl_window:set_icon/2
The surface needs to be kept around and not GC otherwise it crashes.
-rw-r--r--c_src/esdl2.h1
-rw-r--r--c_src/sdl_window.c25
-rw-r--r--src/esdl2.erl4
-rw-r--r--src/sdl_window.erl5
4 files changed, 35 insertions, 0 deletions
diff --git a/c_src/esdl2.h b/c_src/esdl2.h
index d8da051..de6b800 100644
--- a/c_src/esdl2.h
+++ b/c_src/esdl2.h
@@ -180,6 +180,7 @@
F(set_window_brightness, 2) \
F(set_window_fullscreen, 2) \
F(set_window_grab, 2) \
+ F(set_window_icon, 2) \
// Generated declarations for the NIF.
diff --git a/c_src/sdl_window.c b/c_src/sdl_window.c
index c32d33c..b79cd4f 100644
--- a/c_src/sdl_window.c
+++ b/c_src/sdl_window.c
@@ -518,3 +518,28 @@ NIF_FUNCTION(set_window_grab)
return nif_thread_cast(env, thread_set_window_grab, 2,
NIF_RES_GET(Window, window_res), b);
}
+
+// set_window_icon
+//
+// We use a call here because we need the surface to exist until this call
+// succeeds. If we didn't, a race condition might happen where the surface
+// is GC before it is used in the main thread.
+
+NIF_CALL_HANDLER(thread_set_window_icon)
+{
+ SDL_SetWindowIcon(args[0], args[1]);
+
+ return atom_ok;
+}
+
+NIF_FUNCTION(set_window_icon)
+{
+ void* window_res;
+ void* surface_res;
+
+ BADARG_IF(!enif_get_resource(env, argv[0], res_Window, &window_res));
+ BADARG_IF(!enif_get_resource(env, argv[1], res_Surface, &surface_res));
+
+ return nif_thread_call(env, thread_set_window_icon, 2,
+ NIF_RES_GET(Window, window_res), NIF_RES_GET(Surface, surface_res));
+}
diff --git a/src/esdl2.erl b/src/esdl2.erl
index 4abc20b..e383e58 100644
--- a/src/esdl2.erl
+++ b/src/esdl2.erl
@@ -92,6 +92,7 @@
-export([set_window_brightness/2]).
-export([set_window_fullscreen/2]).
-export([set_window_grab/2]).
+-export([set_window_icon/2]).
%% @todo We probably want to accept an env variable or somthing for the location.
-on_load(on_load/0).
@@ -297,3 +298,6 @@ set_window_fullscreen(_, _) ->
set_window_grab(_, _) ->
erlang:nif_error({not_loaded, ?MODULE}).
+
+set_window_icon(_, _) ->
+ erlang:nif_error({not_loaded, ?MODULE}).
diff --git a/src/sdl_window.erl b/src/sdl_window.erl
index 0da1e57..0232fc0 100644
--- a/src/sdl_window.erl
+++ b/src/sdl_window.erl
@@ -35,6 +35,7 @@
-export([set_brightness/2]).
-export([set_fullscreen/2]).
-export([grab_input/2]).
+-export([set_icon/2]).
create(Title, X, Y, W, H, Flags) ->
esdl2:create_window(Title, X, Y, W, H, Flags),
@@ -115,3 +116,7 @@ set_fullscreen(Window, Flag) ->
grab_input(Window, Grab) ->
esdl2:set_window_grab(Window, Grab).
+
+set_icon(Window, Surface) ->
+ esdl2:set_window_icon(Window, Surface),
+ receive {'_nif_thread_ret_', Ret} -> Ret end.