diff --git a/source/editor.cpp b/source/editor.cpp index 52147a7..7200fdc 100644 --- a/source/editor.cpp +++ b/source/editor.cpp @@ -2,27 +2,57 @@ #include "editor.hpp" #include "game.h" +#include "maps.h" static bool show_demo_window = true; -static yeti_t yeti; +static bool game_focused = false; +static yeti_t yeti = {}; static u16 backbuffer[YETI_FRAMEBUFFER_WIDTH * YETI_FRAMEBUFFER_HEIGHT]; static SDL_Texture *texture = nullptr; +static int selected_map = 0; +static int highlighted_map = -1; +static bool map_loaded = false; +static char game_window_title[256] = {}; + +static rom_map_t *rom_maps[] = { + &map_e1m1, &map_e1m2, &map_e1m3, &map_e1m4, &map_e1m5, &map_e1m6, &map_e1m7, &map_e1m8, &map_e1m9, + &map_e2m1, &map_e2m2, &map_e2m3, &map_e2m4, &map_e2m5, &map_e2m6, &map_e2m7, &map_e2m8, &map_e2m9, + &map_e3m1, &map_e3m2, &map_e3m3, &map_e3m4, &map_e3m5, &map_e3m6, &map_e3m7, &map_e3m8, &map_e3m9 +}; + +static const char *rom_map_names[] = { + "e1m1", "e1m2", "e1m3", "e1m4", "e1m5", "e1m6", "e1m7", "e1m8", "e1m9", + "e2m1", "e2m2", "e2m3", "e2m4", "e2m5", "e2m6", "e2m7", "e2m8", "e2m9", + "e3m1", "e3m2", "e3m3", "e3m4", "e3m5", "e3m6", "e3m7", "e3m8", "e3m9" +}; + +static void load_map(int n) +{ + game_load_map(&yeti, rom_maps[n]); + game_goto(&yeti.game, GAME_MODE_PLAY); + map_loaded = true; + SDL_snprintf(game_window_title, sizeof(game_window_title), "Game Window (%s)", rom_map_names[n]); +} + static void editor_yeti_tick(void) { void *pixels; int pitch; - const bool *keys = SDL_GetKeyboardState(nullptr); - yeti.keyboard.state.up = keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]; - yeti.keyboard.state.down = keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]; - yeti.keyboard.state.left = keys[SDL_SCANCODE_LEFT]; - yeti.keyboard.state.right = keys[SDL_SCANCODE_RIGHT]; - yeti.keyboard.state.a = keys[SDL_SCANCODE_RCTRL]; - yeti.keyboard.state.b = keys[SDL_SCANCODE_SPACE]; - yeti.keyboard.state.l = keys[SDL_SCANCODE_A]; - yeti.keyboard.state.r = keys[SDL_SCANCODE_Z]; - yeti.keyboard.state.select = keys[SDL_SCANCODE_ESCAPE]; + if (game_focused) + { + const bool *keys = SDL_GetKeyboardState(nullptr); + yeti.keyboard.state.up = keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]; + yeti.keyboard.state.down = keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]; + yeti.keyboard.state.left = keys[SDL_SCANCODE_LEFT]; + yeti.keyboard.state.right = keys[SDL_SCANCODE_RIGHT]; + yeti.keyboard.state.a = keys[SDL_SCANCODE_RCTRL]; + yeti.keyboard.state.b = keys[SDL_SCANCODE_SPACE]; + yeti.keyboard.state.l = keys[SDL_SCANCODE_A]; + yeti.keyboard.state.r = keys[SDL_SCANCODE_Z]; + yeti.keyboard.state.select = keys[SDL_SCANCODE_ESCAPE]; + } game_loop(&yeti); @@ -44,6 +74,8 @@ void editor_init(void) yeti_init(&yeti, backbuffer, backbuffer, textures, palette); yeti_init_lua(&yeti, YETI_GAMMA); game_init(&yeti); + + load_map(0); } void editor_main(void) @@ -72,10 +104,45 @@ void editor_main(void) if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); - // game window - ImGui::SetNextWindowContentSize(ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT)); - if (ImGui::Begin("Game Window", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) + // rom map select + if (ImGui::Begin("ROM Maps", nullptr, ImGuiWindowFlags_None)) { + if (ImGui::BeginListBox("Maps")) + { + for (int n = 0; n < IM_ARRAYSIZE(rom_maps); n++) + { + bool is_selected = (selected_map == n); + ImGuiSelectableFlags flags = (highlighted_map == n) ? ImGuiSelectableFlags_Highlight : 0; + + if (ImGui::Selectable(rom_map_names[n], is_selected, flags)) + selected_map = n; + + if (is_selected) + ImGui::SetItemDefaultFocus(); + } + + ImGui::EndListBox(); + } + + if (ImGui::Button("Load Selected Map")) + { + load_map(selected_map); + } + + ImGui::End(); + } + + // game window + if (map_loaded) + { + ImGui::SetNextWindowFocus(); + map_loaded = false; + } + + ImGui::SetNextWindowContentSize(ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT)); + if (ImGui::Begin(game_window_title, nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) + { + game_focused = ImGui::IsWindowFocused(); ImGui::Image((ImTextureID)texture, ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT)); ImGui::End(); }