]> Shamusworld >> Repos - thunder/blob - src/screen.cpp
Code cleanup, final fix for sprite lag problem.
[thunder] / src / screen.cpp
1 //
2 // Screen Handler
3 //
4 // This emulates the NAMCO tile/sprite hardware
5 //
6 // by James Hammons
7 // (C) 2003 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -----------------------------------------------------------
13 // JLH  03/12/2003  Ported this steaming pile of crap from VESA to SDL
14 // JLH  04/04/2014  Ported to SDL 2. Much less crappy.
15 //
16
17 #include "screen.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string>                     // For memset()
22 #include "gui.h"
23 #include "video.h"
24
25 // Private function prototypes
26
27 void DrawSprites(uint8_t priority);
28 int FindPCXName(void);
29 static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t baseAddr, bool transparent = true);
30 static void DrawChar(uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll, uint32_t yScroll, bool transparent = true);
31 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl);
32
33 // Private global variables
34
35 uint8_t my_scr[0x14000];                // Screen buffer...
36 uint32_t palette[256];                  // Screen palette
37 uint8_t ccolor[256][8];                 // Character colors
38 uint8_t scolor[128][16];                // Sprite colors
39 bool charBankSwitch;                    // Character bank switch
40 uint8_t spr_color_index;                // Sprite color index
41
42 extern bool show_text;                  // Whether or not to show text
43
44 //
45 // Render the NAMCO screen
46 //
47 void BlitChar(uint8_t * chr, uint8_t * ram)
48 {
49         // Screen structure:
50         //
51         // Each screen RAM is 4K in size, from lowest to highest priority:
52         // $0000-$0FFF, $1000-$1FFF, $2000-$2FFF, $3000-$3FFF
53         //
54         // Screen is 288 x 224 pixels, with character and independent sprites.
55         // Tiles are 36 x 28. There are four tile planes, all of them affected by
56         // their own h/vscroll values. Screens are 128 bytes wide by 32 bytes high.
57         //
58         // Also note that tiles are 16-bits wide, meaning the screen is really
59         // only 64 characters wide!
60
61         // Base address is $9000-1 (top 13 bits) + $9002 >> 3 << 7 (top 5 bits)
62 /*
63 $9000-2: $02 $0C $FF  0000 0010  0000 1100  1111 1111
64 $9004-6: $06 $0E $FF  0000 0110  0000 1110  1111 1111
65 $9400-2: $0A $0B $FF  0000 1010  0000 1011  1111 1111
66 $9404-6: $0E $0D $07  0000 1110  0000 1101  0000 0111
67                       ---- PP?H  HHHH Hhhh  VVVV Vvvv
68 ? = refresh layer bit?
69 PP = which 4K block to write to?
70 */
71         uint16_t screenX0 = (((ram[0x9000] << 8) | ram[0x9001]) + 20) & 0x1FF;
72         uint16_t screenX1 = (((ram[0x9004] << 8) | ram[0x9005]) + 18) & 0x1FF;
73         uint16_t screenX2 = (((ram[0x9400] << 8) | ram[0x9401]) + 21) & 0x1FF;
74         uint16_t screenX3 = (((ram[0x9404] << 8) | ram[0x9405]) + 19) & 0x1FF;
75
76         uint16_t screenY0 = ram[0x9002] + 25;
77         uint16_t screenY1 = ram[0x9006] + 25;
78         uint16_t screenY2 = ram[0x9402] + 25;
79         uint16_t screenY3 = ram[0x9406] + 25;
80
81         uint16_t ramBlock0 = (ram[0x9000] >> 2) & 0x03;
82         uint16_t ramBlock1 = (ram[0x9004] >> 2) & 0x03;
83         uint16_t ramBlock2 = (ram[0x9400] >> 2) & 0x03;
84         uint16_t ramBlock3 = (ram[0x9404] >> 2) & 0x03;
85
86         DrawScreen(ramBlock0, screenX0, screenY0, (charBankSwitch ? 2 : 0), false);
87         DrawSprites(0x40);
88         DrawScreen(ramBlock1, screenX1, screenY1, (charBankSwitch ? 3 : 1));
89         DrawSprites(0x80);
90         DrawScreen(ramBlock2, screenX2, screenY2, 4);
91         DrawSprites(0xC0);
92         DrawScreen(ramBlock3, screenX3, screenY3, 5);
93
94         // Draw a msg if needed...
95         if (show_text)
96                 DrawText();
97
98         // Show GUI if active...
99         if (ShowGUI())
100                 DrawGUI();
101
102         // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not
103         // be... (and we don't have to care about that, the OpenGL backend takes
104         // care of it.)
105         for(uint32_t i=0; i<VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT; i++)
106                 scrBuffer[i] = palette[my_scr[i]];
107
108         RenderScreenBuffer();
109 }
110
111 //
112 // Render tilemap
113 //
114 static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t tileBase, bool transparent/*= true*/)
115 {
116         uint16_t ramBase = (ramBlock << 12) | ((yAddress << 4) & 0xF80)
117                 | ((xAddress >> 2) & 0x7E);
118
119         for(uint8_t sy=0; sy<29; sy++)
120         {
121                 for(uint8_t sx=0; sx<37; sx++)
122                         DrawChar(sx, sy, ramBase, tileBase << 16, xAddress & 0x07, yAddress & 0x07, transparent);
123         }
124 }
125
126 //
127 // Draw character on screen
128 //
129 static inline void DrawChar(uint8_t sx, uint8_t sy, uint16_t ramBase, uint32_t tileBase, uint32_t xScroll, uint32_t yScroll, bool transparent/*= true*/)
130 {
131         extern uint8_t charROM[];
132         extern uint8_t gram1[];
133
134         // Calculate address in RAM of tile to draw
135         uint16_t addr = (ramBase & 0xF000) | ((ramBase + (sy << 7)) & 0x0F80)
136                 | ((ramBase + (sx << 1)) & 0x7F);
137         uint16_t tile  = ((gram1[addr + 1] << 8) | gram1[addr]) & 0x03FF;
138         // Yes, it really requires all 8 bits... even though the bottom 3 are used
139         // as a tile address!
140         uint8_t color = gram1[addr + 1];
141         uint32_t chind = tileBase + (tile << 6);
142
143         int xStart = (int)(sx * 8) - xScroll;
144         int yStart = (int)(sy * 8) - yScroll;
145         int32_t sc_addr = xStart + (yStart * 288);
146
147         for(int y=0; y<8; y++)
148         {
149                 for(int x=0; x<8; x++)
150                 {
151                         // Clipping...
152                         if (((xStart + x) < 0) || ((xStart + x) >= 288)
153                                 || ((yStart + y) < 0) || ((yStart + y) >= 224))
154                         {
155                                 sc_addr++;
156                                 chind++;
157                                 continue;
158                         }
159
160                         if (!(transparent && (charROM[chind] == 7)))
161                                 my_scr[sc_addr] = ccolor[color][charROM[chind]];
162
163                         sc_addr++;
164                         chind++;
165                 }
166
167                 sc_addr += (288 - 8);           // Do next line of char...
168         }
169 }
170
171 //
172 // Copy sprites in sprite RAM from positions 4-9 to 10-15
173 //
174 void CopySprites(void)
175 {
176         extern uint8_t gram1[];
177
178         for(uint16_t i=0x5800; i<0x6000; i+=0x10)
179         {
180                 for(uint16_t j=4; j<=9; j++)
181                 {
182                         gram1[i + j + 6] = gram1[i + j];
183                 }
184         }
185 }
186
187 //
188 // Draw sprites at priority level
189 // We read from the sprite copy RAM to render sprites, as that seems to be the
190 // way the real H/W does it.
191 //
192 void DrawSprites(uint8_t priority)
193 {
194 // Sprite blocks:
195 //
196 // Offset  Note
197 // ------  --------------------------------------------------------------------
198 // 4 (10)  h.fb .nnn (f = horz. flip, h = horz. expand, b = sprite offset lo
199 //         bit, nnn = upper bits of sprite #)
200 // 5 (11)  Lower 7 bits of sprite #
201 // 6 (12)  Sprite color index (top 7 bits only), bottom bit is bit 8 of X
202 //         position
203 // 7 (13)  Sprite X position (bits 0-7)
204 // 8 (14)  Top two bits are sprite priority, bits 4 & 2 are sprite offset hi
205 //         bit, vert. expand
206 // 9 (15)  Sprite Y position (192 - value)
207
208         extern uint8_t gram1[];                                 // Game RAM space
209
210         for(uint16_t i=0x5800; i<0x6000; i+=0x10)
211         {
212                 uint8_t * sprRAM = &gram1[i];
213
214                 // Skip sprite if it's not on the correct layer...
215                 if ((sprRAM[14] & 0xC0) != priority)
216                         continue;
217
218                 spr_color_index = sprRAM[12] >> 1;      // Set color...
219                 uint16_t x = ((sprRAM[12] & 0x01) << 8) | sprRAM[13];
220
221                 if (x > 512 - 32)
222                         x -= 512;                                               // Handle neg x values
223
224                 uint16_t y = 192 - sprRAM[15];
225                 uint8_t horzFlip = sprRAM[10] & 0x20;
226                 uint32_t spr_num = ((sprRAM[10] & 0x07) << 9)
227                         | ((sprRAM[11] & 0x7F) << 2)
228                         | ((sprRAM[10] & 0x10) >> 4)
229                         | ((sprRAM[14] & 0x10) >> 3);
230
231                 // Draw the sprite...
232                 Sprite(spr_num, x, y, horzFlip, sprRAM[10] & 0x80, sprRAM[14] & 0x04);
233         }
234 }
235
236 static inline void DrawSpriteBlock(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
237 {
238         extern uint8_t spr_rom[];
239
240         for(uint16_t sy=0; sy<16; sy++)
241         {
242                 for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
243                 {
244                         uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
245                         uint16_t spy = y + sy, spx = x + sx;    // Need to optimize this clipping! [eh?]
246
247                         // This handles negative values, by casting as unsigned
248                         uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
249
250                         if (b1 != 15)
251                                 my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
252
253                         sc_addr++;
254
255                         if (b2 != 15)
256                                 my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
257                 }
258         }
259 }
260
261 static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
262 {
263         extern uint8_t spr_rom[];
264
265         for(uint16_t sy=0; sy<16; sy++)
266         {
267                 for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
268                 {
269                         uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
270                         uint16_t spy = y + sy, spx = x + sx;    // Need to optimize this clipping! [eh?]
271
272                         // This handles negative values, by casting as unsigned
273                         uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
274
275                         if (b2 != 15)
276                                 my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
277
278                         sc_addr++;
279
280                         if (b1 != 15)
281                                 my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
282                 }
283         }
284 }
285
286 //
287 // Sprite handler
288 //
289 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
290         uint8_t horiz_bl, uint8_t vert_bl)
291 {
292         // 128 bytes per sprite (16 x 16 chunks, 4 bits per pixel)
293         sprnum <<= 7;
294
295         if (!vert_bl)
296                 y += 16;
297
298         if (!flip)
299         {
300                 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
301
302                 if (horiz_bl)
303                         DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
304                 else
305                         sprnum += 128;  // Advance to next...
306
307                 if (vert_bl)
308                 {
309                         y += 16;                // Do next row...
310
311                         DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
312
313                         if (horiz_bl)
314                                 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
315                 }
316         }
317         else    // Flip
318         {
319                 if (horiz_bl)
320                         DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
321
322                 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
323
324                 if (!horiz_bl)
325                         sprnum += 128;  // If single, skip sprite...
326
327                 if (vert_bl)
328                 {
329                         y += 16;                // Adjust Y coord...
330
331                         if (horiz_bl)
332                                 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
333
334                         DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
335                 }
336         }
337 }
338
339 int FindPCXName(void)
340 {
341         static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
342         char filename[30];
343         FILE * fr;
344
345         pcxNum++;
346
347         while (pcxNum < 10000)
348         {
349                 sprintf(filename, "thnd%04i.pcx", pcxNum);
350
351                 // file does not exist - we can create it
352                 if ((fr = fopen(filename, "r")) == NULL)
353                         return pcxNum;
354
355                 pcxNum++;
356         }
357
358         return -1;
359 }
360
361 void SavePCXSnapshot(void)
362 {
363         char filename[30];
364         int xMax = 287;
365         int yMax = 223;
366         int bytesPerLine = 288;
367         int i = FindPCXName();
368
369         if (i < 0)
370                 return;
371
372         sprintf(filename, "thnd%04i.pcx", i);
373         FILE * fw = fopen(filename, "wb");
374
375         if (fw == NULL)
376                 return;
377
378         // Write the header
379
380         fputc(0x0A, fw);        // PCX signature
381         fputc(0x05, fw);        // Version 5
382         fputc(0x01, fw);        // RLE encoding
383         fputc(0x08, fw);        // Bits per pixel
384         fputc(0, fw);
385         fputc(0, fw);
386         fputc(0, fw);
387         fputc(0, fw);           // XMin=0,YMin=0
388         fputc(xMax & 0xFF, fw);
389         fputc(xMax >> 8, fw);
390         fputc(yMax & 0xFF, fw);
391         fputc(yMax >> 8, fw);
392         fputc(0, fw);
393         fputc(0, fw);
394         fputc(0, fw);
395         fputc(0, fw);           // Unknown DPI
396
397         // EGA color palette
398         for(i=0; i<48; i++)
399                 fputc(0, fw);
400
401         fputc(0, fw);           // Reserved
402         fputc(1, fw);           // Number of bit planes
403         fputc(bytesPerLine & 0xFF, fw);
404         fputc(bytesPerLine >> 8, fw);
405         fputc(1, fw);
406         fputc(0, fw);           // Palette info - unused
407         fputc((xMax + 1) & 0xFF, fw);
408         fputc((xMax + 1) >> 8, fw);
409         fputc((yMax + 1) & 0xFF, fw);
410         fputc((yMax + 1) >> 8, fw);     // Screen resolution
411
412         // Unused
413         for (i=0; i<54; i++)
414                 fputc(0, fw);
415
416         uint8_t * mem = my_scr;
417
418         for(int line=0; line<=yMax; line++)
419         {
420                 int xpos = 0;
421
422                 while (xpos < bytesPerLine)
423                 {
424                         uint8_t count = 1;
425                         uint8_t last = *mem;
426                         mem++;
427                         xpos++;
428
429                         while ((*mem == last) && (xpos < bytesPerLine) && (count < 63))
430                         {
431                                 mem++;
432                                 count++;
433                                 xpos++;
434                         }
435
436                         if ((count > 1) || ((last & 0xC0) == 0xC0))
437                         {
438                                 fputc(0xC0 | (count & 0x3F), fw);
439                                 fputc(last & 0xFF, fw);
440                         }
441                         else
442                                 fputc(last & 0xFF, fw);
443                 }
444         }
445
446         // Write out the palette
447         fputc(0x0C, fw);
448
449         for(int i=0; i<256; i++)
450         {
451                 fputc(palette[i] & 0xFF, fw);
452                 fputc((palette[i] >> 8) & 0xFF, fw);
453                 fputc((palette[i] >> 16) & 0xFF, fw);
454         }
455
456         // Success!
457         fclose(fw);
458 }