KP3Dii/Sandbox/src/Sandbox.cpp

116 lines
3 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>
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};
}});
}
Sandbox::~Sandbox()
{
}
void Sandbox::Update()
{
// Console shortcut
if (IsKeyDown(kp3d::KEY_GRAVE))
{
kp3d::console::open ^= 1;
2024-07-24 05:50:42 +00:00
SetMouseLockEnabled(!GetMouseLockEnabled());
2024-07-22 07:48:42 +00:00
KeyReset(kp3d::KEY_GRAVE);
}
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))
{
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;
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;
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.0f, 0.1f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 3D scene
kp3d::Renderer3D::Begin(m_camera, m_projection);
map.Render();
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();
// UI
kp3d::console::Render();
}