diff --git a/CMakeLists.txt b/CMakeLists.txt index 0581e8e..b497248 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ target_link_libraries(${RUNNER_NAME} PRIVATE ${LIBRARY_NAME}) add_executable("${COMPILER_NAME}" "bootstrap/bloat-buster/main.c" "bootstrap/bloat-buster/pdb_image.c" - "bootstrap/bloat-buster/gui.c" + "bootstrap/bloat-buster/bb_core.c" ) target_link_libraries(${COMPILER_NAME} PRIVATE ${LIBRARY_NAME}) @@ -212,7 +212,10 @@ if (NOT BB_IS_CI) target_sources(${COMPILER_NAME} PRIVATE "bootstrap/bloat-buster/shader_compilation.c" "bootstrap/bloat-buster/image_loader.c" - "bootstrap/bloat-buster/font.c" + "bootstrap/std/font_cache.c" + "bootstrap/std/font_provider.c" + "bootstrap/std/graphics.c" + "bootstrap/std/render.c" ) target_include_directories(${COMPILER_NAME} PRIVATE dependencies/stb) diff --git a/bootstrap/bloat-buster/gui.c b/bootstrap/bloat-buster/bb_core.c similarity index 98% rename from bootstrap/bloat-buster/gui.c rename to bootstrap/bloat-buster/bb_core.c index 033b337..dd97205 100644 --- a/bootstrap/bloat-buster/gui.c +++ b/bootstrap/bloat-buster/bb_core.c @@ -1,12 +1,11 @@ #if BB_CI == 0 -#include +#include -#define GLFW_INCLUDE_NONE -#include -#include #include #include +#include + #include #include #include @@ -487,18 +486,8 @@ fn VulkanImage vk_image_from_texture(VkDevice device, const VkAllocationCallback void run_app(Arena* arena) { -#if defined(VK_USE_PLATFORM_XLIB_KHR) - glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11); -#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) - glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND); -#endif - - if (glfwInit() != GLFW_TRUE) - { - failed_execution(); - } - - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + u8 use_x11 = 1; + graphics_init(use_x11); vkok(volkInitialize()); const VkAllocationCallbacks* allocation_callbacks = 0; @@ -586,7 +575,13 @@ void run_app(Arena* arena) int initial_width = 1024; int initial_height = 768; - GLFWwindow* window = glfwCreateWindow(initial_width, initial_height, "Bloat Buster", 0, 0); + GraphicsWindow* window = graphics_window_create((GraphicsWindowCreate) { + .name = strlit("Bloat Buster"), + .size = { + .width = initial_width, + .height= initial_height, + }, + }); if (!window) { @@ -600,8 +595,8 @@ void run_app(Arena* arena) .sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR, .pNext = 0, .flags = 0, - .hinstance = GetModuleHandleW(0), - .hwnd = glfwGetWin32Window(window), + .hinstance = os_windows_get_module_handle(), + .hwnd = graphics_win32_window_get(window), }; vkok(vkCreateWin32SurfaceKHR(instance, &create_info, allocation_callbacks, &surface)); #endif @@ -610,8 +605,8 @@ void run_app(Arena* arena) .sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, .pNext = 0, .flags = 0, - .dpy = glfwGetX11Display(), - .window = glfwGetX11Window(window), + .dpy = graphics_x11_display_get(), + .window = graphics_x11_window_get(window), }; vkok(vkCreateXlibSurfaceKHR(instance, &create_info, allocation_callbacks, &surface)); #endif @@ -1349,14 +1344,14 @@ strlit("/usr/share/fonts/TTF/FiraSans-Regular.ttf") } u32 frame_completed = 0; - for (u32 frame_number = 0; !glfwWindowShouldClose(window); frame_number += frame_completed) + for (u32 frame_number = 0; !graphics_window_should_close(window); frame_number += frame_completed) { frame_completed = 0; - glfwPollEvents(); + graphics_poll_events(); - int width; - int height; - glfwGetWindowSize(window, &width, &height); + auto window_size = graphics_window_size_get(window); + auto width = window_size.width; + auto height = window_size.height; if (width == 0 || height == 0) { diff --git a/bootstrap/bloat-buster/main.c b/bootstrap/bloat-buster/main.c index b6c6027..7e2da13 100644 --- a/bootstrap/bloat-buster/main.c +++ b/bootstrap/bloat-buster/main.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #ifdef __APPLE__ #define clang_path "/opt/homebrew/opt/llvm/bin/clang" diff --git a/bootstrap/include/bloat-buster/gui.h b/bootstrap/include/bloat-buster/bb_core.h similarity index 100% rename from bootstrap/include/bloat-buster/gui.h rename to bootstrap/include/bloat-buster/bb_core.h diff --git a/bootstrap/include/std/graphics.h b/bootstrap/include/std/graphics.h new file mode 100644 index 0000000..97d7266 --- /dev/null +++ b/bootstrap/include/std/graphics.h @@ -0,0 +1,35 @@ +#include +#include + +STRUCT(GraphicsWindowSize) +{ + u32 width; + u32 height; +}; + +STRUCT(GraphicsWindowCreate) +{ + String name; + GraphicsWindowSize size; +}; + +typedef struct GraphicsWindow GraphicsWindow; + +EXPORT void graphics_init(u8 should_use_x11); +EXPORT GraphicsWindow* graphics_window_create(GraphicsWindowCreate create); +EXPORT u8 graphics_window_should_close(GraphicsWindow* window); +EXPORT void graphics_poll_events(); +EXPORT GraphicsWindowSize graphics_window_size_get(GraphicsWindow* window); + +#ifdef __linux__ +typedef unsigned long XID; +typedef struct _XDisplay Display; +typedef XID Window; + +EXPORT Display* graphics_x11_display_get(); +EXPORT Window graphics_x11_window_get(GraphicsWindow* window); +#endif + +#ifdef _WIN32 +EXPORT HANDLE graphics_win32_window_get(GraphicsWindow* window); +#endif diff --git a/bootstrap/include/std/os.h b/bootstrap/include/std/os.h index dfab787..c754d9c 100644 --- a/bootstrap/include/std/os.h +++ b/bootstrap/include/std/os.h @@ -94,3 +94,8 @@ EXPORT void os_directory_make(String path); EXPORT void calibrate_cpu_timer(); EXPORT void print_string(String string); + +#if _WIN32 +typedef void* HANDLE; +EXPORT HANDLE os_windows_get_module_handle(); +#endif diff --git a/bootstrap/include/std/render.h b/bootstrap/include/std/render.h new file mode 100644 index 0000000..e69de29 diff --git a/bootstrap/std/font_cache.c b/bootstrap/std/font_cache.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/bootstrap/std/font_cache.c @@ -0,0 +1 @@ + diff --git a/bootstrap/bloat-buster/font.c b/bootstrap/std/font_provider.c similarity index 100% rename from bootstrap/bloat-buster/font.c rename to bootstrap/std/font_provider.c diff --git a/bootstrap/std/graphics.c b/bootstrap/std/graphics.c new file mode 100644 index 0000000..4d2e5b2 --- /dev/null +++ b/bootstrap/std/graphics.c @@ -0,0 +1,77 @@ +#include + +#define GLFW_INCLUDE_NONE +#include +#include + +global_variable u8 use_x11 = 0; + +fn GraphicsWindow* graphics_window_from_glfw(GLFWwindow* window) +{ + return (GraphicsWindow*)window; +} + +fn GLFWwindow* glfw_window_from_graphics(GraphicsWindow* window) +{ + return (GLFWwindow*)window; +} + +void graphics_init(u8 should_use_x11) +{ +#ifdef __linux__ + use_x11 = should_use_x11; + int platform_hint = use_x11 ? GLFW_PLATFORM_X11 : GLFW_PLATFORM_WAYLAND; + glfwInitHint(GLFW_PLATFORM, platform_hint); +#endif + + if (glfwInit() != GLFW_TRUE) + { + failed_execution(); + } +} + +GraphicsWindow* graphics_window_create(GraphicsWindowCreate create) +{ + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + GLFWwindow* window = glfwCreateWindow(create.size.width, create.size.height, string_to_c(create.name), 0, 0); + + return graphics_window_from_glfw(window); +} + +u8 graphics_window_should_close(GraphicsWindow* window) +{ + return glfwWindowShouldClose(glfw_window_from_graphics(window)); +} + +void graphics_poll_events() +{ + glfwPollEvents(); +} + +GraphicsWindowSize graphics_window_size_get(GraphicsWindow* window) +{ + GLFWwindow* w = glfw_window_from_graphics(window); + GraphicsWindowSize result; + glfwGetWindowSize(w, (int*)&result.width, (int*)&result.height); + + return result; +} + +#ifdef _WIN32 +HANDLE graphics_win32_window_get(GraphicsWindow* window) +{ + return glfwGetWin32Window(glfw_window_from_graphics(window)); +} +#endif + +#ifdef __linux__ +Display* graphics_x11_display_get() +{ + return glfwGetX11Display(); +} + +Window graphics_x11_window_get(GraphicsWindow* window) +{ + return glfwGetX11Window(glfw_window_from_graphics(window)); +} +#endif diff --git a/bootstrap/std/os.c b/bootstrap/std/os.c index 097e5d4..89d4547 100644 --- a/bootstrap/std/os.c +++ b/bootstrap/std/os.c @@ -1653,3 +1653,10 @@ void print_string(String message) unused(message); #endif } + +#if _WIN32 +HANDLE os_windows_get_module_handle() +{ + return GetModuleHandleW(0); +} +#endif diff --git a/bootstrap/std/render.c b/bootstrap/std/render.c new file mode 100644 index 0000000..e69de29