Thu, 23 Nov 2023 23:08:57 +0100
improve text node API
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@19 | 37 | #include <SDL2/SDL_pixels.h> |
universe@4 | 38 | |
universe@12 | 39 | // -------------------------------------------------------------------------- |
universe@12 | 40 | // Datatype Definitions |
universe@12 | 41 | // -------------------------------------------------------------------------- |
universe@12 | 42 | |
universe@4 | 43 | typedef unsigned char asc_ubyte; |
universe@4 | 44 | typedef signed char asc_sbyte; |
universe@4 | 45 | |
universe@5 | 46 | typedef union asc_vec2i { |
universe@3 | 47 | int data[2]; |
universe@3 | 48 | struct { int x, y; }; |
universe@3 | 49 | struct { int width, height; }; |
universe@3 | 50 | } asc_vec2i; |
universe@3 | 51 | |
universe@16 | 52 | typedef struct asc_col4i { |
universe@16 | 53 | asc_ubyte red, green, blue, alpha; |
universe@4 | 54 | } asc_col4i; |
universe@4 | 55 | |
universe@16 | 56 | typedef struct asc_col4f { |
universe@16 | 57 | float red, green, blue, alpha; |
universe@4 | 58 | } asc_col4f; |
universe@4 | 59 | |
universe@16 | 60 | typedef float asc_mat4f[16]; |
universe@12 | 61 | |
universe@12 | 62 | // -------------------------------------------------------------------------- |
universe@12 | 63 | // General Utility Functions |
universe@12 | 64 | // -------------------------------------------------------------------------- |
universe@12 | 65 | |
universe@4 | 66 | static inline int asc_clamp_i(int v, int min, int max) { |
universe@4 | 67 | if (v < min) return min; |
universe@4 | 68 | if (v > max) return max; |
universe@4 | 69 | return v; |
universe@4 | 70 | } |
universe@4 | 71 | |
universe@4 | 72 | /** |
universe@4 | 73 | * Converts a float color (0.0f to 1.0f) to an int color (0 to 255). |
universe@4 | 74 | * |
universe@4 | 75 | * This operation is quite expensive. When you need to use it often, it's |
universe@4 | 76 | * quite obvious that you should change the data type. |
universe@4 | 77 | * |
universe@4 | 78 | * @param c the color using floats |
universe@4 | 79 | * @return the same color using ints |
universe@4 | 80 | */ |
universe@4 | 81 | static inline asc_col4i asc_col_ftoi(asc_col4f c) { |
universe@4 | 82 | int red = (int)(255*c.red); |
universe@4 | 83 | int green = (int)(255*c.green); |
universe@4 | 84 | int blue = (int)(255*c.blue); |
universe@4 | 85 | int alpha = (int)(255*c.alpha); |
universe@4 | 86 | asc_col4i r; |
universe@4 | 87 | r.red = (asc_ubyte)asc_clamp_i(red, 0, 255); |
universe@4 | 88 | r.green = (asc_ubyte)asc_clamp_i(green, 0, 255); |
universe@4 | 89 | r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255); |
universe@4 | 90 | r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255); |
universe@4 | 91 | return r; |
universe@4 | 92 | } |
universe@3 | 93 | |
universe@19 | 94 | static inline SDL_Color asc_col_sdl(asc_col4i col) { |
universe@19 | 95 | return (SDL_Color) {.r = col.red, .g = col.green, .b =col.blue, .a = col.alpha}; |
universe@19 | 96 | } |
universe@19 | 97 | |
universe@12 | 98 | // -------------------------------------------------------------------------- |
universe@12 | 99 | // Matrix Functions |
universe@12 | 100 | // -------------------------------------------------------------------------- |
universe@12 | 101 | |
universe@16 | 102 | /** |
universe@16 | 103 | * Computes a matrix index in column-major order. |
universe@16 | 104 | * @param col the column |
universe@16 | 105 | * @param row the row |
universe@16 | 106 | * @param rows the number of rows |
universe@16 | 107 | */ |
universe@16 | 108 | #define asc_mat_index(col, row, rows) ((row) + (col) * (rows)) |
universe@16 | 109 | |
universe@16 | 110 | /** |
universe@16 | 111 | * Computes a 4x4 matrix index in column-major order. |
universe@16 | 112 | * @param col the column |
universe@16 | 113 | * @param row the row |
universe@16 | 114 | */ |
universe@16 | 115 | #define asc_mat4_index(col, row) asc_mat_index(col, row, 4) |
universe@16 | 116 | |
universe@12 | 117 | static inline void asc_mat4f_ortho( |
universe@12 | 118 | asc_mat4f mat, |
universe@12 | 119 | float left, |
universe@12 | 120 | float right, |
universe@12 | 121 | float bottom, |
universe@12 | 122 | float top |
universe@12 | 123 | ) { |
universe@12 | 124 | memset(mat, 0, sizeof(float) * 16); |
universe@16 | 125 | mat[asc_mat4_index(0,0)] = 2.f / (right - left); |
universe@16 | 126 | mat[asc_mat4_index(1,1)] = 2.f / (top - bottom); |
universe@16 | 127 | mat[asc_mat4_index(2,2)] = -1; |
universe@16 | 128 | mat[asc_mat4_index(3,0)] = -(right + left) / (right - left); |
universe@16 | 129 | mat[asc_mat4_index(3,1)] = -(top + bottom) / (top - bottom); |
universe@16 | 130 | mat[asc_mat4_index(3,3)] = 1; |
universe@12 | 131 | } |
universe@12 | 132 | |
universe@3 | 133 | #endif //ASCENSION_DATATYPES_H |