LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
libgame.h
1 /*
2  * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2025
3  * All rights reserved
4  *
5  * LibGame is a SDL2/OpenGL ES 2.0 2D/3D minimalist game engine, which doesn't
6  * attempt to do *everything* but does only certain things and does them *well*,
7  * while not preventing you from doing other things (huh ?)
8  *
9  * This version supports both Linux Desktop and Android (with NDK).
10  */
11 
12 #ifndef LIBGAME_H
13 #define LIBGAME_H
14 
15 #ifndef _GNU_SOURCE
16  #define _GNU_SOURCE
17 #endif
18 #ifndef _ISOC11_SOURCE
19  #define _ISOC11_SOURCE
20 #endif
21 
22 #define LIBGAME_NAME "LibGame"
23 #define LIBGAME_V_NUM "0.4.0~beta-20250408"
24 #define LIBGAME_COPYRIGHT_STR "Copyright (C) Emmanuel Thomas-Maurin 2012-2025 - All rights reserved"
25 
26 #define LIBGAME_WEBSITE "https://www.etmsoftware.com" /*"https://www.aetm-games.com"*/
27 #define LIBGAME_DOWNLOAD_WEBSITE LIBGAME_WEBSITE "/download.php"
28 #define LIBGAME_SUPPORT_WEBSITE LIBGAME_WEBSITE "/help.php"
29 #define LIBGAME_SUPPORT_EMAIL "[email protected]" /*"[email protected]"*/
30 
31 #if !defined(ANDROID_V) && !defined(WIN32_V)
32  #define LINUX_V
33 #endif
34 
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <math.h>
40 #include <errno.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <byteswap.h>
46 #include <stdalign.h>
47 #ifndef WIN32_V /* Linux and Android versions */
48  #include <grp.h>
49  #include <pwd.h>
50  #include <signal.h>
51 #else
52  #include <direct.h>
53 #endif
54 
55 #ifdef ANDROID_V
56  #include <jni.h>
57  #include <android/log.h>
58 #endif
59 
60 #ifndef WIN32_V /* Linux and Android versions */
61  #include <SDL.h>
62  #include <SDL_image.h>
63  #include <SDL_mixer.h>
64  #include <SDL_ttf.h>
65  #include <SDL_syswm.h>
66  #ifdef LINUX_V
67  #include <SDL_mouse.h>
68  #endif
69 #else /* Not tested */
70  #include <SDL.h>
71  #include <SDL2/SDL_image.h>
72  #include <SDL2/SDL_mixer.h>
73  #include <SDL2/SDL_ttf.h>
74  #include <SDL_mouse.h>
75  #include <SDL_syswm.h>
76 #endif
77 
78 #include <GLES2/gl2.h>
79 #include <GLES2/gl2ext.h>
80 #include <GLES3/gl3.h>
81 #include <GLES3/gl3ext.h>
82 
83 /* Logging stuff for libetm - must appear before #include libetm.h */
84 #define LIBETM_VERBOSE_OUTPUT
85 /*#define LIBETM_DEBUG_OUTPUT*/
86 
87 /* LIBETM_DEBUG_OUTPUT implies LIBETM_VERBOSE_OUTPUT */
88 #ifdef LIBETM_DEBUG_OUTPUT
89  #ifndef LIBETM_VERBOSE_OUTPUT
90  #define LIBETM_VERBOSE_OUTPUT
91  #endif
92 #endif
93 
94 /*
95  * === Need sth to easily switch on/off GL debugging ===
96  * CHECK_GL_E and CHECK_GL_E_2 are defined in lg_shader_progs.h
97  */
98 //#define GL_DEBUG
99 #ifdef GL_DEBUG
100  #
101 #else
102  #
103 #endif
104 
105 #ifdef ANDROID_V
106  #include "../extra_libs/libetm-0.5.1/libetm.h"
107 #else
108  #ifndef WIN32_V /* Linux version */
109  #include <libetm/libetm.h>
110  #else
111  #include "../extra_libs/libetm-0.5.1/libetm.h"
112  #endif
113 #endif
114 
115 #ifndef ANDROID_V
116  #undef INFO_ERR
117  #define INFO_ERR INFO_ERR2
118 #endif
119 
120 /* Logging stuff for libgame */
121 #define VERBOSE_OUTPUT
122 /*#define DEBUG_OUTPUT*/
123 
124 /* DEBUG_OUTPUT implies VERBOSE_OUTPUT */
125 #ifdef DEBUG_OUTPUT
126  #ifndef VERBOSE_OUTPUT
127  #define VERBOSE_OUTPUT
128  #endif
129 #endif
130 
131 //#define FUNC2(...) FUNC1(hello, __VA_ARGS__)
132 
133 #define TMPSTR_SIZE ((4 * 1024) - 1) /* = 4 KiB - 1 */
134 #define FILE_NAME_MAXLEN ((2 * 1024) - 1) /* = 2 KiB - 1 */
135 
136 #ifdef LIBGAME_COMPILE_TIME
137  #include "../extra_libs/math_3d.h"
138  #include "../extra_libs/stb_image.h"
139  #include "../extra_libs/ufbx.h"
140 #else
141  #include <libgame/extra_libs/math_3d.h>
142  #include <libgame/extra_libs/stb_image.h>
143  #include <libgame/extra_libs/ufbx.h>
144 #endif
145 
146 /* What order for header files ? */
147 /* "lg_env_instance.h" is included after typedef of LG_Env */
148 #include "lg_vbo.h"
149 #include "lg_shader_progs.h"
150 #include "lg_vertex.h"
151 #include "lg_gr_func.h"
152 #include "lg_textures.h"
153 #include "lg_dds_loader.h"
154 #include "lg_astc_loader.h"
155 #include "lg_string.h"
156 #include "lg_sprites.h"
157 #include "lg_background.h"
158 #include "lg_wins.h"
159 #include "lg_keyboard.h"
160 #include "lg_mouse.h"
161 #include "lg_touchscreen.h"
162 #include "lg_audio.h"
163 #include "lg_dirs_stuff.h"
164 #include "lg_quaternions.h"
165 #include "lg_misc.h"
166 #include "lg_ui.h"
167 #include "lg_3d_primitives.h"
168 #include "lg_opengl_2d.h"
169 #include "lg_mesh.h"
170 #include "lg_camera.h"
171 #include "lg_obj_parser.h"
172 #include "lg_light.h"
173 #include "lg_scene_graph.h"
174 #include "lg_scene.h"
175 #include "lg_collision_detect.h"
176 #include "lg_admob.h"
177 #include "lg_vao_gl_ext.h"
178 #include "lg_cubemap.h"
179 #include "lg_error.h"
180 #include "lg_file_ops.h"
181 #include "lg_android_assets.h"
182 #include "lg_render.h"
183 #include "lg_terrain.h"
184 #include "lg_perlin_noise.h"
185 #include "lg_landscape.h"
186 #include "lg_fbx_parser.h"
187 
188 typedef enum {
189  LG_OK = LIBETM_LASTERRORCODE + 1,
190  LG_ERROR,
191  LG_INIT_ERROR,
192  LG_EXTRA_LIB_INIT_ERROR,
193  LG_OPENGL_ERROR,
194  LG_GLSL_ERROR,
195  LG_CREATE_FILE_ERROR,
196  LG_OPEN_FILE_ERROR,
197  LG_SAVE_FILE_ERROR,
198  LG_READ_FROM_FILE_ERROR,
199  LG_WRITE_TO_FILE_ERROR,
200  LG_FILE_ACCESS_ERROR,
201  LG_READ_FROM_FILE_EOF,
202  LG_EOF,
203  LG_OOM,
204  LG_INVALID_PTR,
205  /* Unix signals */
206  LG_BUSERR, /* SIGBUS Bus error (bad memory access) */
207  LG_FPE, /* SIGFPE Floating-point exception */
208  LG_ILL, /* SIGILL Illegal Instruction */
209  LG_SEGFAULT, /* SIGSEGV Invalid memory reference */
210  /* Must really be the last one */
211  LG_LASTERRORCODE
212 } lg_error_code;
213 
214 typedef enum {
215  LG_CONTINUE = LG_LASTERRORCODE + 1,
216  LG_CANCEL,
217  LG_QUIT,
218  LG_WON,
219  LG_LOST,
220  LG_LASTRETURNCODE
221 } lg_return_code;
222 
223 /*
224  * WIN_LOGICAL_W and WIN_LOGICAL_H must be defined in app
225  * 16/9 or (on new mobiles) 18/9 ?
226  * #define WIN_LOGICAL_W 1000
227  * #define WIN_LOGICAL_H 500
228  * On Android, we *always* want/use landscape orientation
229  */
230 #define LG_WIN_WIDTH_MIN 800
231 #define LG_WIN_HEIGHT_MIN 400
232 
233 #define LG_WIN_D_HEIGHT_HACK 2 /* DIRTY HACK: We want the whole win to be visible */
234 
235 /* Requested bit-depth */
236 #define LG_REQUESTED_RED_SIZE 5
237 #define LG_REQUESTED_GREEN_SIZE 6
238 #define LG_REQUESTED_BLUE_SIZE 5
239 #define LG_REQUESTED_ALPHA_SIZE 0
240 #define LG_REQUESTED_DEPTH_SIZE 16
241 #define LG_REQUESTED_STENCIL_SIZE 8
242 
243 /*
244  * === LibGame environnement struct ===
245  * This is also a spec that the code is supposed to implement
246  * (should be like that for all comments).
247  *
248  * === LibGame 2D coords sys ===
249  * - Usually = SDL coords sys (y axis goes down) != OpenGL coords sys (y axis goes up)
250  * - SDL uses int's in [0 - win_w] x [0 - win_h] with origin at top left corner
251  * - OpenGL uses float's in [-1.0f, 1.0f] x [-1.0f, 1.0f] with origin at center
252  * - OpenGL win uses int's in [0 - gl_win w] x [0 - gl_win h] with origin at bottom left corner
253  * (-> glViewport() and glScissor() coords sys is (0, 0) = bottom-left, in pixels)
254  *
255  * ============================================================================
256  * To avoid endless confusion, always mention 2D coords sys in func definition
257  * ============================================================================
258  */
259 typedef struct {
260  /*
261  * SDL display mode stuff
262  */
263  int sdl_display;
264  SDL_DisplayMode sdl_display_mode;
265  int sdl_convert_surf_format; /* For all SDL_ConvertSurfaceFormat() ops */
266  int sdl_create_rgb_surf_bitdepth; /* For all SDL_CreateRGBSurface() ops */
267  int r_size, g_size, b_size, a_size;
268  uint32_t r_mask, g_mask, b_mask, a_mask; /* SDL interprets each pixel as a 32-bit integer */
269  /*
270  * Screen dims
271  */
272  int screen_w;
273  int screen_h;
274  /*
275  * OpenGL ES 2.0 stuff
276  */
277  SDL_Window *gl_win; /* Top SDL/GL win */
278  SDL_GLContext gl_context;
279  /*
280  * Opengl ES version >= 2.0
281  */
282  int gles_maj_v;
283  int gles_min_v;
284  /*
285  * Top win dims are virtual/logical dims in fullscreen mode.
286  * In fullscreen mode, we compute an optimal viewport_rect (with letterboxing)
287  * and use scaling, keeping the same aspect ratio (like SDL2 with render logical size set).
288  * In windowed mode, viewport_rect and gl_win rect have the same size.
289  */
290  int top_win_logical_w; /* In fullscreen mode, logical w */
291  int top_win_logical_h; /* In fullscreen mode, logical h */
292  Rec2Di viewport_rect; /* THE DEFAULT VIEWPORT, COMPUTED DURING INIT, MUST *NOT* BE MODIFIED AFTERWARDS */
293  zboolean fullscreen; /* Should always be set to TRUE on Android ? Seems it's expected from SDL */
294  float fullscreen_scale; /* MIN(scale_x, scale_y), to keep aspect ratio */
295  /*
296  * Texture max values
297  */
298  int max_combined_texture_image_units;
299  int max_texture_size;
300  int max_cubemap_texture_size;
301  /*
302  * Background
303  */
304  LG_Texture *bg_uniq_tex;
305  LG_Color_u bg_color; /* Always set after a call to lg_clear_bg() */
306  int bg_shift_x; /* Not sure what the purpose was exactly */
307  int bg_shift_y; /* Not sure what the purpose was exactly */
308  /*
309  * App stuff
310  */
311  char *app_name;
312  char *app_cmd;
313  char *org_name_android; /* Android-specific */
314  char *app_name_android; /* Android-specific */
315  char *assets_dir; /* App data dir on Linux, app assets on Android */
316  char *app_wr_dir; /* App home dir on Linux, app-specific writable persistent dir on Android */
317  char *vbo_cache;
318  char *obj_cache;
319  /*
320  * Mouse support
321  */
322  zboolean enable_mouse;
323  /*
324  * OpenGL ES extensions support
325  */
326  zboolean oes_vao_extension;
327  void (*glGenVertexArraysOES)(GLsizei, GLuint *);
328  void (*glBindVertexArrayOES)(GLuint);
329  void (*glDeleteVertexArraysOES)(GLsizei, const GLuint *);
330  void (*glIsVertexArrayOES)(GLuint);
331  zboolean oes_element_index_uint_extension;
332  zboolean ext_draw_instanced_extension;
333  zboolean ext_texcompr_s3tc_extension;
334  zboolean oes_texcompr_etc1_rgb8_extension;
335  zboolean khr_texcompr_astc_ldr_extension;
336  /*zboolean khr_texcompr_astc_hdr_extension;
337  zboolean ext_texcompr_astc_decode_mode_extension;*/
338  /*
339  * Other stuff
340  */
341  LG_ErrorContext last_error_context;
342  float luminosity_k;
343  LG_Sound **sounds;
344 } LG_Env;
345 
346 #include "lg_env_instance.h"
347 
348 int lg_init(int, int, const char *, const char *, const char *, const char *, const char *, const char *, const char *);
349 
351 
353 
354 void lg_reset_viewport();
355 
356 void lg_enable_mouse();
357 
358 void lg_disable_mouse();
359 
361 
363 
364 void lg_quit(int);
365 
366 void lg_swap_fb();
367 
368 void lg_show_lib_info();
369 
370 void lg_show_sys_info();
371 
372 void lg_show_extra_sys_info(SDL_Window *);
373 
374 zboolean sdl2_is_installed();
375 
376 /* Must be freed afterwards with l_str_free() */
377 char *lg_get_sdl_win_flags(SDL_Window *);
378 
379 #endif /* LIBGAME_H */
lg_reset_viewport
void lg_reset_viewport()
Definition: libgame.c:813
lg_show_sys_info
void lg_show_sys_info()
Definition: libgame.c:1013
lg_load_engine_fonts
int lg_load_engine_fonts()
Definition: libgame.c:867
LG_Sound
Definition: lg_audio.h:40
LG_Env
Definition: libgame.h:259
lg_enable_mouse
void lg_enable_mouse()
Definition: libgame.c:822
lg_swap_fb
void lg_swap_fb()
Definition: libgame.c:981
lg_get_default_viewport
Rec2Di lg_get_default_viewport()
Definition: libgame.c:795
LG_Color_u
Definition: lg_vertex.h:111
lg_set_new_viewport
void lg_set_new_viewport(Rec2Di viewport)
Definition: libgame.c:805
lg_show_extra_sys_info
void lg_show_extra_sys_info(SDL_Window *w)
Definition: libgame.c:1110
lg_get_sdl_win_flags
char * lg_get_sdl_win_flags(SDL_Window *w)
Definition: libgame.c:1191
lg_init
int lg_init(int win_w, int win_h, const char *win_title, const char *app_name, const char *app_cmd, const char *org_name_android, const char *app_name_android, const char *assets_folder, const char *app_wr_folder)
Definition: libgame.c:57
sdl2_is_installed
zboolean sdl2_is_installed()
Definition: libgame.c:1171
lg_quit
void lg_quit(int exit_code)
Definition: libgame.c:902
LG_Texture
Definition: lg_textures.h:42
LG_ErrorContext
Definition: lg_error.h:40
lg_list_opengl_extensions
void lg_list_opengl_extensions()
Definition: libgame.c:838
lg_disable_mouse
void lg_disable_mouse()
Definition: libgame.c:830
lg_show_lib_info
void lg_show_lib_info()
Definition: libgame.c:989
Rec2Di
Definition: lg_gr_func.h:49