From d120cd005a187374c4e213d5affa62096e0e2336 Mon Sep 17 00:00:00 2001 From: erysdren Date: Mon, 14 Oct 2024 11:53:16 -0500 Subject: [PATCH] Initial commit --- .gitignore | 2 + .gitmodules | 6 +++ CMakeLists.txt | 35 +++++++++++++++ README.md | 5 +++ imgui | 1 + source/editor.cpp | 84 ++++++++++++++++++++++++++++++++++++ source/editor.hpp | 18 ++++++++ source/main.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++ y3d | 1 + 9 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 160000 imgui create mode 100644 source/editor.cpp create mode 100644 source/editor.hpp create mode 100644 source/main.cpp create mode 160000 y3d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..70c6c50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +untracked/ +cmake-build*/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1c6696b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "imgui"] + path = imgui + url = https://git.erysdren.me/mirrors/imgui.git +[submodule "y3d"] + path = y3d + url = https://git.erysdren.me/erysdren/yeti3dpro-sdl3 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bc7b5f9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.28) +project(yeti3dpro-editor LANGUAGES C CXX) + +find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared) + +# import y3d + +add_subdirectory(${PROJECT_SOURCE_DIR}/y3d) + +# import imgui + +add_library(imgui STATIC) +target_sources(imgui PRIVATE + ${PROJECT_SOURCE_DIR}/imgui/imgui_demo.cpp + ${PROJECT_SOURCE_DIR}/imgui/imgui_draw.cpp + ${PROJECT_SOURCE_DIR}/imgui/imgui_tables.cpp + ${PROJECT_SOURCE_DIR}/imgui/imgui_widgets.cpp + ${PROJECT_SOURCE_DIR}/imgui/imgui.cpp + ${PROJECT_SOURCE_DIR}/imgui/backends/imgui_impl_sdl3.cpp + ${PROJECT_SOURCE_DIR}/imgui/backends/imgui_impl_sdlrenderer3.cpp +) +target_include_directories(imgui PUBLIC + ${PROJECT_SOURCE_DIR}/imgui + ${PROJECT_SOURCE_DIR}/imgui/backends +) +target_link_libraries(imgui PUBLIC SDL3::SDL3) + +# editor + +add_executable(yeti3dpro-editor) +target_sources(yeti3dpro-editor PRIVATE + ${PROJECT_SOURCE_DIR}/source/main.cpp + ${PROJECT_SOURCE_DIR}/source/editor.cpp +) +target_link_libraries(yeti3dpro-editor PRIVATE imgui y3d game) diff --git a/README.md b/README.md new file mode 100644 index 0000000..b386c8a --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Yeti3D PRO SDL3 Port + +| | | | +|--------------------------------|--------------------------------|--------------------------------| +| ![](./screenshots/yeti006.png) | ![](./screenshots/yeti007.png) | ![](./screenshots/yeti008.png) | diff --git a/imgui b/imgui new file mode 160000 index 0000000..349af87 --- /dev/null +++ b/imgui @@ -0,0 +1 @@ +Subproject commit 349af8766cbb6ea7e56b242b18be8476bebbe977 diff --git a/source/editor.cpp b/source/editor.cpp new file mode 100644 index 0000000..7cfc74c --- /dev/null +++ b/source/editor.cpp @@ -0,0 +1,84 @@ + +#include "editor.hpp" + +#include "game.h" + +static bool show_demo_window = true; +static yeti_t yeti; +static u16 backbuffer[YETI_FRAMEBUFFER_WIDTH * YETI_FRAMEBUFFER_HEIGHT]; +static SDL_Texture *texture = nullptr; + +static void editor_yeti_tick(void) +{ + 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); + + SDL_UpdateTexture(texture, nullptr, backbuffer, YETI_FRAMEBUFFER_WIDTH * sizeof(u16)); +} + +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); +} + +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); + + // game window + ImGui::SetNextWindowContentSize(ImVec2(YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT)); + if (ImGui::Begin("Game Window", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) + { + 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); +} diff --git a/source/editor.hpp b/source/editor.hpp new file mode 100644 index 0000000..1a6bf9b --- /dev/null +++ b/source/editor.hpp @@ -0,0 +1,18 @@ + +#ifndef _EDITOR_HPP_ +#define _EDITOR_HPP_ + +#include +#include "imgui.h" + +extern SDL_Window *window; +extern SDL_Renderer *renderer; + +void quit(int code); +void die(const char *fmt, ...); + +void editor_init(void); +void editor_main(void); +void editor_quit(void); + +#endif // _EDITOR_HPP_ diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..564765a --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,107 @@ + +#include "imgui.h" +#include "imgui_impl_sdl3.h" +#include "imgui_impl_sdlrenderer3.h" +#include +#include + +#include "editor.hpp" + +SDL_Window *window = nullptr; +SDL_Renderer *renderer = nullptr; + +void quit(int code) +{ + editor_quit(); + + ImGui_ImplSDLRenderer3_Shutdown(); + ImGui_ImplSDL3_Shutdown(); + ImGui::DestroyContext(); + + if (window) SDL_DestroyRenderer(renderer); + if (renderer) SDL_DestroyWindow(window); + SDL_Quit(); + + exit(code); +} + +void die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, fmt, ap); + va_end(ap); + + quit(1); +} + +int main(int argc, char **argv) +{ + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) + die("Error: SDL_Init(): %s", SDL_GetError()); + + window = SDL_CreateWindow("Yeti3D World Editor", 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_MAXIMIZED); + if (!window) + die("Error: SDL_CreateWindow(): %s", SDL_GetError()); + + renderer = SDL_CreateRenderer(window, nullptr); + SDL_SetRenderVSync(renderer, 1); + if (!renderer) + die("Error: SDL_CreateRenderer(): %s", SDL_GetError()); + + SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_ShowWindow(window); + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + + ImGui::StyleColorsDark(); + + ImGui_ImplSDL3_InitForSDLRenderer(window, renderer); + ImGui_ImplSDLRenderer3_Init(renderer); + + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + + editor_init(); + + while (true) + { + SDL_Event event; + + while (SDL_PollEvent(&event)) + { + ImGui_ImplSDL3_ProcessEvent(&event); + if (event.type == SDL_EVENT_QUIT) + quit(0); + if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) + quit(0); + } + + if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) + { + SDL_Delay(10); + continue; + } + + ImGui_ImplSDLRenderer3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + ImGui::NewFrame(); + + editor_main(); + + ImGui::Render(); + SDL_SetRenderScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); + SDL_SetRenderDrawColorFloat(renderer, clear_color.x, clear_color.y, clear_color.z, clear_color.w); + SDL_RenderClear(renderer); + ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer); + SDL_RenderPresent(renderer); + } + + quit(0); + + return 0; +} diff --git a/y3d b/y3d new file mode 160000 index 0000000..86ba27d --- /dev/null +++ b/y3d @@ -0,0 +1 @@ +Subproject commit 86ba27d73dd67e99b66cd13adb13dd3c3e2c7dd8