From 9b905a48e4646eacb0c055a3974363d39d11b630 Mon Sep 17 00:00:00 2001 From: Justin Scofield Date: Sun, 12 Jun 2022 12:42:35 -0400 Subject: [PATCH] Continuing the Graphics stuff and removed Events bc im not using it --- src/Application/Core/Controller.cc | 91 +++++++++++----------- src/Application/Data/Overworld.cc | 20 ++--- src/Application/Editor/Editor.cc | 47 ++++-------- src/Application/Editor/Editor.h | 6 +- src/Application/Editor/OverworldEditor.cc | 93 +++++++++++++++++++---- src/Application/Editor/OverworldEditor.h | 2 + src/Application/Events/Event.cc | 12 --- src/Application/Events/Event.h | 23 ------ src/Application/Graphics/Palette.cc | 35 ++++++++- src/Application/Graphics/Palette.h | 8 ++ src/Application/Graphics/Scene.cc | 59 ++++++++------ src/Application/Graphics/Scene.h | 13 ++-- src/Application/Graphics/Tile.cc | 79 +++++++++---------- src/Application/Graphics/Tile.h | 2 - src/Application/Utils/ROM.cc | 50 ++++++++---- src/Application/Utils/ROM.h | 18 +++-- src/CMakeLists.txt | 1 - 17 files changed, 328 insertions(+), 231 deletions(-) delete mode 100644 src/Application/Events/Event.cc delete mode 100644 src/Application/Events/Event.h diff --git a/src/Application/Core/Controller.cc b/src/Application/Core/Controller.cc index d2c0d8dc..9fca7c9e 100644 --- a/src/Application/Core/Controller.cc +++ b/src/Application/Core/Controller.cc @@ -16,6 +16,7 @@ void Controller::onEntry() noexcept(false) { io.KeyMap[ImGuiKey_UpArrow] = SDL_GetScancodeFromKey(SDLK_UP); io.KeyMap[ImGuiKey_DownArrow] = SDL_GetScancodeFromKey(SDLK_DOWN); io.KeyMap[ImGuiKey_Tab] = SDL_GetScancodeFromKey(SDLK_TAB); + io.KeyMap[ImGuiKey_LeftCtrl] = SDL_GetScancodeFromKey(SDLK_LCTRL); active = true; } @@ -26,51 +27,52 @@ void Controller::onInput() { while (SDL_PollEvent(&event)) { switch (event.type) { - case SDL_KEYDOWN: - switch (event.key.keysym.sym) { - case SDLK_UP: - case SDLK_DOWN: - case SDLK_RETURN: - case SDLK_BACKSPACE: - case SDLK_TAB: - io.KeysDown[event.key.keysym.scancode] = (event.type == SDL_KEYDOWN); + case SDL_KEYDOWN: + switch (event.key.keysym.sym) { + case SDLK_UP: + case SDLK_DOWN: + case SDLK_RETURN: + case SDLK_BACKSPACE: + case SDLK_TAB: + io.KeysDown[event.key.keysym.scancode] = + (event.type == SDL_KEYDOWN); + break; + default: + break; + } break; - default: - break; - } - break; - case SDL_KEYUP: { - int key = event.key.keysym.scancode; - IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown)); - io.KeysDown[key] = (event.type == SDL_KEYDOWN); - io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); - io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); - io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); - io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); - break; - } - case SDL_WINDOWEVENT: - switch (event.window.event) { - case SDL_WINDOWEVENT_CLOSE: - active = false; + case SDL_KEYUP: { + int key = event.key.keysym.scancode; + IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown)); + io.KeysDown[key] = (event.type == SDL_KEYDOWN); + io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); + io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); + io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); + io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); break; - case SDL_WINDOWEVENT_SIZE_CHANGED: - io.DisplaySize.x = static_cast(event.window.data1); - io.DisplaySize.y = static_cast(event.window.data2); + } + case SDL_WINDOWEVENT: + switch (event.window.event) { + case SDL_WINDOWEVENT_CLOSE: + active = false; + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: + io.DisplaySize.x = static_cast(event.window.data1); + io.DisplaySize.y = static_cast(event.window.data2); + break; + default: + break; + } + break; + case SDL_TEXTINPUT: + io.AddInputCharactersUTF8(event.text.text); + break; + case SDL_MOUSEWHEEL: + wheel = event.wheel.y; break; default: break; - } - break; - case SDL_TEXTINPUT: - io.AddInputCharactersUTF8(event.text.text); - break; - case SDL_MOUSEWHEEL: - wheel = event.wheel.y; - break; - default: - break; } } @@ -86,7 +88,10 @@ void Controller::onInput() { void Controller::onLoad() { editor.UpdateScreen(); } -void Controller::doRender() { renderer.Render(); } +void Controller::doRender() { + SDL_Delay(10); + renderer.Render(); +} void Controller::onExit() { ImGui_ImplSDLRenderer_Shutdown(); @@ -97,6 +102,6 @@ void Controller::onExit() { SDL_Quit(); } -} // namespace Core -} // namespace Application -} // namespace yaze \ No newline at end of file +} // namespace Core +} // namespace Application +} // namespace yaze \ No newline at end of file diff --git a/src/Application/Data/Overworld.cc b/src/Application/Data/Overworld.cc index cc575b7b..9db9a0f2 100644 --- a/src/Application/Data/Overworld.cc +++ b/src/Application/Data/Overworld.cc @@ -10,17 +10,17 @@ using namespace Core; using namespace Graphics; Overworld::~Overworld() { - for (int i = 0; i < (int) tiles32.size(); i++) { - free(allmapsTilesLW[i]); - free(allmapsTilesDW[i]); - free(allmapsTilesSP[i]); - } - free(allmapsTilesLW); - free(allmapsTilesDW); - free(allmapsTilesSP); + // for (int i = 0; i < (int) tiles32.size(); i++) { + // free(allmapsTilesLW[i]); + // free(allmapsTilesDW[i]); + // free(allmapsTilesSP[i]); + // } + // free(allmapsTilesLW); + // free(allmapsTilesDW); + // free(allmapsTilesSP); - delete[] overworldMapPointer; - delete[] owactualMapPointer; + // delete[] overworldMapPointer; + // delete[] owactualMapPointer; } static TileInfo GetTilesInfo(ushort tile) { diff --git a/src/Application/Editor/Editor.cc b/src/Application/Editor/Editor.cc index a45507cd..9f32ae5d 100644 --- a/src/Application/Editor/Editor.cc +++ b/src/Application/Editor/Editor.cc @@ -93,9 +93,9 @@ void Editor::UpdateScreen() { ImGuiWindowFlags flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoScrollbar | - ImGuiWindowFlags_MenuBar; + ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoTitleBar; - if (!ImGui::Begin(title_.c_str(), nullptr, flags)) { + if (!ImGui::Begin("##YazeMain", nullptr, flags)) { ImGui::End(); return; } @@ -103,12 +103,12 @@ void Editor::UpdateScreen() { DrawYazeMenu(); if (ImGui::BeginTabBar("##TabBar")) { + DrawProjectEditor(); DrawOverworldEditor(); DrawDungeonEditor(); DrawGraphicsEditor(); DrawSpriteEditor(); DrawScreenEditor(); - DrawROMInfo(); ImGui::EndTabBar(); } @@ -121,6 +121,7 @@ void Editor::DrawYazeMenu() { DrawEditMenu(); DrawViewMenu(); DrawHelpMenu(); + ImGui::EndMenuBar(); } @@ -281,24 +282,18 @@ void Editor::DrawHelpMenu() const { } } -// first step would be to decompress all graphics data from the game -// (in alttp that's easy they're all located in the same location all the -// same sheet size 128x32) have a code that convert PC address to SNES and -// vice-versa +void Editor::DrawProjectEditor() { + if (ImGui::BeginTabItem("Project")) { + if (rom.isLoaded()) { + ImGui::Text("Title: %s", rom.getTitle()); + ImGui::Text("Version: %d", rom.getVersion()); + ImGui::Text("ROM Size: %ld", rom.getSize()); + } + + ImGui::EndTabItem(); + } +} -// 1) find the gfx pointers (you could use ZS constant file) -// 2) decompress all the gfx with your lz2 decompressor -// 3) convert the 3bpp snes data into PC 4bpp (probably the hardest part) -// 4) get the tiles32 data -// 5) get the tiles16 data -// 6) get the map32 data (they must be decompressed as well with a lz2 -// variant not the same as gfx compression but pretty similar) 7) get the -// gfx data of the map yeah i forgot that one and load 4bpp in a pseudo vram -// and use that to render tiles on screen 8) try to render the tiles on the -// bitmap in black & white to start 9) get the palettes data and try to find -// how they're loaded in the game that's a big puzzle to solve then 9 you'll -// have an overworld map viewer, in less than few hours if are able to -// understand the data quickly void Editor::DrawOverworldEditor() { if (ImGui::BeginTabItem("Overworld")) { overworld_editor_.Update(); @@ -369,18 +364,6 @@ void Editor::DrawScreenEditor() { } } -void Editor::DrawROMInfo() { - if (ImGui::BeginTabItem("ROM Info")) { - if (rom.isLoaded()) { - ImGui::Text("Title: %s", rom.getTitle()); - ImGui::Text("Version: %d", rom.getVersion()); - ImGui::Text("ROM Size: %ld", rom.getSize()); - } - - ImGui::EndTabItem(); - } -} - } // namespace Editor } // namespace Application } // namespace yaze \ No newline at end of file diff --git a/src/Application/Editor/Editor.h b/src/Application/Editor/Editor.h index 3f9d3f81..717fb5da 100644 --- a/src/Application/Editor/Editor.h +++ b/src/Application/Editor/Editor.h @@ -31,12 +31,12 @@ class Editor { void DrawViewMenu(); void DrawHelpMenu() const; + void DrawProjectEditor(); void DrawOverworldEditor(); void DrawDungeonEditor(); void DrawGraphicsEditor(); void DrawSpriteEditor(); void DrawScreenEditor(); - void DrawROMInfo(); void *rom_data_; bool isLoaded = true; @@ -48,6 +48,10 @@ class Editor { TextEditor::LanguageDefinition language65816Def; OverworldEditor overworld_editor_; + Graphics::Scene current_scene_; + Graphics::SNESPalette current_palette_; + Graphics::TilePreset current_set_; + ImGuiTableFlags toolset_table_flags = ImGuiTableFlags_SizingFixedFit; }; diff --git a/src/Application/Editor/OverworldEditor.cc b/src/Application/Editor/OverworldEditor.cc index 86d6bb1d..66f8969b 100644 --- a/src/Application/Editor/OverworldEditor.cc +++ b/src/Application/Editor/OverworldEditor.cc @@ -8,25 +8,90 @@ #include "Graphics/Bitmap.h" #include "Graphics/Tile.h" +// first step would be to decompress all graphics data from the game +// (in alttp that's easy they're all located in the same location all the +// same sheet size 128x32) have a code that convert PC address to SNES and +// vice-versa + +// 1) find the gfx pointers (you could use ZS constant file) +// 2) decompress all the gfx with your lz2 decompressor +// 3) convert the 3bpp snes data into PC 4bpp (probably the hardest part) +// 4) get the tiles32 data +// 5) get the tiles16 data +// 6) get the map32 data (they must be decompressed as well with a lz2 +// variant not the same as gfx compression but pretty similar) 7) get the +// gfx data of the map yeah i forgot that one and load 4bpp in a pseudo vram +// and use that to render tiles on screen 8) try to render the tiles on the +// bitmap in black & white to start 9) get the palettes data and try to find +// how they're loaded in the game that's a big puzzle to solve then 9 you'll +// have an overworld map viewer, in less than few hours if are able to +// understand the data quickly namespace yaze { namespace Application { namespace Editor { void OverworldEditor::Update() { if (rom_.isLoaded()) { if (!doneLoaded) { - overworld.Load(rom_); - Graphics::CreateAllGfxData(rom_.GetRawData(), allGfx16Ptr); - SDL_Surface *surface = - SDL_CreateRGBSurfaceFrom(allGfx16Ptr, 128, 7104, 4, 64, - 0x0000FF, // red mask - 0x00FF00, // green mask - 0xFF0000, // blue mask - 1); + //overworld.Load(rom_); + // name=The Legend of Zelda - Link Sprites - surface = current_scene_.buildSurface( - rom_.ExtractTiles(4, 2048), palette_, current_set_.tilesPattern); - - gfx_texture = SDL_CreateTextureFromSurface(Core::renderer, surface); + // [rom] + // name= + // type=LoROM + + // [tiles] + // pc_location=80000 + // snes_location=0 + // length=28672 + // bpp=4 + // compression=None + // pattern=normal + + // [tiles_arrangement] + // tiles_per_row=16 + + // [palette] + // pc_location=dd308 + // snes_location=0 + // nozerocolor=true + + // name="The Legend of Zelda - Action Sprites, shields, shovel and book" + + // [rom] + // name= + // type=LoROM + + // [tiles] + // pc_location=c0d64 + // snes_location=0 + // length=1081 + // bpp=3 + // compression=zelda3 + + // [tiles_arrangement] + // tiles_per_row=16 + + // [palette] + // pc_location=0 + // snes_location=0 + // nozerocolor=true + + current_set_.pcTilesLocation = 0x8000; + current_set_.SNESTilesLocation = 0; + current_set_.length = 1000; + current_set_.bpp = 3; + current_set_.compression = "zelda3"; + current_set_.pcPaletteLocation = 0; + current_set_.SNESPaletteLocation = 0; + + palette_.colors.push_back(ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); + palette_.colors.push_back(ImVec4(0.0f, 0.5f, 0.0f, 1.0f)); + palette_.colors.push_back(ImVec4(0.0f, 0.0f, 0.4f, 1.0f)); + palette_.colors.push_back(ImVec4(0.3f, 0.0f, 0.0f, 1.0f)); + palette_.colors.push_back(ImVec4(0.3f, 0.7f, 0.9f, 1.0f)); + palette_.colors.push_back(ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); + + current_scene_.buildSurface(rom_.ExtractTiles(current_set_), palette_, current_set_.tilesPattern); doneLoaded = true; } } @@ -261,7 +326,9 @@ void OverworldEditor::DrawTileSelector() { if (ImGui::BeginTabBar("##TabBar", ImGuiTabBarFlags_FittingPolicyScroll)) { if (ImGui::BeginTabItem("Tile8")) { if (rom_.isLoaded()) { - ImGui::Image((void *)(intptr_t)gfx_texture, ImVec2(128, 7104)); + for (const auto & [key, value] : current_scene_.imagesCache) { + ImGui::Image((void *)(SDL_Texture*)value, ImVec2(8, 8)); + } } ImGui::EndTabItem(); diff --git a/src/Application/Editor/OverworldEditor.h b/src/Application/Editor/OverworldEditor.h index 57512feb..b59ff900 100644 --- a/src/Application/Editor/OverworldEditor.h +++ b/src/Application/Editor/OverworldEditor.h @@ -16,6 +16,8 @@ namespace Editor { using byte = unsigned char; +static constexpr unsigned int k4BPP = 4; + class OverworldEditor { public: void Update(); diff --git a/src/Application/Events/Event.cc b/src/Application/Events/Event.cc deleted file mode 100644 index 5a80c893..00000000 --- a/src/Application/Events/Event.cc +++ /dev/null @@ -1,12 +0,0 @@ -#include "Event.h" - -namespace yaze { -namespace Application { -namespace Events { - -void Event::Assign(const std::function& event) { event_ = event; } -void Event::Trigger() const { event_(); } - -} // namespace Events -} // namespace Application -} // namespace yaze \ No newline at end of file diff --git a/src/Application/Events/Event.h b/src/Application/Events/Event.h deleted file mode 100644 index 7fd9ae82..00000000 --- a/src/Application/Events/Event.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef YAZE_APPLICATION_EVENTS_EVENT_H -#define YAZE_APPLICATION_EVENTS_EVENT_H - -#include - -namespace yaze { -namespace Application { -namespace Events { - -class Event { - public: - void Assign(const std::function & event); - void Trigger() const; - - private: - std::function event_; -}; - -} // namespace Events -} // namespace Application -} // namespace yaze - -#endif // YAZE_APPLICATION_EVENTS_EVENT_H \ No newline at end of file diff --git a/src/Application/Graphics/Palette.cc b/src/Application/Graphics/Palette.cc index c4bafcb9..73a15938 100644 --- a/src/Application/Graphics/Palette.cc +++ b/src/Application/Graphics/Palette.cc @@ -12,6 +12,15 @@ SNESColor::SNESColor() { snes = 0; } +SNESColor::SNESColor(ImVec4 val) { + rgb = val; + m_color col; + col.red = val.x; + col.blue = val.y; + col.green = val.z; + snes = convertcolor_rgb_to_snes(col); +} + void SNESColor::setRgb(ImVec4 val) { rgb = val; m_color col; @@ -38,8 +47,8 @@ SNESPalette::SNESPalette(uint8_t mSize) { } SNESPalette::SNESPalette(char* data) { - //assert((data.size() % 4 == 0) && data.size() <= 32); - //size = data.size() / 2; + // assert((data.size() % 4 == 0) && data.size() <= 32); + // size = data.size() / 2; size = sizeof(data) / 2; for (unsigned i = 0; i < sizeof(data); i += 2) { SNESColor col; @@ -61,16 +70,34 @@ SNESPalette::SNESPalette(std::vector cols) { } char* SNESPalette::encode() { - //char* data(size * 2, 0); + // char* data(size * 2, 0); char* data = new char[size * 2]; for (unsigned int i = 0; i < size; i++) { - //std::cout << QString::number(colors[i].snes, 16); + // std::cout << QString::number(colors[i].snes, 16); data[i * 2] = (char)(colors[i].snes & 0xFF); data[i * 2 + 1] = (char)(colors[i].snes >> 8); } return data; } +SDL_Palette* SNESPalette::GetSDL_Palette() { + SDL_Palette* result = new SDL_Palette; + result->ncolors = size; + SDL_Color* sdl_colors = new SDL_Color[size]; + for (int i = 0; i < size; i++) { + sdl_colors[i].r = (uint8_t) colors[i].rgb.x * 100; + sdl_colors[i].g = (uint8_t) colors[i].rgb.y * 100; + sdl_colors[i].b = (uint8_t) colors[i].rgb.z * 100; + } + result->colors = sdl_colors; + + // store the pointers to free them later + sdl_palettes_.push_back(result); + colors_arrays_.push_back(sdl_colors); + + return result; +} + } // namespace Graphics } // namespace Application } // namespace yaze \ No newline at end of file diff --git a/src/Application/Graphics/Palette.h b/src/Application/Graphics/Palette.h index 3a6ed9a2..6762989d 100644 --- a/src/Application/Graphics/Palette.h +++ b/src/Application/Graphics/Palette.h @@ -1,6 +1,7 @@ #ifndef YAZE_APPLICATION_GRAPHICS_PALETTE_H #define YAZE_APPLICATION_GRAPHICS_PALETTE_H +#include #include #include #include @@ -9,12 +10,14 @@ #include #include + namespace yaze { namespace Application { namespace Graphics { struct SNESColor { SNESColor(); + SNESColor(ImVec4); uint16_t snes; ImVec4 rgb; void setRgb(ImVec4); @@ -31,8 +34,13 @@ class SNESPalette { SNESPalette(std::vector); char* encode(); + + SDL_Palette* GetSDL_Palette(); + uint8_t size; std::vector colors; + std::vector sdl_palettes_; + std::vector colors_arrays_; }; } // namespace Graphics diff --git a/src/Application/Graphics/Scene.cc b/src/Application/Graphics/Scene.cc index 2149cd40..d19dbc5a 100644 --- a/src/Application/Graphics/Scene.cc +++ b/src/Application/Graphics/Scene.cc @@ -4,6 +4,37 @@ namespace yaze { namespace Application { namespace Graphics { +void Scene::buildSurface(const std::vector& tiles, SNESPalette& mPalette, + const TilesPattern& tp) { + arrangedTiles = TilesPattern::transform(tp, tiles); + tilesPattern = tp; + allTiles = tiles; + + for (unsigned int j = 0; j < arrangedTiles.size(); j++) { + for (unsigned int i = 0; i < arrangedTiles[0].size(); i++) { + tile8 tile = arrangedTiles[j][i]; + // SDL_PIXELFORMAT_RGB888 ? + SDL_Surface* newImage = SDL_CreateRGBSurfaceWithFormat( + 0, 8, 8, SDL_BITSPERPIXEL(3), SDL_PIXELFORMAT_RGB444); + SDL_PixelFormat* format = newImage->format; + format->palette = mPalette.GetSDL_Palette(); + + + char* ptr = (char*)newImage->pixels; + + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 8; j++) { + ptr[i * 8 + j] = (char)tile.data[i * 8 + j]; + } + } + + SDL_Texture* texture = + SDL_CreateTextureFromSurface(Core::renderer, newImage); + imagesCache[tile.id] = texture; + } + } +} + void Scene::buildScene(const std::vector& tiles, const SNESPalette mPalette, const TilesPattern& tp) { arrangedTiles = TilesPattern::transform(tp, tiles); @@ -31,30 +62,10 @@ void Scene::buildScene(const std::vector& tiles, // j * newTileItem->boundingRect().width() + j); } } - // unsigned max_w = - // items()[0]->boundingRect().width() * arrangedTiles[0].size() + - // arrangedTiles[0].size(); - // unsigned max_h = items()[0]->boundingRect().width() * arrangedTiles.size() - // + - // arrangedTiles.size(); - // setSceneRect(QRect(0, 0, max_w, max_h)); -} - -SDL_Surface* Scene::buildSurface(const std::vector& tiles, - const SNESPalette mPalette, - const TilesPattern& tp) { - - SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, 128, 7104, SDL_BITSPERPIXEL(4), SDL_PIXELFORMAT_RGB444); - - arrangedTiles = TilesPattern::transform(tp, tiles); - tilesPattern = tp; - allTiles = tiles; - for (unsigned int j = 0; j < arrangedTiles.size(); j++) { - for (unsigned int i = 0; i < arrangedTiles[0].size(); i++) { - tile8 tile = arrangedTiles[j][i]; - - } - } + // unsigned max_w = items()[0]->boundingRect().width() * + // arrangedTiles[0].size() + arrangedTiles[0].size(); unsigned max_h = + // items()[0]->boundingRect().width() * arrangedTiles.size() + + // arrangedTiles.size(); setSceneRect(QRect(0, 0, max_w, max_h)); } void Scene::updateScene() { diff --git a/src/Application/Graphics/Scene.h b/src/Application/Graphics/Scene.h index 18d1da5c..c60c8db3 100644 --- a/src/Application/Graphics/Scene.h +++ b/src/Application/Graphics/Scene.h @@ -10,7 +10,6 @@ #include "Core/Renderer.h" #include "Graphics/Tile.h" - namespace yaze { namespace Application { namespace Graphics { @@ -21,19 +20,21 @@ class Scene { void buildScene(const std::vector& tiles, const SNESPalette mPalette, const TilesPattern& tp); - SDL_Surface* buildSurface(const std::vector& tiles, - const SNESPalette mPalette, const TilesPattern& tp); + void buildSurface(const std::vector& tiles, + SNESPalette& mPalette, const TilesPattern& tp); void updateScene(); void setTilesZoom(unsigned int tileZoom); void setTilesPattern(TilesPattern tp); + std::unordered_map imagesCache; + private: - std::vector allTiles; - std::vector > arrangedTiles; unsigned int tilesZoom; TilesPattern tilesPattern; - // QMap imagesCache; + std::vector allTiles; + std::vector > arrangedTiles; + }; } // namespace Graphics diff --git a/src/Application/Graphics/Tile.cc b/src/Application/Graphics/Tile.cc index a81484f5..94526d3e 100644 --- a/src/Application/Graphics/Tile.cc +++ b/src/Application/Graphics/Tile.cc @@ -46,13 +46,13 @@ char *hexString(const char *str, const unsigned int size) { TilesPattern::TilesPattern() { tilesPerRow = 16; numberOfTiles = 16; - transformVector.push_back(std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 11, 12, 13, 14, 15, 16}); + transformVector.push_back(std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, + 11, 12, 13, 14, 15, 16}); // transformVector.push_back(std::vector{0, 1, 2, 3}); // transformVector.push_back(std::vector{4, 5, 6, 7}); // transformVector.push_back(std::vector{8, 9, 11, 12}); // transformVector.push_back(std::vector{13, 14, 15, 16}); - // default_settings(); } // [pattern] // name = "32x32 B (4x4)" @@ -97,43 +97,43 @@ void TilesPattern::default_settings() { std::cout << transformVector.size() << std::endl; } -bool TilesPattern::load(std::string patternFile) { - // QSettings pFile(patternFile, QSettings::IniFormat); - // name = pFile.value("pattern/name").toString(); - // description = pFile.value("pattern/description").toString(); - // numberOfTiles = pFile.value("pattern/number_of_tile").toInt(); - // std::cout << name; - // // unsigned int nbOfTile = pFile.value("_/number_of_tile").toUInt(); - // std::string patternString = pFile.value("pattern/pattern").toString(); - // std::cout << patternString; +// bool TilesPattern::load(std::string patternFile) { +// QSettings pFile(patternFile, QSettings::IniFormat); +// name = pFile.value("pattern/name").toString(); +// description = pFile.value("pattern/description").toString(); +// numberOfTiles = pFile.value("pattern/number_of_tile").toInt(); +// std::cout << name; +// // unsigned int nbOfTile = pFile.value("_/number_of_tile").toUInt(); +// std::string patternString = pFile.value("pattern/pattern").toString(); +// std::cout << patternString; - // // Pattern String is a array description +// // Pattern String is a array description - // transformVector.clear(); - // QRegExp arrayRegExp("(\\[[\\s|0-F|a-f|,]+\\])"); - // int pos = 0; - // while (arrayRegExp.indexIn(patternString, pos) != -1) { - // std::string arrayString = arrayRegExp.cap(1); - // std::vector tmpVect; - // // std::cout << arrayString; - // unsigned int stringPos = 1; +// transformVector.clear(); +// QRegExp arrayRegExp("(\\[[\\s|0-F|a-f|,]+\\])"); +// int pos = 0; +// while (arrayRegExp.indexIn(patternString, pos) != -1) { +// std::string arrayString = arrayRegExp.cap(1); +// std::vector tmpVect; +// // std::cout << arrayString; +// unsigned int stringPos = 1; - // while (arrayString[stringPos] != ']') { - // while (arrayString[stringPos].isSpace()) stringPos++; - // QRegExp hex("([0-F|a-f]+)"); - // bool ok; - // if (hex.indexIn(arrayString, stringPos) == stringPos) { - // tmpVect.append(hex.cap(1).toInt(&ok, 16)); - // } - // while (arrayString[stringPos].isSpace()) stringPos++; - // stringPos++; // should be the comma - // } - // pos += arrayRegExp.matchedLength(); - // transformVector.append(tmpVect); - // } - // std::cout << transformVector.size() << transformVector; - return true; -} +// while (arrayString[stringPos] != ']') { +// while (arrayString[stringPos].isSpace()) stringPos++; +// QRegExp hex("([0-F|a-f]+)"); +// bool ok; +// if (hex.indexIn(arrayString, stringPos) == stringPos) { +// tmpVect.append(hex.cap(1).toInt(&ok, 16)); +// } +// while (arrayString[stringPos].isSpace()) stringPos++; +// stringPos++; // should be the comma +// } +// pos += arrayRegExp.matchedLength(); +// transformVector.append(tmpVect); +// } +// std::cout << transformVector.size() << transformVector; +// return true; +// } bool TilesPattern::loadPatterns() { // foreach (std::string fileName, patternDirectory.entryList(QDir::Files)) { @@ -165,6 +165,7 @@ std::vector > TilesPattern::transform( unsigned int nbTransform = tiles.size() / numberOfTiles; printf("Tiles size : %d - nbtransform : %d - pattern number of tiles : %d", tiles.size(), nbTransform, numberOfTiles); + if (transPerRow > nbTransform) toret.resize(tVectHeight); else @@ -174,7 +175,7 @@ std::vector > TilesPattern::transform( std::vector > vec(toret); auto it = vec.begin(); - for (auto each : vec) { + for (auto &each : vec) { each.resize(tilesPerRow); } // while (it.hasNext()) { @@ -182,13 +183,13 @@ std::vector > TilesPattern::transform( // } // std::cout << toret[0].size() << "x" << toret.size(); while (repeat != nbTransform) { - // std::cout << "repeat" << repeat; + std::cout << "repeat" << repeat; for (unsigned int j = 0; j < tVectHeight; j++) { for (unsigned int i = 0; i < tVectWidth; i++) { unsigned int posTile = transformVector[j][i] + numberOfTiles * repeat; unsigned int posX = i + repeatOffsetX; unsigned int posY = j + repeatOffsetY; - // qDebug("X: %d - Y: %d - posTile : %d", posX, posY, posTile); + printf("X: %d - Y: %d - posTile : %d", posX, posY, posTile); toret[posY][posX] = tiles[posTile]; } } diff --git a/src/Application/Graphics/Tile.h b/src/Application/Graphics/Tile.h index 9c9a835d..26a84c01 100644 --- a/src/Application/Graphics/Tile.h +++ b/src/Application/Graphics/Tile.h @@ -103,8 +103,6 @@ class TilesPattern { void default_settings(); - bool load(std::string patternFile); - static bool loadPatterns(); static TilesPattern pattern(std::string name); static std::unordered_map Patterns(); diff --git a/src/Application/Utils/ROM.cc b/src/Application/Utils/ROM.cc index 58a90873..09853289 100644 --- a/src/Application/Utils/ROM.cc +++ b/src/Application/Utils/ROM.cc @@ -20,45 +20,67 @@ void ROM::LoadFromFile(const std::string &path) { // Reading data to array of unsigned chars file = fopen(path.c_str(), "r+"); current_rom_ = (unsigned char *)malloc(size); + rom_data_ = (char *) malloc(size); + fread(rom_data_, sizeof(char), size, file); int bytes_read = fread(current_rom_, sizeof(unsigned char), size, file); fclose(file); memcpy(title, current_rom_ + 32704, 21); - type = LoROM; - fastrom = (current_rom_[21] & 0b00110000) == 0b00110000; - if (current_rom_[21] & 1) type = HiROM; - if ((current_rom_[21] & 0b00000111) == 0b00000111) type = ExHiROM; - - sram_size = 0x400 << current_rom_[24]; - creator_id = (current_rom_[26] << 8) | current_rom_[25]; version = current_rom_[27]; loaded = true; } - -std::vector ROM::ExtractTiles(unsigned int bpp, unsigned int length) { +std::vector ROM::ExtractTiles(TilePreset &preset) { + std::cout << "Begin ROM::ExtractTiles" << std::endl; std::vector rawTiles; - unsigned int lastCompressedSize; - unsigned int size = length; - char *data = alttp_decompress_gfx((char*)current_rom_, 0, length, &size, &lastCompressedSize); + uint filePos = 0; + unsigned int size = preset.length; + filePos = + getRomPosition(preset, preset.pcTilesLocation, preset.SNESTilesLocation); + char *data = (char *)rom_data_ + filePos; + memcpy(data, rom_data_ + filePos, preset.length); + + data = + alttp_decompress_gfx(data, 0, preset.length, &size, &lastCompressedSize); + std::cout << "size: " << size << std::endl; + std::cout << "lastCompressedSize: " << lastCompressedSize << std::endl; if (data == NULL) { + std:: cout << alttp_decompression_error << std::endl; return rawTiles; } unsigned tileCpt = 0; - for (unsigned int tilePos = 0; tilePos < size; tilePos += bpp * 8) { - tile8 newTile = unpack_bpp_tile(data, tilePos, bpp); + for (unsigned int tilePos = 0; tilePos < size; tilePos += preset.bpp * 8) { + std::cout << "Unpacking tile..." << std::endl; + tile8 newTile = unpack_bpp_tile(data, tilePos, preset.bpp); newTile.id = tileCpt; rawTiles.push_back(newTile); tileCpt++; } free(data); + std::cout << "End ROM::ExtractTiles" << std::endl; return rawTiles; } +unsigned int ROM::getRomPosition(const TilePreset &preset, int directAddr, + unsigned int snesAddr) { + bool romHasHeader = false; // romInfo.hasHeader + if (overrideHeaderInfo) romHasHeader = overridenHeaderInfo; + unsigned int filePos = -1; + enum rom_type rType = LoROM; + std::cout << "ROM::getRomPosition: directAddr:" << directAddr << std::endl; + if (directAddr == -1) { + filePos = rommapping_snes_to_pc(snesAddr, rType, romHasHeader); + } else { + filePos = directAddr; + if (romHasHeader) filePos += 0x200; + } + std::cout << "ROM::getRomPosition: filePos:" << filePos << std::endl; + return filePos; +} int ROM::SnesToPc(int addr) { if (addr >= 0x808000) { diff --git a/src/Application/Utils/ROM.h b/src/Application/Utils/ROM.h index 498f2562..4235ba68 100644 --- a/src/Application/Utils/ROM.h +++ b/src/Application/Utils/ROM.h @@ -31,9 +31,11 @@ int AddressFromBytes(byte addr1, byte addr2, byte addr3); class ROM { public: void LoadFromFile(const std::string& path); - std::vector ExtractTiles(unsigned int bpp, unsigned int length); + std::vector ExtractTiles(TilePreset& preset); + unsigned int getRomPosition(const TilePreset& preset, int directAddr, + unsigned int snesAddr); - int SnesToPc(int addr); + int SnesToPc(int addr); short AddressFromBytes(byte addr1, byte addr2); ushort ReadShort(int addr); void Write(int addr, byte value); @@ -55,17 +57,19 @@ class ROM { bool loaded = false; byte* current_rom_; + char* rom_data_; + + bool overrideHeaderInfo; + bool overridenHeaderInfo; + unsigned int lastUnCompressSize; + unsigned int lastCompressedSize; + unsigned int lastCompressSize; enum rom_type type; bool fastrom; - bool make_sense; unsigned char title[21]; long int size; - unsigned int sram_size; - uint16_t creator_id; unsigned char version; - unsigned char checksum_comp; - unsigned char checksum; }; } // namespace Utils diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f310de90..d3f8b581 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,7 +45,6 @@ add_executable( Application/Graphics/Scene.cc Application/Editor/Editor.cc Application/Editor/OverworldEditor.cc - Application/Events/Event.cc Application/Utils/Compression.cc Application/Utils/ROM.cc # GUI libraries