getting stuff working on windows again
[vg.git] / vg_platform.h
1 #ifndef VG_PLATFORM_H
2 #define VG_PLATFORM_H
3
4 #ifdef VG_RELEASE
5 #define VG_STATIC static
6 #else
7 #define VG_STATIC
8 #endif
9
10 //#include "vg.h"
11 #include "vg_stdint.h"
12
13 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
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];
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 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <math.h>
47 #include <assert.h>
48 #include <setjmp.h>
49 #include <sys/time.h>
50 #include <math.h>
51 #include <stdio.h>
52 #include <errno.h>
53
54 enum strncpy_behaviour{
55 k_strncpy_always_add_null = 0,
56 k_strncpy_allow_cutoff = 1,
57 k_strncpy_overflow_fatal = 2
58 };
59
60 VG_STATIC void vg_fatal_error( const char *fmt, ... );
61 VG_STATIC u32 vg_strncpy( const char *src, char *dst, u32 len,
62 enum strncpy_behaviour behaviour )
63 {
64 for( u32 i=0; i<len; i++ ){
65 dst[i] = src[i];
66
67 if( !src[i] ) return i;
68
69 if( i == len-1 ){
70 if( behaviour == k_strncpy_always_add_null ){
71 dst[i] = '\0';
72 return i;
73 }
74 else if( behaviour == k_strncpy_overflow_fatal ){
75 vg_fatal_error( "Strncpy dest exceeded buffer length\n" );
76 }
77 }
78 }
79
80 return 0;
81 }
82
83 typedef struct vg_str vg_str;
84 struct vg_str{
85 char *buffer;
86 u32 i, len;
87 };
88
89 VG_STATIC void vg_strnull( vg_str *str, char *buffer, u32 len )
90 {
91 str->buffer = buffer;
92 str->buffer[0] = '\0';
93 str->i = 0;
94 str->len = len;
95 }
96
97 VG_STATIC void vg_strcat( vg_str *str, const char *append )
98 {
99 for( u32 i=0; str->i < str->len; i++, str->i ++ ){
100 str->buffer[ str->i ] = append[i];
101
102 if( append[i] == '\0' ) return;
103 }
104 }
105
106 VG_STATIC int vg_strgood( vg_str *str )
107 {
108 if( str->i == str->len ){
109 if( str->buffer[str->i -1] == '\0' ) return 1;
110 else return 0;
111 }
112 else return 1;
113 }
114
115 VG_STATIC char *vg_strch( vg_str *str, char c )
116 {
117 char *ptr = NULL;
118 for( u32 i=0; i<str->i; i++ ){
119 if( str->buffer[i] == c )
120 ptr = str->buffer+i;
121 }
122
123 return ptr;
124 }
125
126 VG_STATIC u32 vg_strdjb2( const char *str )
127 {
128 u32 hash = 5381, c;
129
130 while( (c = *str++) )
131 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
132
133 return hash;
134 }
135
136 VG_STATIC int vg_strdjb2_eq( const char *s1, u32 h1,
137 const char *s2, u32 h2 )
138 {
139 if( h1 == h2 ){
140 if(!strcmp(s1, s2)) return 1;
141 else return 0;
142 } else return 0;
143 }
144
145 #define VG_STRDJB2_EQ( CS1, S2, H2 ) \
146 vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 )
147
148
149 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
150 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
151 #endif