From 1541942e108484312c05ca457bb4d1f5a1f6aa2d Mon Sep 17 00:00:00 2001 From: david072 Date: Sun, 12 Nov 2023 12:04:50 +0100 Subject: [PATCH] LibCards/CardPainter: Add a helper for accessing the caches The CardPainter in LibCards caches already painted bitmaps. This adds a helper that gets the bitmap from a cache or creates a new one, if the it doesn't exist yet. It does this by calling a creator function with the new bitmap, which can then paint into the bitmap. This makes the code a bit simpler and shorter. --- Userland/Libraries/LibCards/CardPainter.cpp | 59 ++++++++------------- Userland/Libraries/LibCards/CardPainter.h | 15 ++++-- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/Userland/Libraries/LibCards/CardPainter.cpp b/Userland/Libraries/LibCards/CardPainter.cpp index 044f31be28..bb403c1e52 100644 --- a/Userland/Libraries/LibCards/CardPainter.cpp +++ b/Userland/Libraries/LibCards/CardPainter.cpp @@ -82,19 +82,26 @@ static constexpr Gfx::CharacterBitmap s_club { static constexpr u8 s_disabled_alpha = 90; -NonnullRefPtr CardPainter::card_front(Suit suit, Rank rank) +NonnullRefPtr CardPainter::get_bitmap_or_create(Suit suit, Rank rank, CardPainter::PaintCache& cache, Function creator) { auto suit_id = to_underlying(suit); auto rank_id = to_underlying(rank); - auto& existing_bitmap = m_cards[suit_id][rank_id]; + auto& existing_bitmap = cache[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); + auto bitmap = create_card_bitmap(); + creator(bitmap); + cache[suit_id][rank_id] = move(bitmap); + return *cache[suit_id][rank_id]; +} - return *m_cards[suit_id][rank_id]; +NonnullRefPtr CardPainter::card_front(Suit suit, Rank rank) +{ + return get_bitmap_or_create(suit, rank, m_cards, [this, suit, rank](auto& bitmap) { + paint_card_front(bitmap, suit, rank); + }); } NonnullRefPtr CardPainter::card_back() @@ -110,47 +117,23 @@ NonnullRefPtr CardPainter::card_back() NonnullRefPtr CardPainter::card_front_highlighted(Suit suit, Rank rank) { - auto suit_id = to_underlying(suit); - auto rank_id = to_underlying(rank); - - auto& existing_bitmap = m_cards_highlighted[suit_id][rank_id]; - if (!existing_bitmap.is_null()) - return *existing_bitmap; - - m_cards_highlighted[suit_id][rank_id] = create_card_bitmap(); - paint_highlighted_card(*m_cards_highlighted[suit_id][rank_id], card_front(suit, rank)); - - return *m_cards_highlighted[suit_id][rank_id]; + return get_bitmap_or_create(suit, rank, m_cards_highlighted, [this, suit, rank](auto& bitmap) { + paint_highlighted_card(bitmap, card_front(suit, rank)); + }); } NonnullRefPtr CardPainter::card_front_disabled(Suit suit, Rank rank) { - auto suit_id = to_underlying(suit); - auto rank_id = to_underlying(rank); - - auto& existing_bitmap = m_cards_disabled[suit_id][rank_id]; - if (!existing_bitmap.is_null()) - return *existing_bitmap; - - m_cards_disabled[suit_id][rank_id] = create_card_bitmap(); - paint_disabled_card(*m_cards_disabled[suit_id][rank_id], card_front(suit, rank)); - - return *m_cards_disabled[suit_id][rank_id]; + return get_bitmap_or_create(suit, rank, m_cards_disabled, [this, suit, rank](auto& bitmap) { + paint_disabled_card(bitmap, card_front(suit, rank)); + }); } NonnullRefPtr 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]; + return get_bitmap_or_create(suit, rank, m_cards_inverted, [this, suit, rank](auto& bitmap) { + paint_inverted_card(bitmap, card_front(suit, rank)); + }); } NonnullRefPtr CardPainter::card_back_inverted() diff --git a/Userland/Libraries/LibCards/CardPainter.h b/Userland/Libraries/LibCards/CardPainter.h index ea94083d3c..b5c400f5ea 100644 --- a/Userland/Libraries/LibCards/CardPainter.h +++ b/Userland/Libraries/LibCards/CardPainter.h @@ -32,7 +32,12 @@ public: void set_background_color(Color); private: + using PaintCache = Array, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)>; + CardPainter(); + + NonnullRefPtr get_bitmap_or_create(Suit, Rank, PaintCache&, Function); + NonnullRefPtr create_card_bitmap(); void paint_card_front(Gfx::Bitmap&, Suit, Rank); void paint_card_front_pips(Gfx::Bitmap&, Suit, Rank); @@ -43,10 +48,12 @@ private: Array, to_underlying(Suit::__Count)> m_suit_pips; Array, to_underlying(Suit::__Count)> m_suit_pips_flipped_vertically; - Array, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards; - Array, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_inverted; - Array, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_highlighted; - Array, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_disabled; + + PaintCache m_cards; + PaintCache m_cards_inverted; + PaintCache m_cards_highlighted; + PaintCache m_cards_disabled; + RefPtr m_card_back; RefPtr m_card_back_inverted; RefPtr m_card_back_disabled;