File: configuration.cpp

package info (click to toggle)
stax 1.37-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,768 kB
  • ctags: 809
  • sloc: cpp: 8,151; makefile: 30
file content (675 lines) | stat: -rw-r--r-- 18,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
 * Copyright 1999-2006 Trent Gamblin
 *
 * This file is part of Stax.
 *
 * Stax is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Stax is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stax; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <vector>

#include "stax.h"

static std::vector<RGB*> color_schemes;
static std::vector<char*> color_scheme_names;

static char *DuplicateString(char *s) throw (std::bad_alloc)
{
	char *n = new char[strlen(s)+1];
	memcpy(n, s, strlen(s)+1);
	return n;
}

void AddColorScheme(char *name, RGB colors[NUM_CONFIGURABLE_COLORS]) throw (std::bad_alloc)
{
	RGB *scheme = new RGB[NUM_CONFIGURABLE_COLORS];
	for (int i = 0; i < NUM_CONFIGURABLE_COLORS; i++) {
		scheme[i].r = colors[i].r;
		scheme[i].g = colors[i].g;
		scheme[i].b = colors[i].b;
	}
	color_schemes.push_back(scheme);
	color_scheme_names.push_back(DuplicateString(name));
}

void SetColorScheme(char* name) throw (Invalid)
{
	for (unsigned int i = 0; i < color_schemes.size(); i++) {
		if (!strcmp(color_scheme_names[i], name)) {
			for (int j = 0; j < NUM_CONFIGURABLE_COLORS; j++) {
				configuration.colors[j].r = color_schemes[i][j].r;
				configuration.colors[j].g = color_schemes[i][j].g;
				configuration.colors[j].b = color_schemes[i][j].b;
			}
			return;
		}
	}
	throw Invalid();
}

int GetNumColorSchemes(void)
{
	return color_schemes.size();
}

char* GetColorSchemeName(unsigned int i) throw (Invalid)
{
	if (i >= color_scheme_names.size())
		throw Invalid();
	return color_scheme_names[i];
}

static char *game_type_getter(int n, int *size)
{
	char *strings[3] = {
		"Sucker", "SpringShot", "Shifty"
	};

	if (n < 0) {
		*size = 3;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static char *num_blocks_getter(int n, int *size)
{
	char *strings[4] = {
		"2", "3", "4", "5"
	};

	if (n < 0) {
		*size = 4;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static char *initial_height_getter(int n, int *size)
{
	char *strings[15] = {
		"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
		"12", "13", "14", "15"
	};

	if (n < 0) {
		*size = 15;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static void ConfigureGameOptions(void)
{
	Widget game_widgets[] = {
		{ WIDGET_TEXT, ALIGN_CENTER, 140, 20, 0, 0, "Game Options", 0, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 60, 0, 0, "Game type:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_RIGHT, 275, 60, 0, 0, NULL, 0, 0, 0, 0, (void *)game_type_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 85, 0, 0, "Initial height:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_RIGHT, 275, 85, 0, 0, NULL, 0, 0, 0, 0, (void *)initial_height_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 110, 0, 0, "Block types:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_RIGHT, 275, 110, 0, 0, NULL, 0, 0, 0, 0, (void *)num_blocks_getter, NULL, NULL, 0 },
		{ WIDGET_BUTTON, ALIGN_CENTER, 140, 140, -1, 0, "OK", 6, 7, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_END, }
	};

	game_widgets[0].d1 = makecol(255, 255, 0);

	game_widgets[2].d1 = (int)configuration.game_type;
	game_widgets[4].d1 = configuration.initial_height-1;
	game_widgets[6].d1 = configuration.number_of_block_types-2;

	int ret = GUI_Go(400-147, 300-97, 295, 195, game_widgets, 2, 0);

	configuration.game_type = (GameType)game_widgets[2].d1;
	configuration.initial_height = game_widgets[4].d1+1;
	configuration.number_of_block_types = game_widgets[6].d1+2;
}

static char *player1_controls_getter(int n, int *size)
{
	char *strings[4] = {
		"Arrows & Control", "W, A, S, D & Space", "Joystick 1", "Joystick 2"
	};

	if (n < 0) {
		*size = 4;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static char *player2_controls_getter(int n, int *size)
{
	char *strings[5] = {
		"None", "Arrows & Control", "W, A, S, D & Space", "Joystick 1", "Joystick 2"
	};

	if (n < 0) {
		*size = 5;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static void ConfigureControls(void)
{
	Widget control_widgets[] = {
		{ WIDGET_TEXT, ALIGN_CENTER, 143, 20, 0, 0, "Controls", 0, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 60, 0, 0, "Player 1:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_CENTER, 183, 60, 0, 0, NULL, 0, 0, 0, 0, (void *)player1_controls_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 85, 0, 0, "Player 2:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_CENTER, 183, 85, 0, 0, NULL, 0, 0, 0, 0, (void *)player2_controls_getter, NULL, NULL, 0 },
		{ WIDGET_BUTTON, ALIGN_CENTER, 143, 115, -1, 0, "OK", 4, 5, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_END, }
	};

	control_widgets[0].d1 = makecol(255, 255, 0);

	control_widgets[2].d1 = (int)(configuration.input_type_1) - 1;
	control_widgets[4].d1 = (int)(configuration.input_type_2);

	int ret = 0;

	while (ret != 5 && ret >= 0) {
		ret = GUI_Go(400-143, 300-80, 286, 160, control_widgets, 2, 0);
		if (control_widgets[2].d1 == (control_widgets[4].d1-1)) {
			GUI_WaitMessage("|255000000Player 1 and Player 2 controls\n|255000000cannot be the same");
			ret = 0;
		}
	}

	configuration.input_type_1 = (InputType)(control_widgets[2].d1 + 1);
	configuration.input_type_2 = (InputType)(control_widgets[4].d1);
}

static char *graphics_mode_getter(int n, int *size)
{
	char *strings[2] = {
		"Windowed", "Full Screen"
	};

	if (n < 0) {
		*size = 2;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static char *resolution_getter(int n, int *size)
{
	char *strings[3] = {
		"640x480", "800x600", "1024x768"
	};

	if (n < 0) {
		*size = 3;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static char *color_depth_getter(int n, int *size)
{
	char *strings[4] = {
		"15", "16", "24", "32"
	};

	if (n < 0) {
		*size = 4;
		return NULL;
	}
	else {
		return strings[n];
	}
}

static void ConfigureGraphics(void)
{
	Widget graphics_widgets[] = {
		{ WIDGET_TEXT, ALIGN_CENTER, 158, 20, 0, 0, "Graphics", 0, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 60, 0, 0, "Graphics mode:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_RIGHT,287, 60, 0, 0, NULL, 0, 0, 0, 0, (void *)graphics_mode_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 85, 0, 0, "Resolution:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_RIGHT, 287, 85, 0, 0, NULL, 0, 0, 0, 0, (void *)resolution_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 110, 0, 0, "Color depth:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_RIGHT, 287, 110, 0, 0, NULL, 0, 0, 0, 0, (void *)color_depth_getter, NULL, NULL, 0 },
		{ WIDGET_BUTTON, ALIGN_CENTER, 158, 140, -1, 0, "OK", 2, 3, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_END, }
	};

	graphics_widgets[0].d1 = makecol(255, 255, 0);

	int old_driver = configuration.graphics_driver;
	int old_depth = configuration.color_depth;
	int old_width = configuration.screen_width;
	int old_height = configuration.screen_height;

	if (old_driver == GFX_AUTODETECT_FULLSCREEN)
		graphics_widgets[2].d1 = 1;
	else
		graphics_widgets[2].d1 = 0;

	if (configuration.screen_width == 640 && configuration.screen_height == 480)
		graphics_widgets[4].d1 = 0;
	else if (configuration.screen_width == 1024 && configuration.screen_height == 768)
		graphics_widgets[4].d1 = 2;
	else
		graphics_widgets[4].d1 = 1;

	switch (configuration.color_depth) {
		case 32:
			graphics_widgets[6].d1 = 3;
			break;
		case 24:
			graphics_widgets[6].d1 = 2;
			break;
		case 16:
			graphics_widgets[6].d1 = 1;
			break;
		case 15:
			graphics_widgets[6].d1 = 0;
			break;
	}

	GUI_Go(400-158, 300-97, 316, 195, graphics_widgets, 2, 0);

	if (graphics_widgets[2].d1 == 0)
		configuration.graphics_driver = GFX_AUTODETECT_WINDOWED;
	else
		configuration.graphics_driver = GFX_AUTODETECT_FULLSCREEN;

	switch (graphics_widgets[4].d1) {
		case 0:
			configuration.screen_width = 640;
			configuration.screen_height = 480;
			break;
		case 1:
			configuration.screen_width = 800;
			configuration.screen_height = 600;
			break;
		case 2:
			configuration.screen_width = 1024;
			configuration.screen_height = 768;
			break;
	}

	switch (graphics_widgets[6].d1) {
		case 0:
			configuration.color_depth = 15;
			break;
		case 1:
			configuration.color_depth = 16;
			break;
		case 2:
			configuration.color_depth = 24;
			break;
		case 3:
			configuration.color_depth = 32;
			break;
	}

	try {
		SetGraphicsMode();
		DestroyBuffer();
		CreateBuffer();
		DestroyData();
		LoadData();
		GUI_Initialize();
	}
	catch (BadGraphicsMode) {
		configuration.graphics_driver = old_driver;
		configuration.screen_width = old_width;
		configuration.screen_height = old_height;
		configuration.color_depth = old_depth;
		SetGraphicsMode();
		GUI_WaitMessage("|255000000Error setting graphics mode...\n|255000000Reverted to previous mode.");
	}
	catch (...) {
		allegro_message("Error reloading data");
		allegro_exit();
		exit(1);
	}
	PlayIntroMusic();
}

struct SoundDriver {
	int driver;
	char* name;
};

#ifdef __linux__
const int num_digi_drivers = 7;
static SoundDriver digi_drivers[] = {
	{ DIGI_NONE, "None" },
	{ DIGI_AUTODETECT, "Autodetect" },
	{ DIGI_ALSA, "ALSA" },
	{ DIGI_OSS, "OSS" },
	{ DIGI_ESD, "ESD" },
	{ DIGI_ARTS, "aRts" },
	{ DIGI_JACK, "JACK" },
};

const int num_midi_drivers = 5;
static SoundDriver midi_drivers[] = {
	{ MIDI_NONE, "None" },
	{ MIDI_AUTODETECT, "Autodetect" },
	{ MIDI_OSS, "OSS" },
	{ MIDI_ALSA, "ALSA" },
	{ MIDI_DIGMID, "DIGMID" }
};
#else // Windows
const int num_digi_drivers = 2;
static SoundDriver digi_drivers[] = {
	{ DIGI_NONE, "None" },
	{ DIGI_AUTODETECT, "Autodetect" }
};

const int num_midi_drivers = 3;
static SoundDriver midi_drivers[] = {
	{ MIDI_NONE, "None" },
	{ MIDI_AUTODETECT, "Autodetect" },
	{ MIDI_DIGMID, "DIGMID" }
};
#endif

static char *digi_getter(int n, int *size)
{
	if (n < 0) {
		*size = num_digi_drivers;
		return NULL;
	}
	else {
		return digi_drivers[n].name;
	}
}

static char *midi_getter(int n, int *size)
{
	if (n < 0) {
		*size = num_midi_drivers;
		return NULL;
	}
	else {
		return midi_drivers[n].name;
	}
}

static void ConfigureSound(void)
{
	Widget sound_widgets[] = {
		{ WIDGET_TEXT, ALIGN_CENTER, 213, 20, 0, 0, "Sound", 0, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 60, 0, 0, "Audio Device:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_LEFT, 119, 60, 0, 0, NULL, 0, 0, 0, 0, (void *)digi_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 85, 0, 0, "Music Device:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_OPTION, ALIGN_LEFT, 119, 85, 0, 0, NULL, 0, 0, 0, 0, (void *)midi_getter, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 110, 0, 0, "Audio volume:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_SLIDER, ALIGN_LEFT, 119, 110, 255, 0, 0, 0, 1, 10, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXT, ALIGN_LEFT, 20, 135, 0, 0, "Music volume:", -1, 0, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_SLIDER, ALIGN_LEFT, 119, 135, 255, 0, 0, 0, 1, 10, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_BUTTON, ALIGN_CENTER, 213, 165, -1, 0, "OK", 8, 9, 0, 0, NULL, NULL, NULL, 0 },
		{ WIDGET_END, }
	};

	sound_widgets[0].d1 = makecol(255, 255, 0);

	sound_widgets[2].d1 = 0;
	for (int i = 0; i < num_digi_drivers; i++)
		if (digi_drivers[i].driver == configuration.sound_driver) {
			sound_widgets[2].d1 = i;
			break;
		}

	sound_widgets[4].d1 = 0;
	for (int i = 0; i < num_midi_drivers; i++)
		if (midi_drivers[i].driver == configuration.midi_driver) {
			sound_widgets[4].d1 = i;
			break;
		}

	sound_widgets[6].d1 = configuration.sound_volume;
	sound_widgets[8].d1 = configuration.music_volume;
	
	GUI_Go(400-213, 300-110, 427, 220, sound_widgets, 2, 0);

	rest(200); // allow sample to finish playing

	configuration.sound_driver = digi_drivers[sound_widgets[2].d1].driver;
	configuration.midi_driver = midi_drivers[sound_widgets[4].d1].driver;
	configuration.sound_volume = sound_widgets[6].d1;
	configuration.music_volume = sound_widgets[8].d1;

	for (;;) {
		RemoveSound();
		try {
			InstallSound();
		}
		catch (BadInstall) {
			if (GUI_Prompt("|255000000Error setting up sound... Try again?", "Yes", "No") == 1)
				continue;
		}
		break;
	}
	PlayIntroMusic();
}

void Configure(void)
{
	clear(buffer);

	Widget configure_widgets[] = {
		{ WIDGET_TEXT, ALIGN_CENTER, 95, 20, 0, 0, "Configuration", 0, -1, -1, -1, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXTBUTTON, ALIGN_CENTER, 95, 60, 0, 0, "Game Options", -1, -1, -1, -1, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXTBUTTON, ALIGN_CENTER, 95, 80, 0, 0, "Controls", -1, -1, -1, -1, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXTBUTTON, ALIGN_CENTER, 95, 100, 0, 0, "Graphics", -1, -1, -1, -1, NULL, NULL, NULL, 0 },
		{ WIDGET_TEXTBUTTON, ALIGN_CENTER, 95, 120, 0, 0, "Sound", -1, -1, -1, -1, NULL, NULL, NULL, 0 },
		{ WIDGET_BUTTON, ALIGN_CENTER, 95, 160, -1, 0, "OK", -1, -1, -1, -1, NULL, NULL, NULL, 0 },
		{ WIDGET_END, }
	};

	int ret = 0;
	while (ret != 5 && ret >= 0) {
		configure_widgets[0].d1 = makecol(255, 255, 0);
		ret = GUI_Go(400-95, 300-105, 190, 210, configure_widgets, 1, 0);

		switch (ret) {
		case 1: ConfigureGameOptions(); break;
		case 2: ConfigureControls(); break;
		case 3: ConfigureGraphics(); break;
		case 4: ConfigureSound(); break;
		default: break;
		}
	}
}

void ReadConfiguration(char* argv0)
{
	char buf[512];
	char keyword[512], value[512];
	FILE *f;
	int v;

#ifdef __linux__ 
	snprintf(buf, sizeof(buf), "%s/.staxrc", getenv("HOME"));
#else
	replace_filename(buf, argv0, "stax.cfg", sizeof(buf));
#endif

	f = fopen(buf, "r");
	if (!f)
		return;

	while (fgets(buf, sizeof(buf), f)) {
		if (buf[0] == '#')
			continue;
		if (sscanf(buf, "%s %s", keyword, value) == 2) {
			v = atoi(value);
			if (!strcasecmp(keyword, "game")) {
				if (v < 0 || v >= NUMBER_OF_GAME_TYPES)
					v = 0;
				configuration.game_type = (GameType)v;
			}
			else if (!strcasecmp(keyword, "input1")) {
				if (v <= 0 || v >= NUMBER_OF_INPUT_TYPES)
					v = 1;
				configuration.input_type_1 = (InputType)v;
			}
			else if (!strcasecmp(keyword, "input2")) {
				if (v < 0 || v >= NUMBER_OF_INPUT_TYPES)
					v = 0;
				configuration.input_type_2 = (InputType)v;
			}
			else if (!strcasecmp(keyword, "blocks")) {
				if (v < 2 || v > 5)
					v = 4;
				configuration.number_of_block_types = v;
			}
			else if (!strcasecmp(keyword, "height")) {
				if (v < 1 || v > 15)
					v = 5;
				configuration.initial_height = v;
			}
			else if (!strcasecmp(keyword, "mode")) {
				if (v < 0 || v > 1)
					v = 0;
				if (v == 0)
					configuration.graphics_driver = GFX_AUTODETECT_WINDOWED;
				else
					configuration.graphics_driver = GFX_AUTODETECT_FULLSCREEN;
			}
			else if (!strcasecmp(keyword, "digi_driver")) {
				configuration.sound_driver = v;
			}
			else if (!strcasecmp(keyword, "midi_driver")) {
				configuration.midi_driver = v;
			}
			else if (!strcasecmp(keyword, "digi_volume")) {
				if (v < 0 || v > 255)
					v = 255;
				configuration.sound_volume = v;
			}
			else if (!strcasecmp(keyword, "midi_volume")) {
				if (v < 0 || v > 255)
					v = 255;
				configuration.music_volume = v;
			}
			else if (!strcasecmp(keyword, "depth")) {
				if (v != 24 && v != 16 && v != 15)
					v = 32;
				configuration.color_depth = v;
			}
			else if (!strcasecmp(keyword, "sw")) {
				configuration.screen_width = v;
			}
			else if (!strcasecmp(keyword, "sh")) {
				configuration.screen_height = v;
			}
		}
	}

	if (configuration.input_type_1 == configuration.input_type_2) {
		configuration.input_type_1 = INPUT_RIGHT_KEYBOARD;
		configuration.input_type_2 = INPUT_NONE;
	}

	fclose(f);
}

void WriteConfiguration(char* argv0)
{
	FILE *f;
	char filename[512];

#ifdef __linux__ 
	snprintf(filename, sizeof(filename), "%s/.staxrc", getenv("HOME"));
#else
	replace_filename(filename, argv0, "stax.cfg", sizeof(filename));
#endif

	f = fopen(filename, "w");
	if (!f)
		return;
	
	fprintf(f, 
		"# Default Game Type:\n"
		"#\n"
		"# 0 = Sucker\n"
		"# 1 = SpringShot\n"
		"# 2 = Shifty\n"
	);
	fprintf(f, "game %d\n", (int)configuration.game_type);
	fprintf(f, "\n");
	fprintf(f,
		"# Input (Player 1 and 2):\n"
		"#\n"
		"# 0 = Keys 1 (Arrows)\n"
		"# 1 = Keys 2 (WASD)\n"
		"# 2 = Joystick 1\n"
		"# 3 = Joystick 2\n\n"
	);
	fprintf(f, "input1 %d\n", (int)configuration.input_type_1);
	fprintf(f, "input2 %d\n", (int)configuration.input_type_2);
	fprintf(f, "\n");
	fprintf(f, "# Number of different block types\n");
	fprintf(f, "blocks %d\n", configuration.number_of_block_types);
	fprintf(f, "\n");
	fprintf(f, "# Initial height of the blocks\n");
	fprintf(f, "height %d\n", configuration.initial_height);
	fprintf(f, "\n");
	fprintf(f, 
			"# Graphics mode:\n"
			"#\n"
			"# 0 = Windowed\n"
			"# 1 = Full screen\n"
			"\n"
	);
	int mode;
	if (configuration.graphics_driver == GFX_AUTODETECT_WINDOWED)
		mode = 0;
	else
		mode = 1;
	fprintf(f, "mode %d\n", mode);
	fprintf(f, "\n");
	fprintf(f, "# Sound driver (system dependant, see Allegro docs)\n");
	fprintf(f, "digi_driver %d\n", configuration.sound_driver);
	fprintf(f, "# MIDI driver (system dependant, see Allegro docs)\n");
	fprintf(f, "midi_driver %d\n", configuration.midi_driver);
	fprintf(f, "# Sound volume\n");
	fprintf(f, "digi_volume %d\n", configuration.sound_volume);
	fprintf(f, "# MIDI volume\n");
	fprintf(f, "midi_volume %d\n", configuration.music_volume);
	fprintf(f, "# Color depth\n");
	fprintf(f, "depth %d\n", configuration.color_depth);
	fprintf(f, "# Screen width\n");
	fprintf(f, "sw %d\n", configuration.screen_width);
	fprintf(f, "# Screen height\n");
	fprintf(f, "sh %d\n", configuration.screen_height);
	
	fclose(f);
}