yeti3dpro-editor/source/editor.cpp
2024-10-14 16:27:40 -05:00

157 lines
4 KiB
C++

#include "editor.hpp"
#include "game.h"
#include "maps.h"
static bool show_demo_window = true;
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;
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);
if (SDL_LockTexture(texture, NULL, &pixels, &pitch))
{
SDL_memcpy(pixels, (void *)backbuffer, sizeof(backbuffer));
SDL_UnlockTexture(texture);
}
}
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);
load_map(0);
}
void editor_main(void)
{
// main menu bar
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
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);
// 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();
}
// tick yeti state
editor_yeti_tick();
}
void editor_quit(void)
{
SDL_DestroyTexture(texture);
}