about summary refs log tree commit diff stats
path: root/p9c
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2023-05-25 22:07:31 -0400
committerelioat <elioat@tilde.institute>2023-05-25 22:07:31 -0400
commitee89384b2bf9518ee644e9212d67b08f5509839d (patch)
tree12949c64a81f2277879697e98e81f26715128f7c /p9c
parent9d9ec8ce5fef38f991f3a5a708d985b0f5225baa (diff)
downloadtour-ee89384b2bf9518ee644e9212d67b08f5509839d.tar.gz
"*"
Diffstat (limited to 'p9c')
-rwxr-xr-xp9c/hello-plan9/LICENSE22
-rwxr-xr-xp9c/hello-plan9/README.md15
-rwxr-xr-xp9c/hello-plan9/ball.c136
-rwxr-xr-xp9c/hello-plan9/ball.pngbin0 -> 1071 bytes
4 files changed, 173 insertions, 0 deletions
diff --git a/p9c/hello-plan9/LICENSE b/p9c/hello-plan9/LICENSE
new file mode 100755
index 0000000..fa90772
--- /dev/null
+++ b/p9c/hello-plan9/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 nspool
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/p9c/hello-plan9/README.md b/p9c/hello-plan9/README.md
new file mode 100755
index 0000000..8d4544b
--- /dev/null
+++ b/p9c/hello-plan9/README.md
@@ -0,0 +1,15 @@
+# hello-plan9
+
+FROM: <https://github.com/nspool/hello-plan9>
+
+A very simple GUI app for Plan 9 from Bell Labs. [This blog post](https://nspool.github.io/2013/02/bouncing-ball/) explains the code in more detail.
+
+Just a bouncing ball with menu options. Using libdraw to animate the ball and libevent for the menu.
+
+![Screenshot](ball.png?raw=true "Screenshot showing window, ball and Exit menu item")
+
+## Build Instructions
+
+To compile and link from a i386 Plan 9 terminal:
+
+`8c -FTVw ball.c && 8l -o ball ball.8`
diff --git a/p9c/hello-plan9/ball.c b/p9c/hello-plan9/ball.c
new file mode 100755
index 0000000..281beb0
--- /dev/null
+++ b/p9c/hello-plan9/ball.c
@@ -0,0 +1,136 @@
+/*
+ * Ball - a first attempt at Plan 9 graphics programming
+ * Author:	nspool
+ * Date:	2013-02-19
+ * Licence: MIT
+ * /
+
+/* Build with '8c -FTVw ball.c && 8l -o ball ball.8' */
+
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+
+void moveball(void);
+void initball(void);
+
+Image *ball;
+
+Point p;
+
+/* Menus */
+char *buttons[] = {"exit", 0};
+Menu menu = { buttons };
+
+/* Radius of ball */
+int r = 20;
+int borderWidth = 4;
+
+
+
+/* Change direction of ball if collision
+ * else move the ball and draw to screen
+ * The new ball image draws over the previous image
+ * so there is no need to redraw the whole screen */
+
+void
+moveball()
+{
+	static Point bp={6, 6}; /* Ball Position */
+	static double Δi=4, Δj=4;
+
+	/* Collision detection */
+	if(bp.x > p.x - (r*3) || bp.x < -r) Δi = Δi*-1;
+	if(bp.y > p.y - (r*3) || bp.y < -r) Δj = Δj*-1;
+	
+	/* Increment ball position */
+	bp.x = bp.x + Δi;
+	bp.y = bp.y + Δj;
+
+	draw(screen, rectaddpt(screen->r, bp), ball, nil, ZP);
+}
+
+
+
+/* Graphics library requires this function */
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone) < 0)
+		sysfatal("can't reattach to window");
+
+	/* Store new screen coordinates for collision detection */
+	p = Pt(Dx(screen->r), Dy(screen->r));
+
+	/* Draw the background DWhite */
+	draw(screen, insetrect(screen->r, borderWidth), 
+			allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DWhite), 
+			nil, ZP);
+}
+
+
+/* Draw red ball inside a square of white background
+ * When ball is drawn at new position, background will
+ * blot out the previous image */
+
+void
+initball()
+{
+	Image *brush;
+	brush=allocimage(display, Rect(0,0,1,1), CMAP8, 1, DRed);
+	ball=allocimage(display, (Rectangle){(Point){0,0},(Point){r*4,r*4}},
+			screen->chan, 0, DWhite);
+	fillellipse(ball, (Point){r*2,r*2}, r, r, brush, ZP);
+}
+
+
+void
+main(int argc, char *argv[])
+{
+	USED(argc, argv);
+
+	Event ev;
+	int e, timer;
+
+	/* Initiate graphics and mouse */
+	if(initdraw(nil, nil, "bouncing ball demo") < 0)
+		sysfatal("initdraw failed: %r");
+
+	einit(Emouse);
+
+	/* Start our timer
+	 * move the ball every 5 milliseconds
+	 * unless there is an Emouse event */
+
+	timer = etimer(0, 5);
+
+	/* Simulate a resize event to draw the background
+	 * and acquire the screen dimensions */
+
+	eresized(0);
+
+	initball();
+
+	/* Main event loop */
+
+	for(;;)
+	{
+		e = event(&ev);
+
+		/* If there is a mouse event, the rightmost button
+		 * pressed and the first menu option selected
+		 * then exit.. */
+
+		if( (e == Emouse) &&
+			(ev.mouse.buttons & 4) && 
+			(emenuhit(3, &ev.mouse, &menu) == 0)) exits(nil);
+		else 
+			if(e == timer)
+				moveball();
+	}
+}
+
+
+
diff --git a/p9c/hello-plan9/ball.png b/p9c/hello-plan9/ball.png
new file mode 100755
index 0000000..497fe1e
--- /dev/null
+++ b/p9c/hello-plan9/ball.png
Binary files differ