This commit is contained in:
Asuro
2025-02-08 18:54:09 +01:00
parent 149de529e6
commit aa18ffd1a5
8 changed files with 285 additions and 339 deletions

3
.gitmodules vendored
View File

@@ -10,3 +10,6 @@
[submodule "src/dependency/bgfx.cmake"]
path = src/dependency/bgfx.cmake
url = https://github.com/bkaradzic/bgfx.cmake
[submodule "src/dependency/SDL"]
path = src/dependency/SDL
url = https://github.com/libsdl-org/SDL

1
src/.ignore Normal file
View File

@@ -0,0 +1 @@
dependency/

View File

@@ -5,9 +5,15 @@ if (MSVC)
else()
add_compile_options(-g -gcodeview)
add_link_options(-fuse-ld=lld -g -Wl,--pdb=)
add_link_options(-g -Wl,--pdb=)
# add_link_options(-fuse-ld=lld -g -Wl,--pdb=)
endif()
# set the output directory for built objects.
# This makes sure that the dynamic library goes into the build directory automatically.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
# Engine
file(GLOB_RECURSE sources_engine engine/*.cpp engine/*.h)
add_executable(PuzGameEngine ${sources_engine})
@@ -22,11 +28,15 @@ file(GLOB_RECURSE sources_game game/*.cpp game/*.h)
add_library(PuzGame SHARED ${sources_game})
set_property(TARGET PuzGame PROPERTY CXX_STANDARD 17)
# SDL
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/SDL" EXCLUDE_FROM_ALL)
# BGFX
SET(BGFX_BUILD_TOOLS ON)
SET(BGFX_BUILD_EXAMPLES ON)
SET(BGFX_BUILD_EXAMPLES OFF)
#SET(BGFX_CONFIG_MULTITHREADED OFF)
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake")
target_link_libraries(PuzGame bx bimg bgfx)
target_link_libraries(PuzGame bx bimg bgfx SDL3::SDL3)
target_link_libraries(PuzGameEngine bx)
set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2")

1
src/dependency/SDL Submodule

Submodule src/dependency/SDL added at 7c12c63f63

View File

@@ -41,55 +41,6 @@ namespace
Shutdown ShutdownFunc;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
LRESULT Res = 0;
switch (message)
{
default:
{
Res = DefWindowProcW(hwnd, message, wparam, lparam);
break;
}
}
return Res;
}
HWND InitWindow()
{
uint32_t startWidth = 1920;
uint32_t startHeight = 1080;
HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
WNDCLASSEXW wnd;
bx::memSet(&wnd, 0, sizeof(wnd));
wnd.cbSize = sizeof(wnd);
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.lpfnWndProc = WndProc;
wnd.hInstance = instance;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.lpszClassName = L"EngineWin";
wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassExW(&wnd);
HWND window = CreateWindowExW(0, L"EngineWin", L"PuzGame", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, startWidth, startHeight, NULL, NULL, instance, 0);
if (window == NULL)
{
DWORD err = GetLastError();
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;
}
unsigned long FileWatcherThread(void* data)
{
DWORD bytesReturned = 0;
@@ -185,44 +136,28 @@ bool ReloadDLL()
int main()
{
HWND window = InitWindow();
if (window == NULL) return 1;
char PathBuf[512]{ 0 };
GetCurrentDirectory(sizeof(PathBuf), PathBuf);
printf("Current path: %s\n", PathBuf);
char PathBuf[512]{ 0 };
GetCurrentDirectory(sizeof(PathBuf), PathBuf);
if (!ReloadDLL()) return 1;
printf("Current path: %s\n", PathBuf);
DWORD fileWatcherThreadId = 0;
CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId);
if (!ReloadDLL()) return 1;
DWORD fileWatcherThreadId = 0;
CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId);
StartupFunc(window);
bool isRunning = true;
while (isRunning)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
StartupFunc(nullptr);
bool isRunning = true;
while (isRunning)
{
if (DevData.FileWatcher.Change)
{
if (msg.message == WM_QUIT)
{
isRunning = false;
}
DevData.FileWatcher.Change = false;
ShutdownFunc();
ReloadDLL();
StartupFunc(nullptr);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (DevData.FileWatcher.Change)
{
DevData.FileWatcher.Change = false;
ShutdownFunc();
ReloadDLL();
StartupFunc(window);
}
UpdateFunc();
}
return 0;
UpdateFunc();
}
return 0;
}

View File

@@ -1,7 +1,9 @@
#include "Setup.h"
#include "Log.h"
#include "SDL3/SDL_video.h"
#include "rendering/Rendering.h"
#include <cstdint>
#include <SDL3/SDL.h>
namespace Game
{
@@ -20,7 +22,29 @@ namespace Game
void Setup(void* window)
{
Log("Game Setup Start!");
Instance.Rendering.Setup(window);
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);
}
void Update()

View File

@@ -1,188 +1,161 @@
#include "Rendering.h"
#include "../Log.h"
#include "bgfx/defines.h"
#include "bgfx/platform.h"
#include "bx/timer.h"
#include <bx/file.h>
#include <bgfx/bgfx.h>
#include <bx/filepath.h>
#include <bx/readerwriter.h>
namespace Game
{
static VertexData CubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000 },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
};
struct PosColorVertex
{
float x;
float y;
float z;
uint32_t abgr;
};
const uint16_t CubeIndices[] =
{
0, 1, 2, // 0
1, 3, 2,
4, 6, 5, // 2
5, 6, 7,
0, 2, 4, // 4
4, 2, 6,
1, 5, 3, // 6
5, 7, 3,
0, 4, 1, // 8
4, 5, 1,
2, 3, 6, // 10
6, 3, 7,
};
static PosColorVertex cubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000 },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
};
namespace
{
static const uint16_t cubeTriList[] =
{
0, 1, 2,
1, 3, 2,
4, 6, 5,
5, 6, 7,
0, 2, 4,
4, 2, 6,
1, 5, 3,
5, 7, 3,
0, 4, 1,
4, 5, 1,
2, 3, 6,
6, 3, 7,
};
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
{
if (bx::open(_reader, _filePath) )
{
uint32_t size = (uint32_t)bx::getSize(_reader);
const bgfx::Memory* mem = bgfx::alloc(size+1);
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
bx::close(_reader);
mem->data[mem->size-1] = '\0';
return mem;
}
namespace
{
Log("Failed to load %s.", _filePath.getCPtr() );
return NULL;
}
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
{
if (bx::open(_reader, _filePath))
{
uint32_t size = (uint32_t)bx::getSize(_reader);
const bgfx::Memory* mem = bgfx::alloc(size + 1);
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
bx::close(_reader);
mem->data[mem->size - 1] = '\0';
return mem;
}
bgfx::ShaderHandle LoadShader(const char* name)
{
bx::FilePath filePath{"game/compiled-shaders/"};
switch (bgfx::getRendererType())
{
case bgfx::RendererType::Noop:
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: filePath.join("dx11"); break;
case bgfx::RendererType::Agc:
case bgfx::RendererType::Gnm: filePath.join("pssl"); break;
case bgfx::RendererType::Metal: filePath.join("metal"); break;
case bgfx::RendererType::Nvn: filePath.join("nvn"); break;
case bgfx::RendererType::OpenGL: filePath.join("glsl"); break;
case bgfx::RendererType::OpenGLES: filePath.join("essl"); break;
case bgfx::RendererType::Vulkan: filePath.join("spirv"); break;
case bgfx::RendererType::Count:
BX_ASSERT(false, "You should not be here!");
break;
}
char fileName[512]{0};
bx::strCopy(fileName, sizeof(fileName), name);
bx::strCat(fileName, sizeof(fileName), ".bin");
filePath.join(fileName);
bx::FileReader fileReader;
const bgfx::Memory* mem = loadMem(&fileReader, filePath.getCPtr());
if (mem == nullptr) return {};
bgfx::ShaderHandle result = bgfx::createShader(mem);
return result;
}
}
void GameRendering::Setup(void* window)
{
bgfx::Init init;
init.type = bgfx::RendererType::OpenGL;
//init.debug = true;
//init.callback = &Callback;
init.platformData.nwh = (void*)window;
init.platformData.ndt = nullptr;
init.platformData.type = bgfx::NativeWindowHandleType::Default;
init.resolution.width = State.WindowWidth;
init.resolution.height = State.WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
{
Log("BGFX setup failed!");
}
else
{
Log("BGFX setup succeded!");
}
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(CubeVertices, sizeof(CubeVertices)), VertLayout);
IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(CubeIndices, sizeof(CubeIndices)));
bgfx::ShaderHandle vertexShader = LoadShader("vert");
bgfx::ShaderHandle fragmentShader = LoadShader("frag");
bgfx::createProgram(vertexShader, fragmentShader, true);
State.StartTime = bx::getHPCounter();
}
void GameRendering::Update()
{
bgfx::setViewRect(0, 0, 0, State.WindowWidth, State.WindowHeight);
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);
}
// Set render states.
uint64_t state = 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
;
float time = (bx::getHPCounter() - State.StartTime) / (float)bx::getHPFrequency();
for (int32_t i = 0; i < 11 * 11; ++i)
{
uint32_t xx = i % 11;
uint32_t yy = i / 11;
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;
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, VertexBuffer);
bgfx::setIndexBuffer(IndexBuffer);
bgfx::setState(state);
bgfx::submit(0, Shader);
Log("Failed to load %s.", _filePath.getCPtr());
return NULL;
}
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %f", time);
bgfx::frame();
}
void GameRendering::Shutdown()
{
bgfx::shutdown();
}
bgfx::ShaderHandle loadShader(const char* FILENAME)
{
const char* shaderPath = "???";
switch (bgfx::getRendererType()) {
case bgfx::RendererType::Noop: break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: shaderPath = "game/compiled-shaders/dx11/"; break;
case bgfx::RendererType::Gnm: shaderPath = "game/compiled-shaders/pssl/"; break;
case bgfx::RendererType::Metal: shaderPath = "game/compiled-shaders/metal/"; break;
case bgfx::RendererType::OpenGL: shaderPath = "game/compiled-shaders/glsl/"; break;
case bgfx::RendererType::OpenGLES: shaderPath = "game/compiled-shaders/essl/"; break;
case bgfx::RendererType::Vulkan: shaderPath = "game/compiled-shaders/spirv/"; break;
}
char buffer[512]{ 0 };
bx::strCopy(buffer, sizeof(buffer), shaderPath);
bx::strCat(buffer, sizeof(buffer), FILENAME);
bx::strCat(buffer, sizeof(buffer), ".bin");
FILE* file = fopen(buffer, "rb");
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
const bgfx::Memory* mem = bgfx::alloc(fileSize + 1);
fread(mem->data, 1, fileSize, file);
mem->data[mem->size - 1] = '\0';
fclose(file);
return bgfx::createShader(mem);
}
}
void GameRendering::Setup(void* window)
{
bgfx::renderFrame();
bgfx::Init init;
init.type = bgfx::RendererType::Direct3D12;
init.debug = true;
init.callback = &Callback;
init.platformData.nwh = window;
init.platformData.ndt = nullptr;
init.platformData.type = bgfx::NativeWindowHandleType::Default;
init.resolution.width = State.WindowWidth;
init.resolution.height = State.WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
{
Log("BGFX setup failed!");
}
else
{
Log("BGFX setup succeded!");
}
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), VertLayout);
IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
bgfx::ShaderHandle vertexShader = loadShader("vert");
bgfx::ShaderHandle fragmentShader = loadShader("frag");
Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
State.StartTime = bx::getHPCounter();
}
void GameRendering::Update()
{
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::dbgTextPrintf(10, 10, 0x0F, "TEST");
bgfx::submit(0, Shader);
bgfx::frame();
}
void GameRendering::Shutdown()
{
bgfx::shutdown();
}
}

View File

@@ -1,102 +1,101 @@
#pragma once
#include "bimg/bimg.h"
#include <bgfx/bgfx.h>
#include <cstdio>
#include <bx/string.h>
#include <cstdio>
namespace Game
{
struct VertexData
{
float X;
float Y;
float Z;
uint32_t VertCol;
};
struct VertexData
{
float X;
float Y;
float Z;
uint32_t VertCol;
};
struct BgfxCallback : public bgfx::CallbackI
{
virtual ~BgfxCallback()
{
}
struct BgfxCallback : public bgfx::CallbackI
{
virtual ~BgfxCallback()
{
}
virtual void fatal(const char* _filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char* _str) override
{
// Something unexpected happened, inform user and bail out.
printf("Fatal error: 0x%08x: %s", _code, _str);
virtual void fatal(const char* _filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char* _str) override
{
// Something unexpected happened, inform user and bail out.
printf("Fatal error: 0x%08x: %s", _code, _str);
// Must terminate, continuing will cause crash anyway.
abort();
}
// Must terminate, continuing will cause crash anyway.
abort();
}
virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override
{
printf("%s (%d): ", _filePath, _line);
vprintf(_format, _argList);
}
virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override
{
printf("%s (%d): ", _filePath, _line);
vprintf(_format, _argList);
}
virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerEnd() override
{
}
virtual void profilerEnd() override
{
}
virtual uint32_t cacheReadSize(uint64_t _id) override
{
return 0;
}
virtual uint32_t cacheReadSize(uint64_t _id) override
{
return 0;
}
virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) override
{
return false;
}
virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) override
{
return false;
}
virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) override
{
}
virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) override
{
}
virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t /*_size*/, bool _yflip) override
{
}
virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t /*_size*/, bool _yflip) override
{
}
virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) override
{
}
virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) override
{
}
virtual void captureEnd() override
{
}
virtual void captureEnd() override
{
}
virtual void captureFrame(const void* _data, uint32_t /*_size*/) override
{
}
};
virtual void captureFrame(const void* _data, uint32_t /*_size*/) override
{
}
};
struct RenderState
{
int64_t StartTime = 0;
uint32_t WindowWidth = 1920;
uint32_t WindowHeight = 1080;
};
struct RenderState
{
int64_t StartTime = 0;
uint32_t WindowWidth = 1920;
uint32_t WindowHeight = 1080;
};
class GameRendering
{
private:
bgfx::VertexLayout VertLayout;
bgfx::VertexBufferHandle VertexBuffer;
bgfx::IndexBufferHandle IndexBuffer;
bgfx::ProgramHandle Shader;
BgfxCallback Callback;
RenderState State;
public:
void Setup(void* window);
void Update();
void Shutdown();
};
class GameRendering
{
private:
bgfx::VertexLayout VertLayout;
bgfx::VertexBufferHandle VertexBuffer;
bgfx::IndexBufferHandle IndexBuffer;
bgfx::ProgramHandle Shader;
BgfxCallback Callback;
RenderState State;
public:
void Setup(void* window);
void Update();
void Shutdown();
};
}