From c98985fd3a426ebca5733cc02d3ad9fd26a3b72b Mon Sep 17 00:00:00 2001 From: Justin Scofield Date: Wed, 20 Jul 2022 22:13:13 -0400 Subject: [PATCH] Added CreateTiles function very important --- src/app/gfx/bitmap.cc | 32 ++++++++++++++++++++++++++++++++ src/app/gfx/bitmap.h | 8 ++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 0d00bba3..892cf471 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -24,6 +24,21 @@ Bitmap::Bitmap(int width, int height, int depth, uchar *data) surface_->pixels = data; } +Bitmap::Bitmap(int width, int height, int depth, int data_size) + : width_(width), height_(height), depth_(depth), data_size_(data_size) { + surface_ = SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, + SDL_PIXELFORMAT_INDEX8); + // Default grayscale palette + for (int i = 0; i < 8; i++) { + surface_->format->palette->colors[i].r = i * 31; + surface_->format->palette->colors[i].g = i * 31; + surface_->format->palette->colors[i].b = i * 31; + } + + pixel_data_ = (uchar *)SDL_malloc(data_size); + surface_->pixels = pixel_data_; +} + void Bitmap::Create(int width, int height, int depth, uchar *data) { width_ = width; height_ = height; @@ -77,6 +92,23 @@ void Bitmap::CreateTexture(std::shared_ptr renderer) { void Bitmap::ApplyPalette(const SNESPalette &palette) { palette_ = palette; } +std::vector Bitmap::CreateTiles() { + std::vector tiles; + for (int i = 0; i < 16; ++i) { + for (int j = 0; j < 4; ++j) { + tiles.emplace_back(8, 8, 8, 32); + auto surface = tiles[i + j].GetSurface(); + SDL_Rect src_rect; + src_rect.x = i; + src_rect.y = j; + src_rect.w = 8; + src_rect.h = 8; + SDL_BlitSurface(surface_, &src_rect, surface, NULL); + } + } + return tiles; +} + } // namespace gfx } // namespace app } // namespace yaze diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 1a1c7433..59959512 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -16,6 +16,7 @@ class Bitmap { public: Bitmap() = default; Bitmap(int width, int height, int depth, uchar *data); + Bitmap(int width, int height, int depth, int data_size); void Create(int width, int height, int depth, uchar *data); void Create(int width, int height, int depth, int data_size); @@ -25,10 +26,13 @@ class Bitmap { void ApplyPalette(const SNESPalette &palette); + std::vector CreateTiles(); + int GetWidth() const { return width_; } int GetHeight() const { return height_; } - uchar *GetData() const { return pixel_data_; } - SDL_Texture *GetTexture() const { return texture_; } + auto GetData() const { return pixel_data_; } + auto GetTexture() const { return texture_; } + auto GetSurface() const { return surface_; } private: int width_ = 0;