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

Libraries: Make CharacterBitmap instances at compile-time

`CharacterBitmap` instances are generated at run-time and put on the
heap, but they can be created in a `constexpr` context and stored in
static memory.

Also, remove additional `width` and `height` `static` values in favor
of using the `constexpr` member functions of `CharacterBitmap`.

These changes also include the removal of some initialization code
which tests if the `CharacterBitmap` is created since it is always
created and removes function-local `static` values which cause
run-time branches to ensure it is initialized each time the function
is called.
This commit is contained in:
Lenny Maiorani 2022-02-28 19:21:12 -07:00 committed by Linus Groh
parent dc518404ce
commit d5fdc6096c
9 changed files with 97 additions and 135 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,7 +12,7 @@
namespace Cards {
static const NonnullRefPtr<Gfx::CharacterBitmap> s_diamond = Gfx::CharacterBitmap::create_from_ascii(
static constexpr Gfx::CharacterBitmap s_diamond {
" # "
" ### "
" ##### "
@ -21,9 +22,10 @@ static const NonnullRefPtr<Gfx::CharacterBitmap> s_diamond = Gfx::CharacterBitma
" ##### "
" ### "
" # ",
9, 9);
9, 9
};
static const NonnullRefPtr<Gfx::CharacterBitmap> s_heart = Gfx::CharacterBitmap::create_from_ascii(
static constexpr Gfx::CharacterBitmap s_heart {
" # # "
" ### ### "
"#########"
@ -33,9 +35,10 @@ static const NonnullRefPtr<Gfx::CharacterBitmap> s_heart = Gfx::CharacterBitmap:
" ##### "
" ### "
" # ",
9, 9);
9, 9
};
static const NonnullRefPtr<Gfx::CharacterBitmap> s_spade = Gfx::CharacterBitmap::create_from_ascii(
static constexpr Gfx::CharacterBitmap s_spade {
" # "
" ### "
" ##### "
@ -45,9 +48,10 @@ static const NonnullRefPtr<Gfx::CharacterBitmap> s_spade = Gfx::CharacterBitmap:
" ## # ## "
" ### "
" ### ",
9, 9);
9, 9
};
static const NonnullRefPtr<Gfx::CharacterBitmap> s_club = Gfx::CharacterBitmap::create_from_ascii(
static constexpr Gfx::CharacterBitmap s_club {
" ### "
" ##### "
" ##### "
@ -57,7 +61,8 @@ static const NonnullRefPtr<Gfx::CharacterBitmap> s_club = Gfx::CharacterBitmap::
"#### # ####"
" ## ### ## "
" ### ",
11, 9);
11, 9
};
static RefPtr<Gfx::Bitmap> s_background;
static RefPtr<Gfx::Bitmap> s_background_inverted;
@ -105,26 +110,24 @@ Card::Card(Type type, uint8_t value)
auto text_rect = Gfx::IntRect { 4, 6, font.width("10"), font.glyph_height() };
painter.draw_text(text_rect, label, font, Gfx::TextAlignment::Center, color());
NonnullRefPtr<Gfx::CharacterBitmap> symbol = s_diamond;
switch (m_type) {
case Diamonds:
symbol = s_diamond;
break;
case Clubs:
symbol = s_club;
break;
case Spades:
symbol = s_spade;
break;
case Hearts:
symbol = s_heart;
break;
default:
VERIFY_NOT_REACHED();
}
auto const& symbol = [&]() -> Gfx::CharacterBitmap const& {
switch (m_type) {
case Diamonds:
return s_diamond;
case Clubs:
return s_club;
break;
case Spades:
return s_spade;
case 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 },
{ text_rect.x() + (text_rect.width() - symbol.size().width()) / 2, text_rect.bottom() + 5 },
symbol, color());
for (int y = height / 2; y < height; ++y) {