From e9a9a611dcb6d61b7221af8634fc5b2e5624f3b2 Mon Sep 17 00:00:00 2001 From: rodri Date: Sun, 17 May 2020 12:43:24 +0000 Subject: isometric: fix world rframe basis new tiles were also added and promoted to 32x16 --- asset/tile/building.pic | Bin 0 -> 4156 bytes asset/tile/empty.pic | Bin 316 -> 1084 bytes asset/tile/filled.pic | Bin 316 -> 1084 bytes asset/tile/focus.pic | Bin 0 -> 1084 bytes isometric.c | 93 ++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 asset/tile/building.pic create mode 100644 asset/tile/focus.pic diff --git a/asset/tile/building.pic b/asset/tile/building.pic new file mode 100644 index 0000000..1a6139a Binary files /dev/null and b/asset/tile/building.pic differ diff --git a/asset/tile/empty.pic b/asset/tile/empty.pic index 317ba1a..9955ec7 100644 Binary files a/asset/tile/empty.pic and b/asset/tile/empty.pic differ diff --git a/asset/tile/filled.pic b/asset/tile/filled.pic index dc32e49..df096c2 100644 Binary files a/asset/tile/filled.pic and b/asset/tile/filled.pic differ diff --git a/asset/tile/focus.pic b/asset/tile/focus.pic new file mode 100644 index 0000000..df5436a Binary files /dev/null and b/asset/tile/focus.pic differ diff --git a/isometric.c b/isometric.c index 3595035..97bf20d 100644 --- a/isometric.c +++ b/isometric.c @@ -9,12 +9,14 @@ enum { Cbg, Cfg, Ctxtbg, + Cgrid0, + Cgrid1, NCOLOR }; enum { - TW = 16, - TH = 8 + TW = 32, + TH = 16 }; typedef struct Tile Tile; @@ -28,8 +30,11 @@ struct Tile 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 @@ char *map[] = { "eefee" }; Point mpos; +Point2 spacegrid[10][10]; +int showgrid; Point toscreen(Point2 p) @@ -59,6 +66,8 @@ initpalette(void) 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,9 +81,21 @@ inittiles(void) 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) { @@ -92,6 +113,17 @@ drawstats(void) stringbg(screen, addpt(screen->r.min, Pt(20,20+font->height*2)), pal[Cfg], ZP, font, s, pal[Ctxtbg], ZP); } +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) { @@ -102,15 +134,53 @@ redraw(void) 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) { @@ -133,10 +203,10 @@ main(int argc, char *argv[]) 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 @@ main(int argc, char *argv[]) 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 @@ eresized(int) { 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(); } -- cgit v1.2.3