From 4f1b3206c5fd841a311a053f705af923dbe0f5fa Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Mon, 13 Feb 2017 23:01:34 +0100 Subject: [PATCH] Add HomeDir function --- raylib/platform_android.c | 3 ++- raylib/platform_android.go | 5 ++-- raylib/utils.go | 7 +++++- raylib/utils_android.c | 24 +++++++++++++++++++ raylib/utils_android.go | 25 +++++++------------ raylib/utils_windows.go | 49 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 raylib/utils_android.c create mode 100644 raylib/utils_windows.go diff --git a/raylib/platform_android.c b/raylib/platform_android.c index b033eeb..8212e0c 100644 --- a/raylib/platform_android.c +++ b/raylib/platform_android.c @@ -6,8 +6,9 @@ void android_main(struct android_app *app) { androidMain(app); } -void init_asset_manager(void *state) { +void android_init(void *state) { struct android_app *app; app = (struct android_app *)state; asset_manager = app->activity->assetManager; + internal_storage_path = app->activity->internalDataPath; } diff --git a/raylib/platform_android.go b/raylib/platform_android.go index bd6a192..01ebef6 100644 --- a/raylib/platform_android.go +++ b/raylib/platform_android.go @@ -8,9 +8,10 @@ package raylib #include extern void android_main(struct android_app *app); +extern void android_init(void *state); AAssetManager* asset_manager; -extern void init_asset_manager(void *state); +const char* internal_storage_path; */ import "C" @@ -31,7 +32,7 @@ func InitWindow(width int32, height int32, t interface{}) { app, ok := t.(unsafe.Pointer) if ok { C.InitWindow(cwidth, cheight, app) - C.init_asset_manager(app) + C.android_init(app) } } diff --git a/raylib/utils.go b/raylib/utils.go index 75864e6..c8959ec 100644 --- a/raylib/utils.go +++ b/raylib/utils.go @@ -1,4 +1,4 @@ -// +build !android +// +build !android,!windows package raylib @@ -38,3 +38,8 @@ func TraceLog(msgType int, text string, v ...interface{}) { } } } + +// HomeDir returns user home directory +func HomeDir() string { + return os.Getenv("HOME") +} diff --git a/raylib/utils_android.c b/raylib/utils_android.c new file mode 100644 index 0000000..9f290f2 --- /dev/null +++ b/raylib/utils_android.c @@ -0,0 +1,24 @@ +// +build android + +#include "_cgo_export.h" +#include + +void log_info(const char *msg) { + __android_log_print(ANDROID_LOG_INFO, "raylib", msg); +} + +void log_warn(const char *msg) { + __android_log_print(ANDROID_LOG_WARN, "raylib", msg); +} + +void log_error(const char *msg) { + __android_log_print(ANDROID_LOG_ERROR, "raylib", msg); +} + +void log_debug(const char *msg) { + __android_log_print(ANDROID_LOG_DEBUG, "raylib", msg); +} + +const char* get_internal_storage_path() { + return internal_storage_path; +} diff --git a/raylib/utils_android.go b/raylib/utils_android.go index 08af7ae..68f788b 100644 --- a/raylib/utils_android.go +++ b/raylib/utils_android.go @@ -3,29 +3,14 @@ package raylib /* -#include #include -void log_info(const char *msg) { - __android_log_print(ANDROID_LOG_INFO, "raylib", msg); -} - -void log_warn(const char *msg) { - __android_log_print(ANDROID_LOG_WARN, "raylib", msg); -} - -void log_error(const char *msg) { - __android_log_print(ANDROID_LOG_ERROR, "raylib", msg); -} - -void log_debug(const char *msg) { - __android_log_print(ANDROID_LOG_DEBUG, "raylib", msg); -} - void log_info(const char *msg); void log_warn(const char *msg); void log_error(const char *msg); void log_debug(const char *msg); + +extern char* get_internal_storage_path(); */ import "C" @@ -74,3 +59,9 @@ func TraceLog(msgType int, text string, v ...interface{}) { } } } + +// HomeDir returns user home directory +// NOTE: On Android this returns internal data path and must be called after InitWindow +func HomeDir() string { + return C.GoString(C.get_internal_storage_path()) +} diff --git a/raylib/utils_windows.go b/raylib/utils_windows.go new file mode 100644 index 0000000..56a4154 --- /dev/null +++ b/raylib/utils_windows.go @@ -0,0 +1,49 @@ +// +build windows + +package raylib + +import ( + "fmt" + "os" +) + +// Log message types +const ( + LogInfo = iota + LogError + LogWarning + LogDebug +) + +var traceDebugMsgs = false + +// Set debug messages +func SetDebug(enabled bool) { + traceDebugMsgs = enabled +} + +// Trace log +func TraceLog(msgType int, text string, v ...interface{}) { + switch msgType { + case LogInfo: + fmt.Printf("INFO: "+text+"\n", v...) + case LogError: + fmt.Printf("ERROR: "+text+"\n", v...) + os.Exit(1) + case LogWarning: + fmt.Printf("WARNING: "+text+"\n", v...) + case LogDebug: + if traceDebugMsgs { + fmt.Printf("DEBUG: "+text+"\n", v...) + } + } +} + +// HomeDir returns user home directory +func HomeDir() string { + home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + if home == "" { + home = os.Getenv("USERPROFILE") + } + return home +}