aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-04-02 22:41:43 +0200
committerLoïc Hoguin <[email protected]>2014-04-02 22:41:43 +0200
commit3fca1d3a170ac311fc379b3128be411edc21b0ee (patch)
tree48127e185c8aa421f494a39cb7fdda32e37dede7
parent953eb8f45bf1fe37fec3e7fd42490d227971bc78 (diff)
downloadesdl2-3fca1d3a170ac311fc379b3128be411edc21b0ee.tar.gz
esdl2-3fca1d3a170ac311fc379b3128be411edc21b0ee.tar.bz2
esdl2-3fca1d3a170ac311fc379b3128be411edc21b0ee.zip
Add the SDL filesystem functions
The one giving you a safe path to save data is particularly important for writing games.
-rw-r--r--c_src/esdl2.h3
-rw-r--r--c_src/sdl_filesystem.c57
-rw-r--r--src/esdl2.erl12
-rw-r--r--src/sdl_filesystem.erl24
4 files changed, 96 insertions, 0 deletions
diff --git a/c_src/esdl2.h b/c_src/esdl2.h
index 8024a9d..7af7097 100644
--- a/c_src/esdl2.h
+++ b/c_src/esdl2.h
@@ -115,6 +115,9 @@
F(set_clipboard_text, 1) \
/* sdl_events */ \
F(poll_event, 0) \
+ /* sdl_filesystem */ \
+ F(get_base_path, 0) \
+ F(get_pref_path, 2) \
/* sdl_power */ \
F(get_power_info, 0) \
/* sdl_renderer */ \
diff --git a/c_src/sdl_filesystem.c b/c_src/sdl_filesystem.c
new file mode 100644
index 0000000..cf6c601
--- /dev/null
+++ b/c_src/sdl_filesystem.c
@@ -0,0 +1,57 @@
+// Copyright (c) 2014, Loïc Hoguin <[email protected]>
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+#include "esdl2.h"
+
+// get_base_path
+
+NIF_FUNCTION(get_base_path)
+{
+ char* path;
+ ERL_NIF_TERM term;
+
+ path = SDL_GetBasePath();
+
+ if (!path)
+ return sdl_error_tuple(env);
+
+ term = enif_make_string(env, path, ERL_NIF_LATIN1);
+
+ SDL_free(path);
+
+ return term;
+}
+
+// get_pref_path
+
+NIF_FUNCTION(get_pref_path)
+{
+ char org[255], app[255];
+ char* path;
+ ERL_NIF_TERM term;
+
+ BADARG_IF(!enif_get_string(env, argv[0], org, 255, ERL_NIF_LATIN1));
+ BADARG_IF(!enif_get_string(env, argv[1], app, 255, ERL_NIF_LATIN1));
+
+ path = SDL_GetPrefPath(org, app);
+
+ if (!path)
+ return sdl_error_tuple(env);
+
+ term = enif_make_string(env, path, ERL_NIF_LATIN1);
+
+ SDL_free(path);
+
+ return term;
+}
diff --git a/src/esdl2.erl b/src/esdl2.erl
index e6f6738..39ff1e3 100644
--- a/src/esdl2.erl
+++ b/src/esdl2.erl
@@ -30,6 +30,10 @@
%% sdl_events
-export([poll_event/0]).
+%% sdl_filesystem
+-export([get_base_path/0]).
+-export([get_pref_path/2]).
+
%% sdl_power
-export([get_power_info/0]).
@@ -102,6 +106,14 @@ set_clipboard_text(_) ->
poll_event() ->
erlang:nif_error({not_loaded, ?MODULE}).
+%% sdl_filesystem
+
+get_base_path() ->
+ erlang:nif_error({not_loaded, ?MODULE}).
+
+get_pref_path(_, _) ->
+ erlang:nif_error({not_loaded, ?MODULE}).
+
%% sdl_power
get_power_info() ->
diff --git a/src/sdl_filesystem.erl b/src/sdl_filesystem.erl
new file mode 100644
index 0000000..5dd5441
--- /dev/null
+++ b/src/sdl_filesystem.erl
@@ -0,0 +1,24 @@
+%% Copyright (c) 2014, Loïc Hoguin <[email protected]>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+-module(sdl_filesystem).
+
+-export([get_base_path/0]).
+-export([get_pref_path/2]).
+
+get_base_path() ->
+ esdl2:get_base_path().
+
+get_pref_path(Org, App) ->
+ esdl2:get_pref_path(Org, App).