diff options
author | quou <quou@disroot.org> | 2024-12-19 19:20:50 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-12-19 19:22:20 +1100 |
commit | bec7a1c90f7cda0a9e7d2e2628ac69b645108dc4 (patch) | |
tree | 830660aab41d90c24302d2c64e096292b62a4394 /app.cpp | |
parent | 73744341846d4e76d6910dc5a15dff6d74586e39 (diff) |
basic vulkan setup
Diffstat (limited to 'app.cpp')
-rw-r--r-- | app.cpp | 94 |
1 files changed, 69 insertions, 25 deletions
@@ -2,6 +2,7 @@ extern "C" { #include "memory.h" +#include "plat.h" } #ifdef plat_x11 @@ -98,15 +99,15 @@ struct App_Internal { const int default_w = 1920; const int default_h = 1080; XWindowAttributes wa; - XSetWindowAttributes attribs = { - .event_mask = - ExposureMask | - KeyPressMask | - KeyReleaseMask | - ButtonPressMask | - ButtonReleaseMask | - PointerMotionMask - }; + XSetWindowAttributes attribs{}; + attribs.event_mask = + ExposureMask | + KeyPressMask | + KeyReleaseMask | + ButtonPressMask | + ButtonReleaseMask | + PointerMotionMask + ; Window root; Visual* v; d = XOpenDisplay(0); @@ -142,6 +143,7 @@ struct App_Internal { void begin(App* app) { while (XPending(d)) { XEvent e; + XWindowAttributes wa; KeySym sym; Key key; int btn; @@ -155,6 +157,14 @@ struct App_Internal { app->running = 0; } break; + case Expose: + XGetWindowAttributes(d, wi, &wa); + if (app->w != wa.width || app->h != wa.height) { + app->w = wa.width; + app->h = wa.height; + app->on_resize(); + } + break; case MotionNotify: app->mx = e.xmotion.x; app->my = e.xmotion.y; @@ -229,19 +239,20 @@ struct App_Internal { #endif -App* App::create(const char* name) { - App* app; - App_Internal* i; +void App::init(const char* name) { + internal = (App_Internal*)arena_alloc( + arena, + sizeof *internal + ); + internal->init(this, name); +} + +void* App::alloc(int size, Arena*& arena) { char* mem = (char*)malloc(app_memory_size); - Arena* a = (Arena*)mem; - mem += sizeof *a; - init_arena(a, mem, app_memory_size - sizeof *a); - app = (App*)arena_alloc(a, sizeof *app + sizeof *i); - app->running = 0; - app->arena = a; - i = (App_Internal*)&app[1]; - i->init(app, name); - return app; + arena = (Arena*)mem; + mem += sizeof *arena; + init_arena(arena, mem, app_memory_size - sizeof *arena); + return arena_alloc(arena, size); } void App::destroy() { @@ -250,7 +261,6 @@ void App::destroy() { void App::begin() { int j; - App_Internal* i = (App_Internal*)&this[1]; for (j = 0; j < key_count; j++) key_states[j] &= ~( key_state_just_pressed | key_state_just_released @@ -259,12 +269,11 @@ void App::begin() { mbtn_states[j] &= ~( key_state_just_pressed | key_state_just_released ); - i->begin(this); + internal->begin(this); } void App::end() { - App_Internal* i = (App_Internal*)&this[1]; - i->end(this); + internal->end(this); } Key_State App::ks(Key k) { @@ -298,3 +307,38 @@ bool App::mjp(Mbtn b) { bool App::mjr(Mbtn b) { return mbtn_states[b] & key_state_just_released; } + +void App::get_vk_exts(const char** exts, int& count) { + exts[count++] = "VK_KHR_surface"; + exts[count++] = "VK_KHR_xlib_surface"; +} + +extern "C" { +#define VK_USE_PLATFORM_XLIB_KHR +#include "glad_vk.h" +VkSurfaceKHR app_create_vk_surface(App* app, VkInstance inst) { + App_Internal* i; + VkSurfaceKHR s; + VkXlibSurfaceCreateInfoKHR ci{}; + VkResult r; + i = app->internal; + ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; + ci.dpy = i->d; + ci.window = i->wi; + r = vkCreateXlibSurfaceKHR(inst, &ci, 0, &s); + if (r != VK_SUCCESS) { + print_err("Failed to create a Vulkan surface."); + pbreak(r); + } + return s; +} + +void app_destroy_vk_surface( + App* app, + VkInstance inst, + VkSurfaceKHR surf +) { + (void)app; + vkDestroySurfaceKHR(inst, surf, 0); +} +} |