summaryrefslogtreecommitdiff
path: root/app.cpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-12-27 18:52:48 +1100
committerquou <quou@disroot.org>2024-12-27 18:52:48 +1100
commitd920e5d62020d751ccaa3491cc66275ade749011 (patch)
treee806a95c598e255f9883de98cc350a3b9279d0ce /app.cpp
parente8c93463b2ae5114f0c88e768e5625abac9d5a50 (diff)
building and running on windows with visual studio
Diffstat (limited to 'app.cpp')
-rw-r--r--app.cpp312
1 files changed, 281 insertions, 31 deletions
diff --git a/app.cpp b/app.cpp
index e1d3913..0eaf439 100644
--- a/app.cpp
+++ b/app.cpp
@@ -3,8 +3,11 @@
extern "C" {
#include "memory.h"
#include "plat.h"
+
+extern int entrypoint();
}
+
#ifdef plat_x11
#include <X11/Xlib.h>
#include <X11/Xutil.h>
@@ -237,6 +240,277 @@ struct App_Internal {
}
};
+extern "C" {
+#define GLAD_VULKAN_IMPLEMENTATION
+#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);
+}
+}
+
+int main() {
+ return entrypoint();
+}
+
+#endif
+
+#ifdef plat_win
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <winuser.h>
+
+Key key_from_vk(unsigned key) {
+ switch (key) {
+ case 0x00: return key_unknown;
+ case 0x41: return key_A;
+ case 0x42: return key_B;
+ case 0x43: return key_C;
+ case 0x44: return key_D;
+ case 0x45: return key_E;
+ case 0x46: return key_F;
+ case 0x47: return key_G;
+ case 0x48: return key_H;
+ case 0x49: return key_I;
+ case 0x4a: return key_J;
+ case 0x4b: return key_K;
+ case 0x4c: return key_L;
+ case 0x4d: return key_M;
+ case 0x4e: return key_N;
+ case 0x4f: return key_O;
+ case 0x50: return key_P;
+ case 0x51: return key_Q;
+ case 0x52: return key_R;
+ case 0x53: return key_S;
+ case 0x54: return key_T;
+ case 0x55: return key_U;
+ case 0x56: return key_V;
+ case 0x57: return key_W;
+ case 0x58: return key_X;
+ case 0x59: return key_Y;
+ case 0x5a: return key_Z;
+ case VK_F1: return key_f1;
+ case VK_F2: return key_f2;
+ case VK_F3: return key_f3;
+ case VK_F4: return key_f4;
+ case VK_F5: return key_f5;
+ case VK_F6: return key_f6;
+ case VK_F7: return key_f7;
+ case VK_F8: return key_f8;
+ case VK_F9: return key_f9;
+ case VK_F10: return key_f10;
+ case VK_F11: return key_f11;
+ case VK_F12: return key_f12;
+ case VK_DOWN: return key_down;
+ case VK_LEFT: return key_left;
+ case VK_RIGHT: return key_right;
+ case VK_UP: return key_up;
+ case VK_ESCAPE: return key_escape;
+ case VK_BACK: return key_backspace;
+ case VK_RETURN: return key_return;
+ case VK_TAB: return key_tab;
+ case VK_DELETE: return key_delete;
+ case VK_HOME: return key_home;
+ case VK_END: return key_end;
+ case VK_PRIOR: return key_page_up;
+ case VK_NEXT: return key_page_down;
+ case VK_INSERT: return key_insert;
+ case 16: return key_shift;
+ case 17: return key_control;
+ case 91: return key_super;
+ case VK_LMENU: return key_alt;
+ case VK_RMENU: return key_alt;
+ case VK_SPACE: return key_space;
+ case 0x30: return key_0;
+ case 0x31: return key_1;
+ case 0x32: return key_2;
+ case 0x33: return key_3;
+ case 0x34: return key_4;
+ case 0x35: return key_5;
+ case 0x36: return key_6;
+ case 0x37: return key_7;
+ case 0x38: return key_8;
+ case 0x39: return key_9;
+ }
+ return key_unknown;
+}
+
+static LRESULT CALLBACK win32_event_callback(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
+
+struct App_Internal {
+ HWND window;
+
+ RECT rect;
+
+ void init(App* app, const char* name) {
+ const int default_w = 1920;
+ const int default_h = 1080;
+ int create_w, create_h;
+ const DWORD dw_ex_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
+ const DWORD dw_style = WS_CAPTION | WS_SYSMENU |
+ WS_MINIMIZEBOX | WS_VISIBLE | WS_THICKFRAME | WS_MAXIMIZEBOX;
+ RECT win_rect = { 0, 0, default_w, default_h };
+ WNDCLASSA wc{};
+ wc.hIcon = LoadIcon(0, IDI_APPLICATION);
+ wc.hCursor = LoadCursor(0, IDC_ARROW);
+ wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+ wc.hInstance = GetModuleHandle(0);
+ wc.lpfnWndProc = win32_event_callback;
+ wc.lpszClassName = "corrosion_2";
+ RegisterClassA(&wc);
+ AdjustWindowRectEx(&win_rect, dw_style, FALSE, dw_ex_style);
+ create_w = win_rect.right - win_rect.left;
+ create_h = win_rect.bottom - win_rect.top;
+ window = CreateWindowExA(dw_ex_style, "corrosion_2", name, dw_style, 100, 100,
+ create_w, create_h, 0, 0, GetModuleHandle(0), 0);
+ assert(window != 0);
+ app->w = create_w;
+ app->h = create_h;
+ SetWindowLongPtr(window, GWLP_USERDATA, (LONG_PTR)app);
+ }
+
+ void begin(App* app) {
+ MSG msg;
+ while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ }
+
+ void end(App* app) {
+ (void)app;
+ }
+};
+
+static LRESULT CALLBACK win32_event_callback(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
+ App* app = (App*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
+ if (!app) return DefWindowProc(hwnd, msg, wparam, lparam);
+ switch (msg) {
+ case WM_CLOSE:
+ app->running = 0;
+ return 0;
+ case WM_SIZE:
+ app->w = lparam & 0xffff;
+ app->h = (lparam >> 16) & 0xffff;
+ app->on_resize();
+ return 0;
+ case WM_MOUSEMOVE:
+ app->mx = lparam & 0xffff;
+ app->my = (lparam >> 16) & 0xffff;
+ return 0;
+ case WM_LBUTTONDOWN:
+ app->mbtn_states[mbtn_left] |=
+ key_state_pressed |
+ key_state_just_pressed;
+ return 0;
+ case WM_MBUTTONDOWN:
+ app->mbtn_states[mbtn_middle] |=
+ key_state_pressed |
+ key_state_just_pressed;
+ return 0;
+ case WM_RBUTTONDOWN:
+ app->mbtn_states[mbtn_right] |=
+ key_state_pressed |
+ key_state_just_pressed;
+ return 0;
+ case WM_LBUTTONUP:
+ app->mbtn_states[mbtn_left] &= ~key_state_pressed;
+ app->mbtn_states[mbtn_left] |= key_state_just_released;
+ return 0;
+ case WM_MBUTTONUP:
+ app->mbtn_states[mbtn_middle] &= ~key_state_pressed;
+ app->mbtn_states[mbtn_middle] |= key_state_just_released;
+ return 0;
+ case WM_RBUTTONUP:
+ app->mbtn_states[mbtn_right] &= ~key_state_pressed;
+ app->mbtn_states[mbtn_right] |= key_state_just_released;
+ return 0;
+ case WM_MOUSEWHEEL:
+ app->scrolly += GET_WHEEL_DELTA_WPARAM(wparam) > 1 ? 1 : -1;
+ return 0;
+ case WM_KEYDOWN: {
+ Key key = key_from_vk((unsigned)wparam);
+ app->key_states[key] |=
+ key_state_pressed |
+ key_state_just_pressed;
+ } return 0;
+ case WM_KEYUP: {
+ Key key = key_from_vk((unsigned)wparam);
+ app->key_states[key] &= ~key_state_pressed;
+ app->key_states[key] |= key_state_just_released;
+ } return 0;
+ }
+ return DefWindowProc(hwnd, msg, wparam, lparam);
+}
+
+HINSTANCE g_instance;
+
+extern "C" {
+#define GLAD_VULKAN_IMPLEMENTATION
+#define VK_USE_PLATFORM_WIN32_KHR
+#include "glad_vk.h"
+VkSurfaceKHR app_create_vk_surface(App* app, VkInstance inst) {
+ App_Internal* i;
+ VkSurfaceKHR s;
+ VkWin32SurfaceCreateInfoKHR ci{};
+ VkResult r;
+ i = app->internal;
+ ci.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
+ ci.hwnd = i->window;
+ ci.hinstance = g_instance;
+ r = vkCreateWin32SurfaceKHR(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);
+}
+}
+
+int WinMain(
+ HINSTANCE instance,
+ HINSTANCE pinstance,
+ LPSTR argv,
+ int show_cmd
+) {
+ (void)pinstance;
+ (void)argv;
+ (void)show_cmd;
+ g_instance = instance;
+ return entrypoint();
+}
+
#endif
void App::init(const char* name) {
@@ -310,35 +584,11 @@ bool App::mjr(Mbtn b) {
void App::get_vk_exts(const char** exts, int& count) {
exts[count++] = "VK_KHR_surface";
+#if defined(plat_x11)
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);
-}
-}
+#elif defined(plat_win)
+ exts[count++] = "VK_KHR_win32_surface";
+#else
+ #error
+#endif
+} \ No newline at end of file