simplify gitignore
[vg.git] / src / vg / vg_platform.h
1 #ifndef VG_PLATFORM_H
2 #define VG_PLATFORM_H
3
4 //#include "vg.h"
5 #include "vg_stdint.h"
6
7 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
8
9 typedef unsigned int uint;
10
11 typedef int v2i[2];
12 typedef int v3i[3];
13 typedef int v4i[4];
14 typedef float v2f[2];
15 typedef float v3f[3];
16 typedef float v4f[4];
17 typedef v2f m2x2f[2];
18 typedef v3f m3x3f[3];
19 typedef v3f m4x3f[4];
20 typedef v4f m4x4f[4];
21 typedef v3f boxf[2];
22
23 // Resource types
24 typedef struct vg_tex2d vg_tex2d;
25
26 struct vg_achievement
27 {
28 int is_set;
29 const char *name;
30 };
31
32 #ifndef VG_STATIC
33 #define VG_STATIC static
34 #endif
35
36 #define vg_static_assert _Static_assert
37 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
38 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
39
40 #ifdef _WIN32_NO
41 #include <windows.h>
42
43 #ifdef I_THINK_THIS_IS_WHAT_MSCV_WANTS_BUT_HAVNT_TESTED_IT_YET
44
45 #define VG_DEPRECATED __declspec(deprecated)
46 #define VG_THREAD_LOCAL __declspec( thread )
47
48 #else /* MINGW-64 */
49
50 #define VG_THREAD_LOCAL __thread
51 #define VG_DEPRECATED __attribute__((deprecated))
52
53 #endif
54
55 typedef HANDLE vg_semaphore;
56 typedef HANDLE vg_mutex;
57 typedef u64 vg_timespec;
58
59 #else
60 #include <pthread.h>
61 #include <semaphore.h>
62
63 #define VG_DEPRECATED __attribute__((deprecated))
64 #define VG_THREAD_LOCAL __thread
65
66 typedef sem_t vg_semaphore;
67 typedef pthread_mutex_t vg_mutex;
68 typedef struct timespec vg_timespec;
69
70 #endif
71
72 VG_STATIC void vg_strncpy( const char *src, char *dst, u32 len )
73 {
74 for( u32 i=0; i<len; i++ )
75 {
76 dst[i] = src[i];
77
78 if( !src[i] )
79 break;
80 }
81 }
82
83 #define VG_REQUIRED_ASSET( TYPE, DECL, FN, PATH, ... ) \
84 TYPE DECL = FN( PATH,##__VA_ARGS__ ); \
85 vg_required( DECL, "Resource is required but failed to load: '" PATH "'" );
86
87 #if 0
88 VG_DEPRECATED
89 char *strcpy(char* destination, const char* source);
90 VG_DEPRECATED
91 char *strncpy(char *restrict dest, const char *restrict src, size_t n);
92 VG_DEPRECATED
93 char *strcat(char *restrict dest, const char *restrict src);
94 VG_DEPRECATED
95 char *strncat(char *restrict dest, const char *restrict src, size_t n);
96 #endif
97
98 #include <stdio.h>
99 #include <dirent.h>
100 #include <string.h>
101 #include <stdarg.h>
102 #include <ctype.h>
103 #include <math.h>
104 #include <assert.h>
105
106 VG_STATIC int vg_thread_run( void *pfunc, void *data );
107 VG_STATIC void vg_thread_exit(void);
108 VG_STATIC void vg_set_thread_name( const char *name );
109 VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value );
110 VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem );
111 VG_STATIC int vg_semaphore_wait( vg_semaphore *sem );
112 VG_STATIC int vg_semaphore_post( vg_semaphore *sem );
113 VG_STATIC void vg_semaphore_free( vg_semaphore *sem );
114 VG_STATIC int vg_mutex_init( vg_mutex *mutex );
115 VG_STATIC int vg_mutex_lock( vg_mutex *mutex );
116 VG_STATIC int vg_mutex_unlock( vg_mutex *mutex );
117 VG_STATIC void vg_mutex_free( vg_mutex *mutex );
118 VG_STATIC void vg_sleep_ms( long msec );
119 VG_STATIC double vg_time_diff( vg_timespec start, vg_timespec end );
120
121 #ifdef _WIN32_NO
122
123 VG_STATIC int vg_thread_run( void *pfunc, void *data )
124 {
125 HANDLE hThread = CreateThread
126 (
127 NULL, /* Thread attributes */
128 0, /* Stack size (0 = use default) */
129 pfunc, /* Thread start address */
130 data, /* Parameter to pass to the thread */
131 0, /* Creation flags */
132 NULL /* Thread id */
133 );
134
135 if ( hThread == NULL )
136 {
137 /*
138 * Thread creation failed.
139 * More details can be retrieved by calling GetLastError()
140 */
141 return 1;
142 }
143 else
144 {
145 CloseHandle( hThread );
146 return 0;
147 }
148 }
149
150 VG_STATIC void vg_thread_exit(void)
151 {
152 ExitThread(0);
153 }
154
155 VG_STATIC void vg_set_thread_name( const char *name )
156 {
157 /* I believe this is a meaningless concept in windows */
158 }
159
160 VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value );
161 VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem );
162 VG_STATIC int vg_semaphore_wait( vg_semaphore *sem );
163 VG_STATIC int vg_semaphore_post( vg_semaphore *sem );
164 VG_STATIC void vg_semaphore_free( vg_semaphore *sem );
165 VG_STATIC int vg_mutex_init( vg_mutex *mutex );
166 VG_STATIC int vg_mutex_lock( vg_mutex *mutex );
167 VG_STATIC int vg_mutex_unlock( vg_mutex *mutex );
168 VG_STATIC void vg_mutex_free( vg_mutex *mutex );
169 VG_STATIC void vg_sleep_ms( long msec );
170 VG_STATIC double vg_time_diff( vg_timespec start, vg_timespec end );
171
172 #else
173
174 VG_STATIC int vg_thread_run( void *pfunc, void *data )
175 {
176 pthread_t hThread;
177 if( pthread_create( &hThread, NULL, pfunc, data ) )
178 {
179 return 1;
180 }
181 else
182 {
183 pthread_detach( hThread );
184 return 0;
185 }
186 }
187
188
189 VG_STATIC void vg_thread_exit(void)
190 {
191 pthread_exit(NULL);
192 }
193
194 VG_STATIC void vg_set_thread_name( const char *name )
195 {
196 /* not defined but links?? */
197 #if 0
198 pthread_setname_np(pthread_self());
199 #endif
200 }
201
202 VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value )
203 {
204 return !sem_init( sem, 0, value );
205 }
206
207 VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem )
208 {
209 return !sem_trywait( sem );
210 }
211
212 VG_STATIC int vg_semaphore_wait( vg_semaphore *sem )
213 {
214 return !sem_wait( sem );
215 }
216
217 VG_STATIC int vg_semaphore_post( vg_semaphore *sem )
218 {
219 return !sem_post( sem );
220 }
221
222 VG_STATIC void vg_semaphore_free( vg_semaphore *sem )
223 {
224 sem_destroy( sem );
225 }
226
227 VG_STATIC int vg_mutex_init( vg_mutex *mutex )
228 {
229 memset( mutex, 0, sizeof(vg_mutex) );
230 return 1;
231 }
232
233 VG_STATIC int vg_mutex_lock( vg_mutex *mutex )
234 {
235 if( !pthread_mutex_lock( mutex ) )
236 return 1;
237 else
238 return 0;
239 }
240
241 VG_STATIC int vg_mutex_unlock( vg_mutex *mutex )
242 {
243 if( !pthread_mutex_unlock( mutex ) )
244 return 1;
245 else
246 return 0;
247 }
248
249 VG_STATIC void vg_mutex_free( vg_mutex *mutex )
250 {
251
252 }
253
254 VG_STATIC void vg_sleep_ms( long msec )
255 {
256 struct timespec ts;
257
258 ts.tv_sec = msec / 1000;
259 ts.tv_nsec = (msec % 1000) * 1000000;
260 nanosleep( &ts, &ts );
261 }
262
263 /* diff two timespecs in MS */
264 VG_STATIC double vg_time_diff( struct timespec start, struct timespec end )
265 {
266 double elapsed = 1000.0*end.tv_sec + 1e-6*end.tv_nsec
267 - (1000.0*start.tv_sec + 1e-6*start.tv_nsec);
268
269 return elapsed;
270 }
271
272 #endif
273
274 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
275 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
276
277 #endif