Add window
This commit is contained in:
1 parent
82710fba6b
commit
9ec343b86a
4 files changed
+168
No files matched your search
@@ -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 <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_error.h>
|
||||||
|
#include <SDL3/SDL_init.h>
|
||||||
|
#include <SDL3/SDL_keycode.h>
|
||||||
|
#include <SDL3/SDL_video.h>
|
||||||
|
#include <SDL3/SDL_vulkan.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
Reference in new issue
Block a user