diff --git a/build.bat b/build.bat index 0ddb0b2..c38f006 100644 --- a/build.bat +++ b/build.bat @@ -3,9 +3,12 @@ setlocal enabledelayedexpansion set vulkan_include=C:\VulkanSDK\1.4.304.1\Include set linker_flags=-incremental:no -opt:ref User32.lib kernel32.lib +set vma_src=..\external\vma\vma.cpp +set game_src=..\src\entry_windows.c +set vma_obj=.\vma.obj mkdir build pushd build -cl /c /I ..\external\vma /I %vulkan_include% ..\external\vma\vma.cpp -cl /DEBUG /I ..\external /I %vulkan_include% -DBUILD_DEBUG -DSTG_VULKAN_RENDERER ..\src\main.c /link %linker_flags% +cl /DEBUG /c /I ..\external\vma /I %vulkan_include% /EHsc ..\external\vma\vma.cpp +cl /DEBUG /I ..\external /I %vulkan_include% -DBUILD_DEBUG -DSTG_VULKAN_RENDERER ..\src\entry_windows.c %vma_obj% /link %linker_flags% popd diff --git a/src/entry_windows.c b/src/entry_windows.c new file mode 100644 index 0000000..1b935b1 --- /dev/null +++ b/src/entry_windows.c @@ -0,0 +1,45 @@ +#ifdef _WIN32 + +#include "entry_windows.h" + +#include "platform.c" +#include "util.c" +#include "arena.c" +#include "render.c" +#include "game.c" + +int CALLBACK WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd_line, int show_code) +{ + win32_instance = instance; + +#ifdef BUILD_DEBUG + { + AllocConsole(); + } +#endif + + u8 *mem = (u8 *)MemAllocZeroed(MB(32)); + Arena *arena = CreateArenaDebug(mem, MB(32), 1); + + isize renderer_mem_size = MB(8); + + rawptr renderer_mem = ArenaAlloc(arena, renderer_mem_size); + Arena *renderer_arena = CreateArenaDebug(renderer_mem, MB(8), 2); + + Assert(CreatePlatformWindow(), "Failed to initialize window"); + + GameInput *inputs = ArenaAlloc(arena, sizeof(GameInput) * 10); + u32 i_count = 0; + + InitializeGame(renderer_arena); + + while (!global_quit) + { + GetWindowEvents(); + RunCycle(inputs, i_count); + } + + DestroyGame(); +} + +#endif // _WIN32 diff --git a/src/entry_windows.h b/src/entry_windows.h new file mode 100644 index 0000000..5e02805 --- /dev/null +++ b/src/entry_windows.h @@ -0,0 +1,20 @@ +#ifdef _WIN32 + +#pragma once + +#define STB_SPRINTF_IMPLEMENTATION + +#define WINDOW_NAME "Video Game" + +#include "stb/stb_sprintf.h" + +#include "shared_types.h" +#include "platform.h" +#include "util.h" +#include "arena.h" +#include "render.h" +#include "game.h" + +int CALLBACK WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd_line, int show_code); + +#endif // _WIN32 diff --git a/src/platform_linux.c b/src/platform_linux.c index c30bf22..cc31f0c 100644 --- a/src/platform_linux.c +++ b/src/platform_linux.c @@ -69,9 +69,8 @@ i32 _EPrintf(const char *fmt, va_list arg) int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg); if (sprf_res < 0) return sprf_res; - i32 pr_res = EPrint(&buffer); - return pr_res; + return EPrint(&buffer); } i32 _Printf(const char *fmt, va_list arg) diff --git a/src/platform_windows.c b/src/platform_windows.c index 05b1306..e3b2e8b 100644 --- a/src/platform_windows.c +++ b/src/platform_windows.c @@ -1,5 +1,5 @@ -HINSTANCE win32_instance = {}; -Window win32_window = {}; +HINSTANCE win32_instance = {0}; +GameWindow win32_window = {0}; b32 global_quit = false; LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM w_param, LPARAM l_param) @@ -10,13 +10,15 @@ LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM w_param, LPARAM l_ { case WM_SIZE: { + Printfln("Window resizing"); win32_window.w = LOWORD(l_param); win32_window.h = HIWORD(l_param); - win32_window.resize_requested = true; + //SetRenderResolution(win32_window.w, win32_window.h); } break; case WM_DESTROY: // TODO(MA): Probably handle these separately but for now, they're together case WM_CLOSE: { + Printf("quitting"); global_quit = true; } break; case WM_ACTIVATEAPP: @@ -32,15 +34,6 @@ LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM w_param, LPARAM l_ return result; } -int CALLBACK WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd_line, int show_code) -{ - win32_instance = instance; - - int result = main(__argc, __argv); - - PostQuitMessage(result); -} - b32 _LoadLib(const char *name, Library *out_lib) { b32 success = true; @@ -72,12 +65,12 @@ b32 _InitPlatform() rawptr _MemAlloc(isize size) { - return NULL; + return (rawptr)VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); } rawptr _MemAllocZeroed(isize size) { - return NULL; + return _MemAlloc(size); } isize _GetPageSize() @@ -85,24 +78,61 @@ isize _GetPageSize() return 0; } +i32 _Write(void const *str, DWORD std_handle) +{ + DWORD written; + BOOL success = WriteFile(GetStdHandle(std_handle), str, StrLen(str), &written, NULL); + return success ? (i32)written : -1; +} + +i32 _Printf(const char *fmt, va_list arg) +{ + char buffer[1024]; + + int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg); + + i32 pr_res; + if (sprf_res < 0) + pr_res = sprf_res; + else + pr_res = _Write(buffer, STD_OUTPUT_HANDLE); + + return pr_res; +} + +i32 _Printfln(const char *fmt, va_list arg) +{ + char buffer[1024]; + + int sprf_res = stbsp_vsnprintf(&buffer[0], 1023, fmt, arg); + + i32 pr_res; + if (sprf_res < 0) + pr_res = sprf_res; + else + { + buffer[sprf_res] = '\n'; + buffer[sprf_res+1] = '\0'; + pr_res = _Write(&buffer, STD_OUTPUT_HANDLE); + } + + return pr_res; +} + i32 _EPrint(void const *str) { - return 0; + return _Write(str, STD_ERROR_HANDLE); } -i32 _Printf(const char *fmt, ...) +i32 _EPrintf(const char *fmt, va_list arg) { - return 0; -} + char buffer[1024]; -i32 _Printfln(const char *fmt, ...) -{ - return 0; -} + int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg); -i32 _EPrintf(const char *fmt, ...) -{ - return 0; + if (sprf_res < 0) return sprf_res; + + return EPrint(&buffer); } b32 _CreateWindow() @@ -122,11 +152,11 @@ b32 _CreateWindow() if (success) { - HWND window_handle = CreateWindowEx( + HWND window_handle = CreateWindowExA( 0, window_class.lpszClassName, "Video Game", - WS_OVERLAPPEDWINDOW|WS_VISIBLE, + (WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME)|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, @@ -146,17 +176,28 @@ b32 _CreateWindow() return success; } -Window *_GetWindow() +GameWindow *_GetWindow() { return &win32_window; } -void _ProcessWindowEvents() +void GetWindowEvents() { - + BOOL has_msg = false; + MSG message; + do + { + has_msg = PeekMessage(&message, 0, 0, 0, PM_REMOVE); + if (has_msg > 0) + { + TranslateMessage(&message); + DispatchMessage(&message); + } + } + while (has_msg); } -void _WaitForWindowEvent(WindowEvent *event) +void WaitForWindowEvent() { MSG message; BOOL message_result = GetMessageA(&message, 0, 0, 0); diff --git a/src/platform_windows.h b/src/platform_windows.h index 31543fd..43efd10 100644 --- a/src/platform_windows.h +++ b/src/platform_windows.h @@ -6,35 +6,6 @@ #define WINDOW_CLASS_NAME "GearsWindowClass" -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -typedef intptr_t intptr; -typedef uintptr_t uintptr; - -#if defined ( _WIN64 ) -typedef int64_t isize; -typedef uint64_t usize; -#elif defined ( _WIN32 ) -typedef int32_t isize; -typedef uint32_t usize; -#endif - -typedef float f32; -typedef double f64; - -typedef uint8_t b8; -typedef uint32_t b32; - -typedef void * rawptr; - typedef struct { HMODULE module; @@ -46,7 +17,7 @@ typedef struct HWND handle; u16 h, w; b32 resize_requested; -} Window; +} GameWindow; typedef struct { @@ -62,12 +33,12 @@ rawptr _MemAllocZeroed(isize size); isize _GetPageSize(); i32 _EPrint(void const *str); -i32 _Printf(const char *fmt, ...); -i32 _Printfln(const char *fmt, ...); -i32 _EPrintf(const char *fmt, ...); +i32 _Printf(const char *fmt, va_list arg); +i32 _Printfln(const char *fmt, va_list arg); +i32 _EPrintf(const char *fmt, va_list arg); b32 _CreateWindow(); -Window *_GetWindow(); -b32 _GetWindowEvent(WindowEvent *event); -void _WaitForWindowEvent(WindowEvent *event); +GameWindow *_GetWindow(); +void GetWindowEvents(); +void WaitForWindowEvent(); WindowSize _GetWindowSize(); diff --git a/src/render_vulkan.c b/src/render_vulkan.c index 8a17697..0fb92c1 100644 --- a/src/render_vulkan.c +++ b/src/render_vulkan.c @@ -321,11 +321,11 @@ static void CopyImageToImage(VkCommandBuffer cmd, VkImage src, VkImage dst, VkEx VkImageBlit2 blit = { .sType = STYPE(IMAGE_BLIT_2), .srcOffsets = { - {}, + {0}, { .x = (i32)src_ext.width, .y = (i32)src_ext.height, .z = 1 }, }, .dstOffsets = { - {}, + {0}, { .x = (i32)dst_ext.width, .y = (i32)dst_ext.height, .z = 1 }, }, .srcSubresource = { @@ -492,7 +492,7 @@ static DeviceQueues CheckDeviceQueueSupport(VkPhysicalDevice device, VkSurfaceKH static b32 CheckDevicePropertiesSupport(VkPhysicalDevice device, VkSurfaceKHR surface, b32 *discrete) { b32 success = false; - VkPhysicalDeviceProperties properties = {}; + VkPhysicalDeviceProperties properties = {0}; vkGetPhysicalDeviceProperties(device, &properties); if (VK_API_VERSION_MINOR(properties.apiVersion) >= 3) @@ -593,7 +593,7 @@ static b32 CreateDevice() b32 success = false; if (phys_device != NULL) { - VkDeviceQueueCreateInfo queue_info[2] = {}; + VkDeviceQueueCreateInfo queue_info[2] = {0}; f32 priority = 1.0f; u32 count = 1; @@ -652,6 +652,14 @@ static b32 InitVkInstanceFunctions() { INIT_INST_FN(vkCreateXcbSurfaceKHR); } + #elif _WIN32 + { + INIT_INST_FN(vkCreateWin32SurfaceKHR); + } + #elif __APPLE__ || __MACH__ + #error Not yet implemented + #elif __unix__ && !__linux__ + #error Not yet implemented #endif #ifdef BUILD_DEBUG @@ -762,11 +770,23 @@ static b32 CreateSurface() #elif _WIN32 static b32 CreateSurface() { - Window *window = GetWindowPtr(); + b32 success = true; + + GameWindow *window = GetWindowPtr(); VkWin32SurfaceCreateInfoKHR surface_info = { - .sType = STYPE(WIN_32_SURFACE_CREATE_INFO_KHR), - .hinstance = + .sType = STYPE(WIN32_SURFACE_CREATE_INFO_KHR), + .hinstance = window->instance, + .hwnd = window->handle, }; + + VkResult result = vkCreateWin32SurfaceKHR(renderer.vk.inst, &surface_info, NULL, &renderer.vk.surface); + if (result != VK_SUCCESS) + { + Printfln("Unable to create surface: %s", VkResultStr(result)); + success = false; + } + + return success; } #endif diff --git a/src/render_vulkan.h b/src/render_vulkan.h index 79f46d6..42ab995 100644 --- a/src/render_vulkan.h +++ b/src/render_vulkan.h @@ -63,8 +63,15 @@ VK_DECLARE(vkGetPhysicalDeviceImageFormatProperties); #ifdef __linux__ VK_DECLARE(vkCreateXcbSurfaceKHR); +#elif _WIN32 +VK_DECLARE(vkCreateWin32SurfaceKHR); +#elif __APPLE__ || __MACH__ +#error Not yet implemented +#elif __unix__ && !__linux__ +#error Not yet implemented #endif + #ifdef BUILD_DEBUG VK_DECLARE(vkCreateDebugUtilsMessengerEXT); VK_DECLARE(vkDestroyDebugUtilsMessengerEXT); @@ -271,10 +278,10 @@ static b32 InitVkInstanceFunctions(); static b32 InitVkGlobalFunctions(); static b32 CreateSurface(); static b32 CreateDevice(); -static b32 CheckQueueSurfaceSupport(); -static DeviceQueues CheckDeviceQueueSupport(); -static b32 CheckDevicePropertiesSupport(); -static b32 CheckDeviceFeatureSupport(); +static b32 CheckQueueSurfaceSupport(i32 index, VkPhysicalDevice device, VkSurfaceKHR surface); +static DeviceQueues CheckDeviceQueueSupport(VkPhysicalDevice device, VkSurfaceKHR surface); +static b32 CheckDevicePropertiesSupport(VkPhysicalDevice device, VkSurfaceKHR surface, b32 *discrete); +static b32 CheckDeviceFeatureSupport(VkPhysicalDevice device); static b32 InitVkDeviceFunctions(); static b32 CreateVmaAllocator(); static b32 CreateFrameStructures(); diff --git a/src/shared_types.h b/src/shared_types.h index 4b17024..ca6abf3 100644 --- a/src/shared_types.h +++ b/src/shared_types.h @@ -1,6 +1,7 @@ #pragma once #ifdef __linux__ + #include #include @@ -27,8 +28,40 @@ typedef uint8_t b8; typedef uint32_t b32; typedef void * rawptr; + #elif _WIN32 +#include + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef intptr_t intptr; +typedef uintptr_t uintptr; + +#if defined ( _WIN64 ) +typedef int64_t isize; +typedef uint64_t usize; +#elif defined ( _WIN32 ) +typedef int32_t isize; +typedef uint32_t usize; +#endif + +typedef float f32; +typedef double f64; + +typedef uint8_t b8; +typedef uint32_t b32; + +typedef void * rawptr; + #elif __APPLE__ || __MACH__ #else // unix