ref: 70bad7738f3b48f89ed1a624a38109d987bc048e
parent: 0428625c683eb42701271bafabb318a3728a503f
author: qwx <qwx@sciops.net>
date: Mon Oct 4 18:47:57 EDT 2021
add some ugly graphical tiles, no drawing yet
--- a/city.c
+++ b/city.c
@@ -63,6 +63,7 @@
sysfatal("chancreate: %r");
if(proccreate(timeproc, nil, 8192) < 0)
sysfatal("init: %r");
+ readfs();
startsim();
mo.xy = ZP;
enum{
binary files /dev/null b/city/crop.bit differ
binary files /dev/null b/city/goldvein.bit differ
binary files /dev/null b/city/ironvein.bit differ
binary files /dev/null b/city/mountain.bit differ
binary files /dev/null b/city/plain.bit differ
binary files /dev/null b/city/pond.bit differ
binary files /dev/null b/city/townhall.bit differ
binary files /dev/null b/city/woods.bit differ
--- a/dat.h
+++ b/dat.h
@@ -1,3 +1,4 @@
+typedef struct Pic Pic;
typedef struct Resource Resource;
typedef struct Good Good;
typedef struct Building Building;
@@ -5,6 +6,15 @@
typedef struct Tile Tile;
enum{
+ Tilesz = 32,
+};
+struct Pic{
+ int picw;
+ int pich;
+ u32int *pic;
+};
+
+enum{
Gfish,
Gcattle,
Gwheat,
@@ -66,6 +76,7 @@
int prodcost[Gtot];
int terrain;
int upkeep[Rtot];
+ Pic;
};
extern Building buildings[Btot];
@@ -85,6 +96,7 @@
int good;
int initialstock;
double yield;
+ Pic;
};
extern Terrain terrains[Ttot];
--- a/fns.h
+++ b/fns.h
@@ -3,6 +3,7 @@
void initmap(void);
int mhdist(int, int, int, int);
void startsim(void);
+void readfs(void);
void initdrw(void);
void resetdraw(void);
void updatedraw(void);
--- /dev/null
+++ b/fs.c
@@ -1,0 +1,68 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include "dat.h"
+#include "fns.h"
+
+char *prefix = "/sys/games/lib/city";
+
+static void
+loadpic(char *name, int dim, Pic *pic)
+{
+ int fd, n, w;
+ char *f;
+ uchar *buf, *s;
+ u32int v, *p;
+ Image *i;
+
+ if((f = smprint("%s.bit", name)) == nil)
+ sysfatal("loadpic:smprint: %r");
+ if((fd = open(f, OREAD)) < 0)
+ sysfatal("loadpic:open: %r");
+ if((i = readimage(display, fd, 0)) == nil)
+ sysfatal("loadpic:readimage: %r");
+ close(fd);
+ if(i->chan != RGB24 || Dx(i->r) != dim || Dy(i->r) != dim)
+ sysfatal("loadpic %s: inappropriate image format", f);
+ free(f);
+ n = dim * dim;
+ pic->picw = dim;
+ pic->pich = dim;
+ pic->pic = emalloc(n * sizeof *pic->pic);
+ n *= i->depth / 8;
+ buf = emalloc(n);
+ unloadimage(i, i->r, buf, n);
+ w = i->depth / 8;
+ n = dim * dim;
+ s = buf;
+ p = pic->pic;
+ while(n-- > 0){
+ v = 0xff << 24 | s[2] << 16 | s[1] << 8 | s[0];
+ *p++ = v;
+ s += w;
+ }
+ freeimage(i);
+ free(buf);
+}
+
+static void
+readimg(void)
+{
+ Terrain *t;
+ Building *b;
+
+ for(t=terrains; t<terrains+nelem(terrains); t++)
+ loadpic(t->name, Tilesz, &t->Pic);
+ b = buildings + Btownhall;
+ loadpic(b->name, Tilesz, &b->Pic);
+}
+
+void
+readfs(void)
+{
+ rfork(RFNAMEG);
+ if(bind(".", prefix, MBEFORE|MCREATE) == -1 || chdir(prefix) < 0)
+ fprint(2, "initfs: %r\n");
+ readimg();
+}
--- a/mkfile
+++ b/mkfile
@@ -5,8 +5,13 @@
city.$O\
defs.$O\
drw.$O\
+ fs.$O\
map.$O\
sim.$O\
HFILES=dat.h fns.h
</sys/src/cmd/mkone
+
+sysinstall:V:
+ mkdir -p /sys/games/lib/$TARG
+ dircp $TARG /sys/games/lib/$TARG