extend cocoa drawing test

Test app now draws 500 "meters" at random levels @ 50Hz.
This commit is contained in:
Paul Davis 2024-11-16 18:37:14 -07:00
parent d588b86e54
commit bb19a59315
3 changed files with 55 additions and 28 deletions

View file

@ -1,17 +1,38 @@
/* -*- c++ -*- */
#include <vector>
#include <AppKit/AppKit.h>
#include "meter.h"
#include "view.h"
std::vector<Meter*> 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
*/

View file

@ -1,10 +1,15 @@
/* -*- c++ -*- */
#include <cstdlib>
#include <vector>
#include <stdio.h>
#include "meter.h"
#include "view.h"
extern std::vector<Meter*> 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

View file

@ -3,8 +3,9 @@ import os
import sys
cocoatea_sources = [
'cocoatea.mm',
'view.mm'
'cocoatea.mm',
'view.mm',
'meter.mm'
]
def options(opt):