Skip to content

Commit

Permalink
Fix OpenTTD#9743: [OSX] Don't try to render touchbar sprites with inv…
Browse files Browse the repository at this point in the history
…alid zoom level.
  • Loading branch information
michicc committed Dec 31, 2021
1 parent b0c41bf commit 299567e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 56 deletions.
30 changes: 16 additions & 14 deletions src/gfx.cpp
Expand Up @@ -1190,39 +1190,41 @@ static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mo
* Draws a sprite to a new RGBA buffer (see Colour union) instead of drawing to the screen.
*
* @param spriteId The sprite to draw.
* @param zoom The zoom level at which to draw the sprites.
* @return Pixel buffer, or nullptr if an 8bpp blitter is being used.
*/
std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId)
std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom)
{
/* Invalid zoom level requested? */
if (zoom < _settings_client.gui.zoom_min || zoom > _settings_client.gui.zoom_max) return nullptr;

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

/* Gather information about the sprite to write, reserve memory */
const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
const Sprite *sprite = GetSprite(real_sprite, ST_NORMAL);
std::unique_ptr<uint32[]> result(new uint32[sprite->width * sprite->height]);
Dimension dim = GetSpriteSize(real_sprite, nullptr, zoom);
std::unique_ptr<uint32[]> result(new uint32[dim.width * dim.height]);
/* Set buffer to fully transparent. */
MemSetT(result.get(), 0, dim.width * dim.height);

/* Prepare new DrawPixelInfo - Normally this would be the screen but we want to draw to another buffer here.
* Normally, pitch would be scaled screen width, but in our case our "screen" is only the sprite width wide. */
DrawPixelInfo dpi;
dpi.dst_ptr = result.get();
dpi.pitch = sprite->width;
dpi.pitch = dim.width;
dpi.left = 0;
dpi.top = 0;
dpi.width = sprite->width;
dpi.height = sprite->height;
dpi.zoom = ZOOM_LVL_NORMAL;

/* Zero out the allocated memory, there may be garbage present. */
uint32 *writeHead = (uint32*)result.get();
for (int i = 0; i < sprite->width * sprite->height; i++) {
writeHead[i] = 0;
}
dpi.width = dim.width;
dpi.height = dim.height;
dpi.zoom = zoom;

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

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gfx_func.h
Expand Up @@ -91,7 +91,7 @@ void GfxScroll(int left, int top, int width, int height, int xo, int yo);
Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId);
std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom = ZOOM_LVL_GUI);

int DrawString(int left, int right, int top, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
int DrawString(int left, int right, int top, const std::string &str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
Expand Down
2 changes: 0 additions & 2 deletions src/video/cocoa/cocoa_wnd.h
Expand Up @@ -90,8 +90,6 @@ static NSDictionary *touchBarFallbackText = @{
@interface OTTD_CocoaWindow : NSWindow
#ifdef HAVE_TOUCHBAR_SUPPORT
<NSTouchBarDelegate>

- (NSImage *)generateImage:(int)spriteId;
#endif

- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag driver:(VideoDriver_Cocoa *)drv;
Expand Down
71 changes: 32 additions & 39 deletions src/video/cocoa/cocoa_wnd.mm
Expand Up @@ -132,6 +132,37 @@ static NSUInteger CountUtf16Units(const char *from, const char *to)
return unicode_str;
}

static void CGDataFreeCallback(void *, const void *data, size_t)
{
delete[] (const uint32 *)data;
}

/**
* Render an OTTD sprite to a Cocoa image.
* @param sprite_id Sprite to make a NSImage from.
* @param zoom Zoom level to render the sprite in.
* @return Autorelease'd image or nullptr on any error.
*/
static NSImage *NSImageFromSprite(SpriteID sprite_id, ZoomLevel zoom)
{
if (!SpriteExists(sprite_id)) return nullptr;

/* 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.

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

CGBitmapInfo info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host;
CFAutoRelease<CGColorSpaceRef> color_space(CGColorSpaceCreateWithName(kCGColorSpaceSRGB));
CFAutoRelease<CGImage> bitmap(CGImageCreate(dim.width, dim.height, 8, 32, dim.width * 4, color_space.get(), info, data.get(), nullptr, false, kCGRenderingIntentDefault));
if (!bitmap) return nullptr;

return [ [ [ NSImage alloc ] initWithCGImage:bitmap.get() size:NSZeroSize ] autorelease ];
}


/**
* The main class of the application, the application's delegate.
Expand Down Expand Up @@ -433,48 +464,10 @@ - (nullable NSTouchBar *)makeTouchBar
return bar;
}

-(NSImage *)generateImage:(int)spriteId
{
if (!SpriteExists(spriteId)) {
return nullptr;
}

/* Fetch the sprite and create a new bitmap */
const Sprite *fullspr = GetSprite(spriteId, ST_NORMAL);
const std::unique_ptr<uint32[]> buffer = DrawSpriteToRgbaBuffer(spriteId);
if (!buffer) {
return nullptr; // failed to blit sprite or we're using an 8bpp blitter.
}

NSBitmapImageRep *bitmap = [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes:nil pixelsWide:fullspr->width pixelsHigh:fullspr->height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:0 ];

/* Copy the sprite to the NSBitmapImageRep image buffer */
const Colour *src = (const Colour *)buffer.get();
for (int y = 0; y < fullspr->height; y++) {
for (int x = 0; x < fullspr->width; x++) {
NSUInteger pixel[4];
pixel[0] = src->r;
pixel[1] = src->g;
pixel[2] = src->b;
pixel[3] = src->a;
[ bitmap setPixel:pixel atX:x y:y ];

src += 1;
}
}

/* Finally, convert the NSBitmapImageRep we created to a NSimage we can put on the button and clean up. */
NSImage *outImage = [ [ NSImage alloc ] initWithSize:NSMakeSize(fullspr->width, fullspr->height) ];
[ outImage addRepresentation:bitmap ];
[ bitmap release ];

return outImage;
}

- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
NSNumber *num = touchBarButtonSprites[identifier];
NSImage *image = [ self generateImage:num.unsignedIntValue ];
NSImage *image = NSImageFromSprite(num.unsignedIntValue, _settings_client.gui.zoom_min);

NSButton *button;
if (image != nil) {
Expand Down

0 comments on commit 299567e

Please sign in to comment.