Rev 7 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | /* |
| 2 | * src/system.c |
||
| 3 | * |
||
| 4 | * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved. |
||
| 5 | * |
||
| 6 | * The use and distribution terms for this software are contained in the file |
||
| 7 | * named README, which can be found in the root of this distribution. By |
||
| 8 | * using this software in any fashion, you are agreeing to be bound by the |
||
| 9 | * terms of this license. |
||
| 10 | * |
||
| 11 | * You must not remove this notice, or any other, from this software. |
||
| 12 | */ |
||
| 13 | |||
| 11 | pmbaty | 14 | #include <SDL2/SDL.h> |
| 1 | pmbaty | 15 | |
| 16 | #include <signal.h> |
||
| 17 | |||
| 18 | #include "system.h" |
||
| 11 | pmbaty | 19 | #if defined(_WIN32) |
| 1 | pmbaty | 20 | #include "windows.h" |
| 2 | pmbaty | 21 | char *sys_getbasepath (void) |
| 22 | { |
||
| 23 | static char app_path[1024] = ""; |
||
| 24 | if (app_path[0] == 0) |
||
| 25 | { |
||
| 26 | GetModuleFileName (NULL, app_path, 1024); |
||
| 27 | if (strrchr (app_path, '\\') != NULL) |
||
| 28 | *strrchr (app_path, '\\') = 0; |
||
| 29 | } |
||
| 30 | return (app_path); |
||
| 31 | } |
||
| 11 | pmbaty | 32 | #elif defined(__APPLE__) |
| 2 | pmbaty | 33 | #include <CoreFoundation/CoreFoundation.h> |
| 34 | #include <dlfcn.h> |
||
| 35 | void MessageBox (void *handle, char *msg, char *title, int buttons) |
||
| 36 | { |
||
| 37 | const void *keys[2]; |
||
| 38 | const void *values[2]; |
||
| 39 | SInt32 result; |
||
| 1 | pmbaty | 40 | |
| 2 | pmbaty | 41 | if (title == NULL) |
| 42 | title = ""; |
||
| 1 | pmbaty | 43 | |
| 2 | pmbaty | 44 | keys[0] = kCFUserNotificationAlertHeaderKey; |
| 45 | values[0] = CFStringCreateWithCString (kCFAllocatorDefault, title, kCFStringEncodingUTF8);; |
||
| 46 | |||
| 47 | keys[1] = kCFUserNotificationAlertMessageKey; |
||
| 48 | values[1] = CFStringCreateWithCString (kCFAllocatorDefault, msg, kCFStringEncodingUTF8); |
||
| 49 | |||
| 50 | result = 0; |
||
| 51 | CFUserNotificationCreate |
||
| 52 | ( |
||
| 53 | kCFAllocatorDefault, |
||
| 54 | 0, |
||
| 55 | kCFUserNotificationPlainAlertLevel, |
||
| 56 | &result, |
||
| 57 | CFDictionaryCreate |
||
| 58 | ( |
||
| 59 | 0, |
||
| 60 | keys, |
||
| 61 | values, |
||
| 62 | sizeof (keys) / sizeof (*keys), |
||
| 63 | &kCFTypeDictionaryKeyCallBacks, |
||
| 64 | &kCFTypeDictionaryValueCallBacks |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | #define MB_OK 0 |
||
| 69 | char *sys_getbasepath (void) |
||
| 70 | { |
||
| 71 | static char app_path[1024] = ""; |
||
| 72 | if (app_path[0] == 0) |
||
| 73 | { |
||
| 74 | Dl_info addr_info; |
||
| 7 | pmbaty | 75 | if (dladdr (sys_getbasepath, &addr_info) == 0) |
| 2 | pmbaty | 76 | return ("."); |
| 77 | strcpy_s (app_path, 1024, addr_info.dli_fname); |
||
| 78 | if (strrchr (app_path, '/') != NULL) |
||
| 79 | *strrchr (app_path, '/') = 0; |
||
| 4 | pmbaty | 80 | if (strrchr (app_path, '/') != NULL) |
| 81 | *strrchr (app_path, '/') = 0; |
||
| 82 | strcat_s (app_path, 1024, "/Resources"); |
||
| 2 | pmbaty | 83 | } |
| 84 | return (app_path); |
||
| 85 | } |
||
| 11 | pmbaty | 86 | #else // !_WIN32 && !__APPLE__ |
| 2 | pmbaty | 87 | #define MessageBox(handle,msg,title,btns) fprintf (stderr, msg) |
| 88 | #define MB_OK 0 |
||
| 89 | char *sys_getbasepath (void) |
||
| 90 | { |
||
| 91 | return ("."); |
||
| 92 | } |
||
| 11 | pmbaty | 93 | #endif // _WIN32 || __APPLE__ |
| 2 | pmbaty | 94 | |
| 95 | |||
| 1 | pmbaty | 96 | /* |
| 97 | * Panic |
||
| 98 | */ |
||
| 99 | void sys_panic (char *err, ...) |
||
| 100 | { |
||
| 101 | va_list argptr; |
||
| 102 | char s[1024]; |
||
| 103 | |||
| 104 | /* prepare message */ |
||
| 105 | va_start (argptr, err); |
||
| 106 | vsprintf_s (s, sizeof (s), err, argptr); |
||
| 107 | va_end (argptr); |
||
| 108 | |||
| 109 | /* print message and die */ |
||
| 110 | MessageBox (NULL, s, NULL, MB_OK); |
||
| 111 | exit (1); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /* |
||
| 116 | * Print a message |
||
| 117 | */ |
||
| 118 | void sys_printf (char *msg, ...) |
||
| 119 | { |
||
| 120 | va_list argptr; |
||
| 121 | char s[2048]; |
||
| 122 | |||
| 123 | /* prepare message */ |
||
| 124 | va_start (argptr, msg); |
||
| 125 | vsprintf_s (s, sizeof (s), msg, argptr); |
||
| 126 | va_end (argptr); |
||
| 127 | |||
| 128 | MessageBox (NULL, s, NULL, MB_OK); |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /* |
||
| 133 | * Return number of microseconds elapsed since first call |
||
| 134 | */ |
||
| 135 | U32 sys_gettime (void) |
||
| 136 | { |
||
| 137 | static U32 ticks_base = 0; |
||
| 138 | U32 ticks; |
||
| 139 | |||
| 140 | ticks = SDL_GetTicks (); |
||
| 141 | |||
| 142 | if (!ticks_base) |
||
| 143 | ticks_base = ticks; |
||
| 144 | |||
| 145 | return ticks - ticks_base; |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /* |
||
| 150 | * Sleep a number of microseconds |
||
| 151 | */ |
||
| 152 | void sys_sleep (int s) |
||
| 153 | { |
||
| 154 | SDL_Delay (s); |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | /* |
||
| 159 | * Initialize system |
||
| 160 | */ |
||
| 161 | void sys_init (int argc, char **argv) |
||
| 162 | { |
||
| 163 | sysarg_init (argc, argv); |
||
| 164 | sysvid_init (); |
||
| 165 | sysjoy_init (); |
||
| 166 | |||
| 167 | if (sysarg_args_nosound == 0) |
||
| 168 | syssnd_init (); |
||
| 169 | |||
| 170 | atexit (sys_shutdown); |
||
| 171 | signal (SIGINT, exit); |
||
| 172 | signal (SIGTERM, exit); |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | /* |
||
| 177 | * Shutdown system |
||
| 178 | */ |
||
| 179 | void sys_shutdown (void) |
||
| 180 | { |
||
| 181 | syssnd_shutdown (); |
||
| 182 | sysjoy_shutdown (); |
||
| 183 | sysvid_shutdown (); |
||
| 184 | } |