more cocoa drawing experiments

This time mostly just don't draw a meter if it wasn't
invalidated/reset.
This commit is contained in:
Paul Davis 2024-11-20 20:15:41 -07:00
parent 2884c502c1
commit e66975bc56
3 changed files with 34 additions and 11 deletions

View File

@ -15,6 +15,7 @@ struct Meter {
double a;
double level;
NSView* view;
bool draw_queued;
Meter (NSView* view, double ax, double ay, double aw, double ah, double ar, double ag, double ab, double aa);

View File

@ -2,6 +2,8 @@
#include "meter.h"
#include "view.h"
int queued_draws = 0;
Meter::Meter (NSView* v, double ax, double ay, double aw, double ah, double ar, double ag, double ab, double aa)
: x (ax)
, y (ay)
@ -13,6 +15,7 @@ Meter::Meter (NSView* v, double ax, double ay, double aw, double ah, double ar,
, a (aa)
, level (0.)
, view (v)
, draw_queued (true)
{
}
@ -22,6 +25,11 @@ Meter::draw (CGContextRef cg)
NSRect bbox = NSMakeRect (x, y, width, height);
if (! [view needsToDrawRect:bbox]) {
draw_queued = false;
return;
}
if (!draw_queued) {
return;
}
@ -34,11 +42,15 @@ Meter::draw (CGContextRef cg)
CGContextSetRGBFillColor (cg, 0, 0., 0., 1.0);
CGContextFillRect (cg, NSMakeRect (x, y, width, fill_height));
draw_queued = false;
}
void
Meter::set_level (double f)
{
level = f;
queued_draws++;
draw_queued = true;
[view setNeedsDisplayInRect:NSMakeRect (x, y, width, height)];
}

View File

@ -9,6 +9,7 @@
#include "view.h"
extern std::vector<Meter*> meters;
extern int queued_draws;
@implementation CTView
@ -48,33 +49,42 @@ extern std::vector<Meter*> meters;
-(void)drawRect: (NSRect)rect
{
if (NSContainsRect (rect, [self bounds])) {
printf ("full redraw, queued = %d\n", queued_draws);
}
#if 0
if (rect.size.width != 10 && rect.size.height != 50) {
printf ("%g, %g %g x %g\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
NSRect me = [self bounds];
printf ("%g, %g %g x %g vs %g, %g %g x %g\n",
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height,
me.origin.x, me.origin.y, me.size.width, me.size.height);
}
#endif
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];
#if 0
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, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor (cg, 1.0, 1.0, 1.0, 1.0);
//CGContextFillRect (cg, drawn_rects[i]);
CGContextStrokeRect (cg, drawn_rects[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);
}
#endif
CGContextRef cg = [[NSGraphicsContext currentContext] CGContext];
for (auto & m : meters) {
m->draw (cg);
}
queued_draws = 0;
}
-(void)windowWillClose:(NSNotification *)notification