#include "Sandbox.h" #include #include #include #include #include #include static kp3d::Texture tex; static std::vector points; Sandbox::Sandbox(const std::string& path): kp3d::Game(path, "sandbox-cfg.json", "sandbox-log.txt") { tex.Load("logo.png"); m_projection.InitPerspective(70.0f, (float) GetWidth() / (float) GetHeight()); map.Init(); using namespace ksi; kp3d::console::environment.Set("spawn-thingy", {CELL_FUNCTION_CPP, [&](std::list args) -> Cell { float x = FromCell(args.front()); float y = FromCell(args.back()); points.push_back({x, y}); return {CELL_VOID}; }}); kp3d::console::environment.Set("edit", {CELL_NUMBER, MODE_EDIT}); kp3d::console::environment.Set("game", {CELL_NUMBER, MODE_GAME}); kp3d::console::environment.Set("edit", {CELL_NUMBER, MODE_MENU}); kp3d::console::environment.Set("set-mode", {CELL_FUNCTION_CPP, [&](std::list args) -> Cell { if (args.empty()) return {CELL_VOID}; int mode = (int) FromCell(args.back()); m_mode = mode; kp3d::console::open = false; SetMouseLockEnabled(true); return {CELL_VOID}; }}); m_mode = MODE_MENU; } Sandbox::~Sandbox() { } void Sandbox::Update() { // Console shortcut if (IsKeyDown(kp3d::KEY_GRAVE)) { kp3d::console::open ^= 1; if (GetMouseLockEnabled()) SetMouseLockEnabled(false); KeyReset(kp3d::KEY_GRAVE); } if (kp3d::console::open || m_mode == MODE_MENU) return; if (m_mode == MODE_EDIT) editor.Update(); if (IsKeyDown(kp3d::KEY_F1)) { map.render_wireframe ^= 1; KeyReset(kp3d::KEY_F1); } if (IsKeyDown(kp3d::KEY_F2)) { map.Rebuild(kp3d::GEN_NORMALS); KeyReset(kp3d::KEY_F2); } if (IsKeyDown(kp3d::KEY_F3)) { map.Rebuild(kp3d::GEN_SMOOTH_NORMALS); KeyReset(kp3d::KEY_F3); } if (IsKeyDown(kp3d::KEY_B)) { //map.Init(); //map.Rebuild(kp3d::GEN_NORMALS); //KeyReset(kp3d::KEY_B); } if (IsKeyDown(kp3d::KEY_Y)) map.test_u += 0.1f; if (IsKeyDown(kp3d::KEY_H)) map.test_u -= 0.1f; if (IsKeyDown(kp3d::KEY_U)) map.test_l += 0.1f; if (IsKeyDown(kp3d::KEY_J)) map.test_l -= 0.1f; // Mouse-look if (IsKeyDown(kp3d::KEY_ESCAPE)) { SetMouseLockEnabled(!GetMouseLockEnabled()); KeyReset(kp3d::KEY_ESCAPE); } float qerf_speed = 1.0f; float move_speed = 0.2f; if (IsKeyDown(kp3d::KEY_W)) camera.Move(camera.forward, move_speed); if (IsKeyDown(kp3d::KEY_S)) camera.Move(camera.forward, -move_speed); if (IsKeyDown(kp3d::KEY_A)) camera.Move(camera.right, move_speed); if (IsKeyDown(kp3d::KEY_D)) camera.Move(camera.right, -move_speed); //if (IsKeyDown(kp3d::KEY_SPACE)) camera.position.y += move_speed; //if (IsKeyDown(kp3d::KEY_LSHIFT)) camera.position.y -= move_speed; //if (IsKeyDown(kp3d::KEY_Q)) camera.Rotate(kp3d::Camera::AXIS_X, qerf_speed); //if (IsKeyDown(kp3d::KEY_E)) camera.Rotate(kp3d::Camera::AXIS_X, -qerf_speed); //if (IsKeyDown(kp3d::KEY_R)) camera.Rotate(kp3d::Camera::AXIS_Y, qerf_speed); //if (IsKeyDown(kp3d::KEY_F)) camera.Rotate(kp3d::Camera::AXIS_Y, -qerf_speed); ResetMouseCursor([&](float x, float y) { camera.UpdateMouseLook({x, y}); }); camera.UpdateMatrix(); } void Sandbox::Render() { glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (m_mode == MODE_GAME || m_mode == MODE_EDIT) { // 3D scene kp3d::Renderer3D::Begin(camera, m_projection); map.Render(); if (m_mode == MODE_EDIT) editor.RenderMap(); kp3d::Renderer3D::DrawBillboard(tex, {}, { 1, 1 }); kp3d::Renderer3D::End(); // 2D scene kp3d::Renderer2D::Begin(); for (auto& p : points) kp3d::Renderer2D::DrawTexture(tex, p.x, p.y, 50, 50); kp3d::Renderer2D::End(); if (m_mode == MODE_EDIT) editor.RenderUI(); } else if (m_mode == MODE_MENU) { RenderMenu(); } // UI kp3d::console::Render(); } void Sandbox::RenderMenu() { // SOUL utilitarian Cube 2: Sauerbraten-esque sorry ass excuse for a main menu (swag) ImGui::SetNextWindowPos({(float) GetWidth() * 0.5f - 320.0f * 0.5f, (float) GetHeight() * 0.5f - 240.0f * 0.5f}); ImGui::SetNextWindowSize({320, 240}); ImGui::Begin("KP3D", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize); ImGui::Image( ImTextureID(tex.GetGLID()), {ImGui::GetWindowSize().x - 20, (ImGui::GetWindowSize().x - 20) / ((float) tex.GetWidth() / (float) tex.GetHeight())} ); ImGui::Separator(); if (ImGui::Button("Launch game")) m_mode = MODE_GAME; if (ImGui::Button("Launch editor")) m_mode = MODE_EDIT; ImGui::Separator(); if (ImGui::Button("Exit")) Exit(kp3d::SUCCESS); ImGui::Separator(); ImGui::Text("Version %.02f", KP3D_VERSION); ImGui::Text("Copyright 2018-2024 kpworld.xyz"); ImGui::End(); }