KP3Dii/Sandbox/src/Sandbox.cpp

183 lines
4.9 KiB
C++
Raw Normal View History

2024-07-22 07:48:42 +00:00
#include "Sandbox.h"
#include <imgui.h>
#include <GL/glew.h>
#include <KP3D_Renderer2D.h>
#include <KP3D_Renderer3D.h>
#include <KP3D_Console.h>
2024-07-24 08:56:45 +00:00
#include <KP3D_StringUtils.h>
2024-07-29 22:06:31 +00:00
#include <KP3D_Resources.h>
2024-07-22 07:48:42 +00:00
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")
{
2024-07-29 22:06:31 +00:00
kp3d::res::LoadMaterials();
2024-07-22 07:48:42 +00:00
tex.Load("logo.png");
2024-07-28 02:07:41 +00:00
crosshair.Load(".kp3d/crosshair.png");
2024-07-31 03:28:17 +00:00
projection.InitPerspective(70.0f, (float) GetWidth() / (float) GetHeight());
2024-07-22 07:48:42 +00:00
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};
}});
2024-07-24 08:56:45 +00:00
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<Cell> args) -> Cell {
if (args.empty()) return {CELL_VOID};
int mode = (int) FromCell<float>(args.back());
m_mode = mode;
kp3d::console::open = false;
SetMouseLockEnabled(true);
return {CELL_VOID};
}});
2024-07-24 08:17:46 +00:00
m_mode = MODE_MENU;
2024-07-22 07:48:42 +00:00
}
Sandbox::~Sandbox()
{
}
void Sandbox::Update()
{
// Console shortcut
if (IsKeyDown(kp3d::KEY_GRAVE))
{
kp3d::console::open ^= 1;
2024-07-24 08:17:46 +00:00
if (GetMouseLockEnabled())
SetMouseLockEnabled(false);
2024-07-22 07:48:42 +00:00
KeyReset(kp3d::KEY_GRAVE);
}
2024-07-24 08:17:46 +00:00
if (kp3d::console::open || m_mode == MODE_MENU)
return;
2024-07-24 18:29:59 +00:00
if (m_mode == MODE_EDIT)
editor.Update();
2024-07-22 07:48:42 +00:00
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);
}
2024-07-23 03:25:33 +00:00
if (IsKeyDown(kp3d::KEY_B))
{
2024-07-24 18:29:59 +00:00
//map.Init();
2024-07-23 03:25:33 +00:00
//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;
2024-07-22 07:48:42 +00:00
// Mouse-look
if (IsKeyDown(kp3d::KEY_ESCAPE))
{
SetMouseLockEnabled(!GetMouseLockEnabled());
KeyReset(kp3d::KEY_ESCAPE);
}
float qerf_speed = 1.0f;
float move_speed = 0.2f;
2024-07-24 18:29:59 +00:00
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);
2024-07-27 22:27:31 +00:00
//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);
2024-07-24 18:29:59 +00:00
ResetMouseCursor([&](float x, float y) { camera.UpdateMouseLook({x, y}); });
camera.UpdateMatrix();
2024-07-22 07:48:42 +00:00
}
void Sandbox::Render()
{
2024-07-24 08:17:46 +00:00
glClearColor(0, 0, 0, 1);
2024-07-22 07:48:42 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2024-07-24 08:17:46 +00:00
if (m_mode == MODE_GAME || m_mode == MODE_EDIT)
{
// 3D scene
2024-07-31 03:28:17 +00:00
kp3d::Renderer3D::Begin(camera, projection);
2024-07-24 08:17:46 +00:00
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);
2024-07-28 02:07:41 +00:00
kp3d::Renderer2D::DrawTexture(
crosshair,
(float) GetWidth() * 0.5f - (float) crosshair.GetWidth() * 0.5f,
(float) GetHeight() * 0.5f - (float) crosshair.GetHeight() * 0.5f
);
2024-07-24 08:17:46 +00:00
kp3d::Renderer2D::End();
if (m_mode == MODE_EDIT)
editor.RenderUI();
}
else if (m_mode == MODE_MENU)
{
RenderMenu();
}
2024-07-22 07:48:42 +00:00
// UI
kp3d::console::Render();
}
2024-07-24 08:17:46 +00:00
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();
}