Move 'glfw' out of the core file

This commit is contained in:
David Gonzalez Martin 2024-11-20 18:46:54 -06:00 committed by David
parent fabbef437a
commit 7f5b4b22c6
12 changed files with 152 additions and 29 deletions

View File

@ -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)

View File

@ -1,12 +1,11 @@
#if BB_CI == 0
#include <bloat-buster/gui.h>
#include <bloat-buster/bb_core.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <volk.h>
#include <std/virtual_buffer.h>
#include <std/graphics.h>
#include <bloat-buster/shader_compilation.h>
#include <bloat-buster/image_loader.h>
#include <bloat-buster/font.h>
@ -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)
{

View File

@ -10,7 +10,7 @@
#include <bloat-buster/llvm.h>
#include <bloat-buster/lld_driver.h>
#include <bloat-buster/lld_api.h>
#include <bloat-buster/gui.h>
#include <bloat-buster/bb_core.h>
#ifdef __APPLE__
#define clang_path "/opt/homebrew/opt/llvm/bin/clang"

View File

@ -0,0 +1,35 @@
#include <std/base.h>
#include <std/os.h>
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

View File

@ -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

View File

View File

@ -0,0 +1 @@

77
bootstrap/std/graphics.c Normal file
View File

@ -0,0 +1,77 @@
#include <std/graphics.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
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

View File

@ -1653,3 +1653,10 @@ void print_string(String message)
unused(message);
#endif
}
#if _WIN32
HANDLE os_windows_get_module_handle()
{
return GetModuleHandleW(0);
}
#endif

0
bootstrap/std/render.c Normal file
View File