ref: ebc87ef165d107747d2f7267e8ed12e55f782bff
parent: 113de1e06a4945445ce898388092b174fe40f2ba
author: henesy <henesy.dev@gmail.com>
date: Fri Mar 11 03:21:44 EST 2022
screen resized and draws in 81x25
--- a/screen.c
+++ b/screen.c
@@ -5,40 +5,37 @@
#include <event.h>
#include "fs.h"
-void moveball(void);
-void initball(void);
-Image *ball;
+Point p; // unused?
+Font *ourfont; // VGA
+Image *brush; // For drawing the text
+Rune *s = L"☺☹σπß"; // for testing
+int bwidth = 4; // border width of window
+Rune **sbuf; // screen buffer
+usize sheight = 25, swidth = 80; // screen height, width
+Lock slock; // screen buffer lock
-Point p;
-Font *ourfont;
-Image *brush;
-Rune *s = L"☺☹σπß";
-
/* 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 */
-
+// Render the active buffer on a timer
void
-moveball()
+renderbuf(void)
{
-
- Point out;
- out = runestring(
- screen, screen->r.min, display->black, ZP,
- ourfont, s
- );
+ int i;
+ lock(&slock);
+ Point out, p;
+ Point at = screen->r.min;
+ for(i = 0; i < sheight; i++){
+ p = runestringsize(ourfont, sbuf[i]);
+ out = runestring(
+ screen, at, display->black, ZP,
+ ourfont, sbuf[i]
+ );
+ at.y += p.y;
+ }
+ unlock(&slock);
flushimage(display, 1);
}
@@ -56,51 +53,30 @@
p = Pt(Dx(screen->r), Dy(screen->r));
/* Draw the background DWhite */
- draw(screen, insetrect(screen->r, borderWidth),
+ draw(screen, insetrect(screen->r, bwidth),
allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DWhite),
nil, ZP);
}
-
-// Center font rendering at a point?
-// from faces(1)
-/*
+// Initialize the screen buffer
void
-center(Font *f, Point p, char *s, Image *color)
+initbuf(void)
{
- int i, n, dx;
- Rune rbuf[32];
- char sbuf[32*UTFmax+1];
-
- dx = stringwidth(f, s);
- if(dx > Facesize){
- n = torune(rbuf, s, nelem(rbuf));
- for(i=0; i<n; i++){
- dx = runestringnwidth(f, rbuf, i+1);
- if(dx > Facesize)
- break;
- }
- sprint(sbuf, "%.*S", i, rbuf);
- s = sbuf;
- dx = stringwidth(f, s);
- }
- p.x += (Facesize-dx)/2;
- string(screen, p, color, ZP, f, s);
-}
-*/
-
-/* 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()
-{
Point out;
+ int y, x;
ourfont = openfont(display, "/lib/font/bit/vga/unicode.font");
p = runestringsize(ourfont, s);
+ lock(&slock);
+ sbuf = calloc(sheight, sizeof (Rune*));
+ for(y = 0; y < sheight; y++){
+ sbuf[y] = calloc(swidth+1, sizeof (Rune));
+ for(x = 0; x < swidth; x++)
+ sbuf[y][x] = L'☺';
+ }
+ unlock(&slock);
+
out = runestring(
screen, screen->r.min, display->black, ZP,
ourfont, s
@@ -129,10 +105,26 @@
/* Simulate a resize event to draw the background
* and acquire the screen dimensions */
- eresized(0);
- initball();
+ initbuf();
+ // Set the screen size (after initbuf)
+ //echo resize -dx 100 -dy 100 > /dev/wctl
+ // TODO - broken
+ int fd;
+ char *str;
+ fd = open("/dev/wctl", OWRITE);
+ usize width = (ourfont->width * swidth) + 2*bwidth +1;
+ usize height = (ourfont->height * sheight) + 2*bwidth +1;
+ str = smprint("resize -dx %ld -dy %ld\n", width, height);
+ int out = write(fd, str, strlen(str));
+ if(out < 1)
+ sysfatal("err: /dev/wctl write failed → %r");
+ close(fd);
+ free(str);
+
+ eresized(0);
+
/* Main event loop */
for(;;){
@@ -147,7 +139,7 @@
(emenuhit(3, &ev.mouse, &menu) == 0)) threadexitsall(nil);
else
if(e == timer)
- moveball();
+ renderbuf();
}
}