From bb19a5931526d140c72f96edfbbcd403706d4b0c Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sat, 16 Nov 2024 18:37:14 -0700 Subject: [PATCH] extend cocoa drawing test Test app now draws 500 "meters" at random levels @ 50Hz. --- cocoatea/cocoatea.mm | 43 +++++++++++++++++++++++++++---------------- cocoatea/view.mm | 35 +++++++++++++++++++++++++---------- cocoatea/wscript | 5 +++-- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/cocoatea/cocoatea.mm b/cocoatea/cocoatea.mm index 5925ae7491..43ff30411d 100644 --- a/cocoatea/cocoatea.mm +++ b/cocoatea/cocoatea.mm @@ -1,17 +1,38 @@ /* -*- c++ -*- */ +#include + #include +#include "meter.h" #include "view.h" +std::vector meters; + +void +layout (NSView* view) +{ + double width = 10; + double height = 50; + double padding = 5; + + for (int line = 0; line < 6; ++line) { + for (int n = 0; n < 100; ++n) { + double r = (random() % 255) / 256.; + double g = (random() % 255) / 256.; + double b = (random() % 255) / 256.; + meters.push_back (new Meter (view, n * (width + padding), line * (height + padding), width, height, r, g, b, 1.0)); + } + } +} int main (int argc, char* argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSRect rect = NSMakeRect (100, 100, 500, 500); - NSRect frameRect = NSMakeRect (10, 10, 480, 480); + NSRect rect = NSMakeRect (100, 100, 1800, 399); + NSRect frameRect = NSMakeRect (0, 0, 1800, 399); NSWindow* win = [[NSWindow alloc] initWithContentRect:rect styleMask:NSWindowStyleMaskClosable @@ -20,28 +41,18 @@ main (int argc, char* argv[]) CTView* view = [[CTView alloc] initWithFrame:frameRect]; + layout (view); + [win setContentView:view]; - [[NSApplication sharedApplication] activateIgnoringOtherApps : YES ]; + [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; [win makeKeyAndOrderFront:win]; [NSRunLoop currentRunLoop]; - [NSTimer scheduledTimerWithTimeInterval:0.1 target:view selector:@selector(timedUpdate) userInfo:nil repeats:YES]; + [NSTimer scheduledTimerWithTimeInterval:0.02 target:view selector:@selector(timedUpdate) userInfo:nil repeats:YES]; [[NSApplication sharedApplication] run]; - [pool release]; return 0; } - - -/* - - 1, derive a new View type that will have our drawing methods - 2. add time to the run loop to trigger redraws - - figure out how to draw - - - */ diff --git a/cocoatea/view.mm b/cocoatea/view.mm index 597f3a7f7a..61160b5a15 100644 --- a/cocoatea/view.mm +++ b/cocoatea/view.mm @@ -1,10 +1,15 @@ /* -*- c++ -*- */ #include +#include + #include +#include "meter.h" #include "view.h" +extern std::vector meters; + @implementation CTView -(BOOL)acceptsFirstResponder @@ -23,34 +28,44 @@ return YES; } +-(BOOL)opaque +{ + return NO; +} + -(void) timedUpdate { - NSRect rect = NSMakeRect (random() % 100, random() % 100, random() % 400, random() % 400); - [self setNeedsDisplayInRect:rect]; + for (auto & m : meters) { + m->set_level ((random() % (int) m->height) / m->height); + } } -(void)drawRect: (NSRect)rect { - //printf ("%g, %g %g x %g\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); + // printf ("%g, %g %g x %g\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); + CGContextRef cg = [[NSGraphicsContext currentContext] CGContext]; + + for (auto & m : meters) { + m->draw (cg); + } + +#if 0 const NSRect *drawn_rects; long count; int i; [self getRectsBeingDrawn: &drawn_rects count: &count]; - NSGraphicsContext* context = [NSGraphicsContext currentContext]; - CGContextRef cg = [context CGContext]; - - //printf ("%ld rects to draw\n", count); + printf ("%ld rects to draw\n", count); for (i = 0; i < count; i++) { //printf ("\trect %d: %g, %g %g x %g\n", i, drawn_rects[i].origin.x, drawn_rects[i].origin.y, drawn_rects[i].size.width, drawn_rects[i].size.height); - // CGContextSetRGBFillColor (cg, 1.0, 1.0, 1.0, 0.0); + //CGContextSetRGBFillColor (cg, 1.0, 0.0, 0.0, 1.0); CGContextSetRGBStrokeColor (cg, 1.0, 1.0, 1.0, 1.0); - // CGContextFillRect (cg, drawn_rects[i]); + //CGContextFillRect (cg, drawn_rects[i]); CGContextStrokeRect (cg, drawn_rects[i]); } - +#endif } -(void)windowWillClose:(NSNotification *)notification diff --git a/cocoatea/wscript b/cocoatea/wscript index 1a9c72315f..03f43e8fad 100644 --- a/cocoatea/wscript +++ b/cocoatea/wscript @@ -3,8 +3,9 @@ import os import sys cocoatea_sources = [ - 'cocoatea.mm', - 'view.mm' + 'cocoatea.mm', + 'view.mm', + 'meter.mm' ] def options(opt):