| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef SPRITECONVERTER_HH |
| 7 |
#define SPRITECONVERTER_HH |
| 8 |
|
| 9 |
#include "SpriteChecker.hh" |
| 10 |
|
| 11 |
#include "DisplayMode.hh" |
| 12 |
|
| 13 |
#include "narrow.hh" |
| 14 |
|
| 15 |
#include <cstdint> |
| 16 |
#include <ranges> |
| 17 |
#include <span> |
| 18 |
|
| 19 |
namespace openmsx { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class SpriteConverter |
| 24 |
{ |
| 25 |
public: |
| 26 |
using Pixel = uint32_t; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
explicit SpriteConverter(SpriteChecker& spriteChecker_, |
| 37 |
std::span<const Pixel, 16> pal) |
| 38 |
: spriteChecker(spriteChecker_) |
| 39 |
, palette(pal) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
void setTransparency(bool enabled) |
| 47 |
{ |
| 48 |
transparency = enabled; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void setDisplayMode(DisplayMode newMode) |
| 55 |
{ |
| 56 |
mode = newMode; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
void setPalette(std::span<const Pixel, 16> newPalette) |
| 65 |
{ |
| 66 |
palette = newPalette; |
| 67 |
} |
| 68 |
|
| 69 |
static bool clipPattern(int& x, SpriteChecker::SpritePattern& pattern, |
| 70 |
int minX, int maxX) |
| 71 |
{ |
| 72 |
if (int before = minX - x; before > 0) { |
| 73 |
if (before >= 32) { |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
pattern <<= before; |
| 78 |
x = minX; |
| 79 |
} |
| 80 |
if (int after = maxX - x; after < 32) { |
| 81 |
|
| 82 |
if (after <= 0) { |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
auto mask = narrow_cast<int>(0x8000'0000); |
| 87 |
pattern &= (mask >> (after - 1)); |
| 88 |
} |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void drawMode1(int absLine, int minX, int maxX, std::span<Pixel> pixelPtr) const |
| 100 |
{ |
| 101 |
|
| 102 |
auto visibleSprites = spriteChecker.getSprites(absLine); |
| 103 |
|
| 104 |
|
| 105 |
if (visibleSprites.empty()) return; |
| 106 |
|
| 107 |
|
| 108 |
for (const auto& si : std::views::reverse(visibleSprites)) { |
| 109 |
|
| 110 |
Pixel colIndex = si.colorAttrib & 0x0F; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if (colIndex == 0 && transparency) continue; |
| 115 |
Pixel color = palette[colIndex]; |
| 116 |
SpriteChecker::SpritePattern pattern = si.pattern; |
| 117 |
int x = si.x; |
| 118 |
|
| 119 |
if (!clipPattern(x, pattern, minX, maxX)) continue; |
| 120 |
|
| 121 |
Pixel* p = &pixelPtr[x]; |
| 122 |
while (pattern) { |
| 123 |
|
| 124 |
if (pattern & 0x8000'0000) { |
| 125 |
*p = color; |
| 126 |
} |
| 127 |
|
| 128 |
pattern <<= 1; |
| 129 |
p++; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
template<unsigned MODE> |
| 145 |
void drawMode2(int absLine, int minX, int maxX, std::span<Pixel> pixelPtr) const |
| 146 |
{ |
| 147 |
|
| 148 |
auto visibleSprites = spriteChecker.getSprites(absLine); |
| 149 |
|
| 150 |
|
| 151 |
if (visibleSprites.empty()) return; |
| 152 |
std::span visibleSpritesWithSentinel{visibleSprites.data(), |
| 153 |
visibleSprites.size() +1}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
int first = 0; |
| 158 |
do { |
| 159 |
if ((visibleSprites[first].colorAttrib & 0x40) == 0) [[likely]] { |
| 160 |
break; |
| 161 |
} |
| 162 |
++first; |
| 163 |
} while (first < int(visibleSprites.size())); |
| 164 |
for (int i = narrow<int>(visibleSprites.size() - 1); i >= first; --i) { |
| 165 |
const SpriteChecker::SpriteInfo& info = visibleSprites[i]; |
| 166 |
int x = info.x; |
| 167 |
SpriteChecker::SpritePattern pattern = info.pattern; |
| 168 |
|
| 169 |
if (!clipPattern(x, pattern, minX, maxX)) continue; |
| 170 |
uint8_t c = info.colorAttrib & 0x0F; |
| 171 |
if (c == 0 && transparency) continue; |
| 172 |
while (pattern) { |
| 173 |
if (pattern & 0x8000'0000) { |
| 174 |
uint8_t color = c; |
| 175 |
|
| 176 |
for (int j = i + 1; ; ++j) { |
| 177 |
const SpriteChecker::SpriteInfo& info2 = |
| 178 |
visibleSpritesWithSentinel[j]; |
| 179 |
if (!(info2.colorAttrib & 0x40)) break; |
| 180 |
unsigned shift2 = x - info2.x; |
| 181 |
if ((shift2 < 32) && |
| 182 |
((info2.pattern << shift2) & 0x8000'0000)) { |
| 183 |
color |= info2.colorAttrib & 0x0F; |
| 184 |
} |
| 185 |
} |
| 186 |
if constexpr (MODE == DisplayMode::GRAPHIC5) { |
| 187 |
Pixel pixL = palette[color >> 2]; |
| 188 |
Pixel pixR = palette[color & 3]; |
| 189 |
pixelPtr[x * 2 + 0] = pixL; |
| 190 |
pixelPtr[x * 2 + 1] = pixR; |
| 191 |
} else { |
| 192 |
Pixel pix = palette[color]; |
| 193 |
if constexpr (MODE == DisplayMode::GRAPHIC6) { |
| 194 |
pixelPtr[x * 2 + 0] = pix; |
| 195 |
pixelPtr[x * 2 + 1] = pix; |
| 196 |
} else { |
| 197 |
pixelPtr[x] = pix; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
++x; |
| 202 |
pattern <<= 1; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
private: |
| 208 |
SpriteChecker& spriteChecker; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
std::span<const Pixel, 16> palette; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
bool transparency; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
DisplayMode mode; |
| 221 |
}; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
#endif |