ref: a0bc995332afdd4b693347cfae94694e6c601aed
parent: d370912d4a7fc88b8262269b83aecee30dbf4990
author: henesy <henesy.dev@gmail.com>
date: Wed Feb 23 22:59:29 EST 2022
add screen code from nspool
--- a/main.c
+++ b/main.c
@@ -45,17 +45,50 @@
// Push a message into the log
void
-logcmd(char* str)
+handlecmd(char* str)
{
- int i;
- for(i = Ncmd-1; i > 0; i--)
- strcpy(clog[i], clog[i-1]);
- strncpy(clog[0], str, Cmdwidth);
+ int maxtoks = 10;
+ char *cmd;
+ char *toks[maxtoks];
+ int ntoks;
+ ntoks = tokenize(str, toks, maxtoks);
+
+ if(cmdcmp(cmd, "clear") == 0){
+
+ }else if(cmd, "newwin") == 0){
+
+ }else if(cmd, "delwin") == 0){
+
+ }else if(cmd, "endwin") == 0){
+
+ }else if(cmd, "mvprintw") == 0){
+
+ }else if(cmd, "mvaddch") == 0){
+
+ }else if(cmd, "getch") == 0){
+
+ }else if(cmd, "initscr") == 0){
+
+ }else if(cmd, "raw") == 0){
+
+ }else if(cmd, "noecho") == 0){
+
+ }else if(cmd, "cursset") == 0){
+
+ }else if(cmd, "setescdelay") == 0){
+
+ }else if(cmd, "refresh") == 0){
+
+ }else if(cmd, "wrefresh") == 0){
+
+ }else if(cmd, "keypad") == 0){
+
+ }
}
// Print the log
char*
-log2str(void)
+screen2str(void)
{
char str[Ncmd * Cmdwidth];
memset(str, '\0', Ncmd * Cmdwidth);
@@ -99,7 +132,7 @@
files[0] = &ctl;
// Setup log file
- File9 log = (File9) { (Ref){ 0 }, 1, "log" };
+ File9 log = (File9) { (Ref){ 0 }, 1, "screen" };
files[1] = &log;
threadpostmountsrv(&srvfs, srv, mnt, MREPL|MCREATE);
@@ -165,7 +198,7 @@
// log file
// TODO -- attach stdout to write to the read stream
// strcpy(readmsg, "log shows prior commands.\n");
- strcpy(readmsg, log2str());
+ strcpy(readmsg, screen2str());
break;
default:
strcpy(readmsg, "Nothing special in this read.\n");
@@ -204,7 +237,7 @@
switch(q.path){
case 0:
// ctl file
- logcmd(str);
+ handlecmd(str);
break;
default:
respond(r, "only ctl may be written to");
--- /dev/null
+++ b/screen.c
@@ -1,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
+initscreen(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();
+ }
+}
+
+
+