Thu, 18 Apr 2024 22:53:55 +0200
consistently refer to windows by ID - fixes #381
This change discovered that the font cache is completely broken. We created issue #387 for this.
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 // Useful Macros
41 // --------------------------------------------------------------------------
43 #define ASC_NANOS_SECOND 1000000000ull
44 #define ASC_NANOS_MILLISECOND 1000000ull
45 #define ASC_NANOS_MICROSECOND 1000000ull
47 // --------------------------------------------------------------------------
48 // Datatype Definitions
49 // --------------------------------------------------------------------------
51 typedef unsigned char asc_ubyte;
52 typedef signed char asc_sbyte;
54 typedef union asc_vec2i {
55 struct { int x, y; };
56 struct { int width, height; };
57 int data[2];
58 } asc_vec2i;
60 typedef struct asc_recti {
61 asc_vec2i pos;
62 asc_vec2i size;
63 } asc_recti;
65 typedef union asc_vec3f {
66 struct { float x, y, z; };
67 struct { float width, height, depth; };
68 struct { float pitch, yaw, roll; };
69 float data[3];
70 } asc_vec3f;
72 typedef struct asc_col4i {
73 asc_ubyte red, green, blue, alpha;
74 } asc_col4i;
76 typedef struct asc_col4f {
77 float red, green, blue, alpha;
78 } asc_col4f;
80 typedef float asc_mat4f[16];
82 // --------------------------------------------------------------------------
83 // General Utility Functions
84 // --------------------------------------------------------------------------
86 static inline int asc_clamp_i(int v, int min, int max) {
87 if (v < min) return min;
88 if (v > max) return max;
89 return v;
90 }
92 /**
93 * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
94 *
95 * This operation is quite expensive. When you need to use it often, it's
96 * quite obvious that you should change the data type.
97 *
98 * @param c the color using floats
99 * @return the same color using ints
100 */
101 static inline asc_col4i asc_col_ftoi(asc_col4f c) {
102 int red = (int)(255*c.red);
103 int green = (int)(255*c.green);
104 int blue = (int)(255*c.blue);
105 int alpha = (int)(255*c.alpha);
106 asc_col4i r;
107 r.red = (asc_ubyte)asc_clamp_i(red, 0, 255);
108 r.green = (asc_ubyte)asc_clamp_i(green, 0, 255);
109 r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255);
110 r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255);
111 return r;
112 }
114 static inline SDL_Color asc_col_sdl(asc_col4i col) {
115 return (SDL_Color) {.r = col.red, .g = col.green, .b = col.blue, .a = col.alpha};
116 }
118 // --------------------------------------------------------------------------
119 // Matrix Functions
120 // --------------------------------------------------------------------------
122 /**
123 * Computes a matrix index in column-major order.
124 * @param col the column
125 * @param row the row
126 * @param rows the number of rows
127 */
128 #define asc_mat_index(col, row, rows) ((row) + (col) * (rows))
130 /**
131 * Computes a 4x4 matrix index in column-major order.
132 * @param col the column
133 * @param row the row
134 */
135 #define asc_mat4_index(col, row) asc_mat_index(col, row, 4)
137 static inline void asc_mat4f_unit(asc_mat4f mat) {
138 memset(mat, 0, sizeof(float) * 16);
139 mat[asc_mat4_index(0,0)] = 1;
140 mat[asc_mat4_index(1,1)] = 1;
141 mat[asc_mat4_index(2,2)] = 1;
142 mat[asc_mat4_index(3,3)] = 1;
143 }
145 static inline void asc_mat4f_ortho(
146 asc_mat4f mat,
147 float left,
148 float right,
149 float bottom,
150 float top,
151 float near,
152 float far
153 ) {
154 memset(mat, 0, sizeof(float) * 16);
155 mat[asc_mat4_index(0,0)] = 2.f / (right - left);
156 mat[asc_mat4_index(1,1)] = 2.f / (top - bottom);
157 mat[asc_mat4_index(2,2)] = -2.f / (far - near);
158 mat[asc_mat4_index(3,0)] = -(right + left) / (right - left);
159 mat[asc_mat4_index(3,1)] = -(top + bottom) / (top - bottom);
160 mat[asc_mat4_index(3,2)] = -(far + near) / (far - near);
161 mat[asc_mat4_index(3,3)] = 1;
162 }
164 static inline void asc_mat4f_mulst(
165 asc_mat4f dest,
166 asc_mat4f const left,
167 asc_mat4f const right
168 ) {
169 for (unsigned i = 0; i < 4; i++) {
170 for (unsigned j = 0; j < 4; j++) {
171 dest[asc_mat4_index(i, j)] = 0;
172 for (int k = 0; k < 4; k++) {
173 dest[asc_mat4_index(i,j)] +=
174 left[asc_mat4_index(i,k)] * right[asc_mat4_index(k,j)];
175 }
176 }
177 }
178 }
180 #endif //ASCENSION_DATATYPES_H