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