summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-09-25 17:16:54 -0400
committerSam Anthony <sam@samanthony.xyz>2024-09-25 17:16:54 -0400
commit44dd6ad61dfed5d89f8abf7e8f41797501f071cc (patch)
tree299f0b12e0f19c9b9f117232a1b0f1266b879632
parent50f5164c529d57aeaf5c435b33683afabfc25c40 (diff)
downloadballs-44dd6ad61dfed5d89f8abf7e8f41797501f071cc.zip
refactor graphics functions
-rw-r--r--balls.c34
-rw-r--r--balls.h4
-rw-r--r--graphics.c35
-rw-r--r--mkfile2
4 files changed, 40 insertions, 35 deletions
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);
@@ -132,13 +129,6 @@ init(char *label, Mousectl **mctl, Keyboardctl **kctl) {
}
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;
Point *ps;
@@ -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