From 81e96bb4f6731254d88f96e5afba2f0a4e9fe1b1 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 7 Jan 2023 09:45:19 +0800 Subject: [PATCH] Isolate window methods Define create window prototype in header. --- src/main.h | 37 +++++++++++++++++++++++++++---------- src/main.m | 29 ++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/main.h b/src/main.h index 3fdeef7..428d58e 100644 --- a/src/main.h +++ b/src/main.h @@ -1,16 +1,33 @@ -// -// main.h -// cerberus -// -// Created by Allan Crisostomo on 2023-01-06. -// - -#ifndef main_h -#define main_h +#ifndef MAIN_H +#define MAIN_H #define WINDOW_X 0 #define WINDOW_Y 0 #define WINDOW_WIDTH 350 #define WINDOW_HEIGHT 250 -#endif /* main_h */ +typedef enum { + CBR_WINDOW_STYLE_FLAG_BORDERLESS = 0, + CBR_WINDOW_STYLE_FLAG_TITLED = 1 << 0, + CBR_WINDOW_STYLE_FLAG_CLOSABLE = 1 << 1, + CBR_WINDOW_STYLE_FLAG_MINIATURIZABLE = 1 << 2, + CBR_WINDOW_STYLE_FLAG_RESIZABLE = 1 << 3, + CBR_WINDOW_STYLE_FLAG_FULL_SCREEN = 1 << 14, +} CBR_WindowStyleFlag; + +typedef struct { + float x; + float y; + float width; + float height; +} CBR_Rect; + +typedef struct { + CBR_Rect rect; + CBR_WindowStyleFlag style_flags; + const char* title; +} CBR_CreateWindowParams; + +void* CBR_CreateWindow(CBR_CreateWindowParams); + +#endif diff --git a/src/main.m b/src/main.m index a219440..266331a 100644 --- a/src/main.m +++ b/src/main.m @@ -1,15 +1,34 @@ #import +#import "main.h" + +void* CBR_CreateWindow(CBR_CreateWindowParams params) { + id window = [ + [NSWindow alloc] initWithContentRect:NSMakeRect(params.rect.x, params.rect.y, params.rect.width, params.rect.height) + styleMask: (NSWindowStyleMask)params.style_flags + backing:NSBackingStoreBuffered + defer:NO + ]; + [window cascadeTopLeftFromPoint:NSMakePoint(0,0)]; + [window setTitle: [NSString stringWithUTF8String:(params.title)]]; + [window makeKeyAndOrderFront:nil]; + return (__bridge void *)(window); +} int main() { @autoreleasepool{ [NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; id applicationName = [[NSProcessInfo processInfo] processName]; - id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 120, 120) - styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO]; - [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; - [window setTitle: applicationName]; - [window makeKeyAndOrderFront:nil]; + void* window = CBR_CreateWindow((CBR_CreateWindowParams) { + .rect = { + .x = WINDOW_X, + .y = WINDOW_Y, + .width = WINDOW_WIDTH, + .height = WINDOW_HEIGHT + }, + .style_flags = CBR_WINDOW_STYLE_FLAG_TITLED, + .title = [applicationName cStringUsingEncoding:NSUTF8StringEncoding] + }); [NSApp activateIgnoringOtherApps:YES]; [NSApp run]; }