api
[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 enum strncpy_behaviour{
43 k_strncpy_always_add_null = 0,
44 k_strncpy_allow_cutoff = 1
45 };
46
47 VG_STATIC u32 vg_strncpy( const char *src, char *dst, u32 len,
48 enum strncpy_behaviour behaviour )
49 {
50 for( u32 i=0; i<len; i++ ){
51 dst[i] = src[i];
52
53 if( !src[i] ) return i;
54
55 if( (behaviour == k_strncpy_always_add_null) && (i == len-1) ){
56 dst[i] = '\0';
57 return i;
58 }
59 }
60
61 return 0;
62 }
63
64 typedef struct vg_str vg_str;
65 struct vg_str{
66 char *buffer;
67 u32 i, len;
68 };
69
70 VG_STATIC void vg_strnull( vg_str *str, char *buffer, u32 len )
71 {
72 str->buffer = buffer;
73 str->i = 0;
74 str->len = len;
75 }
76
77 VG_STATIC void vg_strcat( vg_str *str, const char *append )
78 {
79 for( u32 i=0; str->i < str->len; i++, str->i ++ ){
80 str->buffer[ str->i ] = append[i];
81
82 if( append[i] == '\0' ) return;
83 }
84 }
85
86 VG_STATIC int vg_strgood( vg_str *str )
87 {
88 if( str->i == str->len ){
89 if( str->buffer[str->i -1] == '\0' ) return 1;
90 else return 0;
91 }
92 else{
93 str->buffer[ str->i ++ ] = '\0';
94 return 1;
95 }
96 }
97
98 VG_STATIC u32 vg_strdjb2( const char *str )
99 {
100 u32 hash = 5381, c;
101
102 while( (c = *str++) )
103 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
104
105 return hash;
106 }
107
108 #include <stdio.h>
109 #include <dirent.h>
110 #include <string.h>
111 #include <stdarg.h>
112 #include <ctype.h>
113 #include <math.h>
114 #include <assert.h>
115
116 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
117 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
118 #endif