From 7e405aee7512796740d13af9b1095151376834b8 Mon Sep 17 00:00:00 2001 From: Asuro Date: Sat, 8 Feb 2025 19:49:47 +0100 Subject: [PATCH] it works!! mostly --- src/engine/main.cpp | 182 +++++++++++++++++-------------- src/game/Setup.cpp | 107 +++++++++--------- src/game/rendering/Rendering.cpp | 74 +++++++++++-- 3 files changed, 217 insertions(+), 146 deletions(-) diff --git a/src/engine/main.cpp b/src/engine/main.cpp index bf480e8..a8f15db 100644 --- a/src/engine/main.cpp +++ b/src/engine/main.cpp @@ -15,123 +15,134 @@ typedef void (*Shutdown)(); constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1; #ifdef VISUAL_STUDIO - const char* DLLPath = "PuzGame2.dll"; +constexpr char* DLLPath = "PuzGame.dll"; +constexpr wchar_t* DLLWatch = L"PuzGame2.dll"; #else - const char* DLLPath = "libPuzGame.dll"; +constexpr char* DLLPath = "libPuzGame.dll"; +constexpr wchar_t* DLLWatch = L"libPuzGame2.dll"; #endif struct FileWatcherData { - bool Change = false; + bool Change = false; }; struct DevelopmentData { - uint8_t FileChangeBuffer[1024]{0}; - HMODULE GameLib = NULL; - HANDLE hDevDir = NULL; - FileWatcherData FileWatcher; + uint8_t FileChangeBuffer[1024]{ 0 }; + HMODULE GameLib = NULL; + HANDLE hDevDir = NULL; + FileWatcherData FileWatcher; }; namespace { - DevelopmentData DevData; - Startup StartupFunc; - Update UpdateFunc; - Shutdown ShutdownFunc; + DevelopmentData DevData; + Startup StartupFunc; + Update UpdateFunc; + Shutdown ShutdownFunc; } unsigned long FileWatcherThread(void* data) { - DWORD bytesReturned = 0; - bool isRunning = DevData.hDevDir; - while (isRunning) - { - bx::memSet(DevData.FileChangeBuffer, 0, sizeof(DevData.FileChangeBuffer)); - if (ReadDirectoryChangesW(DevData.hDevDir, DevData.FileChangeBuffer, sizeof(DevData.FileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL)) - { - uint32_t offset = 0; - while (true) - { - FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast(&DevData.FileChangeBuffer[offset]); - wprintf(L"Change: %ls\n", notifyData->FileName); + DWORD bytesReturned = 0; + bool isRunning = DevData.hDevDir; + while (isRunning) + { + bx::memSet(DevData.FileChangeBuffer, 0, sizeof(DevData.FileChangeBuffer)); + if (ReadDirectoryChangesW(DevData.hDevDir, DevData.FileChangeBuffer, sizeof(DevData.FileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL)) + { + uint32_t offset = 0; + while (true) + { + FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast(&DevData.FileChangeBuffer[offset]); + 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) - { - if (wcscmp(notifyData->FileName, L"libPuzGame2.dll") == 0) - { - DevData.FileWatcher.Change = true; - printf("detected dll change!\n"); - } - } - - if (notifyData->NextEntryOffset == 0) break; - offset += notifyData->NextEntryOffset; - } - } - } - printf("File watcher thread ended!\n"); - return 0; + if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME) + { + if (wcscmp(notifyData->FileName, DLLWatch) == 0) + { + DevData.FileWatcher.Change = true; + printf("detected dll change!\n"); + } + } + + if (notifyData->NextEntryOffset == 0) break; + offset += notifyData->NextEntryOffset; + } + } + } + printf("File watcher thread ended!\n"); + return 0; } bool ReloadDLL() { - if (DevData.GameLib != NULL) - { - FreeLibrary(DevData.GameLib); - } - if (!CopyFile("cmake-build\\libPuzGame2.dll", "cmake-build\\libPuzGame.dll", false)) - { - printf("Failed to copy game DLL!\n"); - } - - HMODULE gameLibReloaded = LoadLibraryEx(DLLPath, NULL, 0); - if (gameLibReloaded == NULL) - { - printf("Failed to load game DLL from %s!\n", DLLPath); - return false; - } - DevData.GameLib = gameLibReloaded; - + if (DevData.GameLib != NULL) + { + FreeLibrary(DevData.GameLib); + } #ifdef VISUAL_STUDIO - Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z"); + if (!CopyFile("PuzGame2.dll", "PuzGame.dll", false)) + { + printf("Failed to copy game DLL!\n"); + return false; + } #else - Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv"); + if (!CopyFile("cmake-build\\libPuzGame2.dll", "cmake-build\\libPuzGame.dll", false)) + { + printf("Failed to copy game DLL!\n"); + return false; + } #endif - if (StartupReloaded == NULL) - { - printf("Failed to load startup function from game DLL!\n"); - return false; - } + + HMODULE gameLibReloaded = LoadLibraryEx(DLLPath, NULL, 0); + if (gameLibReloaded == NULL) + { + printf("Failed to load game DLL from %s!\n", DLLPath); + return false; + } + DevData.GameLib = gameLibReloaded; #ifdef VISUAL_STUDIO - Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "?Update@Game@@YAXXZ"); + Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z"); #else - Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "_ZN4Game6UpdateEv"); + Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv"); #endif - if (UpdateReloaded == NULL) - { - printf("Failed to load update function from game DLL!\n"); - return false; - } + if (StartupReloaded == NULL) + { + printf("Failed to load startup function from game DLL!\n"); + return false; + } #ifdef VISUAL_STUDIO - Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "?Shutdown@Game@@YAXXZ"); + Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "?Update@Game@@YAXXZ"); #else - Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "_ZN4Game8ShutdownEv"); + Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "_ZN4Game6UpdateEv"); #endif - if (ShutdownReloaded == NULL) - { - printf("Failed to load shutdown function from game DLL\n"); - return false; - } + if (UpdateReloaded == NULL) + { + printf("Failed to load update function from game DLL!\n"); + return false; + } - StartupFunc = StartupReloaded; - UpdateFunc = UpdateReloaded; - ShutdownFunc = ShutdownReloaded; +#ifdef VISUAL_STUDIO + Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "?Shutdown@Game@@YAXXZ"); +#else + Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "_ZN4Game8ShutdownEv"); +#endif + if (ShutdownReloaded == NULL) + { + printf("Failed to load shutdown function from game DLL\n"); + return false; + } - printf("Loaded Game DLL successfully!\n"); - return true; + StartupFunc = StartupReloaded; + UpdateFunc = UpdateReloaded; + ShutdownFunc = ShutdownReloaded; + + printf("Loaded Game DLL successfully!\n"); + return true; } int main() @@ -142,6 +153,13 @@ int main() if (!ReloadDLL()) return 1; + DevData.hDevDir = CreateFile(".", + FILE_LIST_DIRECTORY, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + NULL, + OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, + NULL); DWORD fileWatcherThreadId = 0; CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId); diff --git a/src/game/Setup.cpp b/src/game/Setup.cpp index 4b7c913..5306869 100644 --- a/src/game/Setup.cpp +++ b/src/game/Setup.cpp @@ -7,60 +7,63 @@ namespace Game { - class GameSetup - { - public: - GameRendering Rendering; - int32_t FrameCounter = 0; - }; + class GameSetup + { + public: + GameRendering Rendering; + int32_t FrameCounter = 0; + }; - namespace - { - GameSetup Instance; - } - - void Setup(void* window) - { - Log("Game Setup Start!"); - if (!SDL_Init(SDL_INIT_VIDEO)) - { - Log("Failed to init SDL!"); - return; - } - 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); - } + namespace + { + GameSetup Instance; + } - void Update() - { - ++Instance.FrameCounter; - if (Instance.FrameCounter % 100 == 0) - { - Log("Frame!"); - } + void Setup(void* window) + { + Log("Game Setup Start!"); + if (!SDL_Init(SDL_INIT_VIDEO)) + { + Log("Failed to init SDL!"); + return; + } + 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); + } - Instance.Rendering.Update(); - } + void Update() + { + SDL_Event evt; + while (SDL_PollEvent(&evt)) {} - void Shutdown() - { - Log("Shutdown"); - Instance.Rendering.Shutdown(); - } + ++Instance.FrameCounter; + if (Instance.FrameCounter % 100 == 0) + { + Log("Frame!"); + } + + Instance.Rendering.Update(); + } + + void Shutdown() + { + Log("Shutdown"); + Instance.Rendering.Shutdown(); + } } diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp index e372bc0..05e4368 100644 --- a/src/game/rendering/Rendering.cpp +++ b/src/game/rendering/Rendering.cpp @@ -101,7 +101,7 @@ namespace Game { bgfx::renderFrame(); bgfx::Init init; - init.type = bgfx::RendererType::Direct3D12; + init.type = bgfx::RendererType::Direct3D11; init.debug = true; init.callback = &Callback; init.platformData.nwh = window; @@ -136,21 +136,71 @@ namespace Game void GameRendering::Update() { + float time = (float)((bx::getHPCounter() - State.StartTime) / double(bx::getHPFrequency())); + + const bx::Vec3 at = { 0.0f, 0.0f, 0.0f }; + const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f }; + + // Set view and projection matrix for view 0. + { + float view[16]; + 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); + bgfx::setViewTransform(0, view, proj); + + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, State.WindowWidth, State.WindowHeight); + } + + // This dummy draw call is here to make sure that view 0 is cleared + // if no other draw calls are submitted to view 0. bgfx::touch(0); - const bx::Vec3 at = { 0.0f, 0.0f, 0.0f }; - const bx::Vec3 eye = { 0.0f, 0.0f, -5.0f }; - float view[16]; - 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); - bgfx::setViewTransform(0, view, proj); - bgfx::setVertexBuffer(0, VertexBuffer); - bgfx::setIndexBuffer(IndexBuffer); + 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_Z + | BGFX_STATE_DEPTH_TEST_LESS + | BGFX_STATE_CULL_CW + | BGFX_STATE_MSAA + | 0 + ; - bgfx::dbgTextPrintf(10, 10, 0x0F, "TEST"); + // Submit 11x11 cubes. + for (uint32_t yy = 0; yy < 11; ++yy) + { + for (uint32_t xx = 0; xx < 11; ++xx) + { + float mtx[16]; + bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f); + mtx[12] = -15.0f + float(xx) * 3.0f; + mtx[13] = -15.0f + float(yy) * 3.0f; + mtx[14] = 0.0f; - bgfx::submit(0, Shader); + // Set model matrix for rendering. + bgfx::setTransform(mtx); + + // Set vertex and index buffer. + bgfx::setVertexBuffer(0, VertexBuffer); + bgfx::setIndexBuffer(ibh); + + // Set render states. + bgfx::setState(state); + + // Submit primitive for rendering to view 0. + bgfx::submit(0, Shader); + } + } + + bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %f", time); + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. bgfx::frame(); }