From df752065af5d380bea2bd15108e32bdea49026a3 Mon Sep 17 00:00:00 2001 From: Asuro Date: Sun, 9 Feb 2025 03:01:43 +0100 Subject: [PATCH] memory setup --- src/CMakeLists.txt | 4 +-- src/engine/Shared.h | 26 ++++++++++++++++ src/engine/Window.cpp | 53 ++++++++++++++++++++++++++++++++ src/engine/Window.h | 15 +++++++++ src/engine/main.cpp | 43 ++++++++++++++++++-------- src/game/Global.cpp | 33 ++++++++++++++++++++ src/game/Global.h | 20 ++++++++++++ src/game/Setup.cpp | 52 +++++++++---------------------- src/game/Setup.h | 4 ++- src/game/rendering/Rendering.cpp | 48 +++++++++++++++++------------ src/game/rendering/Rendering.h | 5 +-- src/setup-vs.bat | 4 --- 12 files changed, 227 insertions(+), 80 deletions(-) create mode 100644 src/engine/Shared.h create mode 100644 src/engine/Window.cpp create mode 100644 src/engine/Window.h create mode 100644 src/game/Global.cpp create mode 100644 src/game/Global.h delete mode 100644 src/setup-vs.bat diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5bd98f7..c542f58 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,6 @@ SET(BGFX_BUILD_TOOLS ON) SET(BGFX_BUILD_EXAMPLES OFF) add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake") -target_link_libraries(PuzGame bx bimg bgfx SDL3::SDL3) -target_link_libraries(PuzGameEngine bx) +target_link_libraries(PuzGame bx bimg bgfx) +target_link_libraries(PuzGameEngine bx SDL3::SDL3) set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2") diff --git a/src/engine/Shared.h b/src/engine/Shared.h new file mode 100644 index 0000000..6669f45 --- /dev/null +++ b/src/engine/Shared.h @@ -0,0 +1,26 @@ +#pragma once +#include + +struct SharedWindowData +{ + void* Handle = nullptr; + int32_t WindowWidth = 1920; + int32_t WindowHeight = 1080; +}; + +struct GameData +{ + void* PermanentStorage = nullptr; + uint64_t PermanentStorageSize = 0; +}; + +struct SharedData +{ + SharedWindowData Window; + GameData Game; +}; + +typedef void (*Startup)(SharedData& shared); +typedef void (*Update)(); +typedef void (*Shutdown)(); + diff --git a/src/engine/Window.cpp b/src/engine/Window.cpp new file mode 100644 index 0000000..c8a12ba --- /dev/null +++ b/src/engine/Window.cpp @@ -0,0 +1,53 @@ +#include "Window.h" +#include "SDL3/SDL_events.h" +#include "Shared.h" +#include +#include + +void EngineWindow::Startup(SharedWindowData& shared) +{ + if (!SDL_Init(SDL_INIT_VIDEO)) + { + printf("Failed to init SDL!\n"); + return; + } + Window = SDL_CreateWindow("SDL", shared.WindowWidth, shared.WindowHeight, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); + if (Window == nullptr) + { + printf("Failed to init SDL Window!\n"); + return; + } + auto props = SDL_GetWindowProperties(Window); + SDL_EnumerateProperties(props, [](void*, SDL_PropertiesID id, const char* name) + { + printf("Prop: %s\n", name); + }, nullptr); + shared.Handle = SDL_GetPointerProperty(props, "SDL.window.win32.hwnd", nullptr); + if (shared.Handle == nullptr) + { + printf("Failed to get window pointer!\n"); + return; + } + +} + +void EngineWindow::Update(SharedWindowData& Shared) +{ + SDL_Event evt; + while (SDL_PollEvent(&evt)) + { + switch (evt.type) + { + case SDL_EVENT_WINDOW_RESIZED: + { + SDL_GetWindowSize(Window, &Shared.WindowWidth, &Shared.WindowHeight); + break; + } + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + { + CloseRequested = true; + break; + } + } + } +} diff --git a/src/engine/Window.h b/src/engine/Window.h new file mode 100644 index 0000000..3051b0d --- /dev/null +++ b/src/engine/Window.h @@ -0,0 +1,15 @@ +#pragma once +#include "Shared.h" +#include + +class EngineWindow +{ +public: + SDL_Window* Window; + bool CloseRequested = false; + +public: + void Startup(SharedWindowData& Shared); + void Update(SharedWindowData& Shared); + void Shutdown(); +}; diff --git a/src/engine/main.cpp b/src/engine/main.cpp index 896f687..8e21251 100644 --- a/src/engine/main.cpp +++ b/src/engine/main.cpp @@ -6,20 +6,19 @@ #include #include -//#define VISUAL_STUDIO +#include "Shared.h" +#include "Window.h" -typedef void (*Startup)(void*); -typedef void (*Update)(); -typedef void (*Shutdown)(); +//#define VISUAL_STUDIO constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1; #ifdef VISUAL_STUDIO -constexpr char* DLLPath = "PuzGame.dll"; -constexpr wchar_t* DLLWatch = L"PuzGame2.dll"; +constexpr const char* DLLPath = "PuzGame.dll"; +constexpr const wchar_t* DLLWatch = L"PuzGame2.dll"; #else -constexpr char* DLLPath = "libPuzGame.dll"; -constexpr wchar_t* DLLWatch = L"libPuzGame2.dll"; +constexpr const char* DLLPath = "libPuzGame.dll"; +constexpr const wchar_t* DLLWatch = L"cmake-build\\libPuzGame2.dll"; #endif struct FileWatcherData @@ -35,9 +34,16 @@ struct DevelopmentData FileWatcherData FileWatcher; }; +struct EngineData +{ + EngineWindow Window; +}; + namespace { DevelopmentData DevData; + EngineData Engine; + SharedData Shared; Startup StartupFunc; Update UpdateFunc; Shutdown ShutdownFunc; @@ -56,7 +62,7 @@ unsigned long FileWatcherThread(void* data) while (true) { FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast(&DevData.FileChangeBuffer[offset]); - wprintf(L"Change: %ls\n", notifyData->FileName); + // wprintf(L"Change: %ls\n", notifyData->FileName); if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME) { @@ -107,7 +113,7 @@ bool ReloadDLL() #ifdef VISUAL_STUDIO Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z"); #else - Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv"); + Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupER10SharedData"); #endif if (StartupReloaded == NULL) { @@ -153,6 +159,13 @@ int main() if (!ReloadDLL()) return 1; + Engine.Window.Startup(Shared.Window); + if (Shared.Window.Handle == nullptr) + { + printf("Failed to set up window!\n"); + return 1; + } + DevData.hDevDir = CreateFile(".", FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, @@ -163,16 +176,22 @@ int main() DWORD fileWatcherThreadId = 0; CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId); - StartupFunc(nullptr); + StartupFunc(Shared); + bool isRunning = true; while (isRunning) { + Engine.Window.Update(Shared.Window); + if (Engine.Window.CloseRequested) + { + isRunning = false; + } if (DevData.FileWatcher.Change) { DevData.FileWatcher.Change = false; ShutdownFunc(); ReloadDLL(); - StartupFunc(nullptr); + StartupFunc(Shared); } UpdateFunc(); diff --git a/src/game/Global.cpp b/src/game/Global.cpp new file mode 100644 index 0000000..cf03445 --- /dev/null +++ b/src/game/Global.cpp @@ -0,0 +1,33 @@ +#include "Global.h" +#include + +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; + } +} diff --git a/src/game/Global.h b/src/game/Global.h new file mode 100644 index 0000000..2443ba6 --- /dev/null +++ b/src/game/Global.h @@ -0,0 +1,20 @@ +#pragma once +#include + +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); +} diff --git a/src/game/Setup.cpp b/src/game/Setup.cpp index 5306869..f64b2c7 100644 --- a/src/game/Setup.cpp +++ b/src/game/Setup.cpp @@ -1,9 +1,7 @@ #include "Setup.h" #include "Log.h" -#include "SDL3/SDL_video.h" #include "rendering/Rendering.h" -#include -#include +#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(&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(); } } diff --git a/src/game/Setup.h b/src/game/Setup.h index a4ad5c2..e92a52e 100644 --- a/src/game/Setup.h +++ b/src/game/Setup.h @@ -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(); } diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp index b272ed3..af09ec0 100644 --- a/src/game/rendering/Rendering.cpp +++ b/src/game/rendering/Rendering.cpp @@ -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 #include @@ -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(); } diff --git a/src/game/rendering/Rendering.h b/src/game/rendering/Rendering.h index 5b732a3..efdd976 100644 --- a/src/game/rendering/Rendering.h +++ b/src/game/rendering/Rendering.h @@ -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(); }; diff --git a/src/setup-vs.bat b/src/setup-vs.bat deleted file mode 100644 index f9b7117..0000000 --- a/src/setup-vs.bat +++ /dev/null @@ -1,4 +0,0 @@ -cd dependency/bgfx.cmake -cmake -G "Visual Studio 17 2022" -S . -B cmake-build-vs -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF -cd ..\.. -cmake -G "Visual Studio 17 2022" -S . -B cmake-build-vs -DCMAKE_EXPORT_COMPILE_COMMANDS=ON