From 8677fdaa2072b47b12704d11fbb6d12df6657876 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 12 Nov 2023 09:54:08 -0500 Subject: [PATCH] Add `BitmapManager` class --- src/app/gfx/bitmap.cc | 2 +- src/app/gfx/bitmap.h | 61 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index e6efbfd5..e1469f00 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -94,7 +94,7 @@ void Bitmap::Create(int width, int height, int depth, Bytes data) { width_ = width; height_ = height; depth_ = depth; - data_ = data; + data_ = std::move(data); pixel_data_ = data_.data(); surface_ = std::unique_ptr( SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 3cf71470..2dd9932e 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -13,7 +13,6 @@ #include "app/core/constants.h" #include "app/gfx/snes_palette.h" - namespace yaze { namespace app { namespace gfx { @@ -21,16 +20,20 @@ namespace gfx { class Bitmap { public: Bitmap() = default; - Bitmap(int width, int height, int depth, uchar *data); Bitmap(int width, int height, int depth, int data_size); - Bitmap(int width, int height, int depth, uchar *data, int data_size); Bitmap(int width, int height, int depth, Bytes data); - void Create(int width, int height, int depth, uchar *data); + [[deprecated]] Bitmap(int width, int height, int depth, uchar *data); + [[deprecated]] Bitmap(int width, int height, int depth, uchar *data, + int data_size); + void Create(int width, int height, int depth, int data_size); - void Create(int width, int height, int depth, uchar *data, int data_size); void Create(int width, int height, int depth, Bytes data); + [[deprecated]] void Create(int width, int height, int depth, uchar *data); + [[deprecated]] void Create(int width, int height, int depth, uchar *data, + int data_size); + void CreateFromSurface(SDL_Surface *surface); void Apply(Bytes data); @@ -115,7 +118,53 @@ class Bitmap { std::shared_ptr surface_ = nullptr; }; -using BitmapTable = absl::flat_hash_map; +using BitmapTable = std::unordered_map; + +class BitmapManager { + private: + std::unordered_map> bitmap_cache_; + + public: + std::shared_ptr LoadBitmap(int id, const Bytes &data, int width, + int height, int depth) { + auto bitmap = std::make_shared(width, height, depth, data); + bitmap_cache_[id] = bitmap; + return bitmap; + } + + std::shared_ptr operator[](int id) { + auto it = bitmap_cache_.find(id); + if (it != bitmap_cache_.end()) { + return it->second; + } + return nullptr; // or handle the error accordingly + } + + using value_type = std::pair>; + using iterator = + std::unordered_map>::iterator; + using const_iterator = + std::unordered_map>::const_iterator; + + iterator begin() noexcept { return bitmap_cache_.begin(); } + iterator end() noexcept { return bitmap_cache_.end(); } + const_iterator begin() const noexcept { return bitmap_cache_.begin(); } + const_iterator end() const noexcept { return bitmap_cache_.end(); } + const_iterator cbegin() const noexcept { return bitmap_cache_.cbegin(); } + const_iterator cend() const noexcept { return bitmap_cache_.cend(); } + + + + std::shared_ptr GetBitmap(int id) { + auto it = bitmap_cache_.find(id); + if (it != bitmap_cache_.end()) { + return it->second; + } + return nullptr; // or handle the error accordingly + } + + void ClearCache() { bitmap_cache_.clear(); } +}; } // namespace gfx } // namespace app