maths and bugs
[vg.git] / src / vg / vg_platform.h
1 #ifndef VG_PLATFORM_H
2 #define VG_PLATFORM_H
3
4 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
5
6 typedef uint8_t u8;
7 typedef uint16_t u16;
8 typedef uint32_t u32;
9 typedef uint64_t u64;
10 typedef int8_t i8;
11 typedef int16_t i16;
12 typedef int32_t i32;
13 typedef int64_t i64;
14
15 typedef unsigned int uint;
16
17 typedef int v2i[2];
18 typedef int v3i[3];
19 typedef int v4i[4];
20 typedef float v2f[2];
21 typedef float v3f[3];
22 typedef float v4f[4];
23 typedef v2f m2x2f[2];
24 typedef v3f m3x3f[3];
25 typedef v3f m4x3f[4]; /* TODO why this is 4x4 too? */
26 typedef v4f m4x4f[4];
27 typedef v3f boxf[2];
28
29 // Resource types
30 typedef struct vg_tex2d vg_tex2d;
31
32 struct vg_achievement
33 {
34 int is_set;
35 const char *name;
36 };
37
38 #define vg_static_assert _Static_assert
39 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
40
41 /* Pthred emulation for windows */
42 #ifdef _WIN32
43 #include <windows.h>
44 #define MUTEX_TYPE HANDLE
45 #define MUTEX_INITIALIZER NULL
46 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
47
48 /* TODO: Why is this defined but never used? */
49 #define MUTEX_CLEANUP(x) (CloseHandle(x))
50 #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
51 #define MUTEX_UNLOCK(x) (ReleaseMutex(x))
52
53 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE *mx )
54 {
55 if( *mx == NULL ) /* static initializer? */
56 {
57 HANDLE p = CreateMutex( NULL, FALSE, NULL );
58 if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL )
59 != NULL )
60 CloseHandle(p);
61 }
62
63 return WaitForSingleObject( *mx, INFINITE ) == WAIT_FAILED;
64 }
65 #else
66 #include <pthread.h>
67 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
68 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
69 #define MUTEX_TYPE pthread_mutex_t
70 #define MUTEX_INITIALIZER {0}
71 #endif
72
73
74 int vg_thread_run( void *pfunc, void *data )
75 {
76 #ifdef _WIN32
77 HANDLE hThread = CreateThread
78 (
79 NULL, /* Thread attributes */
80 0, /* Stack size (0 = use default) */
81 pfunc, /* Thread start address */
82 data, /* Parameter to pass to the thread */
83 0, /* Creation flags */
84 NULL /* Thread id */
85 );
86
87 if ( hThread == NULL )
88 {
89 /*
90 * Thread creation failed.
91 * More details can be retrieved by calling GetLastError()
92 */
93 return 1;
94 }
95 else
96 {
97 CloseHandle( hThread );
98 return 0;
99 }
100 #else
101 pthread_t hThread;
102 if( pthread_create( &hThread, NULL, pfunc, data ) )
103 {
104 return 1;
105 }
106 else
107 {
108 pthread_detach( hThread );
109 return 0;
110 }
111 #endif
112 }
113
114 #endif