| 41 | std::vector<uint32_t> DC6::getFramePointers() const { return _framePointers; } |
| 42 | uint32_t DC6::getFrameCount() const { return _framesPerDirection; } |
| 43 | void DC6::setPalette(const Palette &palette) { |
| 44 | enum class eScanlineType { EndOfLine, RunOfTransparentPixels, RunOfOpaquePixels }; |
| 45 | auto scanlineType = [](const std::byte b) -> eScanlineType { |
| 46 | if (b == DC6EndOfScanline) |
| 47 | return eScanlineType::EndOfLine; |
| 48 | |
| 49 | if (static_cast<int>(b & DC6EndOfScanline) > 0) |
| 50 | return eScanlineType::RunOfTransparentPixels; |
| 51 | |
| 52 | return eScanlineType::RunOfOpaquePixels; |
| 53 | }; |
| 54 | |
| 55 | uint32_t textureWidth = 1; |
| 56 | uint32_t textureHeight = 1; |
| 57 | |
| 58 | for (const auto &frame : _frames) { |
| 59 | textureWidth += frame.getWidth(); |
| 60 | textureHeight = std::max(textureHeight, frame.getHeight()); |
| 61 | } |
| 62 | |
| 63 | _texture.reset(SDL_CreateTexture(Singletons::getRendererProvider().getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, |
| 64 | static_cast<int>(textureWidth), static_cast<int>(textureHeight))); |
| 65 | |
| 66 | if (!_texture) |
| 67 | throw std::runtime_error(SDL_GetError()); |
| 68 | |
| 69 | void *pixels; |
| 70 | int pitch; |
| 71 | if (SDL_LockTexture(_texture.get(), nullptr, &pixels, &pitch)) |
| 72 | throw std::runtime_error(SDL_GetError()); |
| 73 | |
| 74 | std::memset(pixels, 0, textureHeight * pitch); |
| 75 | |
| 76 | auto originX = 0; |
| 77 | for (const auto &frame : _frames) { |
| 78 | auto process = true; |
| 79 | auto x = originX; |
| 80 | auto y = frame.getHeight() - 1; |
| 81 | auto offset = 0; |
| 82 | |
| 83 | const auto &frameData = frame.getFrameData(); |
| 84 | while (process) { |
| 85 | const auto b = frameData[offset]; |
| 86 | offset++; |
| 87 | |
| 88 | switch (scanlineType(b)) { |
| 89 | case eScanlineType::EndOfLine: { |
| 90 | if (y == 0) { |
| 91 | process = false; |
| 92 | break; |
| 93 | } |
| 94 | y--; |
| 95 | x = originX; |
| 96 | break; |
| 97 | } |
| 98 | case eScanlineType::RunOfTransparentPixels: { |
| 99 | const auto &transparentPixels = b & DC6MaxRunLength; |
| 100 | x += static_cast<int>(transparentPixels); |