Wed, 06 Mar 2024 23:08:03 +0100
add behavior nodes + restructure test program
Also, the test program will now officially be a game of snake.
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2023 Mike Becker. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
28 #ifndef ASCENSION_DATATYPES_H
29 #define ASCENSION_DATATYPES_H
31 #ifdef __cplusplus
32 #error You cannot ascend using C++
33 #endif
35 #include <stdbool.h>
36 #include <string.h>
37 #include <SDL2/SDL_pixels.h>
39 // --------------------------------------------------------------------------
40 // Datatype Definitions
41 // --------------------------------------------------------------------------
43 typedef unsigned char asc_ubyte;
44 typedef signed char asc_sbyte;
46 typedef union asc_vec2i {
47 int data[2];
48 struct { int x, y; };
49 struct { int width, height; };
50 } asc_vec2i;
52 typedef struct asc_col4i {
53 asc_ubyte red, green, blue, alpha;
54 } asc_col4i;
56 typedef struct asc_col4f {
57 float red, green, blue, alpha;
58 } asc_col4f;
60 typedef float asc_mat4f[16];
62 // --------------------------------------------------------------------------
63 // General Utility Functions
64 // --------------------------------------------------------------------------
66 static inline int asc_clamp_i(int v, int min, int max) {
67 if (v < min) return min;
68 if (v > max) return max;
69 return v;
70 }
72 /**
73 * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
74 *
75 * This operation is quite expensive. When you need to use it often, it's
76 * quite obvious that you should change the data type.
77 *
78 * @param c the color using floats
79 * @return the same color using ints
80 */
81 static inline asc_col4i asc_col_ftoi(asc_col4f c) {
82 int red = (int)(255*c.red);
83 int green = (int)(255*c.green);
84 int blue = (int)(255*c.blue);
85 int alpha = (int)(255*c.alpha);
86 asc_col4i r;
87 r.red = (asc_ubyte)asc_clamp_i(red, 0, 255);
88 r.green = (asc_ubyte)asc_clamp_i(green, 0, 255);
89 r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255);
90 r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255);
91 return r;
92 }
94 static inline SDL_Color asc_col_sdl(asc_col4i col) {
95 return (SDL_Color) {.r = col.red, .g = col.green, .b = col.blue, .a = col.alpha};
96 }
98 // --------------------------------------------------------------------------
99 // Matrix Functions
100 // --------------------------------------------------------------------------
102 /**
103 * Computes a matrix index in column-major order.
104 * @param col the column
105 * @param row the row
106 * @param rows the number of rows
107 */
108 #define asc_mat_index(col, row, rows) ((row) + (col) * (rows))
110 /**
111 * Computes a 4x4 matrix index in column-major order.
112 * @param col the column
113 * @param row the row
114 */
115 #define asc_mat4_index(col, row) asc_mat_index(col, row, 4)
117 static inline void asc_mat4f_ortho(
118 asc_mat4f mat,
119 float left,
120 float right,
121 float bottom,
122 float top
123 ) {
124 memset(mat, 0, sizeof(float) * 16);
125 mat[asc_mat4_index(0,0)] = 2.f / (right - left);
126 mat[asc_mat4_index(1,1)] = 2.f / (top - bottom);
127 mat[asc_mat4_index(2,2)] = -1;
128 mat[asc_mat4_index(3,0)] = -(right + left) / (right - left);
129 mat[asc_mat4_index(3,1)] = -(top + bottom) / (top - bottom);
130 mat[asc_mat4_index(3,3)] = 1;
131 }
133 #endif //ASCENSION_DATATYPES_H