add load_disk_map() function

This commit is contained in:
erysdren 2024-10-14 18:48:34 -05:00
parent 228f39c305
commit 911c8b9f8c

View file

@ -1,4 +1,7 @@
#include <fstream>
#include <cstring>
#include "editor.hpp"
#include "game.h"
@ -10,10 +13,11 @@ 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 int selected_rom_map = 0;
static int highlighted_rom_map = -1;
static bool map_just_loaded = false;
static bool rom_map_loaded = true;
static char game_window_title[512] = {};
static rom_map_t *rom_maps[] = {
&map_e1m1, &map_e1m2, &map_e1m3, &map_e1m4, &map_e1m5, &map_e1m6, &map_e1m7, &map_e1m8, &map_e1m9,
@ -27,12 +31,38 @@ static const char *rom_map_names[] = {
"e3m1", "e3m2", "e3m3", "e3m4", "e3m5", "e3m6", "e3m7", "e3m8", "e3m9"
};
static void load_map(int n)
rom_map_t current_map = {};
static void load_rom_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]);
map_just_loaded = true;
SDL_snprintf(game_window_title, sizeof(game_window_title), "Game Window (ROM: %s)", rom_map_names[n]);
rom_map_loaded = true;
}
static void load_disk_map(const char *filename)
{
// open map
std::ifstream file(filename, std::ios::binary);
if (!file.is_open())
die("Failed to open \"%s\"", filename);
// read map
file.read((char *)current_map.cells, sizeof(current_map.cells));
// clean up
file.close();
// goto map
game_load_map(&yeti, &current_map);
game_goto(&yeti.game, GAME_MODE_PLAY);
// setup window
rom_map_loaded = false;
map_just_loaded = true;
SDL_snprintf(game_window_title, sizeof(game_window_title), "Game Window (DISK: %s)", filename);
}
static void editor_yeti_tick(void)
@ -75,7 +105,7 @@ void editor_init(void)
yeti_init_lua(&yeti, YETI_GAMMA);
game_init(&yeti);
load_map(0);
load_rom_map(0);
}
void editor_main(void)
@ -107,15 +137,15 @@ void editor_main(void)
// rom map select
if (ImGui::Begin("ROM Maps", nullptr, ImGuiWindowFlags_None))
{
if (ImGui::BeginListBox("Maps"))
if (ImGui::BeginListBox("ROM 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;
bool is_selected = (selected_rom_map == n);
ImGuiSelectableFlags flags = (highlighted_rom_map == n) ? ImGuiSelectableFlags_Highlight : 0;
if (ImGui::Selectable(rom_map_names[n], is_selected, flags))
selected_map = n;
selected_rom_map = n;
if (is_selected)
ImGui::SetItemDefaultFocus();
@ -124,19 +154,19 @@ void editor_main(void)
ImGui::EndListBox();
}
if (ImGui::Button("Load Selected Map"))
if (ImGui::Button("Load Selected ROM Map"))
{
load_map(selected_map);
load_rom_map(selected_rom_map);
}
ImGui::End();
}
// game window
if (map_loaded)
if (map_just_loaded)
{
ImGui::SetNextWindowFocus();
map_loaded = false;
map_just_loaded = false;
}
ImGui::SetNextWindowContentSize(ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT));