From 912174e7250add2af2293f953305a729260b0eac Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Tue, 31 Dec 2019 00:04:33 +0200 Subject: [PATCH] Experimentations with redrawing the opengl view. --- platform_mac/mac_4ed.mm | 84 ++++++++++++++++++++++++++----- platform_mac/mac_4ed_functions.mm | 8 +-- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index 15d73837..db01684c 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -244,8 +244,42 @@ mac_free_object(Mac_Object *object){ // (GLsizei)bounds.size.height); } +- (void)drawRect:(NSRect)bounds{ + // [self getFrame]; + printf("Draw Rect!\n"); +} + +- (BOOL)acceptsFirstResponder{ + return YES; +} + +- (BOOL)becomeFirstResponder{ + return YES; +} + +- (BOOL)resignFirstResponder{ + return YES; +} + +- (void)keyDown:(NSEvent *)event{ + printf("Key Down!\n"); + [self requestDisplay]; +} + +- (void)mouseMoved:(NSEvent*)event{ + printf("Mouse Moved!\n"); + [self requestDisplay]; +} + +- (void)mouseDown:(NSEvent*)event{ + printf("Mouse Down!\n"); + [self requestDisplay]; +} + - (void)requestDisplay{ printf("Display Requested\n"); + + [self setNeedsDisplayInRect:[mac_vars.window frame]]; } @end @@ -269,25 +303,28 @@ main(int arg_count, char **args){ NSRect screen_rect = [[NSScreen mainScreen] frame]; NSRect initial_frame = NSMakeRect((screen_rect.size.width - w) * 0.5f, (screen_rect.size.height - h) * 0.5f, w, h); - NSWindow* window = [[NSWindow alloc] initWithContentRect: initial_frame - styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable - backing: NSBackingStoreBuffered - defer: NO]; + u32 style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; - [window setBackgroundColor: NSColor.blackColor]; - [window setDelegate: app_delegate]; - [window setTitle: @"4coder"]; - [window makeKeyAndOrderFront: nil]; + mac_vars.window = [[NSWindow alloc] initWithContentRect:initial_frame + styleMask:style_mask + backing:NSBackingStoreBuffered + defer:NO]; + + [mac_vars.window setBackgroundColor:NSColor.blackColor]; + [mac_vars.window setDelegate:app_delegate]; + [mac_vars.window setTitle:@"4coder"]; + [mac_vars.window setAcceptsMouseMovedEvents:YES]; // NOTE(yuval): Create OpenGLView - NSView* content_view = [window contentView]; + NSView* content_view = [mac_vars.window contentView]; // TODO(yuval): Finish view setup! mac_vars.view = [[OpenGLView alloc] init]; - [mac_vars.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [mac_vars.view setFrame:[content_view bounds]]; + [mac_vars.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [content_view addSubview:mac_vars.view]; + [mac_vars.window makeKeyAndOrderFront:nil]; dll_init_sentinel(&mac_vars.free_mac_objects); dll_init_sentinel(&mac_vars.timer_objects); @@ -297,9 +334,32 @@ main(int arg_count, char **args){ system_wake_up_timer_set(timer, 5000); // NOTE(yuval): Start the app's run loop +#if 1 [NSApp run]; - - +#else + for (;;) { + u64 count = 0; + + NSEvent* event; + do { + event = [NSApp nextEventMatchingMask:NSEventMaskAny + untilDate:nil//[NSDate distantFuture] + inMode:NSDefaultRunLoopMode + dequeue:YES]; + + if (event != nil) { + // printf("Event: %lu\n", [event type]); + ++count; + } + + [NSApp sendEvent:event]; + } while (event != nil); + + if (count > 1) { + printf("Count: %llu\n", count); + } + } +#endif #if 0 // NOTE(yuval): Context Setup diff --git a/platform_mac/mac_4ed_functions.mm b/platform_mac/mac_4ed_functions.mm index f191f69d..f1938a76 100644 --- a/platform_mac/mac_4ed_functions.mm +++ b/platform_mac/mac_4ed_functions.mm @@ -397,10 +397,10 @@ system_wake_up_timer_set_sig(){ Mac_Object *object = (Mac_Object*)mac_to_object(handle); if (object->kind == MacObjectKind_Timer){ f64 time_seconds = ((f64)time_milliseconds / 1000.0); - object->timer.timer = [NSTimer scheduledTimerWithTimeInterval: time_seconds - target: mac_vars.view - selector: @selector(requestDisplay) - userInfo: nil repeats:NO]; + object->timer.timer = [NSTimer scheduledTimerWithTimeInterval:time_seconds + target:mac_vars.view + selector:@selector(requestDisplay) + userInfo:nil repeats:NO]; } }