Added setup routine and data for building the surface natively in the editor window

This commit is contained in:
scawful
2022-06-14 09:38:29 -04:00
parent e79e159d30
commit efc45bccd3
2 changed files with 67 additions and 9 deletions

View File

@@ -1,5 +1,8 @@
#include "editor.h" #include "editor.h"
#include "Graphics/palette.h"
#include "Graphics/tile.h"
namespace yaze { namespace yaze {
namespace Application { namespace Application {
namespace Editor { namespace Editor {
@@ -67,6 +70,10 @@ Editor::Editor() {
current_set_.SNESPaletteLocation = 0; current_set_.SNESPaletteLocation = 0;
} }
void Editor::SetupScreen(std::shared_ptr<SDL_Renderer> renderer) {
sdl_renderer_ = renderer;
}
void Editor::UpdateScreen() { void Editor::UpdateScreen() {
const ImGuiIO &io = ImGui::GetIO(); const ImGuiIO &io = ImGui::GetIO();
ImGui::NewFrame(); ImGui::NewFrame();
@@ -81,25 +88,25 @@ void Editor::UpdateScreen() {
DrawYazeMenu(); DrawYazeMenu();
TAB_BAR("##TabBar"); TAB_BAR("##TabBar")
DrawProjectEditor(); DrawProjectEditor();
DrawOverworldEditor(); DrawOverworldEditor();
DrawDungeonEditor(); DrawDungeonEditor();
DrawGraphicsEditor(); DrawGraphicsEditor();
DrawSpriteEditor(); DrawSpriteEditor();
DrawScreenEditor(); DrawScreenEditor();
END_TAB_BAR(); END_TAB_BAR()
ImGui::End(); ImGui::End();
} }
void Editor::DrawYazeMenu() { void Editor::DrawYazeMenu() {
MENU_BAR(); MENU_BAR()
DrawFileMenu(); DrawFileMenu();
DrawEditMenu(); DrawEditMenu();
DrawViewMenu(); DrawViewMenu();
DrawHelpMenu(); DrawHelpMenu();
END_MENU_BAR(); END_MENU_BAR()
if (ImGuiFileDialog::Instance()->Display("ChooseFileDlgKey")) { if (ImGuiFileDialog::Instance()->Display("ChooseFileDlgKey")) {
if (ImGuiFileDialog::Instance()->IsOk()) { if (ImGuiFileDialog::Instance()->IsOk()) {
@@ -251,6 +258,39 @@ void Editor::DrawHelpMenu() const {
} }
} }
void Editor::DrawSurface() {
arranged_tiles_ =
Graphics::TilesPattern::transform(current_set_.tilesPattern, tiles_);
for (unsigned int j = 0; j < arranged_tiles_.size(); j++) {
for (unsigned int i = 0; i < arranged_tiles_[0].size(); i++) {
tile8 tile = arranged_tiles_[j][i];
// SDL_PIXELFORMAT_RGB888 ?
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(
0, 8, 8, SDL_BITSPERPIXEL(3), SDL_PIXELFORMAT_RGB444);
if (surface == nullptr) {
SDL_Log("SDL_CreateRGBSurfaceWithFormat() failed: %s", SDL_GetError());
exit(1);
}
SDL_PixelFormat *format = surface->format;
format->palette = current_palette_.GetSDL_Palette();
char *ptr = (char *)surface->pixels;
for (int k = 0; k < 8; k++) {
for (int l = 0; l < 8; l++) {
ptr[k * 8 + l] = tile.data[k * 8 + l];
}
}
SDL_Texture *texture =
SDL_CreateTextureFromSurface(sdl_renderer_.get(), surface);
if (texture == nullptr) {
std::cout << "Error: " << SDL_GetError() << std::endl;
}
imagesCache[tile.id] = texture;
}
}
}
void Editor::DrawProjectEditor() { void Editor::DrawProjectEditor() {
if (ImGui::BeginTabItem("Project")) { if (ImGui::BeginTabItem("Project")) {
if (ImGui::BeginTable("##projectTable", 2, if (ImGui::BeginTable("##projectTable", 2,
@@ -284,16 +324,27 @@ void Editor::DrawProjectEditor() {
} }
} }
BASIC_BUTTON("BuildSurface") { BASIC_BUTTON("ExtractPalette") {
if (rom_.isLoaded()) { if (rom_.isLoaded()) {
current_palette_ = rom_.ExtractPalette(current_set_); current_palette_ = rom_.ExtractPalette(current_set_);
current_scene_.buildSurface(tiles_, current_palette_,
current_set_.tilesPattern);
} }
} }
for (auto &[key, texture] : current_scene_.imagesCache) { BASIC_BUTTON("BuildSurface") {
ImGui::Image((void *)(SDL_Texture *)texture, ImVec2(8, 8)); if (rom_.isLoaded()) {
DrawSurface();
}
}
static int i = 0;
for (auto &[key, texture] : imagesCache) {
ImGui::Image((void *)(SDL_Texture *)texture, ImVec2(32, 32));
if (i != 16) {
ImGui::SameLine();
} else if ( i == 16 ) {
i = 0;
}
i++;
} }
ImGui::TableNextColumn(); ImGui::TableNextColumn();

View File

@@ -12,6 +12,7 @@
#include "Data/rom.h" #include "Data/rom.h"
#include "Editor/overworld_editor.h" #include "Editor/overworld_editor.h"
#include "Graphics/icons.h" #include "Graphics/icons.h"
#include "Graphics/tile.h"
namespace yaze { namespace yaze {
namespace Application { namespace Application {
@@ -20,6 +21,7 @@ namespace Editor {
class Editor { class Editor {
public: public:
Editor(); Editor();
void SetupScreen(std::shared_ptr<SDL_Renderer> renderer);
void UpdateScreen(); void UpdateScreen();
private: private:
@@ -29,6 +31,7 @@ class Editor {
void DrawViewMenu(); void DrawViewMenu();
void DrawHelpMenu() const; void DrawHelpMenu() const;
void DrawSurface();
void DrawProjectEditor(); void DrawProjectEditor();
void DrawOverworldEditor(); void DrawOverworldEditor();
void DrawDungeonEditor(); void DrawDungeonEditor();
@@ -40,6 +43,10 @@ class Editor {
bool is_loaded_ = true; bool is_loaded_ = true;
std::vector<tile8> tiles_; std::vector<tile8> tiles_;
std::vector<std::vector<tile8>> arranged_tiles_;
std::unordered_map<unsigned int, std::shared_ptr<SDL_Texture>> texture_cache_;
std::unordered_map<unsigned int, SDL_Texture *> imagesCache;
std::shared_ptr<SDL_Renderer> sdl_renderer_;
Data::ROM rom_; Data::ROM rom_;
TextEditor asm_editor_; TextEditor asm_editor_;