History

center camera works

mischief committed 11 years ago

Commit 846efaa1 parent 6ddc83c1 file · +57 −28Browse files

Unified · Split

hack9.c+57−28
@@ -13,8 +13,11 @@ Keyboardctl *kc; · 12 unmodified lines
1313 Tileset *tiles;
1414 Level *level;
1515
16+/* in tiles */
17+Point campos;
18+
19+/* of player */
1620 Point pos;
17−Camera cam;
1821
1922 static int
2023 min(int a, int b)
@@ -24,43 +27,64 @@ min(int a, int b) · 3 unmodified lines
2427 return b;
2528 }
2629
30+/* draw the whole level on the screen using tileset ts.
31+ * r.min is the upper left visible tile, and r.max is
32+ * the bottom right visible tile.
33+ * */
2734 static void
28−drawlevel(Level *l, Tileset *ts, int width, int height)
35+drawlevel(Level *l, Tileset *ts, Rectangle r)
2936 {
3037 int x, y, what;
31− Point xy, tpos, cen;
38+ Point xy, tpos;
3239 Tile *t;
3340
34− for(x = 0; x < width; x++){
35− for(y = 0; y < height; y++){
36− xy = (Point){x, y};
41+ draw(screen, screen->r, display->black, nil, ZP);
3742
38− /* very wrong. */
43+ for(x = r.min.x; x < r.max.x; x++){
44+ for(y = r.min.y; y < r.max.y; y++){
45+ /* world cell */
46+ xy = (Point){x, y};
47+ /* screen space */
3948 tpos = xy;
49+ tpos = addpt(tpos, campos);
4050 tpos.x *= ts->width;
4151 tpos.y *= ts->height;
42− tpos = ctrans(&cam, tpos);
52+ tpos = addpt(tpos, screen->r.min);
4353 t = tileat(l, xy);
44− what = t->terrain;
45− if(t->unit)
46− what = t->unit;
47− else if(t->feat)
48− what = t->feat;
49− cen = addpt(screen->r.min, tpos);
50− //if(what == TWIZARD)
51− // fprint(2, "draw %d at %P %P ", what, tpos, cen);
52− drawtile(ts, screen, cen, what);
54+ if(t){
55+ what = t->terrain;
56+ if(t->unit)
57+ what = t->unit;
58+ else if(t->feat)
59+ what = t->feat;
60+ drawtile(ts, screen, tpos, what);
61+ }
5362 }
5463 }
5564 }
5665
66+Rectangle
67+view(Point pos, Level *l, int scrwidth, int scrheight)
68+{
69+ Point p;
70+ Rectangle r;
71+
72+ /* compute vieweable area of the map */
73+ p = Pt((scrwidth/2)+2, (scrheight/2)+2);
74+ r = Rpt(subpt(pos, p), addpt(pos, p));
75+ rectclip(&r, Rect(0, 0, l->width, l->height));
76+ return r;
77+}
78+
5779 void
5880 threadmain(int argc, char *argv[])
5981 {
60− Rune r;
82+ Rune c;
6183
6284 /* of viewport, in size of tiles */
6385 int width, height;
86+ Point pt;
87+ Rectangle r;
6488
6589 ARGBEGIN{
6690 }ARGEND
@@ -87,16 +111,18 @@ threadmain(int argc, char *argv[]) · 20 unmodified lines
87111
88112 width = Dx(screen->r) / tiles->width;
89113 height = Dy(screen->r) / tiles->height;
90− cam = (Camera){ZP, Rect(0, 0, Dy(screen->r), Dx(screen->r)), Rect(0, 0, Dy(screen->r), Dx(screen->r))};
91− ccenter(&cam, Pt(pos.x * tiles->width, pos.y * tiles->height));
92− drawlevel(level, tiles, min(width+1, level->width), min(height+1, level->height));
114+
115+ r = view(pos, level, width, height);
116+ campos = subpt(Pt(width/2, height/2), pos);
117+
118+ drawlevel(level, tiles, r);
93119 flushimage(display, 1);
94120
95121 enum { AMOUSE, ARESIZE, AKEYBOARD, AEND };
96122 Alt a[AEND+1] = {
97123 { mc->c, nil, CHANRCV },
98124 { mc->resizec, nil, CHANRCV },
99− { kc->c, &r, CHANRCV },
125+ { kc->c, &c, CHANRCV },
100126 { nil, nil, CHANEND },
101127 };
102128
@@ -110,13 +136,14 @@ threadmain(int argc, char *argv[]) · 7 unmodified lines
110136 width = Dx(screen->r) / tiles->width;
111137 height = Dy(screen->r) / tiles->height;
112138
113− cam = (Camera){ZP, Rect(0, 0, Dy(screen->r), Dx(screen->r)), Rect(0, 0, Dy(screen->r), Dx(screen->r))};
114− ccenter(&cam, Pt(pos.x * tiles->width, pos.y * tiles->height));
115− drawlevel(level, tiles, min(width+1, level->width), min(height+1, level->height));
139+ r = view(pos, level, width, height);
140+ campos = subpt(Pt(width/2, height/2), pos);
141+
142+ drawlevel(level, tiles, r);
116143 flushimage(display, 1);
117144 break;
118145 case AKEYBOARD:
119− switch(r){
146+ switch(c){
120147 case Kdel:
121148 threadexitsall(nil);
122149 break;
@@ -149,8 +176,10 @@ threadmain(int argc, char *argv[]) · 26 unmodified lines
149176 }
150177 break;
151178 }
152− ccenter(&cam, pos);
153− drawlevel(level, tiles, min(width+1, level->width), min(height+1, level->height));
179+
180+ r = view(pos, level, width, height);
181+ campos = subpt(Pt(width/2, height/2), pos);
182+ drawlevel(level, tiles, r);
154183 flushimage(display, 1);
155184 break;
156185 }