yeti3dpro-editor/source/editor.cpp

221 lines
5.4 KiB
C++
Raw Permalink Normal View History

2024-10-14 11:53:16 -05:00
2024-10-14 18:48:34 -05:00
#include <fstream>
#include <cstring>
2024-10-14 21:04:52 -05:00
#include "ImGuiFileDialog.h"
2024-10-14 11:53:16 -05:00
#include "editor.hpp"
#include "game.h"
2024-10-14 16:27:40 -05:00
#include "maps.h"
2024-10-14 11:53:16 -05:00
static bool show_demo_window = true;
2024-10-14 16:27:40 -05:00
static bool game_focused = false;
static yeti_t yeti = {};
2024-10-14 11:53:16 -05:00
static u16 backbuffer[YETI_FRAMEBUFFER_WIDTH * YETI_FRAMEBUFFER_HEIGHT];
static SDL_Texture *texture = nullptr;
2024-10-14 18:48:34 -05:00
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] = {};
2024-10-14 16:27:40 -05:00
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"
};
2024-10-14 18:48:34 -05:00
rom_map_t current_map = {};
static void load_rom_map(int n)
2024-10-14 16:27:40 -05:00
{
game_load_map(&yeti, rom_maps[n]);
game_goto(&yeti.game, GAME_MODE_PLAY);
2024-10-14 18:48:34 -05:00
map_just_loaded = true;
2024-10-14 21:04:52 -05:00
SDL_snprintf(game_window_title, sizeof(game_window_title), "Game Window (rom://%s)", rom_map_names[n]);
2024-10-14 18:48:34 -05:00
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;
2024-10-14 21:04:52 -05:00
SDL_snprintf(game_window_title, sizeof(game_window_title), "Game Window (%s)", filename);
2024-10-14 16:27:40 -05:00
}
2024-10-14 11:53:16 -05:00
static void editor_yeti_tick(void)
{
2024-10-14 15:08:21 -05:00
void *pixels;
int pitch;
2024-10-14 16:27:40 -05:00
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];
}
2024-10-14 11:53:16 -05:00
game_loop(&yeti);
2024-10-14 15:08:21 -05:00
if (SDL_LockTexture(texture, NULL, &pixels, &pitch))
{
SDL_memcpy(pixels, (void *)backbuffer, sizeof(backbuffer));
SDL_UnlockTexture(texture);
}
2024-10-14 11:53:16 -05:00
}
void editor_init(void)
{
// create yeti screens
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_XBGR1555, SDL_TEXTUREACCESS_STREAMING, YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT);
if (!texture)
die("Error: SDL_CreateTexture(): %s", SDL_GetError());
// init yeti state
yeti_init(&yeti, backbuffer, backbuffer, textures, palette);
yeti_init_lua(&yeti, YETI_GAMMA);
game_init(&yeti);
2024-10-14 16:27:40 -05:00
2024-10-14 18:48:34 -05:00
load_rom_map(0);
2024-10-14 11:53:16 -05:00
}
2024-10-14 21:04:52 -05:00
static const char *map_patterns[2] = {
"*.y3d",
"*.Y3D"
};
2024-10-14 11:53:16 -05:00
void editor_main(void)
{
2024-10-14 21:04:52 -05:00
// tick yeti state
editor_yeti_tick();
2024-10-14 11:53:16 -05:00
// main menu bar
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
2024-10-14 21:04:52 -05:00
if (ImGui::MenuItem("Load"))
{
IGFD::FileDialogConfig config;
config.path = ".";
config.countSelectionMax = 1;
config.flags = ImGuiFileDialogFlags_Modal;
ImGuiFileDialog::Instance()->OpenDialog("Choose Map", "Choose Map", ".y3d", config);
}
if (ImGui::MenuItem("Save", "Ctrl+S"))
{
}
2024-10-14 11:53:16 -05:00
if (ImGui::MenuItem("Quit", "Alt+F4"))
quit(0);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Misc"))
{
ImGui::MenuItem("Show ImGui Demo", "", &show_demo_window);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
// demo window for testing
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
2024-10-14 16:27:40 -05:00
// rom map select
if (ImGui::Begin("ROM Maps", nullptr, ImGuiWindowFlags_None))
{
2024-10-14 18:48:34 -05:00
if (ImGui::BeginListBox("ROM Maps"))
2024-10-14 16:27:40 -05:00
{
for (int n = 0; n < IM_ARRAYSIZE(rom_maps); n++)
{
2024-10-14 18:48:34 -05:00
bool is_selected = (selected_rom_map == n);
ImGuiSelectableFlags flags = (highlighted_rom_map == n) ? ImGuiSelectableFlags_Highlight : 0;
2024-10-14 16:27:40 -05:00
if (ImGui::Selectable(rom_map_names[n], is_selected, flags))
2024-10-14 18:48:34 -05:00
selected_rom_map = n;
2024-10-14 16:27:40 -05:00
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndListBox();
}
2024-10-14 18:48:34 -05:00
if (ImGui::Button("Load Selected ROM Map"))
2024-10-14 16:27:40 -05:00
{
2024-10-14 18:48:34 -05:00
load_rom_map(selected_rom_map);
2024-10-14 16:27:40 -05:00
}
ImGui::End();
}
2024-10-14 11:53:16 -05:00
// game window
2024-10-14 18:48:34 -05:00
if (map_just_loaded)
2024-10-14 16:27:40 -05:00
{
ImGui::SetNextWindowFocus();
2024-10-14 18:48:34 -05:00
map_just_loaded = false;
2024-10-14 16:27:40 -05:00
}
2024-10-14 11:53:16 -05:00
ImGui::SetNextWindowContentSize(ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT));
2024-10-14 16:27:40 -05:00
if (ImGui::Begin(game_window_title, nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse))
2024-10-14 11:53:16 -05:00
{
2024-10-14 16:27:40 -05:00
game_focused = ImGui::IsWindowFocused();
2024-10-14 11:53:16 -05:00
ImGui::Image((ImTextureID)texture, ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT));
ImGui::End();
}
2024-10-14 21:04:52 -05:00
// file selection window
if (ImGuiFileDialog::Instance()->Display("Choose Map"))
{
if (ImGuiFileDialog::Instance()->IsOk())
{
std::string filename = ImGuiFileDialog::Instance()->GetFilePathName();
load_disk_map(filename.c_str());
}
ImGuiFileDialog::Instance()->Close();
}
2024-10-14 11:53:16 -05:00
}
void editor_quit(void)
{
SDL_DestroyTexture(texture);
}