refactor: Update Renderer Logic and Introduce Direct Pixel Access Methods

- Removed direct renderer calls from LoadLinkGraphics and LoadAllGraphicsData to prepare for future renderer refactor.
- Added LockTexture and UnlockTexture methods to IRenderer and SDL2Renderer for direct pixel access, enhancing texture management capabilities.
- Updated CMake configuration to include the new SDL2Renderer source file, ensuring proper integration within the graphics backend.
This commit is contained in:
scawful
2025-10-07 14:39:36 -04:00
parent c6a581b202
commit b788d93071
5 changed files with 22 additions and 27 deletions

View File

@@ -73,6 +73,10 @@ public:
*/
virtual void DestroyTexture(TextureHandle texture) = 0;
// --- Direct Pixel Access ---
virtual bool LockTexture(TextureHandle texture, SDL_Rect* rect, void** pixels, int* pitch) = 0;
virtual void UnlockTexture(TextureHandle texture) = 0;
// --- Rendering Primitives ---
/**

View File

@@ -1,5 +1,6 @@
#include "app/gfx/backend/sdl2_renderer.h"
#include "absl/strings/str_format.h"
#include "app/gfx/bitmap.h"
namespace yaze {
namespace gfx {
@@ -76,6 +77,14 @@ void SDL2Renderer::DestroyTexture(TextureHandle texture) {
}
}
bool SDL2Renderer::LockTexture(TextureHandle texture, SDL_Rect* rect, void** pixels, int* pitch) {
return SDL_LockTexture(static_cast<SDL_Texture*>(texture), rect, pixels, pitch) == 0;
}
void SDL2Renderer::UnlockTexture(TextureHandle texture) {
SDL_UnlockTexture(static_cast<SDL_Texture*>(texture));
}
/**
* @brief Clears the screen with the current draw color.
*/

View File

@@ -29,6 +29,10 @@ public:
void UpdateTexture(TextureHandle texture, const Bitmap& bitmap) override;
void DestroyTexture(TextureHandle texture) override;
// --- Direct Pixel Access ---
bool LockTexture(TextureHandle texture, SDL_Rect* rect, void** pixels, int* pitch) override;
void UnlockTexture(TextureHandle texture) override;
// --- Rendering Primitives ---
void Clear() override;
void Present() override;

View File

@@ -15,6 +15,7 @@ set(
app/gfx/tilemap.cc
app/gfx/graphics_optimizer.cc
app/gfx/bpp_format_manager.cc
app/gfx/backend/sdl2_renderer.cc
)
# ==============================================================================

View File

@@ -29,7 +29,6 @@
#include "util/macro.h"
namespace yaze {
using core::Renderer;
constexpr int Uncompressed3BPPSize = 0x0600;
namespace {
@@ -106,9 +105,10 @@ absl::StatusOr<std::array<gfx::Bitmap, kNumLinkSheets>> LoadLinkGraphics(
link_graphics[i].Create(gfx::kTilesheetWidth, gfx::kTilesheetHeight,
gfx::kTilesheetDepth, link_sheet_8bpp);
link_graphics[i].SetPalette(rom.palette_group().armors[0]);
if (SDL_Renderer *renderer = Renderer::Get().renderer(); renderer != nullptr) {
Renderer::Get().RenderBitmap(&link_graphics[i]);
}
// TODO: Renderer refactor to use IRenderer or defer for later when GraphicsEditor is opened.
// if (SDL_Renderer *renderer = Renderer::Get().renderer(); renderer != nullptr) {
// Renderer::Get().RenderBitmap(&link_graphics[i]);
// }
}
return link_graphics;
}
@@ -176,9 +176,6 @@ absl::StatusOr<std::array<gfx::Bitmap, kNumGfxSheets>> LoadAllGraphicsData(
std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
std::vector<uint8_t> sheet;
bool bpp3 = false;
SDL_Renderer *renderer = Renderer::Get().renderer();
const bool renderer_ready = renderer != nullptr;
// CRITICAL: Clear the graphics buffer before loading to prevent corruption!
// Without this, multiple ROM loads would accumulate corrupted data.
rom.mutable_graphics_buffer()->clear();
@@ -211,26 +208,6 @@ absl::StatusOr<std::array<gfx::Bitmap, kNumGfxSheets>> LoadAllGraphicsData(
graphics_sheets[i].Create(gfx::kTilesheetWidth, gfx::kTilesheetHeight,
gfx::kTilesheetDepth, converted_sheet);
// DON'T apply palettes here! Each editor (Dungeon, Graphics, GfxGroup)
// should apply its own palette when displaying sheets.
// This allows users to preview sheets with different palettes in each editor.
// The bitmap data is stored as indexed (8bpp), and palettes are applied
// at texture creation time by each editor.
// if (graphics_sheets[i].is_active()) {
// if (i > 115) {
// graphics_sheets[i].SetPaletteWithTransparent(
// rom.palette_group().global_sprites[0], 0);
// } else {
// graphics_sheets[i].SetPaletteWithTransparent(
// rom.palette_group().dungeon_main[0], 0);
// }
// }
// Don't create textures here either - let editors create them with their palettes
// if (!defer_render && renderer_ready) {
// graphics_sheets[i].CreateTexture(renderer);
// }
for (int j = 0; j < graphics_sheets[i].size(); ++j) {
rom.mutable_graphics_buffer()->push_back(graphics_sheets[i].at(j));