2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <SDL_mixer.h>
24 /*---------------------------------------------------------------------------*/
26 /* Define the chdir symbol. */
34 /*---------------------------------------------------------------------------*/
36 static int option
[CONFIG_OPTION_COUNT
];
37 static char player
[MAXNAM
];
39 /*---------------------------------------------------------------------------*/
41 static void config_key(const char *s
, int i
, int d
)
47 for (c
= 0; c
< SDLK_LAST
; c
++)
48 if (strcmp(s
, SDL_GetKeyName(c
)) == 0)
55 /*---------------------------------------------------------------------------*/
57 void config_init(void)
59 option
[CONFIG_FULLSCREEN
] = DEFAULT_FULLSCREEN
;
60 option
[CONFIG_WIDTH
] = DEFAULT_WIDTH
;
61 option
[CONFIG_HEIGHT
] = DEFAULT_HEIGHT
;
62 option
[CONFIG_STEREO
] = DEFAULT_STEREO
;
63 option
[CONFIG_CAMERA
] = DEFAULT_CAMERA
;
64 option
[CONFIG_TEXTURES
] = DEFAULT_TEXTURES
;
65 option
[CONFIG_GEOMETRY
] = DEFAULT_GEOMETRY
;
66 option
[CONFIG_REFLECTION
] = DEFAULT_REFLECTION
;
67 option
[CONFIG_SHADOW
] = DEFAULT_SHADOW
;
68 option
[CONFIG_AUDIO_RATE
] = DEFAULT_AUDIO_RATE
;
69 option
[CONFIG_AUDIO_BUFF
] = DEFAULT_AUDIO_BUFF
;
70 option
[CONFIG_MOUSE_SENSE
] = DEFAULT_MOUSE_SENSE
;
71 option
[CONFIG_MOUSE_INVERT
] = DEFAULT_MOUSE_INVERT
;
72 option
[CONFIG_NICE
] = DEFAULT_NICE
;
73 option
[CONFIG_FPS
] = DEFAULT_FPS
;
74 option
[CONFIG_SOUND_VOLUME
] = DEFAULT_SOUND_VOLUME
;
75 option
[CONFIG_MUSIC_VOLUME
] = DEFAULT_MUSIC_VOLUME
;
76 option
[CONFIG_JOYSTICK
] = DEFAULT_JOYSTICK
;
77 option
[CONFIG_JOYSTICK_DEVICE
] = DEFAULT_JOYSTICK_DEVICE
;
78 option
[CONFIG_JOYSTICK_AXIS_X
] = DEFAULT_JOYSTICK_AXIS_X
;
79 option
[CONFIG_JOYSTICK_AXIS_Y
] = DEFAULT_JOYSTICK_AXIS_Y
;
80 option
[CONFIG_JOYSTICK_BUTTON_A
] = DEFAULT_JOYSTICK_BUTTON_A
;
81 option
[CONFIG_JOYSTICK_BUTTON_B
] = DEFAULT_JOYSTICK_BUTTON_B
;
82 option
[CONFIG_JOYSTICK_BUTTON_L
] = DEFAULT_JOYSTICK_BUTTON_L
;
83 option
[CONFIG_JOYSTICK_BUTTON_R
] = DEFAULT_JOYSTICK_BUTTON_R
;
84 option
[CONFIG_KEY_CAMERA_1
] = DEFAULT_KEY_CAMERA_1
;
85 option
[CONFIG_KEY_CAMERA_2
] = DEFAULT_KEY_CAMERA_2
;
86 option
[CONFIG_KEY_CAMERA_3
] = DEFAULT_KEY_CAMERA_3
;
87 option
[CONFIG_KEY_CAMERA_R
] = DEFAULT_KEY_CAMERA_R
;
88 option
[CONFIG_KEY_CAMERA_L
] = DEFAULT_KEY_CAMERA_L
;
89 option
[CONFIG_VIEW_FOV
] = DEFAULT_VIEW_FOV
;
90 option
[CONFIG_VIEW_DP
] = DEFAULT_VIEW_DP
;
91 option
[CONFIG_VIEW_DC
] = DEFAULT_VIEW_DC
;
92 option
[CONFIG_VIEW_DZ
] = DEFAULT_VIEW_DZ
;
94 strcpy(player
, DEFAULT_PLAYER
);
97 void config_load(void)
102 if (config_home(path
, USER_CONFIG_FILE
, MAXSTR
) && (fp
= fopen(path
, "r")))
108 while (fgets(buf
, MAXSTR
, fp
))
109 if (sscanf(buf
, "%s %s", key
, val
) == 2)
111 if (strcmp(key
, "fullscreen") == 0)
112 option
[CONFIG_FULLSCREEN
] = atoi(val
);
113 else if (strcmp(key
, "width") == 0)
114 option
[CONFIG_WIDTH
] = atoi(val
);
115 else if (strcmp(key
, "height") == 0)
116 option
[CONFIG_HEIGHT
] = atoi(val
);
117 else if (strcmp(key
, "stereo") == 0)
118 option
[CONFIG_STEREO
] = atoi(val
);
119 else if (strcmp(key
, "camera") == 0)
120 option
[CONFIG_CAMERA
] = atoi(val
);
121 else if (strcmp(key
, "textures") == 0)
122 option
[CONFIG_TEXTURES
] = atoi(val
);
123 else if (strcmp(key
, "geometry") == 0)
124 option
[CONFIG_GEOMETRY
] = atoi(val
);
125 else if (strcmp(key
, "reflection") == 0)
126 option
[CONFIG_REFLECTION
] = atoi(val
);
127 else if (strcmp(key
, "shadow") == 0)
128 option
[CONFIG_SHADOW
] = atoi(val
);
129 else if (strcmp(key
, "audio_rate") == 0)
130 option
[CONFIG_AUDIO_RATE
] = atoi(val
);
131 else if (strcmp(key
, "audio_buff") == 0)
132 option
[CONFIG_AUDIO_BUFF
] = atoi(val
);
133 else if (strcmp(key
, "mouse_sense") == 0)
134 option
[CONFIG_MOUSE_SENSE
] = atoi(val
);
135 else if (strcmp(key
, "mouse_invert") == 0)
136 option
[CONFIG_MOUSE_INVERT
] = atoi(val
);
137 else if (strcmp(key
, "nice") == 0)
138 option
[CONFIG_NICE
] = atoi(val
);
139 else if (strcmp(key
, "fps") == 0)
140 option
[CONFIG_FPS
] = atoi(val
);
141 else if (strcmp(key
, "sound_volume") == 0)
142 option
[CONFIG_SOUND_VOLUME
] = atoi(val
);
143 else if (strcmp(key
, "music_volume") == 0)
144 option
[CONFIG_MUSIC_VOLUME
] = atoi(val
);
145 else if (strcmp(key
, "joystick") == 0)
146 option
[CONFIG_JOYSTICK
] = atoi(val
);
147 else if (strcmp(key
, "joystick_device") == 0)
148 option
[CONFIG_JOYSTICK_DEVICE
] = atoi(val
);
149 else if (strcmp(key
, "joystick_axis_x") == 0)
150 option
[CONFIG_JOYSTICK_AXIS_X
] = atoi(val
);
151 else if (strcmp(key
, "joystick_axis_y") == 0)
152 option
[CONFIG_JOYSTICK_AXIS_Y
] = atoi(val
);
153 else if (strcmp(key
, "joystick_button_a") == 0)
154 option
[CONFIG_JOYSTICK_BUTTON_A
] = atoi(val
);
155 else if (strcmp(key
, "joystick_button_b") == 0)
156 option
[CONFIG_JOYSTICK_BUTTON_B
] = atoi(val
);
157 else if (strcmp(key
, "joystick_button_r") == 0)
158 option
[CONFIG_JOYSTICK_BUTTON_R
] = atoi(val
);
159 else if (strcmp(key
, "joystick_button_l") == 0)
160 option
[CONFIG_JOYSTICK_BUTTON_L
] = atoi(val
);
161 else if (strcmp(key
, "joystick_button_exit") == 0)
162 option
[CONFIG_JOYSTICK_BUTTON_EXIT
] = atoi(val
);
163 else if (strcmp(key
, "view_fov") == 0)
164 option
[CONFIG_VIEW_FOV
] = atoi(val
);
165 else if (strcmp(key
, "view_dp") == 0)
166 option
[CONFIG_VIEW_DP
] = atoi(val
);
167 else if (strcmp(key
, "view_dc") == 0)
168 option
[CONFIG_VIEW_DC
] = atoi(val
);
169 else if (strcmp(key
, "view_dz") == 0)
170 option
[CONFIG_VIEW_DZ
] = atoi(val
);
171 else if (strcmp(key
, "key_camera_1") == 0)
172 config_key(val
, CONFIG_KEY_CAMERA_1
, DEFAULT_KEY_CAMERA_1
);
173 else if (strcmp(key
, "key_camera_2") == 0)
174 config_key(val
, CONFIG_KEY_CAMERA_2
, DEFAULT_KEY_CAMERA_2
);
175 else if (strcmp(key
, "key_camera_3") == 0)
176 config_key(val
, CONFIG_KEY_CAMERA_3
, DEFAULT_KEY_CAMERA_3
);
177 else if (strcmp(key
, "key_camera_r") == 0)
178 config_key(val
, CONFIG_KEY_CAMERA_R
, DEFAULT_KEY_CAMERA_R
);
179 else if (strcmp(key
, "key_camera_l") == 0)
180 config_key(val
, CONFIG_KEY_CAMERA_L
, DEFAULT_KEY_CAMERA_L
);
181 else if (strcmp(key
, "player") == 0)
182 strncpy(player
, val
, MAXNAM
);
189 void config_save(void)
194 if (config_home(path
, USER_CONFIG_FILE
, MAXSTR
) && (fp
= fopen(path
, "w")))
196 fprintf(fp
, "fullscreen %d\n",
197 option
[CONFIG_FULLSCREEN
]);
198 fprintf(fp
, "width %d\n",
199 option
[CONFIG_WIDTH
]);
200 fprintf(fp
, "height %d\n",
201 option
[CONFIG_HEIGHT
]);
202 fprintf(fp
, "stereo %d\n",
203 option
[CONFIG_STEREO
]);
204 fprintf(fp
, "camera %d\n",
205 option
[CONFIG_CAMERA
]);
206 fprintf(fp
, "textures %d\n",
207 option
[CONFIG_TEXTURES
]);
208 fprintf(fp
, "geometry %d\n",
209 option
[CONFIG_GEOMETRY
]);
210 fprintf(fp
, "reflection %d\n",
211 option
[CONFIG_REFLECTION
]);
212 fprintf(fp
, "shadow %d\n",
213 option
[CONFIG_SHADOW
]);
214 fprintf(fp
, "audio_rate %d\n",
215 option
[CONFIG_AUDIO_RATE
]);
216 fprintf(fp
, "audio_buff %d\n",
217 option
[CONFIG_AUDIO_BUFF
]);
218 fprintf(fp
, "mouse_sense %d\n",
219 option
[CONFIG_MOUSE_SENSE
]);
220 fprintf(fp
, "mouse_invert %d\n",
221 option
[CONFIG_MOUSE_INVERT
]);
222 fprintf(fp
, "nice %d\n",
223 option
[CONFIG_NICE
]);
224 fprintf(fp
, "fps %d\n",
226 fprintf(fp
, "sound_volume %d\n",
227 option
[CONFIG_SOUND_VOLUME
]);
228 fprintf(fp
, "music_volume %d\n",
229 option
[CONFIG_MUSIC_VOLUME
]);
230 fprintf(fp
, "joystick %d\n",
231 option
[CONFIG_JOYSTICK
]);
232 fprintf(fp
, "joystick_device %d\n",
233 option
[CONFIG_JOYSTICK_DEVICE
]);
234 fprintf(fp
, "joystick_axis_x %d\n",
235 option
[CONFIG_JOYSTICK_AXIS_X
]);
236 fprintf(fp
, "joystick_axis_y %d\n",
237 option
[CONFIG_JOYSTICK_AXIS_Y
]);
238 fprintf(fp
, "joystick_button_r %d\n",
239 option
[CONFIG_JOYSTICK_BUTTON_R
]);
240 fprintf(fp
, "joystick_button_l %d\n",
241 option
[CONFIG_JOYSTICK_BUTTON_L
]);
242 fprintf(fp
, "joystick_button_a %d\n",
243 option
[CONFIG_JOYSTICK_BUTTON_A
]);
244 fprintf(fp
, "joystick_button_b %d\n",
245 option
[CONFIG_JOYSTICK_BUTTON_B
]);
246 fprintf(fp
, "joystick_button_exit %d\n",
247 option
[CONFIG_JOYSTICK_BUTTON_EXIT
]);
248 fprintf(fp
, "view_fov %d\n",
249 option
[CONFIG_VIEW_FOV
]);
250 fprintf(fp
, "view_dp %d\n",
251 option
[CONFIG_VIEW_DP
]);
252 fprintf(fp
, "view_dc %d\n",
253 option
[CONFIG_VIEW_DC
]);
254 fprintf(fp
, "view_dz %d\n",
255 option
[CONFIG_VIEW_DZ
]);
256 fprintf(fp
, "key_camera_1 %s\n",
257 SDL_GetKeyName(option
[CONFIG_KEY_CAMERA_1
]));
258 fprintf(fp
, "key_camera_2 %s\n",
259 SDL_GetKeyName(option
[CONFIG_KEY_CAMERA_2
]));
260 fprintf(fp
, "key_camera_3 %s\n",
261 SDL_GetKeyName(option
[CONFIG_KEY_CAMERA_3
]));
262 fprintf(fp
, "key_camera_r %s\n",
263 SDL_GetKeyName(option
[CONFIG_KEY_CAMERA_R
]));
264 fprintf(fp
, "key_camrea_l %s\n",
265 SDL_GetKeyName(option
[CONFIG_KEY_CAMERA_L
]));
266 fprintf(fp
, "player %s\n", player
);
272 /*---------------------------------------------------------------------------*/
274 int config_mode(int f
, int w
, int h
)
276 if (SDL_SetVideoMode(w
, h
, 0, SDL_OPENGL
| (f
? SDL_FULLSCREEN
: 0)))
278 option
[CONFIG_FULLSCREEN
] = f
;
279 option
[CONFIG_WIDTH
] = w
;
280 option
[CONFIG_HEIGHT
] = h
;
281 option
[CONFIG_SHADOW
] = (option
[CONFIG_SHADOW
] & glext_init());
283 glViewport(0, 0, w
, h
);
284 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
286 glEnable(GL_NORMALIZE
);
287 glEnable(GL_CULL_FACE
);
288 glEnable(GL_DEPTH_TEST
);
289 glEnable(GL_TEXTURE_2D
);
290 glEnable(GL_LIGHTING
);
298 * Convert the given file name to an absolute path name in the user's
299 * home directory. If the home directory cannot be established,
302 * HACK: under Windows just assume the user has permission to write to
303 * the data directory. This is more reliable than trying to devine
304 * anything reasonable from the environment.
306 int config_home(char *dst
, char *src
, size_t n
)
310 strncpy(dst
, src
, n
);
317 if ((dir
= getenv("HOME")))
319 size_t d
= strlen(dir
);
321 strncpy(dst
, dir
, n
- 1);
322 strncat(dst
, "/", n
- d
- 1);
323 strncat(dst
, src
, n
- d
- 2);
333 * Game assets are accessed via relative paths. Set the current
334 * directory to the root of the asset hierarchy. Confirm the location
335 * by checking for the presence of the named file.
337 int config_path(char *path
, char *test
)
343 if ((fp
= fopen(test
, "r")))
351 /*---------------------------------------------------------------------------*/
353 void config_set(int i
, int d
)
358 void config_tgl(int i
)
360 option
[i
] = (option
[i
] ? 0 : 1);
363 int config_tst(int i
, int d
)
365 return (option
[i
] == d
) ? 1 : 0;
368 int config_get(int i
)
373 void config_set_name(char *src
)
375 strncpy(player
, src
, MAXNAM
);
378 void config_get_name(char *dst
)
380 strncpy(dst
, player
, MAXNAM
);
383 /*---------------------------------------------------------------------------*/
385 void config_push_persp(float fov
, float n
, float f
)
387 GLdouble w
= (GLdouble
) option
[CONFIG_WIDTH
];
388 GLdouble h
= (GLdouble
) option
[CONFIG_HEIGHT
];
390 glMatrixMode(GL_PROJECTION
);
394 gluPerspective(fov
, w
/ h
, n
, f
);
396 glMatrixMode(GL_MODELVIEW
);
399 void config_push_ortho(void)
401 GLdouble w
= (GLdouble
) option
[CONFIG_WIDTH
];
402 GLdouble h
= (GLdouble
) option
[CONFIG_HEIGHT
];
404 glMatrixMode(GL_PROJECTION
);
408 glOrtho(0.0, w
, 0.0, h
, -1.0, +1.0);
410 glMatrixMode(GL_MODELVIEW
);
413 void config_pop_matrix(void)
415 glMatrixMode(GL_PROJECTION
);
419 glMatrixMode(GL_MODELVIEW
);
422 void config_clear(void)
424 if (option
[CONFIG_REFLECTION
])
425 glClear(GL_COLOR_BUFFER_BIT
|
426 GL_DEPTH_BUFFER_BIT
|
427 GL_STENCIL_BUFFER_BIT
);
429 glClear(GL_COLOR_BUFFER_BIT
|
430 GL_DEPTH_BUFFER_BIT
);
433 /*---------------------------------------------------------------------------*/