158 lines
4.1 KiB
C++
158 lines
4.1 KiB
C++
#include "Sandbox.h"
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <KP3D_Renderer2D.h>
|
|
#include <KP3D_Renderer3D.h>
|
|
#include <KP3D_Console.h>
|
|
|
|
static kp3d::Texture tex;
|
|
static std::vector<kp3d::XYf> 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<Cell> args) -> Cell {
|
|
float x = FromCell<float>(args.front());
|
|
float y = FromCell<float>(args.back());
|
|
points.push_back({x, y});
|
|
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 (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)) m_camera.Move(m_camera.forward, move_speed);
|
|
if (IsKeyDown(kp3d::KEY_S)) m_camera.Move(m_camera.forward, -move_speed);
|
|
if (IsKeyDown(kp3d::KEY_A)) m_camera.Move(m_camera.right, move_speed);
|
|
if (IsKeyDown(kp3d::KEY_D)) m_camera.Move(m_camera.right, -move_speed);
|
|
if (IsKeyDown(kp3d::KEY_SPACE)) m_camera.position.y += move_speed;
|
|
if (IsKeyDown(kp3d::KEY_LSHIFT)) m_camera.position.y -= move_speed;
|
|
if (IsKeyDown(kp3d::KEY_Q)) m_camera.Rotate(kp3d::Camera::AXIS_X, qerf_speed);
|
|
if (IsKeyDown(kp3d::KEY_E)) m_camera.Rotate(kp3d::Camera::AXIS_X, -qerf_speed);
|
|
if (IsKeyDown(kp3d::KEY_R)) m_camera.Rotate(kp3d::Camera::AXIS_Y, qerf_speed);
|
|
if (IsKeyDown(kp3d::KEY_F)) m_camera.Rotate(kp3d::Camera::AXIS_Y, -qerf_speed);
|
|
ResetMouseCursor([&](float x, float y) { m_camera.UpdateMouseLook({x, y}); });
|
|
m_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(m_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();
|
|
}
|