diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index d5d36cd0..15020ec7 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -21,44 +21,29 @@ void GrayscalePalette(SDL_Palette *palette) { } } // namespace -Bitmap::Bitmap(int width, int height, int depth, uchar *data) - : width_(width), height_(height), depth_(depth), pixel_data_(data) { - surface_ = std::unique_ptr( - SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, - SDL_PIXELFORMAT_INDEX8), - sdl_deleter()); - GrayscalePalette(surface_->format->palette); - surface_->pixels = data; +Bitmap::Bitmap(int width, int height, int depth, uchar *data) { + Create(width, height, depth, data); } -Bitmap::Bitmap(int width, int height, int depth, int data_size) - : width_(width), height_(height), depth_(depth), data_size_(data_size) { - surface_ = std::unique_ptr( - SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, - SDL_PIXELFORMAT_INDEX8), - sdl_deleter()); - GrayscalePalette(surface_->format->palette); - pixel_data_ = (uchar *)SDL_malloc(data_size); - surface_->pixels = pixel_data_; +Bitmap::Bitmap(int width, int height, int depth, int data_size) { + Create(width, height, depth, data_size); } // Pass raw pixel data directly to the surface -// Be sure to know what the hell you're doing if you use this one. void Bitmap::Create(int width, int height, int depth, uchar *data) { width_ = width; height_ = height; depth_ = depth; pixel_data_ = data; surface_ = std::unique_ptr( - SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, + SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, SDL_PIXELFORMAT_INDEX8), sdl_deleter()); GrayscalePalette(surface_->format->palette); - surface_->pixels = pixel_data_; } -// Reserve data and draw to surface via pointer +// Reserves data to later draw to surface via pointer void Bitmap::Create(int width, int height, int depth, int size) { width_ = width; height_ = height; @@ -73,28 +58,19 @@ void Bitmap::Create(int width, int height, int depth, int size) { surface_->pixels = pixel_data_; } -void Bitmap::Create(int width, int height, int depth, uchar *data, int size, - SNESPalette &palette) { - width_ = width; - height_ = height; - depth_ = depth; - pixel_data_ = data; - surface_ = std::unique_ptr( - SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, - SDL_PIXELFORMAT_INDEX8), - sdl_deleter()); - surface_->format->palette = palette.GetSDL_Palette(); - surface_->pixels = pixel_data_; -} - +// Creates the texture that will be displayed to the screen. void Bitmap::CreateTexture(std::shared_ptr renderer) { texture_ = std::unique_ptr( SDL_CreateTextureFromSurface(renderer.get(), surface_.get()), sdl_deleter()); } -void Bitmap::ApplyPalette(const SNESPalette &palette) { palette_ = palette; } +// Convert SNESPalette to SDL_Palette for surface. +void Bitmap::ApplyPalette(SNESPalette &palette) { + surface_->format->palette = palette.GetSDL_Palette(); +} +// Creates a vector of bitmaps which are individual 8x8 tiles. absl::StatusOr> Bitmap::CreateTiles() { std::vector tiles; for (int i = 0; i < 16; ++i) { @@ -103,29 +79,26 @@ absl::StatusOr> Bitmap::CreateTiles() { bmp.Create(8, 8, 8, 32); auto surface = bmp.GetSurface(); SDL_Rect src_rect = {i, j, 8, 8}; - if (SDL_BlitSurface(surface_.get(), &src_rect, surface, nullptr) != 0) { + if (SDL_BlitSurface(surface_.get(), &src_rect, surface, nullptr) != 0) return absl::InvalidArgumentError( "Failed to blit surface for Bitmap Tilesheet"); - } tiles.push_back(bmp); } } return tiles; } +// Converts a vector of 8x8 tiles into a tilesheet. absl::Status Bitmap::CreateFromTiles(const std::vector &tiles) { - if (tiles.empty()) { - return absl::InvalidArgumentError("Empty tiles"); - } + if (tiles.empty()) return absl::InvalidArgumentError("Empty tiles"); SDL_Rect tile_rect = {0, 0, 8, 8}; SDL_Rect dest_rect = {0, 0, 8, 8}; for (const auto &tile : tiles) { auto src = tile.GetSurface(); - if (SDL_BlitSurface(src, &tile_rect, surface_.get(), &dest_rect) != 0) { + if (SDL_BlitSurface(src, &tile_rect, surface_.get(), &dest_rect) != 0) return absl::InvalidArgumentError( "Failed to blit surface for Bitmap Tilesheet"); - } dest_rect.x++; if (dest_rect.x == 15) { diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 183a2136..d00d85f3 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -21,11 +21,10 @@ class Bitmap { void Create(int width, int height, int depth, uchar *data); void Create(int width, int height, int depth, int data_size); - void Create(int width, int height, int depth, uchar *data, int size, - SNESPalette &palette); + void CreateTexture(std::shared_ptr renderer); - void ApplyPalette(const SNESPalette &palette); + void ApplyPalette(SNESPalette &palette); absl::StatusOr> CreateTiles(); absl::Status CreateFromTiles(const std::vector &tiles); @@ -49,7 +48,6 @@ class Bitmap { uchar *pixel_data_; std::shared_ptr texture_; std::shared_ptr surface_; - SNESPalette palette_; }; } // namespace gfx