shithub: cflood

Download patch

ref: 47de140b5fbf8498bf8da6f43cbfd50227f04d7e
parent: ea2033bc08e7a9b5ab4e8afe0609a61fa4ff098c
author: Sigrid Solveig Haflínudóttir <248148+ftrvxmtrx@users.noreply.github.com>
date: Thu Dec 13 17:13:14 EST 2012

games/cflood: print status and won/failed msg

--- a/games/cflood.c
+++ b/games/cflood.c
@@ -8,6 +8,10 @@
 	Snormal,
 	Slarge,
 
+	Tgame,
+	Twin,
+	Tfail,
+
 	Flood = 1<<7,
 	ColorMask = 0x0f,
 
@@ -34,7 +38,9 @@
 	0
 };
 
+static int sid;
 static int size;
+static int type;
 static int wait4click;
 static int turnsleft;
 static uchar cells[28*28]; // enough for maximal size
@@ -49,13 +55,27 @@
 	Rectangle r = screen->r;
 	draw(screen, r, display->white, nil, ZP);
 
+	char s[64];
+	if(type == Tgame)
+		sprint(s, "%d", turnsleft);
+	else if(type == Twin)
+		sprint(s, "You won using %d turns (of %d)",
+			turns[sid]-turnsleft,
+			turns[sid]);
+	else if(type == Tfail)
+		sprint(s, "You failed");
+	Font *f = display->defaultfont;
+	Point strsize = stringsize(f, s);
+
 	const uchar *cell = &cells[0];
-	int w = Dx(r), h = Dy(r) - ButtonSize - 2;
+	int w = Dx(r);
+	int h = Dy(r) - ButtonSize - 2 - strsize.y;
 	int c = (w < h ? w : h) / size;
 	w = c*size;
+	int left = r.min.x + (Dx(r) - w)/2;
 
 	// cells
-	for(int x = r.min.x; x < r.min.x+size*c; x+=c) {
+	for(int x = left; x < left+size*c; x+=c) {
 		for(int y = r.min.y; y < r.min.y+size*c; y+=c) {
 			Rectangle r = Rect(x, y, x+c, y+c);
 			draw(screen, r, colors[*cell & ColorMask], nil, ZP);
@@ -64,13 +84,18 @@
 	}
 
 	// buttons
-	int startx = r.min.x + (w/2) - NumColors*ButtonSize/2;
-	int y = r.min.y + h;
-	for(int i = 0, x = startx; i < NumColors; i++, x += ButtonSize) {
+	int x = left + (w/2) - NumColors*ButtonSize/2;
+	int y = r.min.y + h + strsize.y;
+	for(int i = 0; i < NumColors; i++, x += ButtonSize) {
 		buttons[i] = Rect(x, y, x+ButtonSize, y+ButtonSize);
 		draw(screen, buttons[i], colors[i], nil, ZP);
 	}
 
+	// caption
+	Point p = {left + w/2 - strsize.x/2, y - strsize.y};
+	Point sp = {0, 0};
+	string(screen, p, display->black, sp, f, s);
+
 	flushimage(display, 1);
 }
 
@@ -119,20 +144,23 @@
 	if(!turnsleft)
 		return;
 
+	turnsleft--;
 	int n = reflood(color);
-	redraw(screen);
 
 	if(n == size*size) {
-		// win
+		type = Twin;
 		wait4click = 1;
-	} else if(--turnsleft == 0) {
-		// fail
+	} else if(!turnsleft) {
+		type = Tfail;
 		wait4click = 1;
 	}
+
+	redraw(screen);
 }
 
 static void
 newgame(int sid) {
+	type = Tgame;
 	size = sizes[sid];
 	turnsleft = turns[sid];
 
@@ -151,7 +179,7 @@
 eresized(int new)
 {
 	if(new && getwindow(display, Refnone) < 0)
-		fprint(2, "can't reattach to window");
+		sysfatal("can't reattach to window: %r");
 	redraw(screen);
 }
 
@@ -162,9 +190,8 @@
 		sysfatal("initdraw failed");
 
 	Rectangle r = Rect(0, 0, 1, 1);
-	for(int i = 0; i < NumColors; i++) {
+	for(int i = 0; i < NumColors; i++)
 		colors[i] = allocimage(display, r, CMAP8, 1, srccolors[i]);
-	}
 
 	einit(Emouse);
 	menu.item = mstr;
@@ -171,7 +198,7 @@
 	menu.lasthit = 0;
 	srand(time(0));
 
-	int sid = Snormal;
+	sid = Snormal;
 	newgame(sid);
 
 	for(int mold = 0;;) {