LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_misc.h
1/*
2 * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2026
3 * All rights reserved
4 */
5
6#ifndef LG_MISC_H
7#define LG_MISC_H
8
9/* GL errors checking */
10#define CHECK_GL_E lg_check_gl_errors(lg_log_time(), __FILE__ , __LINE__, __func__)
11
12#define CHECK_GL_E_2 {if (lg_check_gl_errors(lg_log_time(), __FILE__, __LINE__, __func__) != LG_OK) exit(-1);}
13
14/* Checking for NULL pointers */
15#define CHECKV_R(what, ret_value) {if (what == NULL) {INFO_ERR("NULL pointer") return ret_value;}}
16
17/* Where is this used ? */
18#define STH_WRONG_HERE \
19 { \
20 static int i = 0; \
21 if (i == 0) { \
22 INFO_OUT("VARO TÄSSÄ\n%s [%s: %d] %s(): jotain pitää korjata\n", UTF8_RW_ROUNDED_ARROW, __FILE__, __LINE__, __func__) \
23 i++; \
24 } \
25 }
26
27/* Debugging stuff (trivial) */
28#define DEBUG_ENTER_FUNC INFO_OUT("\n\nDEBUG **** %s() ****\n\n", __func__)
29#define DEBUG_ENTER_FUNC2(str) INFO_OUT("\n\nDEBUG **** %s() -> %s ****\n\n", __func__, str)
30#define DEBUG_CHK_PTR(ptr) \
31 { \
32 if (ptr == NULL) { \
33 INFO_OUT("\n\nDEBUG **** [%s: %d] %s() -> POINTER IS NULL ****\n\n", __FILE__, __LINE__, __func__) \
34 exit(-1); \
35 } \
36 }
37
38/* Testing if value is in interval */
39#define IN_RANGE_i_(v, min, max) (v >= min && v <= max ? TRUE : FALSE) /* _i_ = inclusive */
40#define IN_RANGE_e_(v, min, max) (v > min && v < max ? TRUE : FALSE) /* _e_ = exclusive */
41#define OUT_OF_RANGE_i_(v, min, max) (v <= min && v >= max ? TRUE : FALSE)
42#define OUT_OF_RANGE_e_(v, min, max) (v < min && v > max ? TRUE : FALSE)
43
44typedef enum {LG_LITTLE_ENDIAN, LG_BIG_ENDIAN} lg_endianness;
45
46/*
47 * A few conversions helpers
48 *
49 * 1 knot (KT) = 1 nautical mile (NM) / h
50 */
51#define FEET_TO_METERS 0.3048
52#define METERS_TO_FEET (1 / FEET_TO_METERS)
53#define MILES_TO_KM 1.852
54#define KM_TO_MILES (1 / MILES_TO_KM)
55#define KNOTS_TO_KMH MILES_TO_KM
56#define KMH_TO_KNOTS KM_TO_MILES
57
58/* Only for backward compatibility */
59#define UTF8_RW_ROUNDED_ARROW RW_ROUNDED_ARROW_UTF8
60
61// === Tokens, UTF-8 hex codes and drawings ===
62
63#define LEFT_ARROW_LIGHT_UTF8 "\u2190" // ←
64#define RIGHT_ARROW_LIGHT_UTF8 "\u2192" // →
65#define UP_ARROW_LIGHT_UTF8 "\u2191" // ↑
66#define DOWN_ARROW_LIGHT_UTF8 "\u2193" // ↓
67#define RW_ROUNDED_ARROW_UTF8 "\u279c" // ➜
68
69/*
70 BOX_N_UTF8
71
72 1--2--3-----4
73 5 | |
74 6-----7-----8
75 | | |
76 9----10----11
77*/
78
79#define BOX_1_UTF8 "\u250c" // ┌
80#define BOX_2_UTF8 "\u2500" // ─
81#define BOX_3_UTF8 "\u252c" // ┬
82#define BOX_4_UTF8 "\u2510" // ┐
83#define BOX_5_UTF8 "\u2502" // │
84#define BOX_6_UTF8 "\u251c" // ├
85#define BOX_7_UTF8 "\u253c" // ┼
86#define BOX_8_UTF8 "\u2524" // ┤
87#define BOX_9_UTF8 "\u2514" // └
88#define BOX_10_UTF8 "\u2534" // ┴
89#define BOX_11_UTF8 "\u2518" // ┘
90
91typedef struct {
92 struct timeval start_time; /* In s and µs */
93 float elapsed_time; /* In ms */
95
96#define STACK_N_MAX_ELEMENTS 1024
97
98/* Dynamic stacks */
99typedef struct {
100 int *v;
101 int max_n;
102 int i;
103} LG_Stack_i;
104
105typedef struct {
106 float *v;
107 int max_n;
108 int i;
109} LG_Stack_f;
110
111/* Width in bytes */
112typedef enum {
113 WBY_8_BIT = 1,
114 WBY_16_BIT = 2,
115 WBY_32_BIT = 4,
116 WBY_64_BIT = 8
117} WidthInBytes;
118
120
122
123int lg_check_gl_errors(long long, const char *, int, const char *);
124
126
127lg_endianness lg_get_arch_endianness();
128
129float lg_tv_diff(struct timeval *, struct timeval *, struct timeval *);
130
132
134
136
138
139int delta_i(int);
140
141float delta_f(float);
142
143zboolean has_changed_i(int);
144
145zboolean has_changed_f(float);
146
147double clamp(double, double, double);
148
150
152
153void lg_swap_values(void *, void *, WidthInBytes);
154
156
157void debug_max_int_in_loop(unsigned long, const char *, int);
158
160
162
163void debug_max_float_in_loop(float, const char *, int);
164
166
168
169void lg_show_struct_sizes(zboolean);
170
172
173void lg_show_utf8_drawings(zboolean);
174
175#endif /* LG_MISC_H */
lg_endianness lg_get_arch_endianness()
Definition lg_misc.c:100
void debug_max_int_in_loop(unsigned long i, const char *file, int line)
Definition lg_misc.c:403
void lg_show_struct_sizes(zboolean everything)
Definition lg_misc.c:555
int lg_check_gl_context()
Definition lg_misc.c:74
int delta_i(int i)
Definition lg_misc.c:212
void lg_stopwatch_start(LG_Stopwatch *stopwatch)
Definition lg_misc.c:167
void debug_max_float_in_loop(float f, const char *file, int line)
Definition lg_misc.c:441
float lg_tv_diff(struct timeval *t_diff, struct timeval *t2, struct timeval *t1)
Definition lg_misc.c:133
void lg_swap_values(void *x, void *y, WidthInBytes width)
Definition lg_misc.c:352
int lg_show_pixel_format_info_from_sdl_display_mode(SDL_DisplayMode *display_mode)
Definition lg_misc.c:653
void debug_start_max_float_in_loop()
Definition lg_misc.c:427
void debug_end_max_int_in_loop()
Definition lg_misc.c:414
double clamp(double v, double min, double max)
Definition lg_misc.c:264
float lg_stopwatch_get_elapsed_time(LG_Stopwatch *stopwatch)
Definition lg_misc.c:201
int lg_check_gl_errors(long long log_time, const char *file, int line, const char *func)
Definition lg_misc.c:40
zboolean has_changed_i(int i)
Definition lg_misc.c:240
zboolean has_changed_f(float f)
Definition lg_misc.c:251
void lg_disable_transparency()
Definition lg_misc.c:26
void lg_enable_transparency()
Definition lg_misc.c:17
void debug_end_max_float_in_loop()
Definition lg_misc.c:452
float delta_f(float f)
Definition lg_misc.c:226
void lg_stopwatch_resume(LG_Stopwatch *stopwatch)
Definition lg_misc.c:190
void * lg_align_up_to_next_4_bytes_boundary(void *addr)
Definition lg_misc.c:280
int lg_check_datatype_sizes()
Definition lg_misc.c:464
void lg_show_utf8_drawings(zboolean extra_n)
Definition lg_misc.c:695
void debug_start_max_int_in_loop()
Definition lg_misc.c:374
void lg_stopwatch_pause(LG_Stopwatch *stopwatch)
Definition lg_misc.c:178
void * lg_align_up_to_next_8_bytes_boundary(void *addr)
Definition lg_misc.c:299
Definition lg_misc.h:105
Definition lg_misc.h:99
Definition lg_misc.h:91