From f889ed089a7f09aac9140e9c101c114255f2d317 Mon Sep 17 00:00:00 2001 From: pelya Date: Sun, 12 Apr 2015 22:31:20 +0300 Subject: [PATCH] New GFX functions to draw cenetered sprites --- src/gfx.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/gfx_func.h | 3 +++ 2 files changed, 47 insertions(+) diff --git a/src/gfx.cpp b/src/gfx.cpp index ec45926a71..66fc835399 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -856,6 +856,50 @@ void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, } } +/** + * Draw a sprite, centered at x:y, not in a viewport + * @param img Image number to draw + * @param pal Palette to use. + * @param x Left coordinate of image in pixels + * @param y Top coordinate of image in pixels + * @param sub If available, draw only specified part of the sprite + * @param zoom Zoom level of sprite + */ +void DrawSpriteCentered(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom) +{ + Dimension size = GetSpriteSize(img, NULL, zoom); + DrawSprite(img, pal, x - size.width / 2, y - size.height / 2, sub, zoom); +} + +/** + * Draw a sprite, centered in rect, not in a viewport + * @param img Image number to draw + * @param pal Palette to use. + * @param left Left coordinate of image bounding box in pixels + * @param top Top coordinate of image bounding box in pixels + * @param right Right coordinate of image bounding box in pixels + * @param bottom Bottom coordinate of image bounding box in pixels + * @param sub If available, draw only specified part of the sprite + * @param zoom Zoom level of sprite + */ +void DrawSpriteCenteredInRect(SpriteID img, PaletteID pal, int left, int top, int right, int bottom, const SubSprite *sub, ZoomLevel zoom) +{ + DrawSpriteCentered(img, pal, (left + right) / 2, (top + bottom) / 2, sub, zoom); +} + +/** + * Draw a sprite, centered in rect, not in a viewport + * @param img Image number to draw + * @param pal Palette to use. + * @param rect Image bounding box in pixels + * @param sub If available, draw only specified part of the sprite + * @param zoom Zoom level of sprite + */ +void DrawSpriteCenteredInRect(SpriteID img, PaletteID pal, const Rect &rect, const SubSprite *sub, ZoomLevel zoom) +{ + DrawSpriteCenteredInRect(img, pal, rect.left, rect.top, rect.right, rect.bottom, sub, zoom); +} + /** * The code for setting up the blitter mode and sprite information before finally drawing the sprite. * @param sprite The sprite to draw. diff --git a/src/gfx_func.h b/src/gfx_func.h index f48e3610b3..91b764ad81 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -96,6 +96,9 @@ void GfxScroll(int left, int top, int width, int height, int xo, int yo); Dimension GetSpriteSize(SpriteID sprid, Point *offset = NULL, ZoomLevel zoom = ZOOM_LVL_GUI); void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = NULL); void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = NULL, ZoomLevel zoom = ZOOM_LVL_GUI); +void DrawSpriteCentered(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = NULL, ZoomLevel zoom = ZOOM_LVL_GUI); +void DrawSpriteCenteredInRect(SpriteID img, PaletteID pal, int left, int top, int right, int bottom, const SubSprite *sub = NULL, ZoomLevel zoom = ZOOM_LVL_GUI); +void DrawSpriteCenteredInRect(SpriteID img, PaletteID pal, const Rect &r, const SubSprite *sub = NULL, ZoomLevel zoom = ZOOM_LVL_GUI); /** How to align the to-be drawn text. */ enum StringAlignment {