add support for 3d scenes, depth testing, lighting
[chaz/rasterize] / common.h
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef _COMMON_H_
9 #define _COMMON_H_
10
11 #include <math.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18
19 /*
20 * Define a keyword for use while defining small and fast functions.
21 */
22 #if EXTRA_INLINE
23 #define INLINE_MAYBE static inline
24 #else
25 #define INLINE_MAYBE static
26 #endif
27
28
29 /*
30 * Define a type for scalar values, either float or double.
31 */
32 #if DOUBLE_FLOAT
33 typedef double scal_t;
34 #define SCALAR_SIZE 8
35 #define S(K) K
36 #define scal_floor floor
37 #define scal_ceil ceil
38 #define scal_min fmin
39 #define scal_max fmax
40 #define scal_pow pow
41 #define scal_sqrt sqrt
42 #define scal_sin sin
43 #define scal_cos cos
44 #define scal_tan tan
45 #define scal_asin asin
46 #define scal_acos acos
47 #define scal_atan atan
48 #else
49 typedef float scal_t;
50 #define SCALAR_SIZE 4
51 #define S(K) K##f
52 #define scal_floor floorf
53 #define scal_ceil ceilf
54 #define scal_min fminf
55 #define scal_max fmaxf
56 #define scal_pow powf
57 #define scal_sqrt sqrtf
58 #define scal_sin sinf
59 #define scal_cos cosf
60 #define scal_tan tanf
61 #define scal_asin asinf
62 #define scal_acos acosf
63 #define scal_atan atanf
64 #endif
65
66 #define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
67 #define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
68
69 /*
70 * Clamp a scalar between two values.
71 */
72 INLINE_MAYBE
73 scal_t scal_clamp(scal_t s, scal_t min, scal_t max)
74 {
75 if (s < min) {
76 return min;
77 }
78 if (max < s) {
79 return max;
80 }
81 return s;
82 }
83
84
85 /*
86 * Define min and max functions for integers.
87 */
88 INLINE_MAYBE
89 int imin(int a, int b)
90 {
91 return a < b ? a : b;
92 }
93 INLINE_MAYBE
94 int imax(int a, int b)
95 {
96 return a < b ? b : a;
97 }
98
99
100 /*
101 * Define some macros for packing and unpacking bytes to and from larger ints.
102 */
103 #define PACK(W,N,B) (((B) << (8 * (N))) | ((W) & ~(0xff << (8 * (N)))))
104 #define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff)
105
106
107 /*
108 * Try to execute a statement and print a status message. If the statement
109 * resolves to 0, it is considered to have succeeded; any other evaluation is
110 * an error condition and an abort(3) occurs.
111 * return.
112 */
113 #define TRY_DO(L, K, ARGS...) \
114 printf("* " L "... ", ##ARGS); \
115 fflush(stdout); \
116 if ((K) == 0) printf("done!\n"); \
117 else abort()
118
119
120 /*
121 * Declare a type of destructor functions.
122 */
123 typedef void (*dtor_t)(void*);
124 #define DTOR(A) (dtor_t)(A)
125
126
127 /*
128 * Allocate a block of memory of a certain size. This follows the semantics
129 * of malloc(3), except it will never return NULL and will abort(3) if the
130 * memory could not be allocated.
131 */
132 void* mem_alloc(size_t size);
133
134 /*
135 * Change the size of a block of memory. This follows the semantics of
136 * realloc(3), except it will never return NULL and will abort(3) if the
137 * memory could not be allocated.
138 */
139 void* mem_realloc(void* mem, size_t size);
140
141 /*
142 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
143 * and friends. This is essentially just a call to free(3).
144 */
145 void mem_free(void* mem);
146
147 /*
148 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
149 * no callback should be called. The callback takes the same arguments as
150 * realloc(3) and may try to fulfill the request. The return value of the
151 * callback function will be returned from the allocation function and must be
152 * a valid pointer to an allocated block of memory. The callback function
153 * should not call mem_alloc or mem_realloc and must not return if a block of
154 * memory could not be allocated.
155 */
156 void mem_set_fn(void* (*fn)(void*, size_t));
157
158 /*
159 * Get the number of blocks currently allocated with either mem_alloc or
160 * mem_realloc. This number should be zero at the end of a process running
161 * this program.
162 */
163 int mem_blocks();
164
165
166 /*
167 * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
168 * The result needs to be freed with mem_free.
169 */
170 INLINE_MAYBE
171 char* mem_strdup(const char* str)
172 {
173 size_t size = strlen(str) + 1;
174 return (char*)memcpy(mem_alloc(size), str, size);
175 }
176
177 /*
178 * Get an allocated string made up of two strings concatenated together.
179 * The result needs to be freed with mem_free.
180 */
181 INLINE_MAYBE
182 char* mem_strcat(const char* str1, const char* str2)
183 {
184 size_t size1 = strlen(str1);
185 size_t size2 = strlen(str2);
186 char* str = (char*)mem_alloc(size1 + size2 + 1);
187 memcpy(str + size1, str2, size2 + 1);
188 return memcpy(str, str1, size1);
189 }
190
191
192 /*
193 * Cut a string short at the last matching character. The string will be
194 * modified if the character was matched. Either way, str is returned.
195 */
196 INLINE_MAYBE
197 char* strcut(char* str, int c)
198 {
199 char* ptr = strrchr(str, c);
200 if (ptr != NULL) {
201 *ptr = '\0';
202 }
203 return str;
204 }
205
206 /*
207 * Trim white space off of the right side of a string.
208 */
209 void rtrim(char *str);
210
211 /*
212 * Trim white space off of the left side of a string.
213 */
214 void ltrim(char *str);
215
216 /*
217 * Trim white space off of both sides of a string.
218 */
219 INLINE_MAYBE
220 void trim(char *str)
221 {
222 rtrim(str);
223 ltrim(str);
224 }
225
226
227 /*
228 * Initialize the timer.
229 */
230 void timer_start();
231
232 /*
233 * Stop the timer and return how many microseconds passed after
234 * initialization.
235 */
236 long timer_stop();
237
238
239 #endif // _COMMON_H_
240
This page took 0.053588 seconds and 4 git commands to generate.