Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_games.c
Go to the documentation of this file.
1
9 #include "n_games.h"
10
11 /* initialize a new game environment */
12 int init_game_env( GAME_ENV **game )
13 {
14 if( !(*game) )
15 {
16 Malloc( (*game) , GAME_ENV , 1 );
17 if( !(*game ) )
18 {
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) -> left_attack_pos = 0 ;
46 (*game) -> right_attack_pos = 0 ;
47 (*game) -> scrbuf = NULL ;
48
49 return TRUE;
50 }
51
52 void destroy_game_env( GAME_ENV **game )
53 {
54 if( (*game) )
55 {
56 if( (*game) -> scrbuf )
57 destroy_bitmap( (*game) -> scrbuf );
58 Free( (*game ) );
59 }
60 }
61
62 /* load a config from file */
63 GAME_ENV *load_game_config( char *config_name )
64 {
65 FILE *config = NULL ;
66 char strbuf[ 1024 ] = "" ;
67
68 /* CONFIGURATION STAGE */
69 config = fopen( config_name , "rt" );
70
71 if( !config )
72 {
73 n_log( LOG_ERR , "Error %s opening %s !" , strerror( errno ) , config_name );
74 return NULL ;
75 }
76
77 GAME_ENV *game = NULL ;
78 init_game_env( &game );
79 if( !game )
80 {
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 {
93 n_log( LOG_ERR , "FATAL ERROR: Can not read GFX_CONFIG_MODE in config.txt file !" );
94 goto error ;
95 }
96 int it = 0 ;
97 while( strbuf[ it ] != '\0' && it < 1024 )
98 {
99 if( strbuf[ it ] == '\n' )
100 strbuf[ it ] = '\0' ;
101 it ++ ;
102 }
103
104 if( strcmp( strbuf , "GFX_AUTODETECT" ) == 0 )
105 game -> GFX_CONFIG_MODE = GFX_AUTODETECT;
106 else if( strcmp( strbuf , "GFX_AUTODETECT_WINDOWED" ) == 0 )
107 game -> GFX_CONFIG_MODE = GFX_AUTODETECT_WINDOWED;
108 else if( ustrcmp( strbuf , "GFX_DIRECTX_WIN" ) == 0 )
109 game -> GFX_CONFIG_MODE = GFX_DIRECTX_WIN;
110 else if( ustrcmp( strbuf , "GFX_DIRECTX" ) == 0 )
111 game -> GFX_CONFIG_MODE = GFX_DIRECTX;
112 else
113 {
114 n_log( LOG_NOTICE , "WARNING: NO USABLE GFX_MODE LOADED FROM CONFIG FILE ! USING DEFAULT GFX_DIRECTX_WIN! tmpstr:\"%s\"" , strbuf );
115 game -> GFX_CONFIG_MODE = GFX_DIRECTX_WIN;
116 }
117
118 if( fgets( strbuf , 1024 , config ) == NULL ) /*error reading comment*/
119 {
120 n_log( LOG_ERR , "FATAL ERROR: Can not read comment ( line 3 ) in config.txt file !" );
121 goto error ;
122 }
123
124 if( fgets( strbuf , 1024 , config ) == NULL )
125 {
126 n_log( LOG_ERR , "FATAL ERROR: Can not read CPU_MODE in config.txt file ! \n");
127 goto error ;
128 }
129
130 if( strncmp( strbuf , "CPU_USE_FULL" , 12 ) == 0 )
131 game -> CPU_MODE = CPU_USE_FULL;
132 else if( strncmp( strbuf , "CPU_USE_NICE" , 12 ) == 0 )
133 game -> CPU_MODE = CPU_USE_NICE;
134 else if( strncmp( strbuf , "CPU_USE_LESS" , 12 ) == 0 )
135 game -> CPU_MODE = CPU_USE_LESS;
136 else
137 n_log( LOG_NOTICE , "WARNING: NO USABLE CPU_MODE LOADED FROM CONFIG FILE ! USING DEFAULT CPU_FULL_USE !\n" );
138
139
140 if( fgets( strbuf , 1024 , config ) == NULL ) /*error reading comment*/
141 {
142 n_log( LOG_ERR , "FATAL ERROR: Can not read comment ( line 7 ) in config.txt file !" );
143 goto error ;
144 }
145
146 if( fgets( strbuf , 1024 , config ) == NULL )
147 {
148 n_log( LOG_ERR , "FATAL ERROR: Can not read nb_min_particles in config.txt file !");
149 goto error ;
150 }
151 game -> nb_min_particles = strtol( strbuf , NULL , 10 );
152 if( game -> nb_min_particles < 10 )
153 game -> nb_min_particles = 10 ;
154
155 if( fgets( strbuf , 1024 , config ) == NULL ) /*error reading comment*/
156 {
157 n_log( LOG_ERR , "FATAL ERROR: Can not read comment ( line 9 ) in config.txt file !" );
158 goto error ;
159 }
160
161 if( fgets( strbuf , 1024 , config ) == NULL )
162 {
163 n_log( LOG_ERR , "FATAL ERROR: Can not read DRAWING_UPDATE_RATE in config.txt file !");
164 goto error ;
165 }
166
167 game -> GFX_UPDATE_RATE = strtol( strbuf , NULL , 10 );
168
169 if( game -> GFX_UPDATE_RATE < 0 )
170 {
171 n_log( LOG_NOTICE , "WARNING: You can not have a negative or zero GFX_UPDATE_RATE\nDefault value ( 20000 ) will be used" );
172 game -> GFX_UPDATE_RATE = 20000;
173 }
174 else
175 if( game -> GFX_UPDATE_RATE > 1000000 )
176 {
177 n_log( LOG_NOTICE , "WARNING: You would not want to have a 1 second GFX_UPDATE_RATE, no ?\nDefault value ( 20000 ) will be used" );
178 game -> GFX_UPDATE_RATE = 20000;
179 }
180
181 if( fgets( strbuf , 1024 , config ) == NULL ) /*error reading comment*/
182 {
183 n_log( LOG_ERR , "FATAL ERROR: Can not read comment ( line 11 ) in config.txt file ! \n");
184 goto error ;
185 }
186
187 if( fgets( strbuf , 1024 , config ) == NULL )
188 {
189 n_log( LOG_ERR , "FATAL ERROR: Can not read LOGIC_RATE in config.txt file !");
190 goto error ;
191 }
192
193 game -> LOGIC_RATE = strtol( strbuf , NULL , 10 );
194
195 if( game -> LOGIC_RATE < 0 )
196 {
197 n_log( LOG_NOTICE , "WARNING: You can not have a negative or zero LOGIC_RATE\nDefault value ( 10000 ) will be used" );
198 game -> LOGIC_RATE = 10000;
199 }
200 else
201 {
202 if( game -> LOGIC_RATE > 1000000 )
203 {
204 n_log( LOG_NOTICE , "WARNING: You would not want to have a 1 second LOGIC_RATE, no ?\nDefault value ( 10000 ) will be used" );
205 game -> LOGIC_RATE = 20000;
206 }
207 }
208 fclose( config );
209 goto clean ;
210
211 error:
212 destroy_game_env( &game );
213
214 clean:
215
216 return game ;
217 } /* destroy_game_env */