1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:57:45 +00:00

LibCards: Centralise card bitmap creation

Instead of each card being responsible for painting its own bitmaps, we
now have a CardPainter which is responsible for this. It paints a given
card the first time it is requested, and then re-uses that bitmap when
requested in the future. This saves memory for duplicate cards (such as
in Spider where several sets of the same suit are used) or unused ones
(for example, the inverted cards which are only used by Hearts). It
also means we don't throw away bitmaps and then re-create identical
ones when starting a new game.

We get some nice memory savings from this:

|           | Before   | After    | Before (Virtual) | After (Virtual) |
|:----------|---------:|---------:|-----------------:|----------------:|
| Hearts    | 12.2 MiB |  9.3 MiB |         25.1 MiB |        22.2 MiB |
| Spider    | 12.1 MiB | 10.1 MiB |         29.2 MiB |        22.9 MiB |
| Solitaire | 16.4 MiB |  9.0 MiB |         25.0 MiB |        21.9 MiB |

All these measurements taken from x86_64 build, from a fresh launch of
each game after the animation has finished, but without making any
moves. The Hearts value will go up once inverted cards start being
requested.
This commit is contained in:
Sam Atkins 2022-08-21 12:53:20 +01:00 committed by Andreas Kling
parent aac2488d5c
commit 7f46d31849
5 changed files with 249 additions and 141 deletions

View file

@ -1,6 +1,7 @@
set(SOURCES set(SOURCES
Card.cpp Card.cpp
CardGame.cpp CardGame.cpp
CardPainter.cpp
CardStack.cpp CardStack.cpp
) )

View file

@ -6,145 +6,28 @@
*/ */
#include "Card.h" #include "Card.h"
#include <LibGUI/Widget.h> #include <LibCards/CardPainter.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
namespace Cards { namespace Cards {
static constexpr Gfx::CharacterBitmap s_diamond {
" # "
" ### "
" ##### "
" ####### "
"#########"
" ####### "
" ##### "
" ### "
" # "sv,
9, 9
};
static constexpr Gfx::CharacterBitmap s_heart {
" # # "
" ### ### "
"#########"
"#########"
"#########"
" ####### "
" ##### "
" ### "
" # "sv,
9, 9
};
static constexpr Gfx::CharacterBitmap s_spade {
" # "
" ### "
" ##### "
" ####### "
"#########"
"#########"
" ## # ## "
" ### "
" ### "sv,
9, 9
};
static constexpr Gfx::CharacterBitmap s_club {
" ### "
" ##### "
" ##### "
" ## ### ## "
"###########"
"###########"
"#### # ####"
" ## ### ## "
" ### "sv,
11, 9
};
static RefPtr<Gfx::Bitmap> s_background;
static RefPtr<Gfx::Bitmap> s_background_inverted;
Card::Card(Suit suit, Rank rank) Card::Card(Suit suit, Rank rank)
: m_rect(Gfx::IntRect({}, { width, height })) : m_rect(Gfx::IntRect({}, { width, height }))
, m_front(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }).release_value_but_fixme_should_propagate_errors())
, m_suit(suit) , m_suit(suit)
, m_rank(rank) , m_rank(rank)
{ {
VERIFY(to_underlying(rank) < card_count); VERIFY(to_underlying(rank) < card_count);
Gfx::IntRect paint_rect({ 0, 0 }, { width, height });
if (s_background.is_null()) {
s_background = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }).release_value_but_fixme_should_propagate_errors();
Gfx::Painter bg_painter(*s_background);
auto image = Gfx::Bitmap::try_load_from_file("/res/icons/cards/buggie-deck.png"sv).release_value_but_fixme_should_propagate_errors();
float aspect_ratio = image->width() / static_cast<float>(image->height());
auto target_size = Gfx::IntSize(static_cast<int>(aspect_ratio * (height - 5)), height - 5);
bg_painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
auto inner_paint_rect = paint_rect.shrunken(2, 2);
bg_painter.fill_rect_with_rounded_corners(inner_paint_rect, Color::White, card_radius - 1);
bg_painter.draw_scaled_bitmap(
{ { (width - target_size.width()) / 2, (height - target_size.height()) / 2 }, target_size },
*image, image->rect());
s_background_inverted = invert_bitmap(*s_background);
}
Gfx::Painter painter(m_front);
auto& font = Gfx::FontDatabase::default_font().bold_variant();
painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
paint_rect.shrink(2, 2);
painter.fill_rect_with_rounded_corners(paint_rect, Color::White, card_radius - 1);
paint_rect.set_height(paint_rect.height() / 2);
paint_rect.shrink(10, 6);
auto text_rect = Gfx::IntRect { 4, 6, font.width("10"sv), font.glyph_height() };
painter.draw_text(text_rect, card_rank_label(m_rank), font, Gfx::TextAlignment::Center, color());
auto const& symbol = [&]() -> Gfx::CharacterBitmap const& {
switch (m_suit) {
case Suit::Diamonds:
return s_diamond;
case Suit::Clubs:
return s_club;
break;
case Suit::Spades:
return s_spade;
case Suit::Hearts:
return s_heart;
default:
VERIFY_NOT_REACHED();
}
}();
painter.draw_bitmap(
{ text_rect.x() + (text_rect.width() - symbol.size().width()) / 2, text_rect.bottom() + 5 },
symbol, color());
for (int y = height / 2; y < height; ++y) {
for (int x = 0; x < width; ++x) {
m_front->set_pixel(x, y, m_front->get_pixel(width - x - 1, height - y - 1));
}
}
m_front_inverted = invert_bitmap(*m_front);
} }
void Card::draw(GUI::Painter& painter) const void Card::draw(GUI::Painter& painter) const
{ {
VERIFY(!s_background.is_null()); auto& card_painter = CardPainter::the();
if (m_inverted) auto bitmap = [&]() {
painter.blit(position(), m_upside_down ? *s_background_inverted : *m_front_inverted, m_front_inverted->rect()); if (m_inverted)
else return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
painter.blit(position(), m_upside_down ? *s_background : *m_front, m_front->rect());
return m_upside_down ? card_painter.card_back() : card_painter.card_front(m_suit, m_rank);
}();
painter.blit(position(), bitmap, bitmap->rect());
} }
void Card::clear(GUI::Painter& painter, Color const& background_color) const void Card::clear(GUI::Painter& painter, Color const& background_color) const
@ -167,15 +50,4 @@ void Card::clear_and_draw(GUI::Painter& painter, Color const& background_color)
save_old_position(); save_old_position();
} }
NonnullRefPtr<Gfx::Bitmap> Card::invert_bitmap(Gfx::Bitmap& bitmap)
{
auto inverted_bitmap = bitmap.clone().release_value_but_fixme_should_propagate_errors();
for (int y = 0; y < inverted_bitmap->height(); y++) {
for (int x = 0; x < inverted_bitmap->width(); x++) {
inverted_bitmap->set_pixel(x, y, inverted_bitmap->get_pixel(x, y).inverted());
}
}
return *inverted_bitmap;
}
} }

View file

@ -113,11 +113,7 @@ public:
private: private:
Card(Suit, Rank); Card(Suit, Rank);
static NonnullRefPtr<Gfx::Bitmap> invert_bitmap(Gfx::Bitmap&);
Gfx::IntRect m_rect; Gfx::IntRect m_rect;
NonnullRefPtr<Gfx::Bitmap> m_front;
RefPtr<Gfx::Bitmap> m_front_inverted;
Gfx::IntPoint m_old_position; Gfx::IntPoint m_old_position;
Suit m_suit; Suit m_suit;
Rank m_rank; Rank m_rank;

View file

@ -0,0 +1,203 @@
/*
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CardPainter.h"
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
namespace Cards {
CardPainter& CardPainter::the()
{
static CardPainter s_card_painter;
return s_card_painter;
}
static constexpr Gfx::CharacterBitmap s_diamond {
" # "
" ### "
" ##### "
" ####### "
"#########"
" ####### "
" ##### "
" ### "
" # "sv,
9, 9
};
static constexpr Gfx::CharacterBitmap s_heart {
" # # "
" ### ### "
"#########"
"#########"
"#########"
" ####### "
" ##### "
" ### "
" # "sv,
9, 9
};
static constexpr Gfx::CharacterBitmap s_spade {
" # "
" ### "
" ##### "
" ####### "
"#########"
"#########"
" ## # ## "
" ### "
" ### "sv,
9, 9
};
static constexpr Gfx::CharacterBitmap s_club {
" ### "
" ##### "
" ##### "
" ## ### ## "
"###########"
"###########"
"#### # ####"
" ## ### ## "
" ### "sv,
11, 9
};
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front(Suit suit, Rank rank)
{
auto suit_id = to_underlying(suit);
auto rank_id = to_underlying(rank);
auto& existing_bitmap = m_cards[suit_id][rank_id];
if (!existing_bitmap.is_null())
return *existing_bitmap;
m_cards[suit_id][rank_id] = create_card_bitmap();
paint_card_front(*m_cards[suit_id][rank_id], suit, rank);
return *m_cards[suit_id][rank_id];
}
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back()
{
if (!m_card_back.is_null())
return *m_card_back;
m_card_back = create_card_bitmap();
paint_card_back(*m_card_back);
return *m_card_back;
}
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_inverted(Suit suit, Rank rank)
{
auto suit_id = to_underlying(suit);
auto rank_id = to_underlying(rank);
auto& existing_bitmap = m_cards_inverted[suit_id][rank_id];
if (!existing_bitmap.is_null())
return *existing_bitmap;
m_cards_inverted[suit_id][rank_id] = create_card_bitmap();
paint_inverted_card(*m_cards_inverted[suit_id][rank_id], card_front(suit, rank));
return *m_cards_inverted[suit_id][rank_id];
}
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back_inverted()
{
if (!m_card_back_inverted.is_null())
return *m_card_back_inverted;
m_card_back_inverted = create_card_bitmap();
paint_inverted_card(card_back(), *m_card_back_inverted);
return *m_card_back_inverted;
}
NonnullRefPtr<Gfx::Bitmap> CardPainter::create_card_bitmap()
{
return Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { Card::width, Card::height }).release_value_but_fixme_should_propagate_errors();
}
void CardPainter::paint_card_front(Gfx::Bitmap& bitmap, Cards::Suit suit, Cards::Rank rank)
{
auto const suit_color = (suit == Suit::Diamonds || suit == Suit::Hearts) ? Color::Red : Color::Black;
auto const& suit_symbol = [&]() -> Gfx::CharacterBitmap const& {
switch (suit) {
case Suit::Diamonds:
return s_diamond;
case Suit::Clubs:
return s_club;
case Suit::Spades:
return s_spade;
case Suit::Hearts:
return s_heart;
default:
VERIFY_NOT_REACHED();
}
}();
Gfx::Painter painter { bitmap };
auto paint_rect = bitmap.rect();
auto& font = Gfx::FontDatabase::default_font().bold_variant();
painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, Card::card_radius);
paint_rect.shrink(2, 2);
painter.fill_rect_with_rounded_corners(paint_rect, Color::White, Card::card_radius - 1);
paint_rect.set_height(paint_rect.height() / 2);
paint_rect.shrink(10, 6);
auto text_rect = Gfx::IntRect { 4, 6, font.width("10"sv), font.glyph_height() };
painter.draw_text(text_rect, card_rank_label(rank), font, Gfx::TextAlignment::Center, suit_color);
painter.draw_bitmap(
{ text_rect.x() + (text_rect.width() - suit_symbol.size().width()) / 2, text_rect.bottom() + 5 },
suit_symbol, suit_color);
for (int y = Card::height / 2; y < Card::height; ++y) {
for (int x = 0; x < Card::width; ++x) {
bitmap.set_pixel(x, y, bitmap.get_pixel(Card::width - x - 1, Card::height - y - 1));
}
}
}
void CardPainter::paint_card_back(Gfx::Bitmap& bitmap)
{
Gfx::Painter painter { bitmap };
auto paint_rect = bitmap.rect();
painter.clear_rect(paint_rect, Gfx::Color::Transparent);
auto image = Gfx::Bitmap::try_load_from_file("/res/icons/cards/buggie-deck.png"sv).release_value_but_fixme_should_propagate_errors();
float aspect_ratio = image->width() / static_cast<float>(image->height());
Gfx::IntSize target_size { static_cast<int>(aspect_ratio * (Card::height - 5)), Card::height - 5 };
painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, Card::card_radius);
auto inner_paint_rect = paint_rect.shrunken(2, 2);
painter.fill_rect_with_rounded_corners(inner_paint_rect, Color::White, Card::card_radius - 1);
painter.draw_scaled_bitmap(
{ { (Card::width - target_size.width()) / 2, (Card::height - target_size.height()) / 2 }, target_size },
*image, image->rect());
}
void CardPainter::paint_inverted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_invert)
{
Gfx::Painter painter { bitmap };
painter.clear_rect(bitmap.rect(), Gfx::Color::Transparent);
painter.blit_filtered({ 0, 0 }, source_to_invert, source_to_invert.rect(), [&](Color color) {
return color.inverted();
});
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <LibCards/Card.h>
#include <LibGfx/Bitmap.h>
namespace Cards {
class CardPainter {
public:
static CardPainter& the();
NonnullRefPtr<Gfx::Bitmap> card_front(Suit, Rank);
NonnullRefPtr<Gfx::Bitmap> card_back();
NonnullRefPtr<Gfx::Bitmap> card_front_inverted(Suit, Rank);
NonnullRefPtr<Gfx::Bitmap> card_back_inverted();
private:
NonnullRefPtr<Gfx::Bitmap> create_card_bitmap();
void paint_card_front(Gfx::Bitmap&, Suit, Rank);
void paint_card_back(Gfx::Bitmap&);
void paint_inverted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_invert);
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards;
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_inverted;
RefPtr<Gfx::Bitmap> m_card_back;
RefPtr<Gfx::Bitmap> m_card_back_inverted;
};
}