]> Shamusworld >> Repos - thunder/blob - test/rthack5.cpp
Fixes to makefile, file permissions.
[thunder] / test / rthack5.cpp
1 // Rolling Thunder hacker #5: more character graphics
2 //
3 // I *think* that the ROMs R17-R20 are character graphics data encoded:
4 //
5 //   BYTE 1: char # (0-255)
6 //   BYTE 2: Top 3 bits = charset index (i.e. idx*256)
7 //
8
9 #include <fstream.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <iomanip.h>
13 #include <stdio.h>
14 #include <conio.h>
15 #include <dos.h>
16 #include <new.h>
17 #include "screen.h"
18
19 // With C++ string constants, you need to use a double backslash in
20 // order to indicate a single backslash.  Confusing?  You bet!
21 // [It's called "escaping the escape character."]
22
23 #define fn1 "rt1-7.rom"
24 #define fn2 "rt1-8.rom"
25 #define fn3 "rt1-5.rom"
26 #define fn4 "rt1-6.rom"
27
28 // Global shit (shouldn't be, but I can't think of an alternative)
29
30 fstream ff, ff2, ff3, ff4, ff5;  // Creates FSTREAMs without any file hooks
31 char * charset, * cs2, * cs3;     // Place holders for our charsets
32
33 int main(int argc, char *argv[])
34 {
35   int idx[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
36   char pal[768], file[120], scr[2016];
37   unsigned char ch, ch2;
38   int x, y, index = 1, scridx, chrsel = 0;
39   long i;
40   extern bool transparent;
41   transparent = false;
42
43   strcpy(file, "rt1-");
44
45   if (argc == 2)
46   {
47     strcat(file, argv[1]);  strcat(file, ".rom");
48     if (atoi(argv[1]) > 18)  file[2] = '3';
49   }
50   else
51   {
52     strcat(file, "17.rom");
53   }
54
55   set_new_handler(0);          // Make 'new' return NULL on failure...
56   charset = new char[0xC000];  // charspace
57   if (!charset)  cerr << "Could not allocate character RAM!";
58   cs2 = new char[0xC000];      // charspace
59   if (!cs2)  cerr << "Could not allocate character RAM!";
60   cs3 = new char[0xC000];      // charspace
61   if (!cs3)  cerr << "Could not allocate character RAM!";
62
63   ff.open(fn1, ios::binary|ios::in);   // Open as a binary file
64   if (!ff)   { cerr << "Could not open 'ff'";   // Handle any errors...
65                return -1; }
66   ff2.open(fn2, ios::binary|ios::in);  // ditto
67   if (!ff2)  { cerr << "Could not open 'ff2'";
68                return -1; }
69   ff4.open(fn3, ios::binary|ios::in);   // Open as a binary file
70   if (!ff)   { cerr << "Could not open 'ff'";   // Handle any errors...
71                return -1; }
72   ff5.open(fn4, ios::binary|ios::in);  // ditto
73   if (!ff2)  { cerr << "Could not open 'ff2'";
74                return -1; }
75   ff3.open(file, ios::binary|ios::in);  // ditto
76   if (!ff3)  { cerr << "Could not open '" << file << "'";
77                return -1; }
78
79   for(i=0; i<0xC000; i+=3)
80   {
81     ff.get(ch);   charset[i]   = ch;
82     ff.get(ch);   charset[i+1] = ch;
83     ff2.get(ch);  charset[i+2] = ch;
84   }
85   for(i=0; i<0xC000; i+=3)
86   {
87     ff.get(ch);   cs2[i]   = ch;
88     ff.get(ch);   cs2[i+1] = ch;
89     ff2.get(ch);  cs2[i+2] = ch;
90   }
91   for(i=0; i<0xC000; i+=3)
92   {
93     ff4.get(ch);  cs3[i]   = ch;
94     ff4.get(ch);  cs3[i+1] = ch;
95     ff5.get(ch);  cs3[i+2] = ch;
96   }
97
98   ff.close();  ff2.close();  ff4.close();  ff5.close();  // Close da filez...
99
100 //  for(i=0; i<0xC000; i++)  cout << (unsigned int)charset[i] << " ";
101
102   SetModeX();  // Set up VGA screen
103
104   pal[0]  = 0x00;  pal[1]  = 0x00;  pal[2]  = 0x00;  // Black
105   pal[3]  = 0x30;  pal[4]  = 0x00;  pal[5]  = 0x00;  // Darker Red
106   pal[6]  = 0x28;  pal[7]  = 0x00;  pal[8]  = 0x00;  // Darkest Red
107   pal[9]  = 0x2f;  pal[10] = 0x2f;  pal[11] = 0x2F;  // Light Grey
108   pal[12] = 0x1F;  pal[13] = 0x1f;  pal[14] = 0x1f;  // Med Grey
109   pal[15] = 0x0F;  pal[16] = 0x0f;  pal[17] = 0x0f;  // Dark Grey
110   pal[18] = 0x3F;  pal[19] = 0x30;  pal[20] = 0x20;  // Peach?
111   pal[21] = 0x20;  pal[22] = 0x20;  pal[23] = 0x00;
112   pal[24] = 0x08;  pal[25] = 0x3f;  pal[26] = 0x10;  
113   pal[27] = 0x06;  pal[28] = 0x06;  pal[29] = 0x06;  // Dk Dk Dk Grey
114   pal[30] = 0x08;  pal[31] = 0x08;  pal[32] = 0x10;  // Dk Dk Grey
115   pal[33] = 0x00;  pal[34] = 0x3f;  pal[35] = 0x00;
116
117   pal[45] = 0x00;  pal[46] = 0x00;  pal[47] = 0x00;
118
119   outp(0x03C8, 0);                  // Tell VGA palette data is coming...
120   for(i=0; i<48; i++)
121   {
122     outp(0x03C9, pal[i]);     // Send it...
123   }
124
125   for(i=0; i<2016; i++)  { ff3.get(ch);  scr[i] = ch; }
126   while (1)
127   {
128     scridx = 0;
129     for(x=0; x<36; x+=4)
130     {
131       for(y=0; y<28; y++)
132       {
133         for(int q=0; q<4; q++)
134         {
135           ch = scr[scridx++];  index = (int)((scr[scridx++] >> 5) & 0x07);
136           if (chrsel == 0)
137             PlotChar(x+q, y, ch, idx[index], (unsigned char *)charset);
138           if (chrsel == 1)
139             PlotChar(x+q, y, ch, idx[index], (unsigned char *)cs2);
140           if (chrsel == 2)
141             PlotChar(x+q, y, ch, idx[index], (unsigned char *)cs3);
142         }
143       }
144     }
145     i = getch();
146     if (i==27)  break;  // Break out on ESC
147     if (i==32)  { for(i=0; i<2016; i++)  { ff3.get(ch);  scr[i] = ch; } }
148     if (i=='[') { int temp = idx[0];  for(i=0; i<7; i++)  idx[i] = idx[i+1];
149                   idx[7] = temp; }
150     if (i==']') { chrsel++;  if (chrsel==3)  chrsel = 0; }
151   }
152   SetMode(0x03);        // Reset text mode
153   ff3.close();
154
155   scridx = 0;
156   for(i=0; i<2016; i++)
157   {  printf("%02X ", (unsigned char)scr[i]);
158      scridx++;  if (scridx == 24)  {cout << endl; scridx = 0;}
159   }
160   delete[] charset;  delete[] cs2;  delete[] cs3;
161 }