mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-09 20:52:54 +00:00
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.
53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "Card.h"
|
|
#include <LibCards/CardPainter.h>
|
|
|
|
namespace Cards {
|
|
|
|
Card::Card(Suit suit, Rank rank)
|
|
: m_rect(Gfx::IntRect({}, { width, height }))
|
|
, m_suit(suit)
|
|
, m_rank(rank)
|
|
{
|
|
VERIFY(to_underlying(rank) < card_count);
|
|
}
|
|
|
|
void Card::draw(GUI::Painter& painter) const
|
|
{
|
|
auto& card_painter = CardPainter::the();
|
|
auto bitmap = [&]() {
|
|
if (m_inverted)
|
|
return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
|
|
|
|
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
|
|
{
|
|
painter.fill_rect({ old_position(), { width, height } }, background_color);
|
|
}
|
|
|
|
void Card::save_old_position()
|
|
{
|
|
m_old_position = m_rect.location();
|
|
m_old_position_valid = true;
|
|
}
|
|
|
|
void Card::clear_and_draw(GUI::Painter& painter, Color const& background_color)
|
|
{
|
|
if (is_old_position_valid())
|
|
clear(painter, background_color);
|
|
|
|
draw(painter);
|
|
save_old_position();
|
|
}
|
|
|
|
}
|