Rev 1 | Rev 4 | 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 | |||
| 14 | #include <SDL.h> |
||
| 15 | |||
| 16 | #include <signal.h> |
||
| 17 | |||
| 18 | #include "system.h" |
||
| 2 | pmbaty | 19 | #ifdef 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 | } |
||
| 32 | #else // !WIN32 |
||
| 33 | #ifdef __APPLE__ |
||
| 34 | #include <CoreFoundation/CoreFoundation.h> |
||
| 35 | #include <dlfcn.h> |
||
| 36 | void MessageBox (void *handle, char *msg, char *title, int buttons) |
||
| 37 | { |
||
| 38 | const void *keys[2]; |
||
| 39 | const void *values[2]; |
||
| 40 | SInt32 result; |
||
| 1 | pmbaty | 41 | |
| 2 | pmbaty | 42 | if (title == NULL) |
| 43 | title = ""; |
||
| 1 | pmbaty | 44 | |
| 2 | pmbaty | 45 | keys[0] = kCFUserNotificationAlertHeaderKey; |
| 46 | values[0] = CFStringCreateWithCString (kCFAllocatorDefault, title, kCFStringEncodingUTF8);; |
||
| 47 | |||
| 48 | keys[1] = kCFUserNotificationAlertMessageKey; |
||
| 49 | values[1] = CFStringCreateWithCString (kCFAllocatorDefault, msg, kCFStringEncodingUTF8); |
||
| 50 | |||
| 51 | result = 0; |
||
| 52 | CFUserNotificationCreate |
||
| 53 | ( |
||
| 54 | kCFAllocatorDefault, |
||
| 55 | 0, |
||
| 56 | kCFUserNotificationPlainAlertLevel, |
||
| 57 | &result, |
||
| 58 | CFDictionaryCreate |
||
| 59 | ( |
||
| 60 | 0, |
||
| 61 | keys, |
||
| 62 | values, |
||
| 63 | sizeof (keys) / sizeof (*keys), |
||
| 64 | &kCFTypeDictionaryKeyCallBacks, |
||
| 65 | &kCFTypeDictionaryValueCallBacks |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | #define MB_OK 0 |
||
| 70 | char *sys_getbasepath (void) |
||
| 71 | { |
||
| 72 | static char app_path[1024] = ""; |
||
| 73 | if (app_path[0] == 0) |
||
| 74 | { |
||
| 75 | Dl_info addr_info; |
||
| 76 | if (dladdr (main, &addr_info) == 0) |
||
| 77 | return ("."); |
||
| 78 | strcpy_s (app_path, 1024, addr_info.dli_fname); |
||
| 79 | if (strrchr (app_path, '/') != NULL) |
||
| 80 | *strrchr (app_path, '/') = 0; |
||
| 81 | } |
||
| 82 | return (app_path); |
||
| 83 | } |
||
| 84 | #else // !__APPLE__ |
||
| 85 | #define MessageBox(handle,msg,title,btns) fprintf (stderr, msg) |
||
| 86 | #define MB_OK 0 |
||
| 87 | char *sys_getbasepath (void) |
||
| 88 | { |
||
| 89 | return ("."); |
||
| 90 | } |
||
| 91 | #endif // __APPLE__ |
||
| 92 | #endif // WIN32 |
||
| 93 | |||
| 94 | |||
| 1 | pmbaty | 95 | /* |
| 96 | * Panic |
||
| 97 | */ |
||
| 98 | void sys_panic (char *err, ...) |
||
| 99 | { |
||
| 100 | va_list argptr; |
||
| 101 | char s[1024]; |
||
| 102 | |||
| 103 | /* prepare message */ |
||
| 104 | va_start (argptr, err); |
||
| 105 | vsprintf_s (s, sizeof (s), err, argptr); |
||
| 106 | va_end (argptr); |
||
| 107 | |||
| 108 | /* print message and die */ |
||
| 109 | MessageBox (NULL, s, NULL, MB_OK); |
||
| 110 | exit (1); |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /* |
||
| 115 | * Print a message |
||
| 116 | */ |
||
| 117 | void sys_printf (char *msg, ...) |
||
| 118 | { |
||
| 119 | va_list argptr; |
||
| 120 | char s[2048]; |
||
| 121 | |||
| 122 | /* prepare message */ |
||
| 123 | va_start (argptr, msg); |
||
| 124 | vsprintf_s (s, sizeof (s), msg, argptr); |
||
| 125 | va_end (argptr); |
||
| 126 | |||
| 127 | MessageBox (NULL, s, NULL, MB_OK); |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | /* |
||
| 132 | * Return number of microseconds elapsed since first call |
||
| 133 | */ |
||
| 134 | U32 sys_gettime (void) |
||
| 135 | { |
||
| 136 | static U32 ticks_base = 0; |
||
| 137 | U32 ticks; |
||
| 138 | |||
| 139 | ticks = SDL_GetTicks (); |
||
| 140 | |||
| 141 | if (!ticks_base) |
||
| 142 | ticks_base = ticks; |
||
| 143 | |||
| 144 | return ticks - ticks_base; |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | /* |
||
| 149 | * Sleep a number of microseconds |
||
| 150 | */ |
||
| 151 | void sys_sleep (int s) |
||
| 152 | { |
||
| 153 | SDL_Delay (s); |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /* |
||
| 158 | * Initialize system |
||
| 159 | */ |
||
| 160 | void sys_init (int argc, char **argv) |
||
| 161 | { |
||
| 162 | sysarg_init (argc, argv); |
||
| 163 | sysvid_init (); |
||
| 164 | sysjoy_init (); |
||
| 165 | |||
| 166 | if (sysarg_args_nosound == 0) |
||
| 167 | syssnd_init (); |
||
| 168 | |||
| 169 | atexit (sys_shutdown); |
||
| 170 | signal (SIGINT, exit); |
||
| 171 | signal (SIGTERM, exit); |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | /* |
||
| 176 | * Shutdown system |
||
| 177 | */ |
||
| 178 | void sys_shutdown (void) |
||
| 179 | { |
||
| 180 | syssnd_shutdown (); |
||
| 181 | sysjoy_shutdown (); |
||
| 182 | sysvid_shutdown (); |
||
| 183 | } |