memory setup

This commit is contained in:
Asuro
2025-02-09 03:01:43 +01:00
parent fbed568fae
commit df752065af
12 changed files with 227 additions and 80 deletions

33
src/game/Global.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "Global.h"
#include <cassert>
namespace
{
SharedData* SharedInstance = nullptr;
Game::GameInstance* GameInst = nullptr;
}
namespace Game
{
SharedData& GetShared()
{
assert(SharedInstance != nullptr);
return *SharedInstance;
}
void SetShared(SharedData& instance)
{
SharedInstance = &instance;
}
GameInstance& GetInstance()
{
assert(GameInst != nullptr);
return *GameInst;
}
void SetInstance(Game::GameInstance& instance)
{
GameInst = &instance;
}
}

20
src/game/Global.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
struct SharedData;
namespace Game
{
struct GameInstance
{
bool IsInitialized = false;
uint64_t Size = sizeof(GameInstance);
uint32_t FrameCounter = 0;
int64_t StartTime = 0;
};
SharedData& GetShared();
void SetShared(SharedData& instance);
GameInstance& GetInstance();
void SetInstance(GameInstance& instance);
}

View File

@@ -1,9 +1,7 @@
#include "Setup.h"
#include "Log.h"
#include "SDL3/SDL_video.h"
#include "rendering/Rendering.h"
#include <cstdint>
#include <SDL3/SDL.h>
#include "Global.h"
namespace Game
{
@@ -11,59 +9,37 @@ namespace Game
{
public:
GameRendering Rendering;
int32_t FrameCounter = 0;
};
namespace
{
GameSetup Instance;
GameSetup SetupInstance;
}
void Setup(void* window)
void Setup(SharedData& shared)
{
Log("Game Setup Start!");
if (!SDL_Init(SDL_INIT_VIDEO))
GameInstance& instance = *reinterpret_cast<GameInstance*>(&shared.Game.PermanentStorage);
if (sizeof(GameInstance) != instance.Size)
{
Log("Failed to init SDL!");
return;
Log("Game instance size changed, resetting!");
instance = {};
}
SDL_Window* sdlWindow = SDL_CreateWindow("SDL", 1920, 1080, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (sdlWindow == nullptr)
{
Log("Failed to init SDL Window!");
return;
}
auto props = SDL_GetWindowProperties(sdlWindow);
SDL_EnumerateProperties(props, [](void*, SDL_PropertiesID id, const char* name)
{
Log("Prop: %s", name);
}, nullptr);
void* hwnd = SDL_GetPointerProperty(props, "SDL.window.win32.hwnd", nullptr);
if (hwnd == nullptr)
{
Log("Failed to get window pointer!");
return;
}
Instance.Rendering.Setup(hwnd);
SetShared(shared);
SetInstance(instance);
SetupInstance.Rendering.Setup();
}
void Update()
{
SDL_Event evt;
while (SDL_PollEvent(&evt)) {}
++Instance.FrameCounter;
if (Instance.FrameCounter % 100 == 0)
{
Log("Frame!");
}
Instance.Rendering.Update();
++GetInstance().FrameCounter;
SetupInstance.Rendering.Update();
}
void Shutdown()
{
Log("Shutdown");
Instance.Rendering.Shutdown();
SetupInstance.Rendering.Shutdown();
}
}

View File

@@ -1,10 +1,12 @@
#pragma once
#include "../engine/Shared.h"
#define DLLEXPORT __declspec(dllexport)
namespace Game
{
DLLEXPORT void Setup(void* window);
DLLEXPORT void Setup(SharedData& shared);
DLLEXPORT void Update();
DLLEXPORT void Shutdown();
}

View File

@@ -1,7 +1,8 @@
#include "Rendering.h"
#include "../Log.h"
#include "../../engine/Shared.h"
#include "../Global.h"
#include "bgfx/defines.h"
#include "bgfx/platform.h"
#include "bx/timer.h"
#include <bx/file.h>
#include <bgfx/bgfx.h>
@@ -46,7 +47,6 @@ namespace Game
namespace
{
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
{
if (bx::open(_reader, _filePath))
@@ -68,6 +68,9 @@ namespace Game
const char* shaderPath = "???";
switch (bgfx::getRendererType()) {
case bgfx::RendererType::Agc:
case bgfx::RendererType::Nvn:
case bgfx::RendererType::Count:
case bgfx::RendererType::Noop: break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: shaderPath = "game/compiled-shaders/dx11/"; break;
@@ -99,20 +102,24 @@ namespace Game
}
}
void GameRendering::Setup(void* window)
void GameRendering::Setup()
{
Log("Game rendering setup...");
bgfx::renderFrame();
SharedData& shared = GetShared();
bgfx::Init init;
init.type = bgfx::RendererType::Direct3D12;
init.debug = true;
init.callback = &Callback;
init.platformData.nwh = window;
init.platformData.nwh = shared.Window.Handle;
init.platformData.ndt = nullptr;
init.platformData.type = bgfx::NativeWindowHandleType::Default;
init.resolution.width = State.WindowWidth;
init.resolution.height = State.WindowHeight;
init.resolution.width = shared.Window.WindowWidth;
init.resolution.height = shared.Window.WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
Log("%i by %i", init.resolution.width, init.resolution.height);
if (!bgfx::init(init))
{
Log("BGFX setup failed!");
@@ -134,12 +141,19 @@ namespace Game
bgfx::ShaderHandle vertexShader = loadShader("vert");
bgfx::ShaderHandle fragmentShader = loadShader("frag");
Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
State.StartTime = bx::getHPCounter();
if (!GetInstance().IsInitialized)
{
GetInstance().IsInitialized = true;
GetInstance().StartTime = bx::getHPCounter();
}
}
void GameRendering::Update()
{
float time = (float)((bx::getHPCounter() - State.StartTime) / double(bx::getHPFrequency()));
SharedData& shared = GetShared();
int64_t tickDelta = bx::getHPCounter() - GetInstance().StartTime;
double time = tickDelta / double(bx::getHPFrequency());
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
@@ -150,11 +164,11 @@ namespace Game
bx::mtxLookAt(view, eye, at);
float proj[16];
bx::mtxProj(proj, 60.0f, float(State.WindowWidth) / float(State.WindowHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
bx::mtxProj(proj, 60.0f, float(shared.Window.WindowWidth) / float(shared.Window.WindowHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, State.WindowWidth, State.WindowHeight);
bgfx::setViewRect(0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
}
// This dummy draw call is here to make sure that view 0 is cleared
@@ -163,15 +177,12 @@ namespace Game
bgfx::IndexBufferHandle ibh = IndexBuffer;
uint64_t state = 0
| (true ? BGFX_STATE_WRITE_R : 0)
| (true ? BGFX_STATE_WRITE_G : 0)
| (true ? BGFX_STATE_WRITE_B : 0)
| (true ? BGFX_STATE_WRITE_A : 0)
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA
| 0
;
// Submit 11x11 cubes.
@@ -200,10 +211,9 @@ namespace Game
}
}
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %f", time);
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", time);
bgfx::dbgTextPrintf(1, 2, 0x0f, "Frame: %u", GetInstance().FrameCounter);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}

View File

@@ -79,9 +79,6 @@ namespace Game
struct RenderState
{
int64_t StartTime = 0;
uint32_t WindowWidth = 1920;
uint32_t WindowHeight = 1080;
};
class GameRendering
@@ -94,7 +91,7 @@ namespace Game
BgfxCallback Callback;
RenderState State;
public:
void Setup(void* window);
void Setup();
void Update();
void Shutdown();
};