diff --git a/src/window/sdl/sdl_window.c b/src/window/sdl/sdl_window.c new file mode 100644 index 0000000..ea3513b --- /dev/null +++ b/src/window/sdl/sdl_window.c @@ -0,0 +1,111 @@ +// vim:fileencoding=utf-8:foldmethod=marker + +#include "../window.h" +#include "../../config/config_inc.h" +#include "../../utils/utils_inc.h" +#include "../../wapp/wapp.h" +#include "../../cglm/struct.h" +#include +#include +#include +#include +#include +#include + +wp_intern WpLogger logger = wpLogMakeLogger(wpStr8LitRo("SDL Window")); + +wp_intern b8 loadGpuLibrary(void); +wp_intern void unloadGpuLibrary(void); +wp_intern SDL_WindowFlags getGpuWindowFlags(void); + +b8 windowBackendInit(void) { + return SDL_Init(SDL_INIT_VIDEO) && loadGpuLibrary(); +} + +Window windowCreate(WpStr8 title, ivec2s size) { + Window window = {0}; + + char character[4096] = {0}; + wpStr8CopyToCstr(character, &title, 4096); + window.display_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); + window.size = (ivec2s){ .x = window.display_scale * size.x, + .y = window.display_scale * size.y }; + window.internal.window = SDL_CreateWindow(character, window.size.x, window.size.y, + SDL_WINDOW_RESIZABLE | getGpuWindowFlags()); + assertWithLogger(window.internal.window != NULL, &logger, "Failed to create SDL window"); + + i32 w = 0; + i32 h = 0; + if (SDL_GetWindowSize(window.internal.window, &w, &h)) { + window.size = (ivec2s){ .x = w, .y = h }; + } + + WpStr8 msg = wpStr8Buf(4096); + wpStr8Format(&msg, "Created SDL window: %p", window.internal.window); + wpLogDebug(&logger, msg); + + return window; +} + +b8 windowShouldClose(const Window *window) { + b8 output = false; + + SDL_Event event = {0}; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_QUIT || + (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE)) { + output = true; + break; + } + } + + return output; +} + +void windowDestroy(Window *window) { + if (window) { + SDL_DestroyWindow((SDL_Window *)window->internal.window); + } +} + +void windowBackendQuit(void) { + unloadGpuLibrary(); + SDL_Quit(); +} + +wp_intern b8 loadGpuLibrary(void) { + Config config = configGet(); + + switch (config.gpu_backend) { + case GPU_BACKEND_VULKAN: { + return SDL_Vulkan_LoadLibrary(NULL); + } break; + default: { + return false; + } + } +} + +wp_intern void unloadGpuLibrary(void) { + Config config = configGet(); + + switch (config.gpu_backend) { + case GPU_BACKEND_VULKAN: { + SDL_Vulkan_UnloadLibrary(); + } break; + default: break; + } +} + +wp_intern SDL_WindowFlags getGpuWindowFlags(void) { + Config config = configGet(); + + switch (config.gpu_backend) { + case GPU_BACKEND_VULKAN: { + return SDL_WINDOW_VULKAN; + } break; + default: { + return 0; + } + } +} diff --git a/src/window/window.h b/src/window/window.h new file mode 100644 index 0000000..6beceaf --- /dev/null +++ b/src/window/window.h @@ -0,0 +1,37 @@ +// vim:fileencoding=utf-8:foldmethod=marker + +#ifndef WINDOW_H +#define WINDOW_H + +#include "../wapp/wapp.h" +#include "../cglm/struct.h" + +#ifdef WP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif + +enum WindowErr { + WINDOW_ERR_CREATION_FAILED, +}; + +typedef struct WindowInternal { + void *window; +} WindowInternal; + +typedef struct Window { + WindowInternal internal; + ivec2s size; + f32 display_scale; +} Window; + +wp_extern b8 windowBackendInit(void); +wp_extern Window windowCreate(WpStr8 title, ivec2s size); +wp_extern b8 windowShouldClose(const Window *window); +wp_extern void windowDestroy(Window *window); +wp_extern void windowBackendQuit(void); + +#ifdef WP_PLATFORM_CPP +END_C_LINKAGE +#endif + +#endif // !WINDOW_H diff --git a/src/window/window_inc.h b/src/window/window_inc.h new file mode 100644 index 0000000..1a8ab3d --- /dev/null +++ b/src/window/window_inc.h @@ -0,0 +1,8 @@ +// vim:fileencoding=utf-8:foldmethod=marker + +#ifndef WINDOW_INC_H +#define WINDOW_INC_H + +#include "window.h" + +#endif // !WINDOW_INC_H diff --git a/src/window/window_src.c b/src/window/window_src.c new file mode 100644 index 0000000..018b600 --- /dev/null +++ b/src/window/window_src.c @@ -0,0 +1,12 @@ +// vim:fileencoding=utf-8:foldmethod=marker + +#ifndef WINDOW_SRC_C +#define WINDOW_SRC_C + +#ifdef USE_SDL_WINDOW_BACKEND +#include "sdl/sdl_window.c" +#else +#error "No window backend selected" +#endif + +#endif // !WINDOW_SRC_C