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
13Tileset *tiles;13Tileset *tiles;
14Level *level;14Level *level;
15 15
16/* in tiles */
17Point campos;
18
19/* of player */
16Point pos;20Point pos;
17Camera cam;
18 21
19static int22static int
20min(int a, int b)23min(int a, int b)
@@ -24,43 +27,64 @@ min(int a, int b) · 3 unmodified lines
24 return b;27 return b;
25}28}
26 29
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 * */
27static void34static void
28drawlevel(Level *l, Tileset *ts, int width, int height)35drawlevel(Level *l, Tileset *ts, Rectangle r)
29{36{
30 int x, y, what;37 int x, y, what;
31 Point xy, tpos, cen;38 Point xy, tpos;
32 Tile *t;39 Tile *t;
33 40
34 for(x = 0; x < width; x++){41 draw(screen, screen->r, display->black, nil, ZP);
35 for(y = 0; y < height; y++){
36 xy = (Point){x, y};
37 42
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 */
39 tpos = xy;48 tpos = xy;
49 tpos = addpt(tpos, campos);
40 tpos.x *= ts->width;50 tpos.x *= ts->width;
41 tpos.y *= ts->height;51 tpos.y *= ts->height;
42 tpos = ctrans(&cam, tpos);52 tpos = addpt(tpos, screen->r.min);
43 t = tileat(l, xy);53 t = tileat(l, xy);
44 what = t->terrain;54 if(t){
45 if(t->unit)55 what = t->terrain;
46 what = t->unit;56 if(t->unit)
47 else if(t->feat)57 what = t->unit;
48 what = t->feat;58 else if(t->feat)
49 cen = addpt(screen->r.min, tpos);59 what = t->feat;
50 //if(what == TWIZARD)60 drawtile(ts, screen, tpos, what);
51 // fprint(2, "draw %d at %P %P ", what, tpos, cen);61 }
52 drawtile(ts, screen, cen, what);
53 }62 }
54 }63 }
55}64}
56 65
66Rectangle
67view(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
57void79void
58threadmain(int argc, char *argv[])80threadmain(int argc, char *argv[])
59{81{
60 Rune r;82 Rune c;
61 83
62 /* of viewport, in size of tiles */84 /* of viewport, in size of tiles */
63 int width, height;85 int width, height;
86 Point pt;
87 Rectangle r;
64 88
65 ARGBEGIN{89 ARGBEGIN{
66 }ARGEND90 }ARGEND
@@ -87,16 +111,18 @@ threadmain(int argc, char *argv[]) · 20 unmodified lines
87 111
88 width = Dx(screen->r) / tiles->width;112 width = Dx(screen->r) / tiles->width;
89 height = Dy(screen->r) / tiles->height;113 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))};114
91 ccenter(&cam, Pt(pos.x * tiles->width, pos.y * tiles->height));115 r = view(pos, level, width, height);
92 drawlevel(level, tiles, min(width+1, level->width), min(height+1, level->height));116 campos = subpt(Pt(width/2, height/2), pos);
117
118 drawlevel(level, tiles, r);
93 flushimage(display, 1);119 flushimage(display, 1);
94 120
95 enum { AMOUSE, ARESIZE, AKEYBOARD, AEND };121 enum { AMOUSE, ARESIZE, AKEYBOARD, AEND };
96 Alt a[AEND+1] = {122 Alt a[AEND+1] = {
97 { mc->c, nil, CHANRCV },123 { mc->c, nil, CHANRCV },
98 { mc->resizec, nil, CHANRCV },124 { mc->resizec, nil, CHANRCV },
99 { kc->c, &r, CHANRCV },125 { kc->c, &c, CHANRCV },
100 { nil, nil, CHANEND },126 { nil, nil, CHANEND },
101 };127 };
102 128
@@ -110,13 +136,14 @@ threadmain(int argc, char *argv[]) · 7 unmodified lines
110 width = Dx(screen->r) / tiles->width;136 width = Dx(screen->r) / tiles->width;
111 height = Dy(screen->r) / tiles->height;137 height = Dy(screen->r) / tiles->height;
112 138
113 cam = (Camera){ZP, Rect(0, 0, Dy(screen->r), Dx(screen->r)), Rect(0, 0, Dy(screen->r), Dx(screen->r))};139 r = view(pos, level, width, height);
114 ccenter(&cam, Pt(pos.x * tiles->width, pos.y * tiles->height));140 campos = subpt(Pt(width/2, height/2), pos);
115 drawlevel(level, tiles, min(width+1, level->width), min(height+1, level->height));141
142 drawlevel(level, tiles, r);
116 flushimage(display, 1);143 flushimage(display, 1);
117 break;144 break;
118 case AKEYBOARD:145 case AKEYBOARD:
119 switch(r){146 switch(c){
120 case Kdel:147 case Kdel:
121 threadexitsall(nil);148 threadexitsall(nil);
122 break;149 break;
@@ -149,8 +176,10 @@ threadmain(int argc, char *argv[]) · 26 unmodified lines
149 }176 }
150 break;177 break;
151 }178 }
152 ccenter(&cam, pos);179
153 drawlevel(level, tiles, min(width+1, level->width), min(height+1, level->height));180 r = view(pos, level, width, height);
181 campos = subpt(Pt(width/2, height/2), pos);
182 drawlevel(level, tiles, r);
154 flushimage(display, 1);183 flushimage(display, 1);
155 break;184 break;
156 }185 }