hotloading!!
This commit is contained in:
@@ -27,5 +27,5 @@ SET(BGFX_BUILD_EXAMPLES OFF)
|
|||||||
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake")
|
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake")
|
||||||
|
|
||||||
target_link_libraries(PuzGame bx bimg bgfx)
|
target_link_libraries(PuzGame bx bimg bgfx)
|
||||||
target_link_libraries(PuzGameEngine bx)
|
target_link_libraries(PuzGameEngine bx bimg bgfx)
|
||||||
|
set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2")
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
cmake --build cmake-build && .\cmake-build\PuzGameEngine.exe
|
cmake --build cmake-build
|
||||||
|
|||||||
@@ -2,15 +2,44 @@
|
|||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#undef min
|
#undef min
|
||||||
#undef max
|
#undef max
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <libloaderapi.h>
|
|
||||||
#include <bx/string.h>
|
#include <bx/string.h>
|
||||||
|
#include <bgfx/bgfx.h>
|
||||||
|
|
||||||
//#define VISUAL_STUDIO
|
//#define VISUAL_STUDIO
|
||||||
|
|
||||||
typedef void (*Startup)(void*);
|
typedef void (*Startup)(void*);
|
||||||
typedef void (*Update)();
|
typedef void (*Update)();
|
||||||
|
|
||||||
|
constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1;
|
||||||
|
|
||||||
|
#ifdef VISUAL_STUDIO
|
||||||
|
const char* DLLPath = "Debug/PuzGame.dll";
|
||||||
|
#else
|
||||||
|
const char* DLLPath = "libPuzGame.dll";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct FileWatcherData
|
||||||
|
{
|
||||||
|
bool Change = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DevelopmentData
|
||||||
|
{
|
||||||
|
uint8_t FileChangeBuffer[1024]{0};
|
||||||
|
HMODULE GameLib = NULL;
|
||||||
|
HANDLE hDevDir = NULL;
|
||||||
|
FileWatcherData FileWatcher;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
DevelopmentData DevData;
|
||||||
|
Startup StartupFunc;
|
||||||
|
Update UpdateFunc;
|
||||||
|
}
|
||||||
|
|
||||||
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
|
||||||
{
|
{
|
||||||
LRESULT Res = 0;
|
LRESULT Res = 0;
|
||||||
@@ -50,66 +79,139 @@ HWND InitWindow()
|
|||||||
DWORD err = GetLastError();
|
DWORD err = GetLastError();
|
||||||
printf("Failed to create window: %#x!\n", err);
|
printf("Failed to create window: %#x!\n", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DevData.hDevDir = CreateFileW(L"cmake-build", FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
||||||
|
if (DevData.hDevDir == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to monitor dev dir!\n");
|
||||||
|
}
|
||||||
|
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<FILE_NOTIFY_INFORMATION*>(&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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
#ifdef VISUAL_STUDIO
|
||||||
|
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z");
|
||||||
|
#else
|
||||||
|
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv");
|
||||||
|
#endif
|
||||||
|
if (StartupReloaded == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load startup function from game DLL!\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef VISUAL_STUDIO
|
||||||
|
Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "?Update@Game@@YAXXZ");
|
||||||
|
#else
|
||||||
|
Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "_ZN4Game6UpdateEv");
|
||||||
|
#endif
|
||||||
|
if (UpdateReloaded == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load update function from game DLL!\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
StartupFunc = StartupReloaded;
|
||||||
|
UpdateFunc = UpdateReloaded;
|
||||||
|
|
||||||
|
printf("Loaded Game DLL successfully!\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
HWND window = InitWindow();
|
HWND window = InitWindow();
|
||||||
if (window == NULL) return 1;
|
if (window == NULL) return 1;
|
||||||
|
|
||||||
char Buf[512];
|
if (!ReloadDLL()) return 1;
|
||||||
GetCurrentDirectoryA(sizeof(Buf), Buf);
|
|
||||||
|
|
||||||
#ifdef VISUAL_STUDIO
|
bgfx::Init init;
|
||||||
const char* dllPath = "Debug/PuzGame.dll";
|
init.platformData.nwh = (void*)window;
|
||||||
#else
|
init.platformData.ndt = nullptr;
|
||||||
const char* dllPath = "libPuzGame.dll";
|
init.platformData.type = bgfx::NativeWindowHandleType::Default;
|
||||||
#endif
|
init.resolution.width = 1920;
|
||||||
HMODULE gameLibModule = LoadLibraryEx(dllPath, NULL, 0);
|
init.resolution.height = 1080;
|
||||||
if (gameLibModule == NULL)
|
init.resolution.reset = BGFX_RESET_VSYNC;
|
||||||
{
|
bgfx::init(init);
|
||||||
printf("Failed to load game DLL from %s!", dllPath);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef VISUAL_STUDIO
|
StartupFunc(window);
|
||||||
Startup StartFunc = (Startup)GetProcAddress(gameLibModule, "?Setup@Game@@YAXPEAX@Z");
|
DWORD fileWatcherThreadId = 0;
|
||||||
#else
|
CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId);
|
||||||
Startup StartFunc = (Startup)GetProcAddress(gameLibModule, "_ZN4Game5SetupEPv");
|
|
||||||
#endif
|
|
||||||
if (StartFunc == NULL)
|
|
||||||
{
|
|
||||||
printf("Failed to load startup function from game DLL!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef VISUAL_STUDIO
|
|
||||||
Update UpdateFunc = (Update)GetProcAddress(gameLibModule, "?Update@Game@@YAXXZ");
|
|
||||||
#else
|
|
||||||
Update UpdateFunc = (Update)GetProcAddress(gameLibModule, "_ZN4Game6UpdateEv");
|
|
||||||
#endif
|
|
||||||
if (UpdateFunc == NULL)
|
|
||||||
{
|
|
||||||
printf("Failed to load update function from game DLL!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Loaded Game DLL successfully!\n");
|
|
||||||
|
|
||||||
StartFunc(window);
|
|
||||||
bool isRunning = true;
|
bool isRunning = true;
|
||||||
while (isRunning)
|
while (isRunning)
|
||||||
{
|
{
|
||||||
MSG Message;
|
MSG msg;
|
||||||
BOOL MessageResult = GetMessage(&Message, 0, 0, 0);
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||||
if (MessageResult > 0)
|
|
||||||
{
|
{
|
||||||
TranslateMessage(&Message);
|
if (msg.message == WM_QUIT)
|
||||||
DispatchMessageW(&Message);
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
// UpdateFunc();
|
|
||||||
// Sleep(1000.0);
|
TranslateMessage(&msg);
|
||||||
|
DispatchMessage(&msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DevData.FileWatcher.Change)
|
||||||
|
{
|
||||||
|
DevData.FileWatcher.Change = false;
|
||||||
|
ReloadDLL();
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateFunc();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
#include <cstdio>
|
#include <bx/string.h>
|
||||||
|
|
||||||
namespace Game
|
namespace
|
||||||
{
|
{
|
||||||
void Log(const char* text)
|
char LineBuffer[1024]{0};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log(const char *format, ...)
|
||||||
{
|
{
|
||||||
printf("%s\n", text);
|
va_list args;
|
||||||
}
|
bx::snprintf(LineBuffer, sizeof(LineBuffer), "%s\n", format);
|
||||||
|
bx::printf(LineBuffer, args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace Game
|
void Log(const char *format, ...);
|
||||||
{
|
|
||||||
void Log(const char* text);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,22 +4,19 @@
|
|||||||
|
|
||||||
namespace Game
|
namespace Game
|
||||||
{
|
{
|
||||||
|
int32_t FrameCounter = 0;
|
||||||
|
|
||||||
void Setup(void* window)
|
void Setup(void* window)
|
||||||
{
|
{
|
||||||
Log("Setup");
|
Log("Game Setup Start!");
|
||||||
bgfx::Init init;
|
|
||||||
init.type = bgfx::RendererType::Direct3D12;
|
|
||||||
init.platformData.nwh = (void*)window;
|
|
||||||
init.platformData.ndt = nullptr;
|
|
||||||
init.platformData.type = bgfx::NativeWindowHandleType::Default;
|
|
||||||
init.resolution.width = 1920;
|
|
||||||
init.resolution.height = 1080;
|
|
||||||
init.resolution.reset = BGFX_RESET_VSYNC;
|
|
||||||
bgfx::init(init);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
Log("Update");
|
++FrameCounter;
|
||||||
|
if (FrameCounter % 1000000 == 0)
|
||||||
|
{
|
||||||
|
Log("Frame!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user