Rev 4 | Rev 11 | 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; |
||
7 | pmbaty | 76 | if (dladdr (sys_getbasepath, &addr_info) == 0) |
2 | pmbaty | 77 | return ("."); |
78 | strcpy_s (app_path, 1024, addr_info.dli_fname); |
||
79 | if (strrchr (app_path, '/') != NULL) |
||
80 | *strrchr (app_path, '/') = 0; |
||
4 | pmbaty | 81 | if (strrchr (app_path, '/') != NULL) |
82 | *strrchr (app_path, '/') = 0; |
||
83 | strcat_s (app_path, 1024, "/Resources"); |
||
2 | pmbaty | 84 | } |
85 | return (app_path); |
||
86 | } |
||
87 | #else // !__APPLE__ |
||
88 | #define MessageBox(handle,msg,title,btns) fprintf (stderr, msg) |
||
89 | #define MB_OK 0 |
||
90 | char *sys_getbasepath (void) |
||
91 | { |
||
92 | return ("."); |
||
93 | } |
||
94 | #endif // __APPLE__ |
||
95 | #endif // WIN32 |
||
96 | |||
97 | |||
1 | pmbaty | 98 | /* |
99 | * Panic |
||
100 | */ |
||
101 | void sys_panic (char *err, ...) |
||
102 | { |
||
103 | va_list argptr; |
||
104 | char s[1024]; |
||
105 | |||
106 | /* prepare message */ |
||
107 | va_start (argptr, err); |
||
108 | vsprintf_s (s, sizeof (s), err, argptr); |
||
109 | va_end (argptr); |
||
110 | |||
111 | /* print message and die */ |
||
112 | MessageBox (NULL, s, NULL, MB_OK); |
||
113 | exit (1); |
||
114 | } |
||
115 | |||
116 | |||
117 | /* |
||
118 | * Print a message |
||
119 | */ |
||
120 | void sys_printf (char *msg, ...) |
||
121 | { |
||
122 | va_list argptr; |
||
123 | char s[2048]; |
||
124 | |||
125 | /* prepare message */ |
||
126 | va_start (argptr, msg); |
||
127 | vsprintf_s (s, sizeof (s), msg, argptr); |
||
128 | va_end (argptr); |
||
129 | |||
130 | MessageBox (NULL, s, NULL, MB_OK); |
||
131 | } |
||
132 | |||
133 | |||
134 | /* |
||
135 | * Return number of microseconds elapsed since first call |
||
136 | */ |
||
137 | U32 sys_gettime (void) |
||
138 | { |
||
139 | static U32 ticks_base = 0; |
||
140 | U32 ticks; |
||
141 | |||
142 | ticks = SDL_GetTicks (); |
||
143 | |||
144 | if (!ticks_base) |
||
145 | ticks_base = ticks; |
||
146 | |||
147 | return ticks - ticks_base; |
||
148 | } |
||
149 | |||
150 | |||
151 | /* |
||
152 | * Sleep a number of microseconds |
||
153 | */ |
||
154 | void sys_sleep (int s) |
||
155 | { |
||
156 | SDL_Delay (s); |
||
157 | } |
||
158 | |||
159 | |||
160 | /* |
||
161 | * Initialize system |
||
162 | */ |
||
163 | void sys_init (int argc, char **argv) |
||
164 | { |
||
165 | sysarg_init (argc, argv); |
||
166 | sysvid_init (); |
||
167 | sysjoy_init (); |
||
168 | |||
169 | if (sysarg_args_nosound == 0) |
||
170 | syssnd_init (); |
||
171 | |||
172 | atexit (sys_shutdown); |
||
173 | signal (SIGINT, exit); |
||
174 | signal (SIGTERM, exit); |
||
175 | } |
||
176 | |||
177 | |||
178 | /* |
||
179 | * Shutdown system |
||
180 | */ |
||
181 | void sys_shutdown (void) |
||
182 | { |
||
183 | syssnd_shutdown (); |
||
184 | sysjoy_shutdown (); |
||
185 | sysvid_shutdown (); |
||
186 | } |