From ec66a3dbf34f6d875d5592d9267ef8067ca78373 Mon Sep 17 00:00:00 2001 From: przemek83 <4788832+przemek83@users.noreply.github.com> Date: Fri, 13 Sep 2024 17:10:45 +0200 Subject: [PATCH] Some simplifications in Screen class. --- src/Menu.cpp | 4 ++-- src/Screen.cpp | 55 +++++++++++++++++++++++++++----------------------- src/Screen.h | 7 +++++-- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Menu.cpp b/src/Menu.cpp index 14617b7..a1738ec 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -169,13 +169,13 @@ int Menu::getLocationOfFirstItem() const int Menu::getItemWidth() const { return std::max(screen_.getWidth() / 3, - screen_.getBitmapWidth(ResourceType::MENU_ITEM)); + screen_.getResourceWidth(ResourceType::MENU_ITEM)); } int Menu::getItemHeight() const { return std::max(screen_.getHeight() / 10, - screen_.getBitmapHeight(ResourceType::MENU_ITEM)); + screen_.getResourceHeight(ResourceType::MENU_ITEM)); } std::pair Menu::getItemPosition(int item) const diff --git a/src/Screen.cpp b/src/Screen.cpp index 0e763d3..100e618 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -27,11 +27,9 @@ void Screen::init() ::abort(); } - const int screenWidth{ - static_cast(Config::getInstance().getBoardWidth() + - Config::getInstance().getSatusWidth())}; - const int screenHeight{ - static_cast(Config::getInstance().getBoardHeight())}; + const int screenWidth{Config::getInstance().getBoardWidth() + + Config::getInstance().getSatusWidth()}; + const int screenHeight{Config::getInstance().getBoardHeight()}; if (::al_create_display(screenWidth, screenHeight) == nullptr) { std::cerr << "failed to create display!\n"; @@ -74,11 +72,9 @@ void Screen::drawTextWithBackground(int x, int y, const std::string& text) const void Screen::drawBackground(ResourceType resourceType) const { - ALLEGRO_BITMAP* bitmapToUse{resources_.getBitmap(resourceType)}; + ALLEGRO_BITMAP* bitmap{resources_.getBitmap(resourceType)}; ::al_draw_scaled_bitmap( - bitmapToUse, 0., 0., - static_cast(::al_get_bitmap_width(bitmapToUse)), - static_cast(::al_get_bitmap_height(bitmapToUse)), 0., 0., + bitmap, 0., 0., getBitmapWidth(bitmap), getBitmapHeight(bitmap), 0., 0., static_cast(getWidth()), static_cast(getHeight()), 0); } @@ -91,27 +87,27 @@ void Screen::drawScaledSquareBitmap(ResourceType resourceType, int x, int y, void Screen::drawScaledBitmap(ResourceType resourceType, int x, int y, int width, int height) const { - ALLEGRO_BITMAP* bitmapToUse{resources_.getBitmap(resourceType)}; - ::al_draw_scaled_bitmap( - bitmapToUse, 0, 0, - static_cast(::al_get_bitmap_width(bitmapToUse)), - static_cast(::al_get_bitmap_height(bitmapToUse)), - static_cast(x), static_cast(y), static_cast(width), - static_cast(height), 0); + ALLEGRO_BITMAP* bitmap{resources_.getBitmap(resourceType)}; + ::al_draw_scaled_bitmap(bitmap, 0, 0, getBitmapWidth(bitmap), + getBitmapHeight(bitmap), static_cast(x), + static_cast(y), static_cast(width), + static_cast(height), 0); } void Screen::drawScaledBitmapWithRotation(ResourceType resourceType, int x, int y, int size, int degrees) const { - ALLEGRO_BITMAP* bitmapToUse{resources_.getBitmap(resourceType)}; - const auto width = static_cast(::al_get_bitmap_width(bitmapToUse)); + ALLEGRO_BITMAP* bitmap{resources_.getBitmap(resourceType)}; + const float width{getBitmapWidth(bitmap)}; const float halfRatio{2.F}; + const float sizeAsFloat{static_cast(size)}; ::al_draw_scaled_rotated_bitmap( - bitmapToUse, width / halfRatio, width / halfRatio, - static_cast(x) + static_cast(size) / halfRatio, - static_cast(y) + static_cast(size) / halfRatio, - static_cast(size) / width, static_cast(size) / width, - static_cast(degrees) * (static_cast(ALLEGRO_PI) / 180.F), + bitmap, width / halfRatio, width / halfRatio, + static_cast(x) + (sizeAsFloat / halfRatio), + static_cast(y) + (sizeAsFloat / halfRatio), sizeAsFloat / width, + sizeAsFloat / width, + (static_cast(degrees) * (static_cast(ALLEGRO_PI)) / + 180.F), 0); } @@ -139,12 +135,12 @@ int Screen::getCenterX() const { return width_ / 2; } int Screen::getCenterY() const { return height_ / 2; } -int Screen::getBitmapWidth(ResourceType resourceType) const +int Screen::getResourceWidth(ResourceType resourceType) const { return ::al_get_bitmap_width(resources_.getBitmap(resourceType)); } -int Screen::getBitmapHeight(ResourceType resourceType) const +int Screen::getResourceHeight(ResourceType resourceType) const { return ::al_get_bitmap_height(resources_.getBitmap(resourceType)); } @@ -168,3 +164,12 @@ void Screen::useWindowedMode() false); updateSize(); } + +float Screen::getBitmapWidth(ALLEGRO_BITMAP* bitmap) const +{ + return static_cast(::al_get_bitmap_width(bitmap)); +} +float Screen::getBitmapHeight(ALLEGRO_BITMAP* bitmap) const +{ + return static_cast(::al_get_bitmap_height(bitmap)); +} diff --git a/src/Screen.h b/src/Screen.h index 1c40751..4a7d431 100644 --- a/src/Screen.h +++ b/src/Screen.h @@ -43,9 +43,9 @@ class Screen int getCenterY() const; - int getBitmapWidth(ResourceType resourceType) const; + int getResourceWidth(ResourceType resourceType) const; - int getBitmapHeight(ResourceType resourceType) const; + int getResourceHeight(ResourceType resourceType) const; static void refresh(); @@ -64,6 +64,9 @@ class Screen private: void updateSize(); + float getBitmapWidth(ALLEGRO_BITMAP* bitmap) const; + float getBitmapHeight(ALLEGRO_BITMAP* bitmap) const; + Resources resources_; int width_;