-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.cpp
715 lines (692 loc) · 22.2 KB
/
main.cpp
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/* $Id$
* MegaZeux
*
* Copyright (C) 1996 Greg Janson
* Copyright (C) 1998 Matthew D. Williams - [email protected]
* Copyright (C) 1999 Charles Goetzman
* Copyright (C) 2002 B.D.A. (Koji) - [email protected]
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
//This source code should be 8086 compatible
#pragma option -2-
#pragma option -1-
#include "beep.h"
#include "profile.h"
#include "ceh.h"
#include "helpsys.h"
#include "sfx.h"
#include "random.h"
#include "edit.h"
#include "ezboard.h"
#include "roballoc.h"
#include <setjmp.h>
#include <dir.h>
#include "blink.h"
#include "timer.h"
#include "egacode.h"
#include "meminter.h"
#include "string.h"
#include "arrowkey.h"
#include "comp_chk.h"
#include "dt_data.h"
#include "graphics.h"
#include "cursor.h"
#include "window.h"
#include "palette.h"
#include "getkey.h"
#include "ems.h"
#include <stdlib.h>
#include "main.h"
#include <dos.h>
#include <stdio.h>
#include "detect.h"
#include "data.h"
#include "mod.h"
#include "game.h"
#include "error.h"
#include <time.h>
#define SAVE_INDIVIDUAL
//new_settings=1 if Addr IRQ or DMA was updated via CMD line so we
//should keep them even if config is entered
char force_ega=0,no_mouse=0,no_ems=0,new_settings=0;
jmp_buf exit_jump;//Used in error function and nowhere else
extern unsigned int Addr,IRQ,DMA;//Sound card parameters (-1=detect)
#ifdef UNREG
char *unreg_exit_mesg=
"This message should not be displayed, it is a bug, if it is.";
#else
char *reg_exit_mesg=
"Thank you for playing MegaZeux.\n\n\n\r"
"Read the files megazeux.doc and readme.1st if you need help.\n\n\r"
"Contributors to MZX:\n\n\r"
"MZX 1.0-2.51 base code:\n\r"
" Greg Janson - everything\n\r"
"MZX spider s1-s3.1 series contributers:\n\r"
" Charles Goetzman - mzx s1 base and misc. code\n\r"
" MenTaLguY - anti-flicker code, mouse buffering, mod \"*\"\n\r"
" Ben Zeigler - under bug fix and getting the thing to run\n\r"
"MadBrain - MZX s3.2 (BIMSG, etc.)\n\r"
"Akwende - MZXAK 1.0 basecode and misc. code\n\r"
"MZX 2.6:\n\r"
" Koji - New functions, real distance, etc.\n\r"
" Exophase - New functions, debugging, etc.\n\r"
" Inmate2993 - Use all 16 bits of a counter.\n\r"
"MZX 2.61:\n\r"
" Koji - Bug fixes and etc.\n\r"
"\nVisit www.Digitalmzx.net for newer versions of this software\n\n\r$";
#endif
#pragma warn -par
int main(int argc,char **argv) {
//Temp var for number->string conversion
char temp[7];
//Set to 1 if any errors occur during startup
char errors=0;
//Used to save result of early ec_init() call
char ec_i=0;
//Temporary variables
int t1;
//Return code
int ret=0;
//Set if we need first time help
char first_time=0;
//Jump destination for exiting to DOS
if(setjmp(exit_jump)) {//Exiting to DOS
mod_exit();
ret=3;
goto escape;
}
//First thing- check for card and processor
if(computer_check()) return 1;
//Scan command line options, exit if need be
if(scan_options()) return 2;
if(force_ega) vga_avail=0;
//Now- Page 0
page_flip(0);
//Init palette system and fade out
init_palette();
vquick_fadeout();
//Setup directory strings
//Get megazeux directory
str_cpy(help_file,argv[0]);
//Chop at last backslash
for(t1=str_len(help_file);t1>=0;t1--)
if(help_file[t1]=='\\') break;
help_file[t1+1]=0;
//Copy to config_file and MSE_file and blank_mod_file and convert_mod_file
str_cpy(config_file,help_file);
str_cpy(MSE_file,help_file);
str_cpy(mzx_blank_mod_file,help_file);
str_cpy(mzx_convert_mod_file,help_file);
//Caton filenames
str_cat(config_file,"megazeux.cfg");
str_cat(help_file,"mzx_help.fil");
//Get current directory and drive (form- C:\DIR\SUBDIR)
getcwd(current_dir,PATHNAME_SIZE);
str_cpy(megazeux_dir,current_dir);
megazeux_drive=current_drive=getdisk();
//Disable smzx mode
//Switch to EGA 14 point mode and turn off cursor
ega_14p_mode();
cursor_off();
//Init ec code and load up default set NOW to avoid seeing the
//line characters shift. Save status of call for later.
ec_i=ec_init();
//These lines prevent a white flash onscreen
init_palette();
insta_fadeout();
//Display Megazeux startup screen
draw_window_box(0,0,79,24,0xB800,127,120,113,0);
draw_window_box(2,1,77,3,0xB800,120,127,113,0);
draw_window_box(2,4,77,16,0xB800,120,127,113,0);
draw_window_box(2,17,77,23,0xB800,120,127,113,0);
write_string("MegaZeux version 2.61",27,2,127,0xB800);
// #ifdef BETA
write_string("BETA- PLEASE DISTRIBUTE",27,17,127,0xB800);
// #endif
#ifdef GAMMA
write_string("GAMMA- MAY CONTAIN BUGz!",27,17,127,0xB800);
#endif
#ifdef UNREG
write_string("Unregistered Evaluation Copy",25,0,122,0xB800);
#endif
write_string("Graphics card:",4,18,122,0xB800);
write_string("EMS available:",4,20,122,0xB800);
write_string("Core mem free:",4,21,122,0xB800);
write_string("Memory allocs:",4,22,122,0xB800);
write_string("Keyboard handler:",41,18,122,0xB800);
write_string("Mouse handler:",44,19,122,0xB800);
write_string("Sound card port:",42,20,122,0xB800);
write_string("Sound card IRQ:",43,21,122,0xB800);
write_string("Sound card DMA:",43,22,122,0xB800);
//Fix palette to be nicer.
//Not if I can help it!
//the random palettes are uglier
//than sin! Couldn't find the original
//so I'll just use the default -Koji
/* randomize();
switch(random(100)%4)
{
case(0):
set_rgb(1,31,0,0);
set_rgb(6,53,0,0);
set_rgb(7,21,0,0);
set_rgb(8,11,0,0);
set_rgb(9,63,63,0);
set_rgb(10,63,31,0);
set_rgb(11,63,21,21);
set_rgb(12,63,42,42);
set_rgb(13,63,42,21);
set_rgb(15,63,0,0);
break;
case(1):
set_rgb(1,46,29,31); //CORNER
set_rgb(6,50,33,35); //BACKGROUN
set_rgb(7,55,38,40); //MID Edge
set_rgb(8,41,24,26); //DARK Edge
set_rgb(9,63,46,48); //ACTIVE Text
set_rgb(10,41,24,26); //STATIC Text
set_rgb(11,55,36,39); //SELECTION Text
set_rgb(12,51,34,38); //
set_rgb(13,63,46,48); //
set_rgb(15,63,46,48); //LIGHT EDGE
break;
case(2):
set_rgb(1,0,31,0);
set_rgb(6,0,53,0);
set_rgb(7,0,21,0);
set_rgb(8,0,11,0);
set_rgb(9,0,63,63);
set_rgb(10,0,63,31);
set_rgb(11,21,63,21);
set_rgb(12,42,63,42);
set_rgb(13,21,63,42);
set_rgb(15,0,63,0);
break;
case(3):
set_rgb(1,0,0,31);
set_rgb(6,0,0,53);
set_rgb(7,0,0,21);
set_rgb(8,0,0,11);
set_rgb(9,0,63,63);
set_rgb(10,0,31,63);
set_rgb(11,21,21,63);
set_rgb(12,42,42,63);
set_rgb(13,21,42,63);
set_rgb(15,0,0,63);
break;
}*/
//Fade in
vquick_fadein();
//Initialize systems and display progess at bottom
//First, show graphics card and processor
if(force_ega) write_string("EGA (command line)",19,18,125,0xB800);
else write_string(gcard_strs[card-3],19,18,121,0xB800);
//Initialize EMS systems
if(no_ems) write_string("Forced off",19,20,125,0xB800);
else if(setup_EMS()) write_string("None",19,20,124,0xB800);
else {
itoa(free_mem_EMS()*16,temp,10);
temp[str_len(temp)+1]=0;
temp[str_len(temp)]='k';
if(free_mem_EMS()>=64) write_string(temp,19,20,121,0xB800);
else write_string(temp,19,20,124,0xB800);
}
//Write up keyboard mode
if(keyb_mode) write_string("Alternate mode",59,18,125,0xB800);
else write_string("Default mode",59,18,121,0xB800);
//Initialize windowing code
if(window_cpp_entry()) {
write_string("OUT OF MEMORY",19,22,124,0xB800);
errors=1;
}
//Initialize mouse handler (from now on must hide/show it)
if(!no_mouse) {
m_init();
m_hide();
}
if(no_mouse) write_string("Forced off",59,19,125,0xB800);
else if(driver_activated) write_string("Ok",59,19,121,0xB800);
else write_string("No mouse found",59,19,124,0xB800);
//Initialize character edit code and character set
if(ec_i) {
write_string("OUT OF MEMORY",19,22,124,0xB800);
errors=1;
}
//Install new timer ISR
install_timer();
//Allocate misc. memory
t1=0;//Set to 1 for error
board_list=(char far *)farmalloc(NUM_BOARDS*BOARD_NAME_SIZE);
if(board_list==NULL) t1=1;
board_offsets=(bOffset far *)farmalloc(NUM_BOARDS*sizeof(bOffset));
if(board_offsets==NULL) t1=1;
board_sizes=(unsigned long far *)farmalloc(NUM_BOARDS*4);
if(board_sizes==NULL) t1=1;
board_filenames=(char far *)farmalloc(NUM_BOARDS*FILENAME_SIZE);
if(board_filenames==NULL) t1=1;
level_id=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(level_id==NULL) t1=1;
level_color=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(level_color==NULL) t1=1;
level_param=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(level_param==NULL) t1=1;
level_under_id=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(level_under_id==NULL) t1=1;
level_under_color=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(level_under_color==NULL) t1=1;
level_under_param=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(level_under_param==NULL) t1=1;
overlay=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(overlay==NULL) t1=1;
overlay_color=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(overlay_color==NULL) t1=1;
update_done=(unsigned char far *)farmalloc(MAX_ARRAY_X*MAX_ARRAY_Y);
if(update_done==NULL) t1=1;
//Allocate one more for global robot
robots=(Robot far *)farmalloc((NUM_ROBOTS+1)*sizeof(Robot));
if(robots==NULL) t1=1;
scrolls=(Scroll far *)farmalloc(NUM_SCROLLS*sizeof(Scroll));
if(scrolls==NULL) t1=1;
counters=(Counter far *)farmalloc(NUM_COUNTERS*sizeof(Counter));
if(counters==NULL) t1=1;
sensors=(Sensor far *)farmalloc(NUM_SENSORS*sizeof(Sensor));
if(sensors==NULL) t1=1;
if(init_robot_mem()) t1=1;
board_setup();
if(sfx_init()) t1=1;
if(t1) {
write_string("OUT OF MEMORY",19,22,124,0xB800);
errors=1;
}
//Initialize blink code, mod code, random number generator, and keyboard
blink_off();
random_seed();
install_i09();
installceh();
//Write up free memory (below 100 shown as bad)
itoa(t1=(int)(farcoreleft()>>10),temp,10);
temp[str_len(temp)+1]=0;
temp[str_len(temp)]='k';
if(t1>=100) write_string(temp,19,21,121,0xB800);
else write_string(temp,19,21,124,0xB800);
//Give error message if appropriate (including if free Kb below 20)
if((errors)||(t1<20)) {
draw_window_box(3,9,76,11,0xB800,76,64,70,0);
write_string("Out of memory error- Please increase available core/EMS memory",
9,10,79,0xB800);
getkey();
goto escape;
}
else {
write_string("Ok",19,22,121,0xB800);
//Configuration time
//Note- music_device is from 0 to 6 at all times.
//Mixing rate is 0 1 or 2, and converted to kHz before game is
//started.
//Search for .CFG file
if(!load_config_file()) {
//Loaded- display and verify
save_config_file();//Update Port/IRQ/DMA
write_string("Current configuration-",3,5,127,0xB800);
write_string("Device for digitized music and sound:",5,6,122,0xB800);
if((music_device>0)&&(music_device<6)) {
write_string("Sound quality for digitized output:",7,7,122,0xB800);
write_string(music_quality[mixing_rate],43,7,123,0xB800);
}
if((music_device>0)&&(music_device<6)) {
write_string("Number of digitized sfx channels:",9,8,122,0xB800);
draw_char('0'+sfx_channels,123,43,8,0xB800);
}
write_string("PC speaker sound effects:",17,9,122,0xB800);
if(music_device==6) write_string("Gravis Ultra-Sound (no sfx)",43,6,123,0xB800);
else write_string(music_devices[music_device],43,6,123,0xB800);
if(sfx_on) write_string("On",43,9,123,0xB800);
else write_string("Off",43,9,123,0xB800);
//Port/IRQ/DMA-
if(Addr==0xFFFF) write_string("Autodetect",59,20,121,0xB800);
else {
itoa(Addr,temp,16);
temp[str_len(temp)+1]=0;
temp[str_len(temp)]='h';
write_string(temp,59,20,125,0xB800);
}
if(IRQ==0xFF) write_string("Autodetect",59,21,121,0xB800);
else {
itoa(IRQ,temp,10);
write_string(temp,59,21,125,0xB800);
}
if(DMA==0xFF) write_string("Autodetect",59,22,121,0xB800);
else {
itoa(DMA,temp,10);
write_string(temp,59,22,125,0xB800);
}
write_string("Press C to configure, ESC to exit, or any other key to continue.",
3,10,127,0xB800);
rekey:
t1=getkey();
if(t1==27) goto escape;
if(t1==0) goto rekey;
if((t1!='c')&&(t1!='C')) goto maingame;
if(!new_settings) {
Addr=0xFFFF;
IRQ=DMA=0xFF;
write_string("Autodetect",59,20,121,0xB800);
write_string("Autodetect",59,21,121,0xB800);
write_string("Autodetect",59,22,121,0xB800);
}
}
else {
first_time=1;
write_string("Autodetect",59,20,121,0xB800);
write_string("Autodetect",59,21,121,0xB800);
write_string("Autodetect",59,22,121,0xB800);
}
//Configure
//Digitized output device
draw_window_box(2,4,77,16,0xB800,120,127,113,0);
write_string("Devices for digitized music and sound-",3,5,127,0xB800);
for(t1=0;t1<(NUM_DEVICES/2+1);t1++) {
draw_char('A'+t1,127,5,6+t1,0xB800);
draw_char(')',127,6,6+t1,0xB800);
write_string(music_devices[t1],8,6+t1,125,0xB800);
}
for(;t1<NUM_DEVICES;t1++) {
draw_char('A'+t1,127,40,6+t1-NUM_DEVICES/2-1,0xB800);
draw_char(')',127,41,6+t1-NUM_DEVICES/2-1,0xB800);
write_string(music_devices[t1],43,6+t1-NUM_DEVICES/2-1,125,0xB800);
}
write_string(music_devices[NUM_DEVICES],43,9,125,0xB800);
write_string("Choose an output device or press ESC to exit.",3,13,127,
0xB800);
do {
t1=getkey();
if((t1>='a')&&(t1<='z')) t1-=32;
} while((t1!=27)&&((t1<'A')||(t1>'G')));
if(t1==27) goto escape;
music_device=t1-'A';
//Mixing rate
if(music_device==0) mixing_rate=0;
else if(music_device==6) mixing_rate=2;
else {
draw_window_box(2,4,77,16,0xB800,120,127,113,0);
write_string("Choose music quality (higher quality requires more processor power)",3,5,127,0xB800);
for(t1=0;t1<3;t1++) {
draw_char('A'+t1,127,5,6+t1,0xB800);
draw_char(')',127,6,6+t1,0xB800);
write_string(music_quality[t1],8,6+t1,125,0xB800);
}
write_string("Choose a music quality or press ESC to exit.",3,15,127,
0xB800);
do {
t1=getkey();
if((t1>='a')&&(t1<='z')) t1-=32;
} while((t1!=27)&&((t1<'A')||(t1>'C')));
if(t1==27) goto escape;
mixing_rate=t1-'A';
}
//Sfx channels
if((music_device==0)||(music_device==6)) sfx_channels=0;
else {
draw_window_box(2,4,77,16,0xB800,120,127,113,0);
write_string("Choose number of digitized sound effect channels-",3,5,
127,0xB800);
write_string("1, 2, 3, or 4, or ESC to exit.",3,7,127,0xB800);
write_string("(higher requires more memory and is slower)",3,8,125,0xB800);
do {
t1=getkey();
} while((t1!=27)&&((t1<'1')||(t1>'4')));
if(t1==27) goto escape;
sfx_channels=t1-'0';
}
//Sound effects
draw_window_box(2,4,77,16,0xB800,120,127,113,0);
write_string("PC speaker sound effects-",3,5,127,0xB800);
write_string("Press 1 for On, 2 for Off, or ESC to exit.",3,7,127,
0xB800);
do {
t1=getkey();
} while((t1!=27)&&(t1!='1')&&(t1!='2'));
if(t1==27) goto escape;
sfx_on='2'-t1;
//Done
save_config_file();
}
maingame:
mixing_rate=mixing_rates[music_device][mixing_rate];
if(music_device==0) music_on=0;
else music_on=1;
vquick_fadeout();
clear_screen(1824,0xB800);
//Init mod code
mod_init();
//First time help
if(first_time) {
default_palette();
insta_fadein();
context=71;
help_system();
insta_fadeout();
}
context=72;
//Run main game (mouse is hidden and palette is faded)
title_screen();
update_config_file();//Save speed setting
mod_exit();
escape:
music_device=0;//To prevent errors in accessing end_mod()
vquick_fadeout();
//Deallocate miscellaneous memory items
sfx_exit();
clear_world();
exit_robot_mem();
if(board_list!=NULL) farfree(board_list);
if(board_offsets!=NULL) farfree(board_offsets);
if(board_sizes!=NULL) farfree(board_sizes);
if(board_filenames!=NULL) farfree(board_filenames);
if(level_id!=NULL) farfree(level_id);
if(level_color!=NULL) farfree(level_color);
if(level_param!=NULL) farfree(level_param);
if(level_under_id!=NULL) farfree(level_under_id);
if(level_under_color!=NULL) farfree(level_under_color);
if(level_under_param!=NULL) farfree(level_under_param);
if(overlay!=NULL) farfree(overlay);
if(overlay_color!=NULL) farfree(overlay_color);
if(update_done!=NULL) farfree(update_done);
if(robots!=NULL) farfree(robots);
if(scrolls!=NULL) farfree(scrolls);
if(counters!=NULL) farfree(counters);
if(sensors!=NULL) farfree(sensors);
//Uninstall systems in reverse order
uninstall_i09();
blink_on();
clear_sfx_queue();
uninstall_timer();
m_deinit();
window_cpp_exit();
ec_exit();
clear_screen(1824,0xB800);
page_flip(0);
init_palette();
if((vga_avail)||((force_ega)&&(card>=VGAm))) vga_16p_mode();
cursor_underline();
asm push ds
#ifdef UNREG
asm mov dx,SEG unreg_exit_mesg
asm mov ds,dx
asm lds dx,ds:unreg_exit_mesg
#else
asm mov dx,SEG reg_exit_mesg
asm mov ds,dx
asm lds dx,ds:reg_exit_mesg
#endif
asm mov ah,9
asm int 21h
asm pop ds
profiling_summary();
//ERRORLEVEL
return ret;
}
#pragma warn +par
char scan_options(void) {
int t1,t2;
char help=0;//Set to 1 for beep+help, 2 for help
//Scan the command line options and adjust global variables accordingly.
//Returns non-0 if need to exit.
if(_argc>1) {
for(t1=1;t1<_argc;t1++) {
if((_argv[t1][0]!='-')&&(_argv[t1][0]!='+')&&
(_argv[t1][0]!='/')) {
help=1;
break;
}
str_lwr(_argv[t1]);
//Figure it out...
if(!str_cmp(&_argv[t1][1],"?")) help=2;
else if(!str_cmp(&_argv[t1][1],"nomouse")) no_mouse=1;
else if(!str_cmp(&_argv[t1][1],"noems")) no_ems=1;
else if(!str_cmp(&_argv[t1][1],"ega")) force_ega=1;
else if(!str_cmp(&_argv[t1][1],"keyb2")) keyb_mode=1;
//Cheat mode- use a - followed by an ALT-254.
else if(((unsigned char)_argv[t1][1])==254) cheats_active++;
else if(_argv[t1][1]=='l') {
//Set "current" file to string after 'l'
if(str_len(&_argv[t1][2])<FILENAME_SIZE) {//Only if short enough
str_cpy(curr_file,&_argv[t1][2]);
add_ext(curr_file,".MZX");
}
}
else {
//Check for variable arguments for sound card port/irq/dma
t2=_argv[t1][5];
_argv[t1][5]=0;
if(!str_cmp(&_argv[t1][1],"port")) {
_argv[t1][5]=t2;
//Set port
Addr=strtol(&_argv[t1][5],NULL,16);
new_settings=1;
}
else {
_argv[t1][5]=t2;
t2=_argv[t1][4];
_argv[t1][4]=0;
if(!str_cmp(&_argv[t1][1],"irq")) {
_argv[t1][4]=t2;
//Set IRQ
IRQ=strtol(&_argv[t1][4],NULL,10);
new_settings=1;
}
else if(!str_cmp(&_argv[t1][1],"dma")) {
_argv[t1][4]=t2;
//Set DMA
DMA=strtol(&_argv[t1][4],NULL,10);
new_settings=1;
}
else {
help=1;
break;
}
}
}
}
}
if(help) {
if(help==1) puts("\a");
else puts("");
puts("MegaZeux version 2.61\tCommand line parameters-\n");
puts(" -? Help with parameters.");
puts("-nomouse Don't use mouse, even if found.");
puts(" -noems Don't use EMS memory, even if available. (NOT RECOMMENDED)");
puts(" -ega Use EGA mode even if VGA or better found.");
puts(" -keyb2 Use the alternate keyboard handler; Try this if the keyboard acts");
puts("\t strange within the game or keys come out as if you are holding the");
puts("\t Ctrl, Shift, or Alt key down, when you aren't.");
puts("-l[file] Loads the world [file] up when starting MegaZeux. Do not include the");
puts("\t [ or ]. You do not have to include the extension.\n");
puts("Sound card settings- (see your sound card manual for details)");
puts("These are only needed if autodetection fails. They are saved in the");
puts("configuration file (MEGAZEUX.CFG) until you 'C'hange your configuration.\n");
puts("-port230 Use base port 230h (substitute 240h, 250h, etc.)");
puts(" -irq5 Use IRQ 5 (substitute 3, 4, etc.)");
puts(" -dma3 Use DMA 3 (substitute 4, 5, etc.)");
return 1;
}
return 0;
}
//Configuration file- MEGAZEUX.CFG.
//Simple file containing music device (byte), mixing rate (word), and
//whether sound is on. (byte)
char load_config_file(void) {//Returns non-0 if not found
int t1;
FILE *fp;
fp=fopen(config_file,"rb");
if(fp==NULL) return 1;
t1=fgetc(fp);
if((t1!=126)&&(t1!=127)) {
//old file- load overall_speed and return "no config file"
fgetc(fp);
fgetc(fp);
fgetc(fp);
overall_speed=fgetc(fp);
if((overall_speed>9)||(overall_speed<1)) overall_speed=4;
fclose(fp);
return 1;
}
music_device=fgetc(fp);
fread(&mixing_rate,2,1,fp);
sfx_on=fgetc(fp);
overall_speed=fgetc(fp);
sfx_channels=fgetc(fp);
if((overall_speed>9)||(overall_speed<1)) overall_speed=4;
if(Addr==0xFFFF) fread(&Addr,2,1,fp);
else fseek(fp,2,SEEK_CUR);
if(DMA==0xFF) fread(&DMA,2,1,fp);
else fseek(fp,2,SEEK_CUR);
if(IRQ==0xFF) fread(&IRQ,2,1,fp);
if(t1==126) {
music_gvol=fgetc(fp);
sound_gvol=fgetc(fp);
}
fclose(fp);
return 0;
}
void save_config_file(void) {//Saves the .CFG file
FILE *fp;
fp=fopen(config_file,"wb");
if(fp==NULL) return;
fputc(126,fp);//Code to signify version 2.50 config file
fputc(music_device,fp);
fwrite(&mixing_rate,2,1,fp);
fputc(sfx_on,fp);
fputc(overall_speed,fp);
fputc(sfx_channels,fp);
fwrite(&Addr,2,1,fp);
fwrite(&DMA,2,1,fp);
fwrite(&IRQ,2,1,fp);
fputc(music_gvol,fp);
fputc(sound_gvol,fp);
fclose(fp);
}
void update_config_file(void) {//Updates the speed setting in the .CFG file
FILE *fp;
fp=fopen(config_file,"rb+");
if(fp==NULL) return;
fseek(fp,5,SEEK_SET);
fputc(overall_speed,fp);
fseek(fp,7,SEEK_CUR);
fputc(music_gvol,fp);
fputc(sound_gvol,fp);
fclose(fp);
}