Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_games.c
Go to the documentation of this file.
1
9#include "nilorea/n_games.h"
10
16 if (!(*game)) {
17 Malloc((*game), GAME_ENV, 1);
18 if (!(*game)) {
19 n_log(LOG_ERR, "Could not allocate a new game env");
20 return FALSE;
21 }
22 }
23 (*game)->BLUR = 1;
24 (*game)->fade_value = 20;
25 (*game)->DONE = 0;
26 (*game)->GFX_CONFIG_MODE = GFX_DIRECTX_WIN;
27 (*game)->CPU_MODE = CPU_USE_FULL;
28 (*game)->nb_min_particles = 0;
29 (*game)->loop_time = 0;
30 (*game)->draw_time = 0;
31 (*game)->logic_time = 0;
32 (*game)->wait_for_slowing_down_cpu = 0;
33 (*game)->GFX_UPDATE_RATE = 20000;
34 (*game)->LOGIC_RATE = 10000;
35 (*game)->real_framerate = 0;
36 (*game)->draw_time = 0;
37 (*game)->score = 0;
38 (*game)->lifes = 100;
39 (*game)->level = 1;
40 (*game)->x = 0;
41 (*game)->y = 0;
42 (*game)->z = 0;
43 (*game)->left_attack = 0;
44 (*game)->right_attack = 0;
45 (*game)->scrbuf = NULL;
46
47 return TRUE;
48} /* init_game_env */
49
54 if ((*game)) {
55 if ((*game)->scrbuf)
56 destroy_bitmap((*game)->scrbuf);
57 Free((*game));
58 }
59} /* destroy_game_env */
60
66GAME_ENV* load_game_config(char* config_name) {
67 FILE* config = NULL;
68 char strbuf[1024] = "";
69
70 /* CONFIGURATION STAGE */
71 config = fopen(config_name, "rt");
72
73 if (!config) {
74 n_log(LOG_ERR, "Error %s opening %s !", strerror(errno), config_name);
75 return NULL;
76 }
77
78 GAME_ENV* game = NULL;
79 init_game_env(&game);
80 if (!game) {
81 n_log(LOG_ERR, "Error allocating a new game object for %s !", config_name);
82 goto error;
83 }
84
85 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
86 {
87 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 1 ) in config.txt file !");
88 goto error;
89 }
90
91 if (fgets(strbuf, 1024, config) == NULL) {
92 n_log(LOG_ERR, "FATAL ERROR: Can not read GFX_CONFIG_MODE in config.txt file !");
93 goto error;
94 }
95 int it = 0;
96 while (it < 1024 && strbuf[it] != '\0') {
97 if (strbuf[it] == '\n')
98 strbuf[it] = '\0';
99 it++;
100 }
101
102 if (strcmp(strbuf, "GFX_AUTODETECT") == 0)
103 game->GFX_CONFIG_MODE = GFX_AUTODETECT;
104 else if (strcmp(strbuf, "GFX_AUTODETECT_WINDOWED") == 0)
105 game->GFX_CONFIG_MODE = GFX_AUTODETECT_WINDOWED;
106 else if (ustrcmp(strbuf, "GFX_DIRECTX_WIN") == 0)
107 game->GFX_CONFIG_MODE = GFX_DIRECTX_WIN;
108 else if (ustrcmp(strbuf, "GFX_DIRECTX") == 0)
109 game->GFX_CONFIG_MODE = GFX_DIRECTX;
110 else {
111 n_log(LOG_NOTICE, "WARNING: NO USABLE GFX_MODE LOADED FROM CONFIG FILE ! USING DEFAULT GFX_DIRECTX_WIN! tmpstr:\"%s\"", strbuf);
112 game->GFX_CONFIG_MODE = GFX_DIRECTX_WIN;
113 }
114
115 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
116 {
117 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 3 ) in config.txt file !");
118 goto error;
119 }
120
121 if (fgets(strbuf, 1024, config) == NULL) {
122 n_log(LOG_ERR, "FATAL ERROR: Can not read CPU_MODE in config.txt file ! \n");
123 goto error;
124 }
125
126 if (strncmp(strbuf, "CPU_USE_FULL", 12) == 0)
127 game->CPU_MODE = CPU_USE_FULL;
128 else if (strncmp(strbuf, "CPU_USE_NICE", 12) == 0)
129 game->CPU_MODE = CPU_USE_NICE;
130 else if (strncmp(strbuf, "CPU_USE_LESS", 12) == 0)
131 game->CPU_MODE = CPU_USE_LESS;
132 else
133 n_log(LOG_NOTICE, "WARNING: NO USABLE CPU_MODE LOADED FROM CONFIG FILE ! USING DEFAULT CPU_FULL_USE !\n");
134
135 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
136 {
137 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 7 ) in config.txt file !");
138 goto error;
139 }
140
141 if (fgets(strbuf, 1024, config) == NULL) {
142 n_log(LOG_ERR, "FATAL ERROR: Can not read nb_min_particles in config.txt file !");
143 goto error;
144 }
145 game->nb_min_particles = strtol(strbuf, NULL, 10);
146 if (game->nb_min_particles < 10)
147 game->nb_min_particles = 10;
148
149 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
150 {
151 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 9 ) in config.txt file !");
152 goto error;
153 }
154
155 if (fgets(strbuf, 1024, config) == NULL) {
156 n_log(LOG_ERR, "FATAL ERROR: Can not read DRAWING_UPDATE_RATE in config.txt file !");
157 goto error;
158 }
159
160 game->GFX_UPDATE_RATE = strtol(strbuf, NULL, 10);
161
162 if (game->GFX_UPDATE_RATE < 0) {
163 n_log(LOG_NOTICE, "WARNING: You can not have a negative or zero GFX_UPDATE_RATE\nDefault value ( 20000 ) will be used");
164 game->GFX_UPDATE_RATE = 20000;
165 } else if (game->GFX_UPDATE_RATE > 1000000) {
166 n_log(LOG_NOTICE, "WARNING: You would not want to have a 1 second GFX_UPDATE_RATE, no ?\nDefault value ( 20000 ) will be used");
167 game->GFX_UPDATE_RATE = 20000;
168 }
169
170 if (fgets(strbuf, 1024, config) == NULL) /*error reading comment*/
171 {
172 n_log(LOG_ERR, "FATAL ERROR: Can not read comment ( line 11 ) in config.txt file ! \n");
173 goto error;
174 }
175
176 if (fgets(strbuf, 1024, config) == NULL) {
177 n_log(LOG_ERR, "FATAL ERROR: Can not read LOGIC_RATE in config.txt file !");
178 goto error;
179 }
180
181 game->LOGIC_RATE = strtol(strbuf, NULL, 10);
182
183 if (game->LOGIC_RATE < 0) {
184 n_log(LOG_NOTICE, "WARNING: You can not have a negative or zero LOGIC_RATE\nDefault value ( 10000 ) will be used");
185 game->LOGIC_RATE = 10000;
186 } else {
187 if (game->LOGIC_RATE > 1000000) {
188 n_log(LOG_NOTICE, "WARNING: You would not want to have a 1 second LOGIC_RATE, no ?\nDefault value ( 10000 ) will be used");
189 game->LOGIC_RATE = 20000;
190 }
191 }
192 fclose(config);
193 goto clean;
194
195error:
196 destroy_game_env(&game);
197
198clean:
199
200 return game;
201} /* destroy_game_env */
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
int nb_min_particles
time before a new logic update is done
Definition n_games.h:54
int GFX_UPDATE_RATE
time between two graphic frames 20000 default
Definition n_games.h:58
int CPU_MODE
status of the cpu mode , CPU_USE_FULL by default
Definition n_games.h:46
int LOGIC_RATE
time between two logic frames 20000 default
Definition n_games.h:60
int GFX_CONFIG_MODE
GFX_OPENGL_WINDOWED.
Definition n_games.h:44
void destroy_game_env(GAME_ENV **game)
Definition n_games.c:53
#define CPU_USE_LESS
wait more
Definition n_games.h:29
#define CPU_USE_FULL
full use of CPU
Definition n_games.h:25
GAME_ENV * load_game_config(char *config_name)
load a config from file
Definition n_games.c:66
int init_game_env(GAME_ENV **game)
Definition n_games.c:15
#define CPU_USE_NICE
let the other process have some times
Definition n_games.h:27
Game Environment structure.
Definition n_games.h:32
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_ERR
error conditions
Definition n_log.h:56
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:60
Games helpers functions.