input
This commit is contained in:
@@ -28,7 +28,6 @@ void EngineWindow::Startup(SharedWindowData& shared)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
shared.SDLWindow = Window;
|
shared.SDLWindow = Window;
|
||||||
// SDL_SetWindowRelativeMouseMode(Window, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EngineWindow::Update(SharedWindowData& shared)
|
void EngineWindow::Update(SharedWindowData& shared)
|
||||||
|
|||||||
@@ -46,6 +46,27 @@ struct Mat4
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline int32_t SetFlags(int32_t in, int32_t flags)
|
||||||
|
{
|
||||||
|
return in | flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32_t ClearFlags(int32_t in, int32_t flags)
|
||||||
|
{
|
||||||
|
return in & ~flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32_t FlagBool(int32_t in, int32_t flags, bool IsSet)
|
||||||
|
{
|
||||||
|
if (IsSet) return SetFlags(in, flags);
|
||||||
|
return ClearFlags(in, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool GetFlag(int32_t in, int32_t flags)
|
||||||
|
{
|
||||||
|
return (in & flags) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct Transform
|
struct Transform
|
||||||
{
|
{
|
||||||
Mat4 M;
|
Mat4 M;
|
||||||
|
|||||||
@@ -1,39 +1,59 @@
|
|||||||
#include "../engine/Shared.h"
|
#include "../engine/Shared.h"
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
|
||||||
namespace Game
|
namespace Game
|
||||||
{
|
{
|
||||||
|
bool IsKeyboardAllowed()
|
||||||
|
{
|
||||||
|
auto& IO = ImGui::GetIO();
|
||||||
|
return !IO.WantCaptureKeyboard || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoKeyboard);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsMouseAllowed()
|
||||||
|
{
|
||||||
|
auto& IO = ImGui::GetIO();
|
||||||
|
return !IO.WantCaptureMouse || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoMouse);
|
||||||
|
}
|
||||||
|
|
||||||
bool GetKey(ScanCode key)
|
bool GetKey(ScanCode key)
|
||||||
{
|
{
|
||||||
|
if (!IsKeyboardAllowed()) return false;
|
||||||
return GetShared().Window.HeldScanCodes[(int32_t)key];
|
return GetShared().Window.HeldScanCodes[(int32_t)key];
|
||||||
}
|
}
|
||||||
bool GetKeyPressedNow(ScanCode key)
|
bool GetKeyPressedNow(ScanCode key)
|
||||||
{
|
{
|
||||||
|
if (!IsKeyboardAllowed()) return false;
|
||||||
auto& win = GetShared().Window;
|
auto& win = GetShared().Window;
|
||||||
return win.HeldScanCodes[(int32_t)key] && !win.LastHeldScanCodes[(int32_t)key];
|
return win.HeldScanCodes[(int32_t)key] && !win.LastHeldScanCodes[(int32_t)key];
|
||||||
}
|
}
|
||||||
bool GetKeyReleasedNow(ScanCode key)
|
bool GetKeyReleasedNow(ScanCode key)
|
||||||
{
|
{
|
||||||
|
if (!IsKeyboardAllowed()) return false;
|
||||||
auto& win = GetShared().Window;
|
auto& win = GetShared().Window;
|
||||||
return !win.HeldScanCodes[(int32_t)key] && win.LastHeldScanCodes[(int32_t)key];
|
return !win.HeldScanCodes[(int32_t)key] && win.LastHeldScanCodes[(int32_t)key];
|
||||||
}
|
}
|
||||||
bool GetMouseButton(MouseButton button)
|
bool GetMouseButton(MouseButton button)
|
||||||
{
|
{
|
||||||
|
if (!IsMouseAllowed()) return false;
|
||||||
return GetShared().Window.HeldMouseButtons[(int32_t)button];
|
return GetShared().Window.HeldMouseButtons[(int32_t)button];
|
||||||
}
|
}
|
||||||
bool GetMouseButtonPressedNow(MouseButton button)
|
bool GetMouseButtonPressedNow(MouseButton button)
|
||||||
{
|
{
|
||||||
|
if (!IsMouseAllowed()) return false;
|
||||||
auto& win = GetShared().Window;
|
auto& win = GetShared().Window;
|
||||||
return win.HeldMouseButtons[(int32_t)button] && !win.LastHeldMouseButtons[(int32_t)button];
|
return win.HeldMouseButtons[(int32_t)button] && !win.LastHeldMouseButtons[(int32_t)button];
|
||||||
}
|
}
|
||||||
bool GetMouseButtonReleasedNow(MouseButton button)
|
bool GetMouseButtonReleasedNow(MouseButton button)
|
||||||
{
|
{
|
||||||
|
if (!IsMouseAllowed()) return false;
|
||||||
auto& win = GetShared().Window;
|
auto& win = GetShared().Window;
|
||||||
return !win.HeldMouseButtons[(int32_t)button] && win.LastHeldMouseButtons[(int32_t)button];
|
return !win.HeldMouseButtons[(int32_t)button] && win.LastHeldMouseButtons[(int32_t)button];
|
||||||
}
|
}
|
||||||
Vec2 GetMouseMovement()
|
Vec2 GetMouseMovement()
|
||||||
{
|
{
|
||||||
|
if (!IsMouseAllowed()) return {};
|
||||||
return {GetShared().Window.MouseDeltaX, GetShared().Window.MouseDeltaY};
|
return {GetShared().Window.MouseDeltaX, GetShared().Window.MouseDeltaY};
|
||||||
}
|
}
|
||||||
} // namespace Game
|
} // namespace Game
|
||||||
|
|||||||
@@ -5,12 +5,18 @@
|
|||||||
|
|
||||||
namespace Game
|
namespace Game
|
||||||
{
|
{
|
||||||
enum class PlayerMode
|
enum class CameraMode
|
||||||
{
|
{
|
||||||
Walk,
|
Walk,
|
||||||
Freefly,
|
Freefly,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class InputMode
|
||||||
|
{
|
||||||
|
UI,
|
||||||
|
Game,
|
||||||
|
};
|
||||||
|
|
||||||
struct Time
|
struct Time
|
||||||
{
|
{
|
||||||
double Now = 0.0;
|
double Now = 0.0;
|
||||||
@@ -28,7 +34,8 @@ namespace Game
|
|||||||
float WalkXRot = 0.0f;
|
float WalkXRot = 0.0f;
|
||||||
float WalkYRot = 0.0f;
|
float WalkYRot = 0.0f;
|
||||||
Transform PlayerCamTransform;
|
Transform PlayerCamTransform;
|
||||||
PlayerMode Mode = PlayerMode::Freefly;
|
CameraMode CameraM = CameraMode::Freefly;
|
||||||
|
InputMode InputM = InputMode::Game;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GameInstance
|
struct GameInstance
|
||||||
|
|||||||
@@ -3,14 +3,13 @@
|
|||||||
#include "Instance.h"
|
#include "Instance.h"
|
||||||
#include "Level.h"
|
#include "Level.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
#include "SDL3/SDL_mouse.h"
|
||||||
#include "bgfx/bgfx.h"
|
#include "bgfx/bgfx.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
#include <bx/math.h>
|
#include <bx/math.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Game
|
namespace Game
|
||||||
{
|
{
|
||||||
void EntityRenderData::Render(const Model* models, const Material* materials)
|
void EntityRenderData::Render(const Model* models, const Material* materials)
|
||||||
@@ -35,6 +34,17 @@ namespace Game
|
|||||||
bgfx::submit(currentMaterial.ViewID, currentMaterial.Shader);
|
bgfx::submit(currentMaterial.ViewID, currentMaterial.Shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
void UpdatePlayerInputMode()
|
||||||
|
{
|
||||||
|
bool IsGaming = GetInstance().Player.InputM == InputMode::Game;
|
||||||
|
SDL_SetWindowRelativeMouseMode(GetShared().Window.SDLWindow, IsGaming);
|
||||||
|
auto& IO = ImGui::GetIO();
|
||||||
|
IO.ConfigFlags = FlagBool(IO.ConfigFlags, ImGuiConfigFlags_NoMouse | ImGuiConfigFlags_NoKeyboard, IsGaming);
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
void Level::Setup(GameData& data)
|
void Level::Setup(GameData& data)
|
||||||
{
|
{
|
||||||
Log("Level setup");
|
Log("Level setup");
|
||||||
@@ -63,6 +73,8 @@ namespace Game
|
|||||||
{
|
{
|
||||||
Tests.New();
|
Tests.New();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdatePlayerInputMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::Update()
|
void Level::Update()
|
||||||
@@ -85,10 +97,23 @@ namespace Game
|
|||||||
|
|
||||||
if (GetKeyPressedNow(ScanCode::F1))
|
if (GetKeyPressedNow(ScanCode::F1))
|
||||||
{
|
{
|
||||||
player.Mode = player.Mode == PlayerMode::Walk ? PlayerMode::Freefly : PlayerMode::Walk;
|
player.CameraM = player.CameraM == CameraMode::Walk ? CameraMode::Freefly : CameraMode::Walk;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player.Mode == PlayerMode::Freefly)
|
if (GetKeyPressedNow(ScanCode::F2))
|
||||||
|
{
|
||||||
|
if (player.InputM == InputMode::Game)
|
||||||
|
{
|
||||||
|
player.InputM = InputMode::UI;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player.InputM = InputMode::Game;
|
||||||
|
}
|
||||||
|
UpdatePlayerInputMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.CameraM == CameraMode::Freefly)
|
||||||
{
|
{
|
||||||
if (GetMouseButton(MouseButton::Left))
|
if (GetMouseButton(MouseButton::Left))
|
||||||
{
|
{
|
||||||
@@ -101,7 +126,7 @@ namespace Game
|
|||||||
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||||
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||||
}
|
}
|
||||||
else if (player.Mode == PlayerMode::Walk)
|
else if (player.CameraM == CameraMode::Walk)
|
||||||
{
|
{
|
||||||
player.PlayerCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
player.PlayerCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||||
player.PlayerCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
player.PlayerCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||||
@@ -118,6 +143,34 @@ namespace Game
|
|||||||
END_PERF(GetShared().Window.PerfCounters, PerfCounterType::GameLevelUpdate, GetShared().Window.FrameCounter);
|
END_PERF(GetShared().Window.PerfCounters, PerfCounterType::GameLevelUpdate, GetShared().Window.FrameCounter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Level::Render(uint16_t viewId, const Model* models, const Material* materials)
|
||||||
|
{
|
||||||
|
auto& shared = GetShared();
|
||||||
|
|
||||||
|
float proj[16];
|
||||||
|
bx::mtxProj(proj,
|
||||||
|
75.0f,
|
||||||
|
float(shared.Window.WindowWidth) / float(shared.Window.WindowHeight),
|
||||||
|
0.1f,
|
||||||
|
1000.0f,
|
||||||
|
bgfx::getCaps()->homogeneousDepth);
|
||||||
|
|
||||||
|
auto& player = GetInstance().Player;
|
||||||
|
if (player.CameraM == CameraMode::Freefly)
|
||||||
|
{
|
||||||
|
player.FreeflyCamTransform.UpdateMatrix();
|
||||||
|
bgfx::setViewTransform(viewId, player.FreeflyCamTransform.M.M, proj);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player.PlayerCamTransform.UpdateMatrix();
|
||||||
|
bgfx::setViewTransform(viewId, player.PlayerCamTransform.M.M, proj);
|
||||||
|
}
|
||||||
|
|
||||||
|
Cubes.Render(models, materials);
|
||||||
|
Tests.Render(models, materials);
|
||||||
|
}
|
||||||
|
|
||||||
void Cube::Setup()
|
void Cube::Setup()
|
||||||
{
|
{
|
||||||
EData.MaterialHandle = 0;
|
EData.MaterialHandle = 0;
|
||||||
@@ -155,7 +208,5 @@ namespace Game
|
|||||||
void TestEntity::Update()
|
void TestEntity::Update()
|
||||||
{
|
{
|
||||||
EData.TestColor[0] = 0.0f;
|
EData.TestColor[0] = 0.0f;
|
||||||
// EData.TestColor[1] = 0.9f;
|
|
||||||
// EData.TestColor[2] = 0.5f;
|
|
||||||
}
|
}
|
||||||
} // namespace Game
|
} // namespace Game
|
||||||
|
|||||||
@@ -115,5 +115,6 @@ namespace Game
|
|||||||
public:
|
public:
|
||||||
void Setup(GameData& data);
|
void Setup(GameData& data);
|
||||||
void Update();
|
void Update();
|
||||||
|
void Render(uint16_t ViewID, const Model* models, const Material* materials);
|
||||||
};
|
};
|
||||||
} // namespace Game
|
} // namespace Game
|
||||||
|
|||||||
@@ -301,41 +301,14 @@ namespace Game
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start Rendering
|
||||||
imguiBeginFrame(20);
|
imguiBeginFrame(20);
|
||||||
ImGui_ImplSDL3_NewFrame();
|
ImGui_ImplSDL3_NewFrame();
|
||||||
// TODO: why does this break stuff??
|
|
||||||
ImGui::DockSpaceOverViewport(0, 0, ImGuiDockNodeFlags_PassthruCentralNode);
|
ImGui::DockSpaceOverViewport(0, 0, ImGuiDockNodeFlags_PassthruCentralNode);
|
||||||
ImGui::ShowDemoWindow();
|
ImGui::ShowDemoWindow();
|
||||||
|
|
||||||
auto& IO = ImGui::GetIO();
|
|
||||||
|
|
||||||
GetInstance().GameLevel.Update();
|
GetInstance().GameLevel.Update();
|
||||||
|
GetInstance().GameLevel.Render(MainViewID, Models, Materials);
|
||||||
// TODO: Move player stuff to level
|
|
||||||
{
|
|
||||||
float proj[16];
|
|
||||||
bx::mtxProj(proj,
|
|
||||||
75.0f,
|
|
||||||
float(shared.Window.WindowWidth) / float(shared.Window.WindowHeight),
|
|
||||||
0.1f,
|
|
||||||
1000.0f,
|
|
||||||
bgfx::getCaps()->homogeneousDepth);
|
|
||||||
|
|
||||||
auto& player = GetInstance().Player;
|
|
||||||
if (player.Mode == PlayerMode::Freefly)
|
|
||||||
{
|
|
||||||
player.FreeflyCamTransform.UpdateMatrix();
|
|
||||||
bgfx::setViewTransform(MainViewID, player.FreeflyCamTransform.M.M, proj);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
player.PlayerCamTransform.UpdateMatrix();
|
|
||||||
bgfx::setViewTransform(MainViewID, player.PlayerCamTransform.M.M, proj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GetInstance().GameLevel.Cubes.Render(Models, Materials);
|
|
||||||
GetInstance().GameLevel.Tests.Render(Models, Materials);
|
|
||||||
|
|
||||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1fs", GetInstance().Time.Now);
|
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1fs", GetInstance().Time.Now);
|
||||||
for (int32_t i = 0; i < (int32_t)PerfCounterType::COUNT; ++i)
|
for (int32_t i = 0; i < (int32_t)PerfCounterType::COUNT; ++i)
|
||||||
@@ -343,6 +316,8 @@ namespace Game
|
|||||||
bgfx::dbgTextPrintf(
|
bgfx::dbgTextPrintf(
|
||||||
1, 2 + i, 0x0F, "%s Max: %.3fs", PerfCounterNames[i], shared.Window.PerfCounters[i].GetMax());
|
1, 2 + i, 0x0F, "%s Max: %.3fs", PerfCounterNames[i], shared.Window.PerfCounters[i].GetMax());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finish Frame
|
||||||
imguiEndFrame();
|
imguiEndFrame();
|
||||||
|
|
||||||
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
||||||
@@ -364,7 +339,7 @@ namespace Game
|
|||||||
}
|
}
|
||||||
|
|
||||||
Material Material::LoadFromShader(
|
Material Material::LoadFromShader(
|
||||||
const char* vertPath, const char* fragPath, uint32_t view, bgfx::TextureHandle tex, bgfx::UniformHandle sampler)
|
const char* vertPath, const char* fragPath, uint16_t view, bgfx::TextureHandle tex, bgfx::UniformHandle sampler)
|
||||||
{
|
{
|
||||||
BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!");
|
BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!");
|
||||||
bgfx::ShaderHandle vertexShader = loadShader(vertPath);
|
bgfx::ShaderHandle vertexShader = loadShader(vertPath);
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace Game
|
|||||||
uint32_t ViewID = 0;
|
uint32_t ViewID = 0;
|
||||||
static Material LoadFromShader(const char* vertPath,
|
static Material LoadFromShader(const char* vertPath,
|
||||||
const char* fragPath,
|
const char* fragPath,
|
||||||
uint32_t view = 0,
|
uint16_t view,
|
||||||
bgfx::TextureHandle = BGFX_INVALID_HANDLE,
|
bgfx::TextureHandle = BGFX_INVALID_HANDLE,
|
||||||
bgfx::UniformHandle sampler = BGFX_INVALID_HANDLE);
|
bgfx::UniformHandle sampler = BGFX_INVALID_HANDLE);
|
||||||
};
|
};
|
||||||
@@ -64,7 +64,7 @@ namespace Game
|
|||||||
int32_t LastWidth = 0;
|
int32_t LastWidth = 0;
|
||||||
int32_t LastHeight = 0;
|
int32_t LastHeight = 0;
|
||||||
uint32_t ResetFlags = BGFX_RESET_VSYNC;
|
uint32_t ResetFlags = BGFX_RESET_VSYNC;
|
||||||
uint32_t MainViewID = 10;
|
uint16_t MainViewID = 10;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Setup();
|
void Setup();
|
||||||
|
|||||||
Reference in New Issue
Block a user