File: main-dos.c

package info (click to toggle)
zangband 1%3A2.7.5pre1-12
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 12,900 kB
  • sloc: ansic: 169,743; tcl: 20,729; makefile: 223; sh: 12
file content (2005 lines) | stat: -rwxr-xr-x 40,322 bytes parent folder | download | duplicates (8)
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
/* File: main-dos.c */

/*
 * Copyright (c) 1997 Ben Harrison, Robert Ruehlmann, and others
 *
 * This software may be copied and distributed for educational, research,
 * and not for profit purposes provided that this copyright and statement
 * are included in all such copies.
 */


/*
 * This file helps Angband work with DOS computers.
 *
 * Adapted from "main-ibm.c".
 *
 * Author: Robert Ruehlmann (rr9@thangorodrim.net).
 * See "http://www.thangorodrim.net/".
 *
 * Initial framework (and some code) by Ben Harrison (benh@phial.com).
 *
 * This file requires the (free) DJGPP compiler (based on "gcc").
 * See "http://www.delorie.com/djgpp/".
 *
 * This file uses the (free) "Allegro" library (SVGA graphics library).
 * See "http://www.talula.demon.co.uk/allegro/".
 *
 * To compile this file, use "Makefile.dos", which defines "USE_DOS".
 *
 * See also "main-ibm.c" and "main-win.c".
 *
 *
 * The "lib/user/pref.prf" file contains macro definitions and possible
 * alternative color set definitions.
 *
 * The "lib/user/font.prf" contains attr/char mappings for use with the
 * special fonts in the "lib/xtra/font/" directory.
 *
 * The "lib/user/graf.prf" contains attr/char mappings for use with the
 * special bitmaps in the "lib/xtra/graf/" directory.
 *
 *
 * Both "shift" keys are treated as "identical", and all the modifier keys
 * (control, shift, alt) are ignored when used with "normal" keys, unless
 * they modify the underlying "ascii" value of the key.  You must use the
 * new "user pref files" to be able to interact with the keypad and such.
 *
 * Note that "Term_xtra_dos_react()" allows runtime color, graphics,
 * screen resolution, and sound modification.
 *
 *
 * The sound code uses *.wav files placed in the "lib/xtra/sound" folder.
 * Every sound-event can have several samples assigned to it and a random
 * one will be played when the event occures. Look at the
 * "lib/xtra/sound/sound.cfg" configuration file for more informations.
 *
 * The background music uses midi-files (and mod files) from the
 * "lib/xtra/music" folder.
 *
 *
 * Comment by Ben Harrison (benh@phial.com):
 *
 * On my Windows NT box, modes "VESA1" and "VESA2B" seem to work, but
 * result in the game running with no visible output.  Mode "VESA2L"
 * clears the screen and then fails silently.  All other modes fail
 * instantly.  To recover from such "invisible" modes, you can try
 * typing escape, plus control-x, plus escape.  XXX XXX XXX
 */

#include "angband.h"


#ifdef USE_DOS

cptr help_dos[] =
{
	"To use DOS (Graphics)",
	NULL
};

#include <allegro.h>

#ifdef USE_MOD_FILES
#include <jgmod.h>
#endif /* USE_MOD_FILES */

#include <bios.h>
#include <dos.h>
#include <keys.h>
#include <unistd.h>
#include <dir.h>

/*
 * Index of the first standard Angband color.
 *
 * All colors below this index are defined by
 * the palette of the tiles-bitmap.
 */
#define COLOR_OFFSET 240


/*
 * Maximum number of terminals
 */
#define MAX_TERM_DATA 8


/*
 * Forward declare
 */
typedef struct term_data term_data;


/*
 * Extra "term" data
 */
struct term_data
{
	term t;

	int number;

	int x;
	int y;

	int cols;
	int rows;

	int tile_wid;
	int tile_hgt;

	int font_wid;
	int font_hgt;

	FONT *font;

#ifdef USE_GRAPHICS

	BITMAP *tiles;

#endif /* USE_GRAPHICS */

#ifdef USE_BACKGROUND

	int window_type;

#endif /* USE_BACKGROUND */

};

/*
 * The current screen resolution
 */
static int resolution;

#ifdef USE_BACKGROUND

/*
 * The background images
 */
BITMAP *background[17];

#endif /* USE_BACKGROUND */


/*
 * An array of term_data's
 */
static term_data data[MAX_TERM_DATA];


#ifdef USE_GRAPHICS

/*
 * Are graphics already initialized ?
 */
static bool graphics_initialized = FALSE;

#endif /* USE_GRAPHICS */


/*
 * Small bitmap for the cursor
 */
static BITMAP *cursor;


#ifdef USE_SOUND

/*
 * Is the sound already initialized ?
 */
static bool sound_initialized = FALSE;

# ifdef USE_MOD_FILES
/*
 * Is the mod-file support already initialized ?
 */
static bool mod_file_initialized = FALSE;

# endif /* USE_MOD_FILES */

/*
 * Volume settings
 */
static int digi_volume;
static int midi_volume;

/*
 * The currently playing song
 */
static MIDI *midi_song = NULL;

# ifdef USE_MOD_FILES

static JGMOD *mod_song = NULL;

# endif /* USE_MOD_FILES */

static int current_song;

/*
 * The number of available songs
 */
static int song_number;

/*
 * The maximum number of available songs
 */
#define MAX_SONGS 255

static char music_files[MAX_SONGS][16];

/*
 * The maximum number of samples per sound-event
 */
#define SAMPLE_MAX 10

/*
 * An array of sound files
 */
static SAMPLE* samples[SOUND_MAX][SAMPLE_MAX];

/*
 * The number of available samples for every event
 */
static int sample_count[SOUND_MAX];

#endif /* USE_SOUND */


/*
 *  Extra paths
 */
static char xtra_font_dir[1024];
static char xtra_graf_dir[1024];
static char xtra_sound_dir[1024];
static char xtra_music_dir[1024];

/*
 * List of used videomodes to reduce executable size
 */
BEGIN_GFX_DRIVER_LIST
	GFX_DRIVER_VBEAF
	GFX_DRIVER_VGA
	GFX_DRIVER_VESA3
	GFX_DRIVER_VESA2L
	GFX_DRIVER_VESA2B
	GFX_DRIVER_VESA1
END_GFX_DRIVER_LIST


/*
 * List of used color depths to reduce executeable size
 */
BEGIN_COLOR_DEPTH_LIST
	COLOR_DEPTH_8
END_COLOR_DEPTH_LIST


/*
 * Keypress input modifier flags (hard-coded by DOS)
 */
#define K_RSHIFT	0	/* Right shift key down */
#define K_LSHIFT	1	/* Left shift key down */
#define K_CTRL		2	/* Ctrl key down */
#define K_ALT		3	/* Alt key down */
#define K_SCROLL	4	/* Scroll lock on */
#define K_NUM		5	/* Num lock on */
#define K_CAPS		6	/* Caps lock on */
#define K_INSERT	7	/* Insert on */


/*
 * Prototypes
 */
static errr Term_xtra_dos_event(int v);
static void Term_xtra_dos_react(void);
static void Term_xtra_dos_clear(void);
static errr Term_xtra_dos(int n, int v);
static errr Term_user_dos(int n);
static errr Term_curs_dos(int x, int y);
static errr Term_wipe_dos(int x, int y, int n);
static errr Term_text_dos(int x, int y, int n, byte a, const char *cp);
static void Term_init_dos(term *t);
static void Term_nuke_dos(term *t);
static void term_data_link(term_data *td);
static void dos_dump_screen(void);
static void dos_quit_hook(cptr str);
static bool init_windows(void);
errr init_dos(void);
#ifdef USE_SOUND
static bool init_sound(void);
static errr Term_xtra_dos_sound(int v);
static void play_song(void);
#endif /* USE_SOUND */
#ifdef USE_GRAPHICS
static bool init_graphics(void);
static errr Term_pict_dos(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp);
#endif /* USE_GRAPHICS */


/*
 * Process an event (check for a keypress)
 *
 * The keypress processing code is often the most system dependant part
 * of Angband, since sometimes even the choice of compiler is important.
 *
 * For this file, we divide all keypresses into two catagories, first, the
 * "normal" keys, including all keys required to play Angband, and second,
 * the "special" keys, such as keypad keys, function keys, and various keys
 * used in combination with various modifier keys.
 *
 * To simplify this file, we use Angband's "macro processing" ability, in
 * combination with the "lib/user/pref.prf" file, to handle most of the
 * "special" keys, instead of attempting to fully analyze them here.  This
 * file only has to determine when a "special" key has been pressed, and
 * translate it into a simple string which signals the use of a "special"
 * key, the set of modifiers used, if any, and the hardware scan code of
 * the actual key which was pressed.  To simplify life for the user, we
 * treat both "shift" keys as identical modifiers.
 *
 * The final encoding is "^_MMMxSS\r", where "MMM" encodes the modifiers
 * ("C" for control, "S" for shift, "A" for alt, or any ordered combination),
 * and "SS" encodes the keypress (as the two "digit" hexidecimal encoding of
 * the scan code of the key that was pressed), and the "^_" and "x" and "\r"
 * delimit the encoding for recognition by the macro processing code.
 *
 * Some important facts about scan codes follow.  All "normal" keys use
 * scan codes from 1-58.  The "function" keys use 59-68 (and 133-134).
 * The "keypad" keys use 69-83.  Escape uses 1.  Enter uses 28.  Control
 * uses 29.  Left Shift uses 42.  Right Shift uses 54.  PrtScrn uses 55.
 * Alt uses 56.  Space uses 57.  CapsLock uses 58.  NumLock uses 69.
 * ScrollLock uses 70.  The "keypad" keys which use scan codes 71-83
 * are ordered KP7,KP8,KP9,KP-,KP4,KP5,KP6,KP+,KP1,KP2,KP3,INS,DEL.
 *
 * Using "bioskey(0x10)" instead of "bioskey(0)" apparently provides more
 * information, including better access to the keypad keys in combination
 * with various modifiers, but only works on "PC's after 6/1/86", and there
 * is no way to determine if the function is provided on a machine.  I have
 * been told that without it you cannot detect, for example, control-left.
 * The basic scan code + ascii value pairs returned by the keypad follow,
 * with values in parentheses only available to "bioskey(0x10)".
 *
 *         /      *      -      +      1      2      3      4
 * Norm:  352f   372a   4a2d   4e2b   4f00   5000   5100   4b00
 * Shft:  352f   372a   4a2d   4e2b   4f31   5032   5133   4b34
 * Ctrl: (9500) (9600) (8e00) (9000)  7500  (9100)  7600   7300
 *
 *         5      6      7      8      9      0      .     Enter
 * Norm: (4c00)  4d00   4700   4800   4900   5200   5300  (e00d)
 * Shft:  4c35   4d36   4737   4838   4939   5230   532e  (e00d)
 * Ctrl: (8f00)  7400   7700  (8d00)  8400  (9200) (9300) (e00a)
 *
 * See "lib/user/pref-win.prf" for the "standard" macros for various keys.
 *
 * Certain "bizarre" keypad keys (such as "enter") return a "scan code"
 * of "0xE0", and a "usable" ascii value.  These keys should be treated
 * like the normal keys, see below.  XXX XXX XXX Note that these "special"
 * keys could be prefixed with an optional "ctrl-^" which would allow them
 * to be used in macros without hurting their use in normal situations.
 *
 * This function also appears in "main-ibm.c".  XXX XXX XXX
 *
 * Addition for the DOS version: Dump-screen function with the
 * "Ctrl-Print" key saves a bitmap with the screen contents to
 * "lib/user/dump.bmp".
 */
static errr Term_xtra_dos_event(int v)
{
	int i, k, s;

	bool mc = FALSE;
	bool ms = FALSE;
	bool ma = FALSE;

	/* Hack -- Check for a keypress */
	if (!v && !bioskey(1)) return (1);

	/* Wait for a keypress */
	k = bioskey(0x10);

	/* Access the "modifiers" */
	i = bioskey(2);

	/* Extract the "scan code" */
	s = ((k >> 8) & 0xFF);

	/* Extract the "ascii value" */
	k = (k & 0xFF);

	/* Process "normal" keys */
	if ((s <= 58) || (s == 0xE0))
	{
		/* Enqueue it */
		if (k) Term_keypress(k);

		/* Success */
		return (0);
	}

	/* Extract the modifier flags */
	if (i & (1 << K_CTRL)) mc = TRUE;
	if (i & (1 << K_LSHIFT)) ms = TRUE;
	if (i & (1 << K_RSHIFT)) ms = TRUE;
	if (i & (1 << K_ALT)) ma = TRUE;

	/* Dump the screen with "Ctrl-Print" */
	if ((s == 0x72) && mc)
	{
		/* Dump the screen */
		dos_dump_screen();

		/* Success */
		return (0);
	}

	/* Begin a "macro trigger" */
	Term_keypress(31);

	/* Hack -- Send the modifiers */
	if (mc) Term_keypress('C');
	if (ms) Term_keypress('S');
	if (ma) Term_keypress('A');

	/* Introduce the hexidecimal scan code */
	Term_keypress('x');

	/* Encode the hexidecimal scan code */
	Term_keypress(hexsym[s/16]);
	Term_keypress(hexsym[s%16]);

	/* End the "macro trigger" */
	Term_keypress(13);

	/* Success */
	return (0);
}


/*
 * React to global changes in the colors, graphics, and sound settings.
 */
static void Term_xtra_dos_react(void)
{
	int i;

#ifdef USE_SPECIAL_BACKGROUND

	int j;

	term_data *td;

#endif /* USE_SPECIAL_BACKGROUND */

	/*
	 * Set the Angband colors
	 */
	for (i = 0; i < 16; i++)
	{
		RGB color;

		/* Extract desired values */
		char rv = angband_color_table[i][1] >> 2;
		char gv = angband_color_table[i][2] >> 2;
		char bv = angband_color_table[i][3] >> 2;

		/* Set the colors */
		color.r = rv;
		color.g = gv;
		color.b = bv;

		set_color(COLOR_OFFSET + i, &color);
	}

#ifdef USE_GRAPHICS

	/*
	 * Handle "arg_graphics"
	 */
	if (use_graphics != arg_graphics)
	{
		/* Initialize (if needed) */
		if (arg_graphics && !init_graphics())
		{
			/* Warning */
			plog("Cannot initialize graphics!");

			/* Cannot enable */
			arg_graphics = GRAPHICS_NONE;
		}

		/* Change setting */
		use_graphics = arg_graphics;
	}

#endif /* USE_GRAPHICS */

#ifdef USE_SOUND

	/*
	 * Handle "arg_sound"
	 */
	if (use_sound != arg_sound)
	{
		/* Clear the old song */
		if (midi_song) destroy_midi(midi_song);
		midi_song = NULL;

#ifdef USE_MOD_FILES
		if (mod_file_initialized)
		{
			stop_mod();
			destroy_mod(mod_song);
		}
#endif /* USE_MOD_FILES */

		/* Initialize (if needed) */
		if (arg_sound && !init_sound())
		{
			/* Warning */
			plog("Cannot initialize sound!");

			/* Cannot enable */
			arg_sound = FALSE;
		}

		/* Change setting */
		use_sound = arg_sound;
	}

#endif /* USE_SOUND */

#ifdef USE_SPECIAL_BACKGROUND

	/*
	 * Initialize the window backgrounds
	 */
	for (i = 0; i < MAX_TERM_DATA; i++)
	{
		td = &data[i];

		/* Window flags */
		for (j = 0; j < 16; j++)
		{
			if (op_ptr->window_flag[i] & (1L << j))
			{
				if (background[j + 1])
				{
					td->window_type = j + 1;
				}
				else
				{
					td->window_type = 0;
				}
			}
		}
	}
#endif /* USE_SPECIAL_BACKGROUND */
}


/*
 * Clear a terminal
 *
 * Fills the terminal area with black color or with
 * the background image
 */
static void Term_xtra_dos_clear(void)
{
	term_data *td = (term_data*)(Term->data);

#ifdef USE_BACKGROUND
	int bgrnd;
#endif /* USE_BACKGROUND */

	int x1, y1;
	int w1, h1;

	/* Location */
	x1 = td->x;
	y1 = td->y;

	/* Size */
	w1 = td->tile_wid * td->cols;
	h1 = td->tile_hgt * td->rows;

#ifdef USE_BACKGROUND

	bgrnd = td->window_type;

	if (background[bgrnd])
	{
		/* Draw the background */
		stretch_blit(background[bgrnd], screen,
			0, 0, background[bgrnd]->w, background[bgrnd]->h,
			x1, y1, w1, h1);
	}
	else

#endif /* USE_BACKGROUND */

	{
		/* Draw the Term black */
		rectfill(screen,
	        	x1, y1, x1 + w1 - 1, y1 + h1 - 1,
			COLOR_OFFSET + TERM_DARK);
	}
}


/*
 * Handle a "special request"
 *
 * The given parameters are "valid".
 */
static errr Term_xtra_dos(int n, int v)
{
	/* Analyze the request */
	switch (n)
	{
		/* Make a "bell" noise */
		case TERM_XTRA_NOISE:
		{
			/* Make a bell noise */
			(void)write(1, "\007", 1);

			/* Success */
			return (0);
		}

		/* Process events */
		case TERM_XTRA_EVENT:
		{
			/* Process one event */
			return (Term_xtra_dos_event(v));
		}

		/* Flush events */
		case TERM_XTRA_FLUSH:
		{
			/* Strip events */
			while (!Term_xtra_dos_event(FALSE));

			/* Success */
			return (0);
		}

		/* Do something useful if bored */
		case TERM_XTRA_BORED:
		{
#ifdef USE_SOUND
			/*
			 * Check for end of song and start a new one
			 */
			if (!use_sound) return (0);

#ifdef USE_MOD_FILES
			if (song_number && (midi_pos < 0) && !is_mod_playing())
#else /* USE_MOD_FILES */
			if (song_number && (midi_pos < 0))
#endif /* USE_MOD_FILES */
			{
				if (song_number > 1)
				{
					/* Get a *new* song at random */
					while (1)
					{
						n = Rand_simple(song_number) + 1;
						if (n != current_song) break;
					}
					current_song = n;
				}
				else
				{
					/* We only have one song, so loop it */
					current_song = 1;
				}

				/* Play the song */
				play_song();
			}

#endif /* USE_SOUND */

			/* Success */
			return (0);
		}

		/* React to global changes */
		case TERM_XTRA_REACT:
		{
			/* Change the colors */
			Term_xtra_dos_react();

			/* Success */
			return (0);
		}

		/* Delay for some milliseconds */
		case TERM_XTRA_DELAY:
		{
			/* Delay if needed */
			if (v > 0) delay(v);

			/* Success */
			return (0);
		}

#ifdef USE_SOUND

		/* Make a sound */
		case TERM_XTRA_SOUND:
		{
			return (Term_xtra_dos_sound(v));
		}

#endif /* USE_SOUND */

	}

	/* Unknown request */
	return (1);
}


/*
 * Do a "user action" on the current "term"
 */
static errr Term_user_dos(int n)
{
	int k;

	char section[80];

	/* Unused parameter */
	(void)n;

	/* Interact */
	while (1)
	{
		/* Clear screen */
		Term_clear();

		/* Print date and time of compilation */
		prtf(45, 1, "Compiled: %s %s\n", __TIME__, __DATE__);

		/* Why are we here */
		prtf(0, 2, "DOS options");

		/* Give some choices */
#ifdef USE_SOUND
		prtf(5, 4, "(V) Sound Volume");
		prtf(5, 5, "(M) Music Volume");
#endif /* USE_SOUND */

#ifdef USE_GRAPHICS

		prtf(5, 7, "(G) Graphics : %s", arg_graphics ? "On" : "Off");

#endif /* USE_GRAPHICS */

#ifdef USE_SOUND

		prtf(5, 8, "(S) Sound/Music : %s", arg_sound ? "On" : "Off");

#endif /* USE_SOUND */

		prtf(5, 12, "(R) Screen resolution");

		prtf(5, 14, "(W) Save current options");

		/* Prompt */
		prtf(0, 18, "Command: ");

		/* Get command */
		k = inkey();

		/* Exit */
		if (k == ESCAPE) break;

		/* Analyze */
		switch (k)
		{
#ifdef USE_SOUND
			/* Sound Volume */
			case 'V':
			case 'v':
			{
				/* Prompt */
				prtf(0, 18, "Command: Sound Volume");

				/* Get a new value */
				while (1)
				{
					prtf(0, 22, "Current Volume: %d", digi_volume);
					prtf(0, 20, "Change Volume (+, - or ESC to accept): ");
					k = inkey();
					if (k == ESCAPE) break;
					switch (k)
					{
						case '+':
						{
							digi_volume++;
							if (digi_volume > 255) digi_volume = 255;
							break;
						}
						case '-':
						{
							digi_volume--;
							if (digi_volume < 0) digi_volume = 0;
							break;
						}
						/* Unknown option */
						default:
						{
							break;
						}
					}
					set_volume(digi_volume, -1);
				}
				break;
			}

			/* Music Volume */
			case 'M':
			case 'm':
			{
				/* Prompt */
				prtf(0, 18, "Command: Music Volume");

				/* Get a new value */
				while (1)
				{
					prtf(0, 22, "Current Volume: %d", midi_volume);
					prtf(0, 20, "Change Volume (+, - or ESC to accept): ");
					k = inkey();
					if (k == ESCAPE) break;
					switch (k)
					{
						case '+':
						{
							midi_volume++;
							if (midi_volume > 255) midi_volume = 255;
							break;
						}
						case '-':
						{
							midi_volume--;
							if (midi_volume < 0) midi_volume = 0;
							break;
						}
						/* Unknown option */
						default:
						{
							break;
						}
					}
					set_volume(-1, midi_volume);
				}
				break;
			}

#endif /* USE_SOUND */

#ifdef USE_GRAPHICS

			/* Switch graphics on/off */
			case 'G':
			case 'g':
			{
				/* Hack - Toggle "arg_graphics" */
				arg_graphics = !arg_graphics;

				/* React to changes */
				Term_xtra_dos_react();

				/* Reset visuals */
#ifdef ANGBAND_2_8_1
				reset_visuals();
#else /* ANGBAND_2_8_1 */
				reset_visuals(TRUE);
#endif /* ANGBAND_2_8_1 */
				break;
			}

#endif /* USE_GRAPHICS */

#ifdef USE_SOUND

			/* Sound/Music On/Off */
			case 'S':
			case 's':
			{
				/* Toggle "arg_sound" */
				arg_sound = !arg_sound;

				/* React to changes */
				Term_xtra_dos_react();

				break;
			}

#endif /* USE_SOUND */

			/* Screen Resolution */
			case 'R':
			case 'r':
			{
				int h, w, i = 1;
				cptr descr;

				/* Clear screen */
				Term_clear();

				/* Prompt */
				prtf(0, 1, "Command: Screen Resolution");
				prtf(0, 3, "Restart %s to get the new screenmode.", VERSION_NAME);

				/* Get a list of the available presets */
				while (1)
				{
					/* Section name */
					strnfmt(section, 80, "Mode-%d", i);

					/* Get new values or end the list */
					if (!(w = get_config_int(section, "screen_wid", 0)) || (i == 16)) break;
					h = get_config_int(section, "screen_hgt", 0);

					/* Get a extra description of the resolution */
					descr = get_config_string(section, "Description", "");

					/* Print it */
					prtf(0, 4 + i, "(%d) %d x %d   %s", i, w, h, descr);

					/* Next */
					i++;
				}

				/* Get a new resolution */
				prtf(0, 20, "Screen Resolution : %d", resolution);
				k = inkey();
				if (k == ESCAPE) break;
				if (isdigit(k)) resolution = D2I(k);

				/* Check for min, max value */
				if ((resolution < 1) || (resolution >= i)) resolution = 1;

				/* Save the resolution */
				set_config_int("Angband", "Resolution", resolution);

				/* Return */
				break;
			}


			/* Save current option */
			case 'W':
			case 'w':
			{
				prtf(0, 18, "Saving current options");

#ifdef USE_SOUND
				set_config_int("sound", "digi_volume", digi_volume);
				set_config_int("sound", "midi_volume", midi_volume);
#endif /* USE_SOUND */
				set_config_int("Angband", "Graphics", arg_graphics);
				set_config_int("Angband", "Sound", arg_sound);

				break;
			}

			/* Unknown option */
			default:
			{
				break;
			}
		}

		/* Flush messages */
		message_flush();
	}

	/* Redraw it */
	Term_key_push(KTRL('R'));

	/* Unknown */
	return (0);
}


/*
 * Move the cursor
 *
 * The given parameters are "valid".
 */
static errr Term_curs_dos(int x, int y)
{
	term_data *td = (term_data*)(Term->data);

	int x1, y1;

	/* Location */
	x1 = x * td->tile_wid + td->x;
	y1 = y * td->tile_hgt + td->y;

	/* Draw the cursor */
	draw_sprite(screen, cursor, x1, y1);

	/* Success */
	return (0);
}


/*
 * Erase a block of the screen
 *
 * The given parameters are "valid".
 */
static errr Term_wipe_dos(int x, int y, int n)
{
	term_data *td = (term_data*)(Term->data);

#ifdef USE_BACKGROUND
	int bgrnd;
#endif /* USE_BACKGROUND */

	int x1, y1;
	int w1, h1;

	/* Location */
	x1 = x * td->tile_wid + td->x;
	y1 = y * td->tile_hgt + td->y;

	/* Size */
	w1 = n * td->tile_wid;
	h1 = td->tile_hgt;

#ifdef USE_BACKGROUND

	bgrnd = td->window_type;

	if (background[bgrnd])
	{
		int source_x = x * background[bgrnd]->w / td->cols;
		int source_y = y * background[bgrnd]->h / td->rows;
		int source_w = n * background[bgrnd]->w / td->cols;
		int source_h = background[bgrnd]->h / td->rows;

		/* Draw the background */
		stretch_blit(background[bgrnd], screen,
			source_x, source_y, source_w, source_h,
			x1, y1, w1, h1);
	}
	else

#endif /* USE_BACKGROUND */

	{
		/* Draw a black block */
		rectfill(screen, x1, y1, x1 + w1 - 1, y1 + h1 - 1,
	        	COLOR_OFFSET + TERM_DARK);
	}

	/* Success */
	return (0);
}


/*
 * Place some text on the screen using an attribute
 *
 * The given parameters are "valid".  Be careful with "a".
 *
 * The string "cp" has length "n" and is NOT null-terminated.
 */
static errr Term_text_dos(int x, int y, int n, byte a, const char *cp)
{
	term_data *td = (term_data*)(Term->data);

	int i;

	int x1, y1;

	unsigned char text[257];

	/* Location */
	x1 = x * td->tile_wid + td->x;
	y1 = y * td->tile_hgt + td->y;

	/* Erase old contents */
	Term_wipe_dos(x, y, n);

#ifdef USE_SPECIAL_BACKGROUND

	/* Show text in black in the message window */
	if (op_ptr->window_flag[td->number] & (PW_MESSAGE)) a = 0;

#endif /* USE_SPECIAL_BACKGROUND */

	/* No stretch needed */
	if (td->font_wid == td->tile_wid)
	{
		/* Copy the string */
		for (i = 0; i < n; ++i) text[i] = cp[i];

		/* Terminate */
		text[i] = '\0';

		/* Dump the text */
		textout(screen, td->font, text, x1, y1,
		       	COLOR_OFFSET + (a & 0x0F));
	}
	/* Stretch needed */
	else
	{
		/* Pre-Terminate */
		text[1] = '\0';

		/* Write the chars to the screen */
		for (i = 0; i < n; ++i)
		{
			/* Build a one character string */
			text[0] = cp[i];

			/* Dump some text */
			textout(screen, td->font, text, x1, y1,
		        	COLOR_OFFSET + (a & 0x0F));

			/* Advance */
			x1 += td->tile_wid;
		}
	}

	/* Success */
	return (0);
}


#ifdef USE_GRAPHICS

/*
 * Place some attr/char pairs on the screen
 *
 * The given parameters are "valid".
 *
 * To prevent crashes, we must not only remove the high bits of the
 * "ap[i]" and "cp[i]" values, but we must map the resulting value
 * onto the legal bitmap size, which is normally 32x32.  XXX XXX XXX
 */
static errr Term_pict_dos(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
{
	term_data *td = (term_data*)(Term->data);

	int i;

	int w, h;

	int x1, y1;
	int x2, y2;
	int x3, y3;

	/* Size */
	w = td->tile_wid;
	h = td->tile_hgt;

	/* Location (window) */
	x1 = x * w + td->x;
	y1 = y * h + td->y;

	/* Dump the tiles */
	for (i = 0; i < n; i++)
	{
		/* Location (bitmap) */
		x2 = (cp[i] & 0x7F) * w;
		y2 = (ap[i] & 0x7F) * h;

		x3 = (tcp[i] & 0x7F) * w;
		y3 = (tap[i] & 0x7F) * h;

		/* Blit the tile to the screen */
		blit(td->tiles, screen, x3, y3, x1, y1, w, h);

		/* Blit the tile to the screen */
		masked_blit(td->tiles, screen, x2, y2, x1, y1, w, h);

		/* Advance (window) */
		x1 += w;
	}

	/* Success */
	return (0);
}

#endif /* USE_GRAPHICS */


/*
 * Init a Term
 */
static void Term_init_dos(term *t)
{
	/* Unused parameter */
	(void)t;

	/* XXX Nothing */
}


/*
 * Nuke a Term
 */
static void Term_nuke_dos(term *t)
{
	term_data *td = (term_data*)(t->data);

	/* Free the terminal font */
	if (td->font)
	{
		destroy_font(td->font);
	}

#ifdef USE_GRAPHICS

	/* Free the terminal bitmap */
	if (td->tiles) destroy_bitmap(td->tiles);

#endif /* USE_GRAPHICS */
}



/*
 * Instantiate a "term_data" structure
 */
static void term_data_link(term_data *td)
{
	term *t = &td->t;

	/* Initialize the term */
	term_init(t, td->cols, td->rows, 255);

	/* Use a "software" cursor */
	t->soft_cursor = TRUE;

	/* Ignore the "TERM_XTRA_BORED" action */
	t->never_bored = FALSE;

	/* Ignore the "TERM_XTRA_FROSH" action */
	t->never_frosh = TRUE;

	/* Erase with "black space" */
	t->attr_blank = TERM_DARK;
	t->char_blank = ' ';

	/* Prepare the init/nuke hooks */
	t->init_hook = Term_init_dos;
	t->nuke_hook = Term_nuke_dos;

	/* Prepare the template hooks */
	t->xtra_hook = Term_xtra_dos;
	t->curs_hook = Term_curs_dos;
	t->wipe_hook = Term_wipe_dos;
	t->user_hook = Term_user_dos;
	t->text_hook = Term_text_dos;

#ifdef USE_GRAPHICS

	/* Prepare the graphics hook */
	t->pict_hook = Term_pict_dos;

	/* Use "Term_pict" for "graphic" data */
	t->higher_pict = TRUE;

#endif /* USE_GRAPHICS */

	/* Remember where we came from */
	t->data = (vptr)(td);
}


/*
 * Shut down visual system, then fall back into standard "quit()"
 */
static void dos_quit_hook(cptr str)
{
	int i;


	/* Unused parameter */
	(void)str;

	/* Destroy windows */
	for (i = MAX_TERM_DATA - 1; i >= 0; i--)
	{
		/* Unused */
		if (!angband_term[i]) continue;

		/* Nuke it */
		term_nuke(angband_term[i]);
	}


	/* Free all resources */
	if (cursor) destroy_bitmap(cursor);

#ifdef USE_BACKGROUND

	/* Free the background bitmaps */
	for (i = 0; i < 17; i++)
	{
		if (background[i]) destroy_bitmap(background[i]);
	}

#endif /* USE_BACKGROUND */


#ifdef USE_SOUND

	if (sound_initialized)
	{
		/* Destroy samples */
		for (i = 1; i < SOUND_MAX; i++)
		{
			int j;

			for (j = 0; j < sample_count[i]; j++)
			{
				if (samples[i][j]) destroy_sample(samples[i][j]);
			}
		}
	}

	/* Clear the old song */
	if (midi_song) destroy_midi(midi_song);
	midi_song =NULL;
# ifdef USE_MOD_FILES
	if (mod_file_initialized)
	{
		stop_mod();
		destroy_mod(mod_song);
	}
# endif /* USE_MOD_FILES */

#endif /* USE_SOUND */

	/* Shut down Allegro */
	allegro_exit();
}


/*
 * Dump the screen to "lib/user/dump.bmp"
 */
static void dos_dump_screen(void)
{
	/* Bitmap and palette of the screen */
	BITMAP *bmp;
	PALETTE pal;

	/* Filename */
	char filename[1024];

	/* Get bitmap and palette of the screen */
	bmp = create_sub_bitmap(screen, 0, 0, SCREEN_W, SCREEN_H);
	get_palette(pal);

	/* Build the filename for the screen-dump */
	path_make(filename, ANGBAND_DIR_USER, "dump.bmp");

	/* Save it */
	save_bmp(filename, bmp, pal);

	/* Free up the memory */
	if (bmp) destroy_bitmap(bmp);

	/* Success message */
	msgf("Screen dump saved.");
	message_flush();
}


/*
 * Initialize the terminal windows
 */
static bool init_windows(void)
{
	int i, num_windows;

	term_data *td;

	char section[80];

	char filename[1024];

	char buf[128];

	/* Section name */
	strnfmt(section, 80, "Mode-%d", resolution);

	/* Get number of windows */
	num_windows = get_config_int(section, "num_windows", 1);

	/* Paranoia */
	if (num_windows > MAX_TERM_DATA) num_windows = MAX_TERM_DATA;

	/* Init the terms */
	for (i = 0; i < num_windows; i++)
	{
		td = &data[i];
		WIPE(td, term_data);

		/* Section name */
		strnfmt(section, 80, "Term-%d-%d", resolution, i);

		/* Term number */
		td->number = i;

		/* Coordinates of left top corner */
		td->x = get_config_int(section, "x", 0);
		td->y = get_config_int(section, "y", 0);

		/* Rows and cols of term */
		td->rows = get_config_int(section, "rows", 24);
		td->cols = get_config_int(section, "cols", 80);

		/* Tile size */
		td->tile_wid = get_config_int(section, "tile_wid", 8);
		td->tile_hgt = get_config_int(section, "tile_hgt", 13);

		/* Font size */
		td->font_wid = get_config_int(section, "tile_wid", 8);
		td->font_hgt = get_config_int(section, "tile_hgt", 13);

		/* Get font filename */
		strcpy(buf, get_config_string(section, "font_file", "xm8x13.fnt"));

		/* Build the name of the font file */
		path_make(filename, xtra_font_dir, buf);

		/* Load a "*.dat" file */
		if (suffix(filename, ".dat"))
		{
			DATAFILE *fontdata;

			/* Load the font file */
			if (!(fontdata = load_datafile(filename)))
			{
				quit_fmt("Error reading font file '%s'", filename);
			}

			/* Save the font data */
			td->font = fontdata[1].dat;

			/* Unload the font file */
			unload_datafile_object(fontdata);
		}

		/* Oops */
		else
		{
			quit_fmt("Unknown suffix in font file '%s'", filename);
		}

		/* Link the term */
		term_data_link(td);
		angband_term[i] = &td->t;
	}

	/* Success */
	return 0;
}


#ifdef USE_BACKGROUND

/*
 * Initialize the window backgrounds
 */
static void init_background(void)
{
	int i;

	char filename[1024];

	char buf[128];

	PALLETE background_pallete;

	/* Get the backgrounds */
	for (i = 0; i < 16; i++)
	{
		/* Get background filename */
		strcpy(buf, get_config_string("Background", format("Background-%d", i), ""));

		/* Build the filename for the background-bitmap */
		path_make(filename, xtra_graf_dir, buf);

		/* Try to open the bitmap file */
		background[i] = load_bitmap(filename, background_pallete);
	}

#ifndef USE_SPECIAL_BACKGROUND
	/*
	 * Set the palette for the background
	 */
	if (background[0])
	{
		set_palette_range(background_pallete, 0, COLOR_OFFSET - 1, 0);
	}
#endif /* USE_SPECIAL_BACKGROUND */
}

#endif /* USE_BACKGROUND */


#ifdef USE_GRAPHICS

/*
 * Initialize graphics
 */
static bool init_graphics(void)
{
	char filename[1024];
	char section[80];
	char name_tiles[128];

	/* Large bitmap for the tiles */
	BITMAP *tiles = NULL;
	PALLETE tiles_pallete;

	/* Size of each bitmap tile */
	int bitmap_wid;
	int bitmap_hgt;

	int num_windows;

	if (!graphics_initialized)
	{
		/* Section name */
		strnfmt(section, 80, "Mode-%d", resolution);

		/* Get bitmap tile size */
		bitmap_wid = get_config_int(section, "bitmap_wid", 8);
		bitmap_hgt = get_config_int(section, "bitmap_hgt", 8);

		/* Get bitmap filename */
		strcpy(name_tiles, get_config_string(section, "bitmap_file", "8x8.bmp"));

		/* Get number of windows */
		num_windows = get_config_int(section, "num_windows", 1);

		/* Build the name of the bitmap file */
		path_make(filename, xtra_graf_dir, name_tiles);

		/* Open the bitmap file */
		if ((tiles = load_bitmap(filename, tiles_pallete)) != NULL)
		{
			int i;
			cptr graf_mode;
			

			/*
			 * Set the graphics mode to "new" if Adam Bolt's
			 * new 16x16 tiles are used.
			 */
			graf_mode = get_config_string(section, "graf-mode", "old");

			if (streq(graf_mode, "new"))
			{
				arg_graphics = GRAPHICS_ADAM_BOLT;

				/* Use transparent blits */
				use_transparency = TRUE;
			}
			else if (streq(graf_mode, "old"))
			{
				arg_graphics = GRAPHICS_ORIGINAL;
			}
			else
			{
				arg_graphics = GRAPHICS_NONE;
			}

			/* Select the bitmap pallete */
			set_palette_range(tiles_pallete, 0, COLOR_OFFSET - 1, 0);

			/* Prepare the graphics */
			for (i = 0; i < num_windows; i++)
			{
				term_data *td;

				int col, row;
				int cols, rows;
				int width, height;
				int src_x, src_y;
				int tgt_x, tgt_y;

				td = &data[i];

				cols = tiles->w / bitmap_wid;
				rows = tiles->h / bitmap_hgt;

				width = td->tile_wid * cols;
				height = td->tile_hgt * rows;

				/* Initialize the tile graphics */
				td->tiles = create_bitmap(width, height);

				for (row = 0; row < rows; ++row)
				{
					src_y = row * bitmap_hgt;
					tgt_y = row * td->tile_hgt;

					for (col = 0; col < cols; ++col)
					{
						src_x = col * bitmap_wid;
						tgt_x = col * td->tile_wid;

						stretch_blit(tiles, td->tiles,
							src_x, src_y,
							bitmap_wid, bitmap_hgt,
							tgt_x, tgt_y,
							td->tile_wid, td->tile_hgt);
					}
				}
			}

			/* Free the old tiles bitmap */
			if (tiles) destroy_bitmap(tiles);

			graphics_initialized = TRUE;

			/* Success */
			return (TRUE);
		}

		/* Failure */
		return (FALSE);
	}

	/* Success */
	return (TRUE);
}

#endif /* USE_GRAPHICS */

#ifdef USE_SOUND

/*
 * Initialize sound
 * We try to get a list of the available sound-files from "lib/xtra/sound/sound.cfg"
 * and then preload the samples. Every Angband-sound-event can have several samples
 * assigned. Angband will randomly select which is played. This makes it easy to
 * create "sound-packs", just copy wav-files into the "lib/xtra/sound/" folder and
 * add the filenames to "sound.cfg" in the same folder.
 */
static bool init_sound(void)
{
	int i, j, done;

	char section[128];
	char filename[1024];
	char **argv;

	struct ffblk f;

	if (sound_initialized) return (TRUE);

	reserve_voices(16, -1);

	/* Initialize Allegro sound */
	if (!install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL))
	{
#ifdef USE_MOD_FILES
		/*
		 * Try to enable support for MOD-, and S3M-files
		 * The parameter for install_mod() is the number
		 * of channels reserved for the MOD/S3M-file.
		 */
		if (install_mod(8) > 0) mod_file_initialized = TRUE;
#endif /* USE_MOD_FILES */

		/* Access the new sample */
		path_make(filename, xtra_sound_dir, "sound.cfg");

		/* Read config info from "lib/xtra/sound/sound.cfg" */
		override_config_file(filename);

		/* Sound section */
		strcpy(section, "Sound");

		/* Prepare the sounds */
		for (i = 1; i < SOUND_MAX; i++)
		{
			/* Get the sample names */
			argv = get_config_argv(section, (char *)angband_sound_name[i], &sample_count[i]);

			/* Limit the number of samples */
			if (sample_count[i] > SAMPLE_MAX) sample_count[i] = SAMPLE_MAX;

			for (j = 0; j < sample_count[i]; j++)
			{
				/* Access the new sample */
				path_make(filename, xtra_sound_dir, argv[j]);

				/* Load the sample */
				samples[i][j] = load_sample(filename);
			}
		}

		/*
		 * Get a list of music files
		 */
#ifdef USE_MOD_FILES
		if (mod_file_initialized)
		{
			done = findfirst(format("%s/*.*", xtra_music_dir), &f, FA_ARCH|FA_RDONLY);
		}
		else
#endif /* USE_MOD_FILES */
		done = findfirst(format("%s/*.mid", xtra_music_dir), &f, FA_ARCH|FA_RDONLY);


		while (!done && (song_number < MAX_SONGS))
		{
			/* Add music files */
			strncpy(music_files[song_number], f.ff_name, 15);
			music_files[song_number][15] = '\0';
			song_number++;

			done = findnext(&f);
		}

		/* Use "angdos.cfg" */
		override_config_file("angdos.cfg");

		/* Sound section */
		strcpy(section, "Sound");

		/* Get the volume setting */
		digi_volume = get_config_int(section, "digi_volume", 255);
		midi_volume = get_config_int(section, "midi_volume", 255);

		/* Set the volume */
		set_volume(digi_volume, midi_volume);

		/* Success */
		return (TRUE);
	}

	/* Init failed */
	return (FALSE);
}


/*
 * Make a sound
 */
static errr Term_xtra_dos_sound(int v)
{
	int n;

	/* Sound disabled */
	if (!use_sound) return (1);

	/* Illegal sound */
	if ((v < 0) || (v >= SOUND_MAX)) return (1);

	/* Get a random sample from the available ones */
	n = Rand_simple(sample_count[v]);

	/* Play the sound, catch errors */
	if (samples[v][n])
	{
		return (play_sample(samples[v][n], 255, 128, 1000, 0) == 0);
	}

	/* Oops */
	return (1);
}


/*
 * Play a song-file
 */
static void play_song(void)
{
	char filename[1024];

	/* Clear the old song */
	if (midi_song) destroy_midi(midi_song);
	midi_song = NULL;

#ifdef USE_MOD_FILES
	if (mod_file_initialized)
	{
		stop_mod();
		destroy_mod(mod_song);
	}
#endif /* USE_MOD_FILES */

	/* Access the new song */
	path_make(filename, xtra_music_dir, music_files[current_song - 1]);

	/* Load and play the new song */
	midi_song = load_midi(filename);

	if (midi_song)
	{
		play_midi(midi_song, 0);
	}
#ifdef USE_MOD_FILES
	else if (mod_file_initialized)
	{
		mod_song = load_mod(filename);

		if (mod_song) play_mod(mod_song, FALSE);
	}
#endif /* USE_MOD_FILES */
}

#endif /* USE_SOUND */


/*
 * Attempt to initialize this file
 *
 * Hack -- we assume that "blank space" should be "white space"
 * (and not "black space" which might make more sense).
 *
 * Note the use of "((x << 2) | (x >> 4))" to "expand" a 6 bit value
 * into an 8 bit value, without losing much precision, by using the 2
 * most significant bits as the least significant bits in the new value.
 *
 * We should attempt to "share" bitmaps (and fonts) between windows
 * with the same "tile" size.  XXX XXX XXX
 */
errr init_dos(void)
{
	term_data *td;

	char section[80];

	int screen_wid;
	int screen_hgt;

	/* Initialize the Allegro library (never fails) */
	if (allegro_init()) return (-1);

	/* Install timer support for music and sound */
	if (install_timer()) return (-1);

	/* Read config info from filename */
	set_config_file("angdos.cfg");

	/* Main section */
	strcpy(section, "Angband");

	/* Get screen size */
	resolution = get_config_int(section, "Resolution", 1);

	/* Section name */
	strnfmt(section, 80, "Mode-%d", resolution);

	/* Get the screen dimensions */
	screen_wid = get_config_int(section, "screen_wid", 640);
	screen_hgt = get_config_int(section, "screen_hgt", 480);

	/* Set the color depth */
	set_color_depth(8);

	/* Auto-detect, and instantiate, the appropriate graphics mode */
	if ((set_gfx_mode(GFX_AUTODETECT, screen_wid, screen_hgt, 0, 0)) < 0)
	{
		/*
		 * Requested graphics mode is not available
		 * We retry with the basic 640x480 mode
		 */
		resolution = 1;

		if ((set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0)) < 0)
		{
			char error_text[1024];

			/* Save the Allegro error description */
			strcpy(error_text, allegro_error);

			/* Shut down Allegro */
			allegro_exit();

			/* Print the error description */
			plog_fmt("Error selecting screen mode: %s", error_text);

			/* Failure */
			return (-1);
		}
	}

	/* Hook in "z-util.c" hook */
	quit_aux = dos_quit_hook;

	/* Build the "graf" path */
	path_make(xtra_graf_dir, ANGBAND_DIR_XTRA, "graf");

	/* Build the "font" path */
	path_make(xtra_font_dir, ANGBAND_DIR_XTRA, "font");

	/* Build the "sound" path */
	path_make(xtra_sound_dir, ANGBAND_DIR_XTRA, "sound");

	/* Build the "music" path */
	path_make(xtra_music_dir, ANGBAND_DIR_XTRA, "music");

	/* Initialize the windows */
	init_windows();

#ifdef USE_SOUND

	/* Look for the sound preferences in "angdos.cfg" */
	if (!arg_sound)
	{
		arg_sound = get_config_int("Angband", "Sound", TRUE);
	}

#endif /* USE_SOUND */

#ifdef USE_GRAPHICS

	/* Look for the graphic preferences in "angdos.cfg" */
	if (!arg_graphics)
	{
		arg_graphics = get_config_int("Angband", "Graphics", GRAPHICS_ORIGINAL);
	}

#endif /* USE_GRAPHICS */

	/* Initialize the "complex" RNG for the midi-shuffle function */
	Rand_quick = FALSE;
	Rand_state_init(time(NULL));

	/* Set the Angband colors/graphics/sound mode */
	Term_xtra_dos_react();

#ifdef USE_BACKGROUND

	/* Initialize the background graphics */
	init_background();

#endif /* USE_BACKGROUND */

	/* Clear the screen */
	clear_to_color(screen, COLOR_OFFSET + TERM_DARK);

	/* Main screen */
	td = &data[0];

	/* Build a cursor bitmap */
	cursor = create_bitmap(td->tile_wid, td->tile_hgt);

	/* Erase the cursor sprite */
	clear(cursor);

	/* Draw the cursor sprite (yellow rectangle) */
	rect(cursor, 0, 0, td->tile_wid - 1, td->tile_hgt - 1,
	     COLOR_OFFSET + TERM_YELLOW);

	/* Activate the main term */
	Term_activate(term_screen);

	/* Place the cursor */
	Term_curs_dos(0, 0);

#ifdef USE_BACKGROUND

	/* Use transparent text */
	text_mode(-1);

#endif /* USE_BACKGROUND */

	/* Success */
	return 0;
}

#endif /* USE_DOS */