|
| 1 | +#include <Energia.h> |
| 2 | +#include <UTFT.h> |
| 3 | +#include <r65emu.h> |
| 4 | + |
| 5 | +#include "display.h" |
| 6 | + |
| 7 | +#include "roms/rom5e.h" // tiles |
| 8 | +#include "roms/rom5f.h" // sprites |
| 9 | +#include "roms/rom82s123_7f.h" // colours |
| 10 | +#include "roms/rom82s126_4a.h" // palette |
| 11 | + |
| 12 | +void Display::begin() { |
| 13 | + UTFTDisplay::begin(VGA_BLACK, VGA_WHITE, portrait); |
| 14 | + clear(); |
| 15 | + _xoff = (_dx - DISPLAY_WIDTH) / 2; |
| 16 | + _yoff = (_dy - DISPLAY_HEIGHT) / 2; |
| 17 | +} |
| 18 | + |
| 19 | +void Display::update() { |
| 20 | + draw_tiles(); |
| 21 | +} |
| 22 | + |
| 23 | +static void get_tile_palette(palette_entry &p, byte index) { |
| 24 | + index <<= 2; |
| 25 | + p.set_colour(colours[palette[index ]], 0); |
| 26 | + p.set_colour(colours[palette[index+1]], 1); |
| 27 | + p.set_colour(colours[palette[index+2]], 2); |
| 28 | + p.set_colour(colours[palette[index+3]], 3); |
| 29 | +} |
| 30 | + |
| 31 | +void Display::draw_sprite_slice(palette_entry &p, byte *cdata, int offset, int x, int y) { |
| 32 | + offset *= 32; |
| 33 | + for (int n = 7; n >= 0; n--) // 8 columns |
| 34 | + for (int m = 3; m >= 0; m--) { // 4 rows |
| 35 | + colour &c = p.colours[cdata[offset]]; |
| 36 | + utft.setColor(c.red, c.green, c.blue); |
| 37 | +// FIXME: investigate this |
| 38 | +if (_dx <= (x+n)) { |
| 39 | +//Serial.print("x offscreen: "); |
| 40 | +//Serial.println(x+n); |
| 41 | +continue; |
| 42 | +} |
| 43 | +if (_dy <= (y+m)) { |
| 44 | +//Serial.print("y offscreen: "); |
| 45 | +//Serial.println(y+m); |
| 46 | +continue; |
| 47 | +} |
| 48 | + |
| 49 | + utft.drawPixel(x+n, y+m); |
| 50 | + offset++; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void Display::draw_tile(word a, int x, int y) { |
| 55 | + byte tile = _tiles[a]; |
| 56 | + byte character[64]; |
| 57 | + for (unsigned i = 0; i < 16; i++) { |
| 58 | + byte b = tiles[tile*16 + i]; |
| 59 | + character[i*4 ] = (b & 0x01) | ((b >> 3) & 0x02); |
| 60 | + character[i*4+1] = ((b >> 1) & 0x01) | ((b >> 4) & 0x02); |
| 61 | + character[i*4+2] = ((b >> 2) & 0x01) | ((b >> 5) & 0x02); |
| 62 | + character[i*4+3] = ((b >> 3) & 0x01) | ((b >> 6) & 0x02); |
| 63 | + } |
| 64 | + |
| 65 | + palette_entry p; |
| 66 | + get_tile_palette(p, _tiles[a + 0x0400]); |
| 67 | + |
| 68 | + draw_sprite_slice(p, character, 0, x, y+4); |
| 69 | + draw_sprite_slice(p, character, 1, x, y); |
| 70 | +} |
| 71 | + |
| 72 | +void Display::_set(word a, byte b) { |
| 73 | + if (_tiles[a] != b) { |
| 74 | + _tiles[a] = b; |
| 75 | + int x, y; |
| 76 | + if (a >= 0x02 && a < 0x1e) { |
| 77 | + x = 27 - a+0x02; |
| 78 | + y = 34; |
| 79 | + } else if (a >= 0x22 && a < 0x3e) { |
| 80 | + x = 27 - a+0x22; |
| 81 | + y = 35; |
| 82 | + } else if (a >= 0x40 && a < 0x3c0) { |
| 83 | + word o = a - 0x40; |
| 84 | + x = 27 - o / 0x20; |
| 85 | + y = 2 + o % 0x20; |
| 86 | + } else if (a >= 0x3c2 && a < 0x3e0) { |
| 87 | + x = 27 - a+0x3c2; |
| 88 | + y = 0; |
| 89 | + } else if (a >= 0x3e2 && a < 0x400) { |
| 90 | + x = 27 - a+0x3e2; |
| 91 | + y = 1; |
| 92 | + } else |
| 93 | + return; |
| 94 | + draw_tile(a, 8*x + _xoff, 8*y + _yoff); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void Display::set_sprite(int n, byte sx, byte sy) { |
| 99 | + int x = DISPLAY_WIDTH - sx + 15 + _xoff; |
| 100 | + int y = DISPLAY_HEIGHT - sy - 16 + _yoff; |
| 101 | + byte sir = _mem[0x4ff0 + n*2]; |
| 102 | + |
| 103 | +/* |
| 104 | + Serial.print("sprite "); |
| 105 | + Serial.print(n); |
| 106 | + Serial.print(" "); |
| 107 | + Serial.print(sir); |
| 108 | + Serial.print(" "); |
| 109 | + Serial.print(sx); |
| 110 | + Serial.print(", "); |
| 111 | + Serial.println(sy); |
| 112 | +*/ |
| 113 | + |
| 114 | + byte character[256]; |
| 115 | + word si = 64*(sir >> 2); |
| 116 | + for (unsigned i = 0; i < 64; i++) { |
| 117 | + byte b = sprites[si + i]; |
| 118 | + character[i*4 ] = (b & 0x01) | ((b >> 3) & 0x02); |
| 119 | + character[i*4+1] = ((b >> 1) & 0x01) | ((b >> 4) & 0x02); |
| 120 | + character[i*4+2] = ((b >> 2) & 0x01) | ((b >> 5) & 0x02); |
| 121 | + character[i*4+3] = ((b >> 3) & 0x01) | ((b >> 6) & 0x02); |
| 122 | + } |
| 123 | + |
| 124 | + palette_entry p; |
| 125 | + get_tile_palette(p, _mem[0x4ff1 + n*2]); |
| 126 | + |
| 127 | + draw_sprite_slice(p, character, 0, x+8, y+12); |
| 128 | + draw_sprite_slice(p, character, 1, x+8, y); |
| 129 | + draw_sprite_slice(p, character, 2, x+8, y+4); |
| 130 | + draw_sprite_slice(p, character, 3, x+8, y+8); |
| 131 | + |
| 132 | + draw_sprite_slice(p, character, 4, x, y+12); |
| 133 | + draw_sprite_slice(p, character, 5, x, y); |
| 134 | + draw_sprite_slice(p, character, 6, x, y+4); |
| 135 | + draw_sprite_slice(p, character, 7, x, y+8); |
| 136 | + |
| 137 | + // FIXME rotation |
| 138 | +} |
0 commit comments