shithub: etoys

Download patch

ref: e9a9a611dcb6d61b7221af8634fc5b2e5624f3b2
parent: 12406c361e2a65637beec739f9983bb088e26da7
author: rodri <[email protected]>
date: Sun May 17 08:43:24 EDT 2020

isometric: fix world rframe basis

new tiles were also added and promoted to 32x16

binary files /dev/null b/asset/tile/building.pic differ
binary files a/asset/tile/empty.pic b/asset/tile/empty.pic differ
binary files a/asset/tile/filled.pic b/asset/tile/filled.pic differ
binary files /dev/null b/asset/tile/focus.pic differ
--- a/isometric.c
+++ b/isometric.c
@@ -9,12 +9,14 @@
 	Cbg,
 	Cfg,
 	Ctxtbg,
+	Cgrid0,
+	Cgrid1,
 	NCOLOR
 };
 
 enum {
-	TW = 16,
-	TH = 8
+	TW = 32,
+	TH = 16
 };
 
 typedef struct Tile Tile;
@@ -28,8 +30,11 @@
 Image *pal[NCOLOR];
 Tile tiles[] = {
 	{ .name = "empty", .id = 'e' },
-	{ .name = "filled", .id = 'f' }
+	{ .name = "filled", .id = 'f' },
+	{ .name = "building", .id = 'b' },
+	{ .name = "focus", .id = 'F' }
 };
+Tile *tfocus;
 RFrame worldrf;
 char *map[] = {
 	"eeeee",
@@ -39,6 +44,8 @@
 	"eefee"
 };
 Point mpos;
+Point2 spacegrid[10][10];
+int showgrid;
 
 Point
 toscreen(Point2 p)
@@ -59,6 +66,8 @@
 	pal[Cbg] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DBlack);
 	pal[Cfg] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkblue);
 	pal[Ctxtbg] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPaleyellow);
+	pal[Cgrid0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DRed);
+	pal[Cgrid1] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DBlue);
 }
 
 void
@@ -72,10 +81,22 @@
 		fd = open(path, OREAD);
 		tiles[i].img = readimage(display, fd, 0);
 		close(fd);
+		if(tiles[i].id == 'F')
+			tfocus = &tiles[i];
 	}
 }
 
 void
+initgrid(void)
+{
+	int i, j;
+
+	for(i = 0; i < nelem(spacegrid); i++)
+		for(j = 0; j < nelem(spacegrid[i]); j++)
+			spacegrid[i][j] = Pt2(j*TW, i*TH, 1);
+}
+
+void
 drawstats(void)
 {
 	Point2 mp, p;
@@ -93,6 +114,17 @@
 }
 
 void
+drawgrid(void)
+{
+	int i, j;
+
+	for(i = 0; i < nelem(spacegrid); i++)
+		line(screen, toscreen(spacegrid[i][0]), toscreen(spacegrid[i][nelem(spacegrid[i])-1]), Endsquare, Endsquare, 0, pal[Cgrid0], ZP);
+	for(j = 0; j < nelem(spacegrid[0]); j++)
+		line(screen, toscreen(spacegrid[0][j]), toscreen(spacegrid[nelem(spacegrid)-1][j]), Endsquare, Endsquare, 0, pal[Cgrid1], ZP);
+}
+
+void
 redraw(void)
 {
 	Point2 dp;
@@ -102,16 +134,54 @@
 	draw(screen, screen->r, pal[Cbg], nil, ZP);
 	for(i = 0; i < nelem(map); i++)
 		for(row = map[i]; *row; row++){
-			dp = Pt2((row-map[i]-i)*TW/2,(i+row-map[i])*TH/2,1);
+			dp = Pt2((row-map[i])*TW,(i)*TH,1);
 			for(j = 0; j < nelem(tiles); j++)
 				if(tiles[j].id == *row)
-					draw(screen, Rpt(toscreen(dp),addpt(toscreen(dp), Pt(TW,TH))), tiles[j].img, nil, ZP);
+					draw(screen, Rpt(subpt(toscreen(dp), Pt(TW/2,0)),addpt(toscreen(dp), Pt(TW,TH))), tiles[j].img, nil, ZP);
 		}
+	dp = fromscreen(mpos);
+	dp.x = ((int)dp.x/TW)*TW;
+	dp.y = ((int)dp.y/TH)*TH;
+	draw(screen, Rpt(subpt(toscreen(dp), Pt(TW/2,0)),addpt(toscreen(dp), Pt(TW,TH))), tfocus->img, nil, ZP);
+	if(showgrid)
+		drawgrid();
 	drawstats();
 	flushimage(display, 1);
 }
 
 void
+mmb(Mouse *m)
+{
+	enum {
+		SHOWGRID,
+		CHGBASIS
+	};
+	static char *items[] = {
+	 [SHOWGRID]	"toggle grid",
+	 [CHGBASIS]	"change basis",
+		nil
+	};
+	static Menu menu = { .item = items };
+	char buf[256], *p;
+
+	switch(emenuhit(2, m, &menu)){
+	case SHOWGRID:
+		showgrid ^= 1;
+		break;
+	case CHGBASIS:
+		snprint(buf, sizeof buf, "%g %g", worldrf.bx.x, worldrf.bx.y);
+		eenter("bx", buf, sizeof buf, m);
+		worldrf.bx.x = strtod(buf, &p);
+		worldrf.bx.y = strtod(p, nil);
+		snprint(buf, sizeof buf, "%g %g", worldrf.by.x, worldrf.by.y);
+		eenter("by", buf, sizeof buf, m);
+		worldrf.by.x = strtod(buf, &p);
+		worldrf.by.y = strtod(p, nil);
+		break;
+	}
+}
+
+void
 usage(void)
 {
 	fprint(2, "usage: %s\n", argv0);
@@ -133,10 +203,10 @@
 		sysfatal("initdraw: %r");
 	initpalette();
 	inittiles();
-	worldrf.p = Pt2(screen->r.min.x,screen->r.min.y,1);
-	worldrf.p = addpt2(worldrf.p, Vec2(Dx(screen->r)/2,Dy(screen->r)/3));
-	worldrf.bx = Vec2(1,0);
-	worldrf.by = Vec2(0,1);
+	initgrid();
+	worldrf.p = Pt2(screen->r.min.x+Dx(screen->r)/2,screen->r.min.y+Dy(screen->r)/3,1);
+	worldrf.bx = Vec2(1,2);
+	worldrf.by = Vec2(-0.5,1);
 	einit(Emouse|Ekeyboard);
 	redraw();
 	for(;;)
@@ -145,6 +215,8 @@
 			mpos = e.mouse.xy;
 			if((e.mouse.buttons&1) != 0)
 				worldrf.p = Pt2(e.mouse.xy.x,e.mouse.xy.y,1);
+			if((e.mouse.buttons&2) != 0)
+				mmb(&e.mouse);
 			redraw();
 			break;
 		case Ekeyboard:
@@ -162,7 +234,6 @@
 {
 	if(getwindow(display, Refnone) < 0)
 		sysfatal("resize failed");
-	worldrf.p = Pt2(screen->r.min.x,screen->r.min.y,1);
-	worldrf.p = addpt2(worldrf.p, Vec2(Dx(screen->r)/2,Dy(screen->r)/3));
+	worldrf.p = Pt2(screen->r.min.x+Dx(screen->r)/2,screen->r.min.y+Dy(screen->r)/3,1);
 	redraw();
 }