Wed, 01 Nov 2023 20:09:49 +0100
move window related stuff to its own unit
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 #include <stdbool.h>
32 #include <SDL2/SDL_endian.h>
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 typedef unsigned char asc_ubyte;
39 typedef signed char asc_sbyte;
41 typedef union asc_vec2i {
42 int data[2];
43 struct { int x, y; };
44 struct { int width, height; };
45 } asc_vec2i;
47 typedef union asc_col4i {
48 asc_ubyte data[4];
49 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
50 struct { asc_ubyte red, green, blue, alpha; };
51 #else
52 struct { asc_ubyte alpha, blue, green, red; };
53 #endif
54 } asc_col4i;
56 typedef union asc_col4f {
57 float data[4];
58 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
59 struct { float red, green, blue, alpha; };
60 #else
61 struct { float alpha, blue, green, red; };
62 #endif
63 } asc_col4f;
65 static inline int asc_clamp_i(int v, int min, int max) {
66 if (v < min) return min;
67 if (v > max) return max;
68 return v;
69 }
71 /**
72 * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
73 *
74 * This operation is quite expensive. When you need to use it often, it's
75 * quite obvious that you should change the data type.
76 *
77 * @param c the color using floats
78 * @return the same color using ints
79 */
80 static inline asc_col4i asc_col_ftoi(asc_col4f c) {
81 int red = (int)(255*c.red);
82 int green = (int)(255*c.green);
83 int blue = (int)(255*c.blue);
84 int alpha = (int)(255*c.alpha);
85 asc_col4i r;
86 r.red = (asc_ubyte)asc_clamp_i(red, 0, 255);
87 r.green = (asc_ubyte)asc_clamp_i(green, 0, 255);
88 r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255);
89 r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255);
90 return r;
91 }
93 #ifdef __cplusplus
94 } // extern "C"
95 #endif
97 #endif //ASCENSION_DATATYPES_H