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