From 44dd6ad61dfed5d89f8abf7e8f41797501f071cc Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 25 Sep 2024 17:16:54 -0400 Subject: refactor graphics functions --- balls.c | 34 ---------------------------------- balls.h | 4 ++++ graphics.c | 35 +++++++++++++++++++++++++++++++++++ mkfile | 2 +- 4 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 graphics.c diff --git a/balls.c b/balls.c index d742d67..1a10e42 100644 --- a/balls.c +++ b/balls.c @@ -48,7 +48,6 @@ static Rectangle bounds = {{PAD, PAD}, {PAD+WIDTH, PAD+HEIGHT}}; int init(char *label, Mousectl **mctl, Keyboardctl **kctl); void spawnball(void); -void drawbg(Image *walls, Image *bg); void spawnballs(int n); Channel **allocchans(int nchans, int elsize, int nel); void mcopycolskip(Channel *vec[], Channel **matrix[], int n, int col, int skip); @@ -57,8 +56,6 @@ void nooverlapcircles(Point centers[], int n); Point randptinrect(Rectangle r); int randint(int lo, int hi); void ball(void *arg); -Image *alloccircle(int fg, int bg); -void drawcircle(Image *m, Point pos); void broadcast(Point p, Channel *cs[], int n); void frametick(void *arg); @@ -131,13 +128,6 @@ init(char *label, Mousectl **mctl, Keyboardctl **kctl) { return 0; } -void -drawbg(Image *walls, Image *bg) { - draw(screen, screen->r, walls, nil, ZP); - draw(screen, screen->r, bg, nil, ZP); - flushimage(display, Refnone); -} - void spawnballs(int n) { Channel **ticks; @@ -314,30 +304,6 @@ ball(void *arg) { } } -Image * -alloccircle(int fg, int bg) { - Image *m, *fill -; - m = allocimage(display, Rect(0, 0, 2*RADIUS, 2*RADIUS), RGBA32, 0, bg); - if (m == nil) - return nil; - - fill = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, fg); - if (fill == nil) { - free(m); - return nil; - } - - fillellipse(m, Pt(RADIUS, RADIUS), RADIUS, RADIUS, fill, ZP); - freeimage(fill); - return m; -} - -void -drawcircle(Image *m, Point pos) { - draw(screen, rectaddpt(bounds, subpt(pos, Pt(RADIUS, RADIUS))), m, nil, ZP); -} - void broadcast(Point p, Channel *cs[], int n) { while (n-- > 0) diff --git a/balls.h b/balls.h index 69adde9..0e98506 100644 --- a/balls.h +++ b/balls.h @@ -32,6 +32,10 @@ Point ptaddv(Point p, Vec v); Vec V(double x, double y); Vec Vpt(Point p, Point q); +void drawbg(Image *walls, Image *bg); +Image *alloccircle(int fg, int bg); +void drawcircle(Image *m, Point pos); + int iscollision(Point p, Point q); void collideball(Ball *b1, const Ball *b2); void collidewall(Ball *b, Rectangle wall); diff --git a/graphics.c b/graphics.c new file mode 100644 index 0000000..c03dc8e --- /dev/null +++ b/graphics.c @@ -0,0 +1,35 @@ +#include "balls.h" + +void +drawbg(Image *walls, Image *bg) { + draw(screen, screen->r, walls, nil, ZP); + draw(screen, screen->r, bg, nil, ZP); + flushimage(display, Refnone); +} + +Image * +alloccircle(int fg, int bg) { + Image *m, *fill; + + m = allocimage(display, Rect(0, 0, 2*RADIUS, 2*RADIUS), RGBA32, 0, bg); + if (m == nil) + return nil; + + fill = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, fg); + if (fill == nil) { + free(m); + return nil; + } + + fillellipse(m, Pt(RADIUS, RADIUS), RADIUS, RADIUS, fill, ZP); + freeimage(fill); + return m; +} + +void +drawcircle(Image *m, Point pos) { + Rectangle r; + + r = Rpt(subpt(pos, Pt(RADIUS, RADIUS)), addpt(pos, Pt(RADIUS, RADIUS))); + draw(screen, r, m, nil, ZP); +} diff --git a/mkfile b/mkfile index e43c880..ce0c5d8 100644 --- a/mkfile +++ b/mkfile @@ -3,7 +3,7 @@ LD=9l O=o LDFLAGS= -OBJ = balls.$O vec.$O collision.$O +OBJ = balls.$O vec.$O collision.$O graphics.$O balls: $OBJ $LD -o balls $OBJ $LDFLAGS -- cgit v1.2.3