4 // This sets screen to VESA 320x240 LFB so we can use the WHOLE laptop screen
5 // Now with VESA2 support!
6 // Also, support routines for video hardware emulation are included
10 // (C) 2003 Underground Software
12 // JLH = James L. Hammons <jlhamm@acm.org>
15 // --- ---------- ------------------------------------------------------------
16 // JLH 03/12/2003 Ported this steaming pile of crap from VESA to SDL
23 #include <string> // For memset()
26 // Private function prototypes
28 void DrawSprites(uint8 priority);
29 int FindPCXName(void);
30 static void DrawChar(uint8 * chr, uint8 * ram, uint8 sx, uint8 sy, uint16 scp, uint32 baseAddr, uint32 scrollOffset, bool transparent = true);
32 // Private global variables
34 uint8 my_scr[0x14000]; // Screen buffer...
35 uint8 palette[768]; // Screen palette
36 uint8 ccolor[256][8]; // Character colors
37 uint8 scolor[128][16]; // Sprite colors
38 bool charbase; // Character base pointer...
39 uint8 hScrollOffset; // Horizontal scroll offset
40 uint8 vScrollOffset; // Vertical scroll offset
41 uint8 spr_color_index; // Sprite color index
42 uint32 offsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; // Scroll offsets...
43 uint32 voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
45 extern bool show_text; // Whether or not to show text
46 extern bool show_scr; // Whether or not to show screen
49 // Render the NAMCO screen
51 void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
54 // Screen is 288 x 224 pixels, with character and independent sprites. Tiles are 36 x 28.
55 // There are four tile planes, three of them affected by the h/vscroll values, the last
56 // one is a static display (also has highest priority). Screens are 128 bytes wide by
62 uint32 sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4; // Adjust hscroll val
63 hScrollOffset = sc_base & 0x07; // Horiz. fine scroll offset
64 sc_base = (sc_base & 0xFFF8) >> 2; // Skip odds..
65 uint8 vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
66 vScrollOffset = ((ram[0x9002] + 1) & 0x07); // Vertical fine scroll amount
67 uint32 scp1 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
68 uint32 scp2 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
69 uint32 scp3 = 0x2180 | ((sc_base + 0x04) & 0x7F); /*0x2188;*/
71 scp1 += vsc_base * 0x80;
72 scp1 &= 0x0FFF; // Set vertical scroll addr
73 scp2 += vsc_base * 0x80;
74 scp2 = 0x1000 | (scp2 & 0x0FFF); // Set vertical scroll addr
75 scp3 += vsc_base * 0x80;
76 scp3 = 0x2000 | (scp3 & 0x0FFF); // Set vertical scroll addr
78 for(sy=0; sy<29; sy++)
80 for(sx=0; sx<37; sx++)
81 DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x20000 : 0x00000), 0, false);
84 scp1 = 0x0000 | (scp1 & 0x0FFF);
87 DrawSprites(0x40); // Draw sprites at lowest layer...
89 for(sy=0; sy<29; sy++)
91 for(sx=0; sx<37; sx++)
92 DrawChar(chr, ram, sx, sy, scp2, (charbase ? 0x30000 : 0x10000), 0);
95 scp2 = 0x1000 | (scp2 & 0x0FFF);
98 DrawSprites(0x80); // Draw sprites under layer #2...
100 for(sy=0; sy<29; sy++)
102 for(sx=0; sx<37; sx++)
103 DrawChar(chr, ram, sx, sy, scp3, 0x40000, 0);
106 scp3 = 0x2000 | (scp3 & 0x0FFF);
109 DrawSprites(0xC0); // Draw highest priority sprites...
111 for(sy=0; sy<28; sy++)
113 for(sx=0; sx<36; sx++)
114 DrawChar(chr, ram, sx, sy, scp, 0x50000, hScrollOffset + voffsets[vScrollOffset]);
121 DrawText(); // Draw a msg if needed...
124 DrawGUI(); // Show GUI if active...
126 if (SDL_LockSurface(scr) < 0)
128 // fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
132 // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not be...
134 // Doubled pixel blit (should be faster now!)
135 // uint8 * pMem = (uint8 *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
136 uint8 * pMem = (uint8 *)scr->pixels;
137 uint32 src = (uint32)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
138 dst1 = 0, dst2 = scr->pitch;
139 // uint32 srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
140 uint32 srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
142 for(int i=0; i<224; i++)
144 for (int j=0; j<288; j++)
146 pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = pMem[dst2 + 1] = my_scr[src++];
156 SDL_UnlockSurface(scr);
157 SDL_UpdateRect(scr, 0, 0, 0, 0);
161 // Draw character on screen
163 static inline void DrawChar(uint8 * chr, uint8 * ram, uint8 sx, uint8 sy, uint16 scp, uint32 baseAddr, uint32 scrollOffset, bool transparent/*= true*/)
165 uint8 scp_lo = (scp + (sx << 1)) & 0x7F;// Let LO byte wrap only...
166 uint16 sp2 = (scp & 0xFF80) | scp_lo;
167 uint8 tile = ram[sp2++];
168 uint8 index = ram[sp2] & 0x03;
169 uint8 color = ram[sp2];
170 uint32 chind = baseAddr + (((index << 8) + tile) * 64);
171 uint32 sc_addr = (sx * 8) + (sy * 2560) + scrollOffset; // Start addr in my_scr[]
173 for(int y=0; y<8; y++)
175 for(int x=0; x<8; x++)
180 my_scr[sc_addr] = ccolor[color][chr[chind]];
183 my_scr[sc_addr] = ccolor[color][chr[chind]];
189 sc_addr += 312; // Do next line of char...
194 // Draw sprites at priority level (new code)
196 void DrawSprites(uint8 priority)
198 extern uint8 gram1[]; // Game RAM space
200 for(uint16 i=0x5800; i<0x6000; i+=0x10)
202 if ((gram1[i + 8] & 0xC0) == priority) // Check for correct layer...
204 spr_color_index = gram1[i + 6] >> 1; // Set color...
205 uint16 x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
208 x -= 512; // Handle neg x values
210 uint16 y = 192 - gram1[i + 9];
211 uint16 hdr = (gram1[i + 4] & 0x90) << 8 | (gram1[i + 8] & 0x14);
212 uint8 flip = gram1[i + 4] & 0x20; // Horizontal flip
213 uint32 spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2);
215 Sprite(spr_num, x, y, flip, hdr); // Draw sprite...
223 void Sprite(uint32 sprnum, uint16 x, uint16 y, uint8 flip, uint16 spr_id)
225 extern uint8 spr_rom[];
226 // To show or not to show a 16x16 block in the 4x4 grid...
227 bool horiz_bl = false, vert_bl = false;
230 x += hScrollOffset; // Adjust x-coord
231 y += vScrollOffset; // Adjust y-coord
233 // 8421 8421 8421 8421
234 // 1xxx xxxx xxxx xxxx horiz_bl?
235 // xxxx xxxx xxxx x1xx vert_bl?
236 // xxxx xxxx xxxx x0xx y + 16?
237 // xxxB xxxx xxxA xxxx spr# (AB) to start
248 sprnum += ((spr_id & 0x1000) >> 12) + ((spr_id & 0x0010) >> 3);
250 if (spr_id == 0x8004)
252 horiz_bl = true; vert_bl = true;
254 if (spr_id == 0x8000)
256 horiz_bl = true; y += 16;
258 if (spr_id == 0x8010)
260 horiz_bl = true; y += 16; sprnum += 2;
262 if (spr_id == 0x0004)
266 if (spr_id == 0x1004)
268 vert_bl = true; sprnum++;
270 if (spr_id == 0x0000)
274 if (spr_id == 0x1000)
278 if (spr_id == 0x0010)
280 y += 16; sprnum += 2;
282 if (spr_id == 0x1010)
284 y += 16; sprnum += 3;
288 sprnum <<= 7; // 128 bytes per sprite
292 for(uint16 sy=0; sy<16; sy++)
294 for(uint16 sx=0; sx<16; sx+=2)
296 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
297 uint16 spy = y + sy, spx = x + sx; // Need to optimize this clipping!
299 if (spy > 223 || spx > 299)
302 sc_addr = spx + spy * 320;
305 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
310 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
316 for(uint16 sy=0; sy<16; sy++)
318 for(uint16 sx=16; sx<32; sx+=2)
320 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
321 uint16 spy = y + sy, spx = x + sx;
323 if (spy > 223 || spx > 299)
326 sc_addr = spx + spy * 320;
329 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
334 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
339 sprnum += 128; // Advance to next...
343 y += 16; // Do next row...
345 for(uint16 sy=0; sy<16; sy++)
347 for(uint16 sx=0; sx<16; sx+=2)
349 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
350 uint16 spy = y + sy, spx = x + sx;
352 if (spy > 223 || spx > 299)
355 sc_addr = spx + spy * 320;
358 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
363 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
369 for(uint16 sy=0; sy<16; sy++)
371 for(uint16 sx=16; sx<32; sx+=2)
373 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
374 uint16 spy = y + sy, spx = x + sx;
376 if (spy > 223 || spx > 299)
379 sc_addr = spx + spy * 320;
382 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
387 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
397 for(uint16 sy=0; sy<16; sy++)
399 for(uint16 sx=30; sx!=14; sx-=2)
401 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
402 uint16 spy = y+sy, spx = x+sx;
403 if ((spy>223) || (spx>299))
406 sc_addr = spx+spy*320;
408 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
411 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
415 for(uint16 sy=0; sy<16; sy++)
417 for(uint16 sx=14; sx!=0xFFFE; sx-=2)
419 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
420 uint16 spy = y+sy, spx = x+sx;
421 if ((spy>223) || (spx>299))
424 sc_addr = spx+spy*320;
426 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
429 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
432 if (!horiz_bl) sprnum += 128; // If single, skip sprite...
435 y += 16; // Adjust Y coord...
438 for(uint16 sy=0; sy<16; sy++)
440 for(uint16 sx=30; sx!=14; sx-=2)
442 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
443 uint16 spy = y+sy, spx = x+sx;
444 if ((spy>223) || (spx>299))
447 sc_addr = spx+spy*320;
449 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
452 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
456 for(uint16 sy=0; sy<16; sy++)
458 for(uint16 sx=14; sx!=0xFFFE; sx-=2)
460 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
461 uint16 spy = y+sy, spx = x+sx;
462 if ((spy>223) || (spx>299))
465 sc_addr = spx+spy*320;
467 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
470 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
479 static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
485 while (pcxNum < 10000)
487 sprintf(filename, "thnd%04i.pcx", pcxNum);
489 if ((fr = fopen(filename, "r")) == NULL)
490 return pcxNum; // file does not exist - we can create it
498 void SnapPCX(SDL_Surface * scr)
503 int XMax = 319; // Need to adjust this to handle 288 bytes per line
505 int bytesPerLine = 320;
507 if ((i = FindPCXName()) < 0)
510 sprintf(filename, "thnd%04i.pcx", i);
512 if ((fw = fopen(filename, "wb")) == NULL)
517 fputc(0xA, fw); // pcx signature
518 fputc(0x5, fw); // version 5
519 fputc(0x1, fw); // RLE encoding
520 fputc(0x8, fw); // bits per pixel
521 fputc(0, fw); fputc(0, fw); fputc(0, fw); fputc(0, fw); // XMin=0,YMin=0
522 fputc(XMax&0xFF, fw); fputc(XMax>>8, fw);
523 fputc(YMax&0xFF, fw); fputc(YMax>>8, fw);
524 fputc(0, fw); fputc(0, fw); fputc(0, fw); fputc(0,fw); // unknown DPI
525 for (i=0; i<48; i++) fputc(0, fw); // EGA color palette
526 fputc(0, fw); // reserved
527 fputc(1, fw); // number of bit planes
528 fputc(bytesPerLine&0xFF, fw); fputc(bytesPerLine>>8, fw);
529 fputc(1, fw); fputc(0, fw); // palette info - unused
530 fputc((XMax+1)&0xFF, fw); fputc((XMax+1)>>8, fw);
531 fputc((YMax+1)&0xFF, fw); fputc((YMax+1)>>8, fw); // screen resolution
532 for (i=0; i<54; i++) fputc(0, fw); // unused
534 // Instead of using the screen, we should use our internal buffer...
535 SDL_LockSurface(scr);
537 uint32 mem = scr->pitch * 8; // Skip first line... WAS:320*8;
538 for (line=0; line<=YMax; line++)
543 uint8 * pMem = (uint8 *)scr->pixels;
546 while (xpos < bytesPerLine)
551 while (pMem[mem] == last && xpos < bytesPerLine && count < 63)
553 mem++; count++; xpos++;
556 if (count > 1 || (last&0xC0) == 0xC0)
558 fputc(0xC0 | (count & 0x3F), fw); fputc(last & 0xFF, fw);
560 else fputc(last & 0xFF, fw);
562 mem += (scr->pitch - 320); // Handle non-standard line lengths...
565 SDL_UnlockSurface(scr);
570 for (i=0; i<768; i++)
571 fputc(palette[i], fw);
573 fclose(fw); // success!