From 3026b4860fd3a1436a59bf2d26792fbbf6b60917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 3 Apr 2014 19:13:04 +0200 Subject: Add sdl_window:set_icon/2 The surface needs to be kept around and not GC otherwise it crashes. --- c_src/esdl2.h | 1 + c_src/sdl_window.c | 25 +++++++++++++++++++++++++ src/esdl2.erl | 4 ++++ src/sdl_window.erl | 5 +++++ 4 files changed, 35 insertions(+) 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. -- cgit v1.2.3