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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include "nilorea/n_games.h"
29
36 if (!(*game)) {
37 Malloc((*game), GAME_ENV, 1);
38 if (!(*game)) {
39 n_log(LOG_ERR, "Could not allocate a new game env");
40 return FALSE;
41 }
42 }
43 (*game)->BLUR = 1;
44 (*game)->fade_value = 20;
45 (*game)->DONE = 0;
46 (*game)->GFX_CONFIG_MODE = GFX_WINDOWED;
47 (*game)->CPU_MODE = CPU_USE_FULL;
48 (*game)->nb_min_particles = 0;
49 (*game)->loop_time = 0;
50 (*game)->draw_time = 0;
51 (*game)->logic_time = 0;
52 (*game)->wait_for_slowing_down_cpu = 0;
53 (*game)->GFX_UPDATE_RATE = 20000;
54 (*game)->LOGIC_RATE = 10000;
55 (*game)->real_framerate = 0;
56 (*game)->score = 0;
57 (*game)->lifes = 100;
58 (*game)->level = 1;
59 (*game)->x = 0;
60 (*game)->y = 0;
61 (*game)->z = 0;
62 (*game)->left_attack = 0;
63 (*game)->right_attack = 0;
64 (*game)->left_attack_pos = 0;
65 (*game)->right_attack_pos = 0;
66
67#ifdef HAVE_ALLEGRO
68 (*game)->display = NULL;
69 (*game)->scrbuf = NULL;
70 (*game)->event_queue = NULL;
71 (*game)->logic_timer = NULL;
72 (*game)->draw_timer = NULL;
73#endif
74
75 return TRUE;
76} /* init_game_env */
77
83 if ((*game)) {
84#ifdef HAVE_ALLEGRO
85 if ((*game)->draw_timer)
86 al_destroy_timer((*game)->draw_timer);
87 if ((*game)->logic_timer)
88 al_destroy_timer((*game)->logic_timer);
89 if ((*game)->event_queue)
90 al_destroy_event_queue((*game)->event_queue);
91 if ((*game)->scrbuf)
92 al_destroy_bitmap((*game)->scrbuf);
93 if ((*game)->display)
94 al_destroy_display((*game)->display);
95#endif
96 // cppcheck-suppress identicalInnerCondition ; Free() macro rechecks pointer internally
97 Free((*game));
98 }
99} /* destroy_game_env */
100
106GAME_ENV* load_game_config(char* config_name) {
107 FILE* config = NULL;
108 char strbuf[1024] = "";
109
110 /* CONFIGURATION STAGE */
111 config = fopen(config_name, "rt");
112
113 if (!config) {
114 n_log(LOG_ERR, "Error %s opening %s !", strerror(errno), config_name);
115 return NULL;
116 }
117
118 GAME_ENV* game = NULL;
119 init_game_env(&game);
120 if (!game) {
121 n_log(LOG_ERR, "Error allocating a new game object for %s !", config_name);
122 goto error;
123 }
124
125 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
126 {
127 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 1 ) in config.txt file !");
128 goto error;
129 }
130
131 if (fgets(strbuf, 1024, config) == NULL) {
132 n_log(LOG_ERR, "FATAL ERROR: Can not read GFX_CONFIG_MODE in config.txt file !");
133 goto error;
134 }
135 int it = 0;
136 while (it < 1024 && strbuf[it] != '\0') {
137 if (strbuf[it] == '\n')
138 strbuf[it] = '\0';
139 it++;
140 }
141
142 if (strcmp(strbuf, "GFX_FULLSCREEN") == 0)
144 else if (strcmp(strbuf, "GFX_FULLSCREEN_WINDOW") == 0)
146 else if (strcmp(strbuf, "GFX_WINDOWED") == 0)
148 else {
149 n_log(LOG_NOTICE, "WARNING: NO USABLE GFX_MODE LOADED FROM CONFIG FILE ! USING DEFAULT GFX_WINDOWED! tmpstr:\"%s\"", strbuf);
151 }
152
153 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
154 {
155 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 3 ) in config.txt file !");
156 goto error;
157 }
158
159 if (fgets(strbuf, 1024, config) == NULL) {
160 n_log(LOG_ERR, "FATAL ERROR: Can not read CPU_MODE in config.txt file !");
161 goto error;
162 }
163
164 if (strncmp(strbuf, "CPU_USE_FULL", 12) == 0)
165 game->CPU_MODE = CPU_USE_FULL;
166 else if (strncmp(strbuf, "CPU_USE_NICE", 12) == 0)
167 game->CPU_MODE = CPU_USE_NICE;
168 else if (strncmp(strbuf, "CPU_USE_LESS", 12) == 0)
169 game->CPU_MODE = CPU_USE_LESS;
170 else
171 n_log(LOG_NOTICE, "WARNING: NO USABLE CPU_MODE LOADED FROM CONFIG FILE ! USING DEFAULT CPU_FULL_USE !");
172
173 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
174 {
175 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 7 ) in config.txt file !");
176 goto error;
177 }
178
179 if (fgets(strbuf, 1024, config) == NULL) {
180 n_log(LOG_ERR, "FATAL ERROR: Can not read nb_min_particles in config.txt file !");
181 goto error;
182 }
183 game->nb_min_particles = strtol(strbuf, NULL, 10);
184 if (game->nb_min_particles < 10)
185 game->nb_min_particles = 10;
186
187 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
188 {
189 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 9 ) in config.txt file !");
190 goto error;
191 }
192
193 if (fgets(strbuf, 1024, config) == NULL) {
194 n_log(LOG_ERR, "FATAL ERROR: Can not read DRAWING_UPDATE_RATE in config.txt file !");
195 goto error;
196 }
197
198 game->GFX_UPDATE_RATE = strtol(strbuf, NULL, 10);
199
200 if (game->GFX_UPDATE_RATE < 0) {
201 n_log(LOG_NOTICE, "WARNING: You can not have a negative or zero GFX_UPDATE_RATE\nDefault value ( 20000 ) will be used");
202 game->GFX_UPDATE_RATE = 20000;
203 } else if (game->GFX_UPDATE_RATE > 1000000) {
204 n_log(LOG_NOTICE, "WARNING: You would not want to have a 1 second GFX_UPDATE_RATE, no ?\nDefault value ( 20000 ) will be used");
205 game->GFX_UPDATE_RATE = 20000;
206 }
207
208 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
209 {
210 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 11 ) in config.txt file !");
211 goto error;
212 }
213
214 if (fgets(strbuf, 1024, config) == NULL) {
215 n_log(LOG_ERR, "FATAL ERROR: Can not read LOGIC_RATE in config.txt file !");
216 goto error;
217 }
218
219 game->LOGIC_RATE = strtol(strbuf, NULL, 10);
220
221 if (game->LOGIC_RATE < 0) {
222 n_log(LOG_NOTICE, "WARNING: You can not have a negative or zero LOGIC_RATE\nDefault value ( 10000 ) will be used");
223 game->LOGIC_RATE = 10000;
224 } else {
225 if (game->LOGIC_RATE > 1000000) {
226 n_log(LOG_NOTICE, "WARNING: You would not want to have a 1 second LOGIC_RATE, no ?\nDefault value ( 10000 ) will be used");
227 game->LOGIC_RATE = 10000;
228 }
229 }
230 fclose(config);
231 goto clean;
232
233error:
234 // cppcheck-suppress useClosedFile ; error/clean paths are mutually exclusive via goto
235 fclose(config);
236 destroy_game_env(&game);
237 return NULL;
238
239clean:
240
241 return game;
242} /* load_game_config */
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
int nb_min_particles
minimum number of particles
Definition n_games.h:84
int GFX_UPDATE_RATE
time between two graphic frames 20000 default
Definition n_games.h:88
int CPU_MODE
status of the cpu mode , CPU_USE_FULL by default
Definition n_games.h:76
int LOGIC_RATE
time between two logic frames 20000 default
Definition n_games.h:90
int GFX_CONFIG_MODE
display mode: GFX_WINDOWED, GFX_FULLSCREEN, or GFX_FULLSCREEN_WINDOW
Definition n_games.h:74
#define GFX_FULLSCREEN
display mode: fullscreen
Definition n_games.h:57
void destroy_game_env(GAME_ENV **game)
destroy a game environment
Definition n_games.c:82
#define CPU_USE_LESS
wait more
Definition n_games.h:52
#define CPU_USE_FULL
full use of CPU
Definition n_games.h:48
GAME_ENV * load_game_config(char *config_name)
load a game config from file
Definition n_games.c:106
#define GFX_WINDOWED
display mode: windowed
Definition n_games.h:55
int init_game_env(GAME_ENV **game)
initialize a new game environment
Definition n_games.c:35
#define GFX_FULLSCREEN_WINDOW
display mode: fullscreen windowed
Definition n_games.h:59
#define CPU_USE_NICE
let the other process have some times
Definition n_games.h:50
Game Environment structure.
Definition n_games.h:62
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_ERR
error conditions
Definition n_log.h:76
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
Games helpers functions.