Update Bitmap fns

This commit is contained in:
scawful
2023-11-18 20:05:32 -05:00
parent 82952e2e73
commit 72ef0d0536
5 changed files with 47 additions and 29 deletions

View File

@@ -11,11 +11,11 @@ set(
app/editor/dungeon_editor.cc app/editor/dungeon_editor.cc
app/editor/graphics_editor.cc app/editor/graphics_editor.cc
app/editor/master_editor.cc app/editor/master_editor.cc
app/editor/music_editor.cc
app/editor/overworld_editor.cc app/editor/overworld_editor.cc
app/editor/palette_editor.cc
app/editor/screen_editor.cc app/editor/screen_editor.cc
app/editor/sprite_editor.cc app/editor/sprite_editor.cc
app/editor/resources/music_editor.cc
app/editor/resources/palette_editor.cc
app/editor/modules/assembly_editor.cc app/editor/modules/assembly_editor.cc
app/editor/modules/tile16_editor.cc app/editor/modules/tile16_editor.cc
app/editor/modules/gfx_group_editor.cc app/editor/modules/gfx_group_editor.cc

View File

@@ -111,7 +111,7 @@ void BitmapCanvasPipeline(gui::Canvas& canvas, const gfx::Bitmap& bitmap,
void BuildAndRenderBitmapPipeline(int width, int height, int depth, Bytes data, void BuildAndRenderBitmapPipeline(int width, int height, int depth, Bytes data,
ROM& z3_rom, gfx::Bitmap& bitmap, ROM& z3_rom, gfx::Bitmap& bitmap,
gfx::SNESPalette& palette) { gfx::SNESPalette& palette) {
bitmap.Create(width, height, depth, data); PRINT_IF_ERROR(bitmap.InitializeFromData(width, height, depth, data));
bitmap.ApplyPalette(palette); bitmap.ApplyPalette(palette);
z3_rom.RenderBitmap(&bitmap); z3_rom.RenderBitmap(&bitmap);
} }

View File

@@ -104,24 +104,6 @@ void Bitmap::Create(int width, int height, int depth, const Bytes &data) {
GrayscalePalette(surface_->format->palette); GrayscalePalette(surface_->format->palette);
} }
void Bitmap::CreateFromSurface(SDL_Surface *surface) {
active_ = true;
width_ = surface->w;
height_ = surface->h;
depth_ = 8;
pixel_data_ = static_cast<uchar *>(surface->pixels);
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
SDL_PIXELFORMAT_INDEX8),
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
}
void Bitmap::Apply(Bytes data) {
pixel_data_ = data.data();
data_ = data;
}
// Creates the texture that will be displayed to the screen. // Creates the texture that will be displayed to the screen.
void Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) { void Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) {
texture_ = std::shared_ptr<SDL_Texture>{ texture_ = std::shared_ptr<SDL_Texture>{
@@ -177,6 +159,43 @@ void Bitmap::ApplyPalette(const std::vector<SDL_Color> &palette) {
SDL_LockSurface(surface_.get()); SDL_LockSurface(surface_.get());
} }
absl::Status Bitmap::InitializeFromData(uint32_t width, uint32_t height,
uint32_t depth, const Bytes &data) {
if (width == 0 || height == 0 || depth == 0) {
return absl::InvalidArgumentError(
absl::StrCat("Invalid arguments: width: ", width, ", height: ", height,
", depth: ", depth));
}
active_ = true;
width_ = width;
height_ = height;
depth_ = depth;
data_ = data;
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
SDL_PIXELFORMAT_INDEX8),
SDL_Surface_Deleter());
if (surface_ == nullptr) {
return absl::InternalError("Failed to create surface.");
}
surface_->pixels = data_.data();
GrayscalePalette(surface_->format->palette);
return absl::OkStatus();
}
void Bitmap::ReserveData(uint32_t width, uint32_t height, uint32_t depth,
uint32_t size) {
width_ = width;
height_ = height;
depth_ = depth;
data_.reserve(size);
pixel_data_ = data_.data();
}
} // namespace gfx } // namespace gfx
} // namespace app } // namespace app
} // namespace yaze } // namespace yaze

View File

@@ -22,8 +22,6 @@ class Bitmap {
Bitmap() = default; Bitmap() = default;
Bitmap(int width, int height, int depth, int data_size); Bitmap(int width, int height, int depth, int data_size);
// Bitmap(int width, int height, int depth, Bytes data);
Bitmap(int width, int height, int depth, const Bytes &data) Bitmap(int width, int height, int depth, const Bytes &data)
: width_(width), height_(height), depth_(depth), data_(data) { : width_(width), height_(height), depth_(depth), data_(data) {
CreateTextureFromData(); CreateTextureFromData();
@@ -48,14 +46,15 @@ class Bitmap {
void Create(int width, int height, int depth, int data_size); void Create(int width, int height, int depth, int data_size);
void Create(int width, int height, int depth, const Bytes &data); void Create(int width, int height, int depth, const Bytes &data);
absl::Status InitializeFromData(uint32_t width, uint32_t height,
uint32_t depth, const Bytes &data);
void ReserveData(uint32_t width, uint32_t height, uint32_t depth,
uint32_t size);
[[deprecated]] void Create(int width, int height, int depth, uchar *data); [[deprecated]] void Create(int width, int height, int depth, uchar *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); int data_size);
void CreateFromSurface(SDL_Surface *surface);
void Apply(Bytes data);
void CreateTexture(std::shared_ptr<SDL_Renderer> renderer); void CreateTexture(std::shared_ptr<SDL_Renderer> renderer);
void UpdateTexture(std::shared_ptr<SDL_Renderer> renderer); void UpdateTexture(std::shared_ptr<SDL_Renderer> renderer);

View File

@@ -5,7 +5,6 @@
#include "app/gui/canvas.h" #include "app/gui/canvas.h"
#include "app/rom.h" #include "app/rom.h"
namespace yaze { namespace yaze {
namespace app { namespace app {
namespace zelda3 { namespace zelda3 {
@@ -62,7 +61,8 @@ void Inventory::Create() {
i++; i++;
} }
} }
bitmap_.Create(256, 256, 128, data_);
PRINT_IF_ERROR(bitmap_.InitializeFromData(256, 256, 8, data_))
bitmap_.ApplyPalette(palette_); bitmap_.ApplyPalette(palette_);
rom()->RenderBitmap(&bitmap_); rom()->RenderBitmap(&bitmap_);
} }