Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_games.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_games.h"
28
35 if (!(*game)) {
36 Malloc((*game), GAME_ENV, 1);
37 if (!(*game)) {
38 n_log(LOG_ERR, "Could not allocate a new game env");
39 return FALSE;
40 }
41 }
42 (*game)->BLUR = 1;
43 (*game)->fade_value = 20;
44 (*game)->DONE = 0;
45 (*game)->GFX_CONFIG_MODE = GFX_WINDOWED;
46 (*game)->CPU_MODE = CPU_USE_FULL;
47 (*game)->nb_min_particles = 0;
48 (*game)->loop_time = 0;
49 (*game)->draw_time = 0;
50 (*game)->logic_time = 0;
51 (*game)->wait_for_slowing_down_cpu = 0;
52 (*game)->GFX_UPDATE_RATE = 20000;
53 (*game)->LOGIC_RATE = 10000;
54 (*game)->real_framerate = 0;
55 (*game)->score = 0;
56 (*game)->lifes = 100;
57 (*game)->level = 1;
58 (*game)->x = 0;
59 (*game)->y = 0;
60 (*game)->z = 0;
61 (*game)->left_attack = 0;
62 (*game)->right_attack = 0;
63 (*game)->left_attack_pos = 0;
64 (*game)->right_attack_pos = 0;
65
66#ifdef HAVE_ALLEGRO
67 (*game)->display = NULL;
68 (*game)->scrbuf = NULL;
69 (*game)->event_queue = NULL;
70 (*game)->logic_timer = NULL;
71 (*game)->draw_timer = NULL;
72#endif
73
74 return TRUE;
75} /* init_game_env */
76
82 if ((*game)) {
83#ifdef HAVE_ALLEGRO
84 if ((*game)->draw_timer)
85 al_destroy_timer((*game)->draw_timer);
86 if ((*game)->logic_timer)
87 al_destroy_timer((*game)->logic_timer);
88 if ((*game)->event_queue)
89 al_destroy_event_queue((*game)->event_queue);
90 if ((*game)->scrbuf)
91 al_destroy_bitmap((*game)->scrbuf);
92 if ((*game)->display)
93 al_destroy_display((*game)->display);
94#endif
95 // cppcheck-suppress identicalInnerCondition -- Free() macro rechecks pointer internally
96 Free((*game));
97 }
98} /* destroy_game_env */
99
105GAME_ENV* load_game_config(char* config_name) {
106 FILE* config = NULL;
107 char strbuf[1024] = "";
108
109 /* CONFIGURATION STAGE */
110 config = fopen(config_name, "rt");
111
112 if (!config) {
113 n_log(LOG_ERR, "Error %s opening %s !", strerror(errno), config_name);
114 return NULL;
115 }
116
117 GAME_ENV* game = NULL;
118 init_game_env(&game);
119 if (!game) {
120 n_log(LOG_ERR, "Error allocating a new game object for %s !", config_name);
121 goto error;
122 }
123
124 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
125 {
126 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 1 ) in config.txt file !");
127 goto error;
128 }
129
130 if (fgets(strbuf, 1024, config) == NULL) {
131 n_log(LOG_ERR, "FATAL ERROR: Can not read GFX_CONFIG_MODE in config.txt file !");
132 goto error;
133 }
134 int it = 0;
135 while (it < 1024 && strbuf[it] != '\0') {
136 if (strbuf[it] == '\n')
137 strbuf[it] = '\0';
138 it++;
139 }
140
141 if (strcmp(strbuf, "GFX_FULLSCREEN") == 0)
143 else if (strcmp(strbuf, "GFX_FULLSCREEN_WINDOW") == 0)
145 else if (strcmp(strbuf, "GFX_WINDOWED") == 0)
147 else {
148 n_log(LOG_NOTICE, "WARNING: NO USABLE GFX_MODE LOADED FROM CONFIG FILE ! USING DEFAULT GFX_WINDOWED! tmpstr:\"%s\"", strbuf);
150 }
151
152 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
153 {
154 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 3 ) in config.txt file !");
155 goto error;
156 }
157
158 if (fgets(strbuf, 1024, config) == NULL) {
159 n_log(LOG_ERR, "FATAL ERROR: Can not read CPU_MODE in config.txt file !");
160 goto error;
161 }
162
163 if (strncmp(strbuf, "CPU_USE_FULL", 12) == 0)
164 game->CPU_MODE = CPU_USE_FULL;
165 else if (strncmp(strbuf, "CPU_USE_NICE", 12) == 0)
166 game->CPU_MODE = CPU_USE_NICE;
167 else if (strncmp(strbuf, "CPU_USE_LESS", 12) == 0)
168 game->CPU_MODE = CPU_USE_LESS;
169 else
170 n_log(LOG_NOTICE, "WARNING: NO USABLE CPU_MODE LOADED FROM CONFIG FILE ! USING DEFAULT CPU_FULL_USE !");
171
172 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
173 {
174 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 7 ) in config.txt file !");
175 goto error;
176 }
177
178 if (fgets(strbuf, 1024, config) == NULL) {
179 n_log(LOG_ERR, "FATAL ERROR: Can not read nb_min_particles in config.txt file !");
180 goto error;
181 }
182 game->nb_min_particles = strtol(strbuf, NULL, 10);
183 if (game->nb_min_particles < 10)
184 game->nb_min_particles = 10;
185
186 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
187 {
188 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 9 ) in config.txt file !");
189 goto error;
190 }
191
192 if (fgets(strbuf, 1024, config) == NULL) {
193 n_log(LOG_ERR, "FATAL ERROR: Can not read DRAWING_UPDATE_RATE in config.txt file !");
194 goto error;
195 }
196
197 game->GFX_UPDATE_RATE = strtol(strbuf, NULL, 10);
198
199 if (game->GFX_UPDATE_RATE < 0) {
200 n_log(LOG_NOTICE, "WARNING: You can not have a negative or zero GFX_UPDATE_RATE\nDefault value ( 20000 ) will be used");
201 game->GFX_UPDATE_RATE = 20000;
202 } else if (game->GFX_UPDATE_RATE > 1000000) {
203 n_log(LOG_NOTICE, "WARNING: You would not want to have a 1 second GFX_UPDATE_RATE, no ?\nDefault value ( 20000 ) will be used");
204 game->GFX_UPDATE_RATE = 20000;
205 }
206
207 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
208 {
209 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 11 ) in config.txt file !");
210 goto error;
211 }
212
213 if (fgets(strbuf, 1024, config) == NULL) {
214 n_log(LOG_ERR, "FATAL ERROR: Can not read LOGIC_RATE in config.txt file !");
215 goto error;
216 }
217
218 game->LOGIC_RATE = strtol(strbuf, NULL, 10);
219
220 if (game->LOGIC_RATE < 0) {
221 n_log(LOG_NOTICE, "WARNING: You can not have a negative or zero LOGIC_RATE\nDefault value ( 10000 ) will be used");
222 game->LOGIC_RATE = 10000;
223 } else {
224 if (game->LOGIC_RATE > 1000000) {
225 n_log(LOG_NOTICE, "WARNING: You would not want to have a 1 second LOGIC_RATE, no ?\nDefault value ( 10000 ) will be used");
226 game->LOGIC_RATE = 10000;
227 }
228 }
229 fclose(config);
230 goto clean;
231
232error:
233 // cppcheck-suppress useClosedFile -- error/clean paths are mutually exclusive via goto
234 fclose(config);
235 destroy_game_env(&game);
236 return NULL;
237
238clean:
239
240 return game;
241} /* load_game_config */
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
int nb_min_particles
minimum number of particles
Definition n_games.h:83
int GFX_UPDATE_RATE
time between two graphic frames 20000 default
Definition n_games.h:87
int CPU_MODE
status of the cpu mode , CPU_USE_FULL by default
Definition n_games.h:75
int LOGIC_RATE
time between two logic frames 20000 default
Definition n_games.h:89
int GFX_CONFIG_MODE
display mode: GFX_WINDOWED, GFX_FULLSCREEN, or GFX_FULLSCREEN_WINDOW
Definition n_games.h:73
#define GFX_FULLSCREEN
display mode: fullscreen
Definition n_games.h:56
void destroy_game_env(GAME_ENV **game)
destroy a game environment
Definition n_games.c:81
#define CPU_USE_LESS
wait more
Definition n_games.h:51
#define CPU_USE_FULL
full use of CPU
Definition n_games.h:47
GAME_ENV * load_game_config(char *config_name)
load a game config from file
Definition n_games.c:105
#define GFX_WINDOWED
display mode: windowed
Definition n_games.h:54
int init_game_env(GAME_ENV **game)
initialize a new game environment
Definition n_games.c:34
#define GFX_FULLSCREEN_WINDOW
display mode: fullscreen windowed
Definition n_games.h:58
#define CPU_USE_NICE
let the other process have some times
Definition n_games.h:49
Game Environment structure.
Definition n_games.h:61
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_ERR
error conditions
Definition n_log.h:75
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
Games helpers functions.