Skip to content

Commit

Permalink
Codechange: Extend DrawSpriteToRgbaBuffer to work with 8bpp blitters.
Browse files Browse the repository at this point in the history
  • Loading branch information
michicc committed Dec 31, 2021
1 parent 582b706 commit 3623da9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/gfx.cpp
Expand Up @@ -1200,7 +1200,7 @@ std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zo
if (zoom < _settings_client.gui.zoom_min || zoom > _settings_client.gui.zoom_max) return nullptr;

Blitter *blitter = BlitterFactory::GetCurrentBlitter();
if (!blitter->Is32BppSupported()) return nullptr;
if (blitter->GetScreenDepth() != 8 && blitter->GetScreenDepth() != 32) return nullptr;

/* Gather information about the sprite to write, reserve memory */
const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
Expand All @@ -1221,11 +1221,29 @@ std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zo
dpi.height = dim.height;
dpi.zoom = zoom;

/* If the current blitter is a paletted blitter, we have to render to an extra buffer and resolve the palette later. */
std::unique_ptr<byte[]> pal_buffer{};
if (blitter->GetScreenDepth() == 8) {
pal_buffer.reset(new byte[dim.width * dim.height]);
MemSetT(pal_buffer.get(), 0, dim.width * dim.height);

dpi.dst_ptr = pal_buffer.get();
}

/* Temporarily disable screen animations while blitting - This prevents 40bpp_anim from writing to the animation buffer. */
Backup<bool> disable_anim(_screen_disable_anim, true, FILE_LINE);
GfxBlitter<1, true>(sprite, 0, 0, BM_NORMAL, nullptr, real_sprite, zoom, &dpi);
disable_anim.Restore();

if (blitter->GetScreenDepth() == 8) {
/* Resolve palette. */
uint32 *dst = result.get();
const byte *src = pal_buffer.get();
for (size_t i = 0; i < dim.height * dim.width; ++i) {
*dst++ = _cur_palette.palette[*src++].data;
}
}

return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/cocoa_wnd.mm
Expand Up @@ -179,7 +179,7 @@ static void CGDataFreeCallback(void *, const void *data, size_t)
/* Fetch the sprite and create a new bitmap */
Dimension dim = GetSpriteSize(sprite_id, nullptr, zoom);
std::unique_ptr<uint32[]> buffer = DrawSpriteToRgbaBuffer(sprite_id, zoom);
if (!buffer) return nullptr; // failed to blit sprite or we're using an 8bpp blitter.
if (!buffer) return nullptr; // Failed to blit sprite for some reason.

CFAutoRelease<CGDataProvider> data(CGDataProviderCreateWithData(nullptr, buffer.release(), dim.width * dim.height * 4, &CGDataFreeCallback));
if (!data) return nullptr;
Expand Down

0 comments on commit 3623da9

Please sign in to comment.