LoadAllGraphicsDataV2 Impl

This commit is contained in:
Justin Scofield
2022-07-23 21:30:17 -04:00
parent 6b04bcf5d6
commit fd73cc1bde
4 changed files with 66 additions and 23 deletions

View File

@@ -5,6 +5,7 @@
#include <cmath> #include <cmath>
#include <unordered_map> #include <unordered_map>
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "app/gfx/bitmap.h" #include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h" #include "app/gfx/snes_palette.h"
@@ -275,7 +276,7 @@ void OverworldEditor::DrawTile8Selector() {
ImVec2(256 + 1, kNumSheetsToLoad * 64 + 1)); ImVec2(256 + 1, kNumSheetsToLoad * 64 + 1));
graphics_bin_canvas_.UpdateContext(); graphics_bin_canvas_.UpdateContext();
if (all_gfx_loaded_) { if (all_gfx_loaded_) {
for (const auto &[key, value] : graphics_bin_) { for (const auto &[key, value] : graphics_bin_v2_) {
int offset = 64 * (key + 1); int offset = 64 * (key + 1);
int top_left_y = graphics_bin_canvas_.GetZeroPoint().y + 2; int top_left_y = graphics_bin_canvas_.GetZeroPoint().y + 2;
if (key >= 1) { if (key >= 1) {
@@ -319,12 +320,18 @@ void OverworldEditor::LoadGraphics() {
rom_.LoadAllGraphicsData(); rom_.LoadAllGraphicsData();
graphics_bin_ = rom_.GetGraphicsBin(); graphics_bin_ = rom_.GetGraphicsBin();
for (auto &[key, value] : graphics_bin_) {
auto tilesheet = value.CreateTiles(); absl::Status graphics_data_status = rom_.LoadAllGraphicsDataV2();
if (!tilesheet.ok()) { if (!graphics_data_status.ok()) {
std::cout << "Error loading" << std::endl; std::cout << "Error " << graphics_data_status.ToString() << std::endl;
}
} }
graphics_bin_v2_ = rom_.GetGraphicsBinV2();
// for (auto &[key, value] : graphics_bin_) {
// auto tilesheet = value.CreateTiles();
// if (!tilesheet.ok()) {
// std::cout << "Error loading" << std::endl;
// }
// }
tile16_blockset_bmp_.Create(128 * 2, 8192 * 2, 8, 1048576); tile16_blockset_bmp_.Create(128 * 2, 8192 * 2, 8, 1048576);
current_gfx_bmp_.Create(128 * 2, 512 * 2, 8, 32768); current_gfx_bmp_.Create(128 * 2, 512 * 2, 8, 32768);
} }

View File

@@ -6,6 +6,7 @@
#include <cmath> #include <cmath>
#include <unordered_map> #include <unordered_map>
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "app/gfx/bitmap.h" #include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h" #include "app/gfx/snes_palette.h"
@@ -63,6 +64,7 @@ class OverworldEditor {
ImGuiTableFlags_SizingStretchSame; ImGuiTableFlags_SizingStretchSame;
std::unordered_map<unsigned int, gfx::Bitmap> graphics_bin_; std::unordered_map<unsigned int, gfx::Bitmap> graphics_bin_;
absl::flat_hash_map<int, gfx::Bitmap> graphics_bin_v2_;
ROM rom_; ROM rom_;
zelda3::Overworld overworld_; zelda3::Overworld overworld_;

View File

@@ -51,7 +51,7 @@ void ROM::Close() {
} }
void ROM::SetupRenderer(std::shared_ptr<SDL_Renderer> renderer) { void ROM::SetupRenderer(std::shared_ptr<SDL_Renderer> renderer) {
sdl_renderer_ = renderer; renderer_ = renderer;
} }
// TODO: check if the rom has a header on load // TODO: check if the rom has a header on load
@@ -100,7 +100,7 @@ void ROM::LoadAllGraphicsData() {
gfx::Bitmap tilesheet_bmp(core::kTilesheetWidth, core::kTilesheetHeight, gfx::Bitmap tilesheet_bmp(core::kTilesheetWidth, core::kTilesheetHeight,
core::kTilesheetDepth, SNES3bppTo8bppSheet(data)); core::kTilesheetDepth, SNES3bppTo8bppSheet(data));
tilesheet_bmp.CreateTexture(sdl_renderer_); tilesheet_bmp.CreateTexture(renderer_);
graphics_bin_[i] = tilesheet_bmp; graphics_bin_[i] = tilesheet_bmp;
for (int j = 0; j < sizeof(data); j++) { for (int j = 0; j < sizeof(data); j++) {
@@ -113,6 +113,42 @@ void ROM::LoadAllGraphicsData() {
master_gfx_bin_ = buffer; master_gfx_bin_ = buffer;
} }
absl::Status ROM::LoadAllGraphicsDataV2() {
Bytes sheet;
int buffer_pos = 0;
for (int i = 0; i < core::NumberOfSheets; i++) {
if (i >= 115 && i <= 126) { // uncompressed sheets
sheet.resize(core::Uncompressed3BPPSize);
auto offset = GetGraphicsAddress(i);
for (int j = 0; j < core::Uncompressed3BPPSize; j++) {
sheet[j] = rom_data_[j + offset];
}
} else {
auto offset = GetGraphicsAddress(i);
absl::StatusOr<Bytes> new_sheet =
DecompressV2(offset, core::UncompressedSheetSize);
if (!new_sheet.ok()) {
return new_sheet.status();
} else {
sheet = std::move(*new_sheet);
}
}
absl::StatusOr<Bytes> converted_sheet = Convert3bppTo8bppSheet(sheet);
if (!converted_sheet.ok()) {
return converted_sheet.status();
} else {
Bytes result = std::move(*converted_sheet);
gfx::Bitmap tilesheet_bmp(core::kTilesheetWidth, core::kTilesheetHeight,
core::kTilesheetDepth, result.data());
tilesheet_bmp.CreateTexture(renderer_);
graphics_bin_v2_[i] = tilesheet_bmp;
}
}
return absl::OkStatus();
}
uint ROM::GetGraphicsAddress(uint8_t offset) const { uint ROM::GetGraphicsAddress(uint8_t offset) const {
uint snes_address = 0; uint snes_address = 0;
uint pc_address = 0; uint pc_address = 0;
@@ -234,15 +270,13 @@ absl::StatusOr<Bytes> ROM::DecompressV2(int offset, int size, bool reversed) {
switch (cmd) { switch (cmd) {
case kCommandDirectCopy: case kCommandDirectCopy:
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++)
buffer[buffer_pos++] = rom_data_[offset++]; buffer[buffer_pos++] = rom_data_[offset++];
}
// Do not advance in the ROM // Do not advance in the ROM
break; break;
case kCommandByteFill: case kCommandByteFill:
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++)
buffer[buffer_pos++] = rom_data_[offset]; buffer[buffer_pos++] = rom_data_[offset];
}
offset += 1; // Advance 1 byte in the ROM offset += 1; // Advance 1 byte in the ROM
break; break;
case kCommandWordFill: case kCommandWordFill:
@@ -254,9 +288,7 @@ absl::StatusOr<Bytes> ROM::DecompressV2(int offset, int size, bool reversed) {
break; break;
case kCommandIncreasingFill: { case kCommandIncreasingFill: {
uchar inc_byte = rom_data_[offset]; uchar inc_byte = rom_data_[offset];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) buffer[buffer_pos++] = inc_byte++;
buffer[buffer_pos++] = inc_byte++;
}
offset += 1; // Advance 1 byte in the ROM offset += 1; // Advance 1 byte in the ROM
} break; } break;
case kCommandRepeatingBytes: { case kCommandRepeatingBytes: {
@@ -265,7 +297,6 @@ absl::StatusOr<Bytes> ROM::DecompressV2(int offset, int size, bool reversed) {
// Reversed byte order for overworld maps // Reversed byte order for overworld maps
if (reversed) { if (reversed) {
auto addr = (rom_data_[offset + 2]) | ((rom_data_[offset + 1]) << 8); auto addr = (rom_data_[offset + 2]) | ((rom_data_[offset + 1]) << 8);
if (addr > buffer_pos) { if (addr > buffer_pos) {
return absl::InternalError( return absl::InternalError(
absl::StrFormat("DecompressOverworldV2: Offset for command " absl::StrFormat("DecompressOverworldV2: Offset for command "
@@ -290,10 +321,6 @@ absl::StatusOr<Bytes> ROM::DecompressV2(int offset, int size, bool reversed) {
offset += 2; // Advance 2 bytes in the ROM offset += 2; // Advance 2 bytes in the ROM
} }
} break; } break;
default: {
return absl::InternalError(
"DecompressV2: Invalid command in the header for decompression");
}
} }
} }
@@ -411,7 +438,7 @@ SDL_Texture *ROM::DrawGraphicsSheet(int offset) {
surface->pixels = sheet_buffer; surface->pixels = sheet_buffer;
std::cout << "Creating texture from surface..." << std::endl; std::cout << "Creating texture from surface..." << std::endl;
SDL_Texture *sheet_texture = nullptr; SDL_Texture *sheet_texture = nullptr;
sheet_texture = SDL_CreateTextureFromSurface(sdl_renderer_.get(), surface); sheet_texture = SDL_CreateTextureFromSurface(renderer_.get(), surface);
if (sheet_texture == nullptr) { if (sheet_texture == nullptr) {
std::cout << "Error: " << SDL_GetError() << std::endl; std::cout << "Error: " << SDL_GetError() << std::endl;
} }

View File

@@ -10,6 +10,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
@@ -41,6 +42,9 @@ class ROM {
public: public:
absl::Status OpenFromFile(const absl::string_view& filename); absl::Status OpenFromFile(const absl::string_view& filename);
// absl::Status SaveOverworld();
// absl::Status SaveDungeons();
void Close(); void Close();
void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer); void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer);
void LoadFromFile(const std::string& path); void LoadFromFile(const std::string& path);
@@ -56,6 +60,7 @@ class ROM {
absl::StatusOr<Bytes> DecompressV2(int offset, int size = 0x800, absl::StatusOr<Bytes> DecompressV2(int offset, int size = 0x800,
bool reversed = false); bool reversed = false);
absl::StatusOr<Bytes> Convert3bppTo8bppSheet(Bytes sheet, int size = 0x1000); absl::StatusOr<Bytes> Convert3bppTo8bppSheet(Bytes sheet, int size = 0x1000);
absl::Status LoadAllGraphicsDataV2();
uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0, uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0,
int size = 0x1000); int size = 0x1000);
@@ -66,8 +71,9 @@ class ROM {
uchar* data() { return current_rom_; } uchar* data() { return current_rom_; }
const uchar* getTitle() const { return title; } const uchar* getTitle() const { return title; }
bool isLoaded() const { return is_loaded_; } bool isLoaded() const { return is_loaded_; }
auto Renderer() { return sdl_renderer_; } auto Renderer() { return renderer_; }
auto GetGraphicsBin() const { return graphics_bin_; } auto GetGraphicsBin() const { return graphics_bin_; }
auto GetGraphicsBinV2() const { return graphics_bin_v2_; }
auto GetMasterGraphicsBin() const { return master_gfx_bin_; } auto GetMasterGraphicsBin() const { return master_gfx_bin_; }
auto GetVRAM() const { return pseudo_vram_; } auto GetVRAM() const { return pseudo_vram_; }
@@ -86,8 +92,9 @@ class ROM {
std::vector<uchar*> decompressed_graphic_sheets_; std::vector<uchar*> decompressed_graphic_sheets_;
std::vector<uchar*> converted_graphic_sheets_; std::vector<uchar*> converted_graphic_sheets_;
std::shared_ptr<SDL_Renderer> sdl_renderer_; std::shared_ptr<SDL_Renderer> renderer_;
std::unordered_map<unsigned int, gfx::Bitmap> graphics_bin_; std::unordered_map<unsigned int, gfx::Bitmap> graphics_bin_;
absl::flat_hash_map<int, gfx::Bitmap> graphics_bin_v2_;
}; };
} // namespace app } // namespace app