Unified · Split
| @@ -13,8 +13,11 @@ Keyboardctl *kc; · 12 unmodified lines | ||
| 13 | 13 | Tileset *tiles; |
| 14 | 14 | Level *level; |
| 15 | 15 | |
| 16 | +/* in tiles */ | |
| 17 | +Point campos; | |
| 18 | + | |
| 19 | +/* of player */ | |
| 16 | 20 | Point pos; |
| 17 | −Camera cam; | |
| 18 | 21 | |
| 19 | 22 | static int |
| 20 | 23 | min(int a, int b) |
| @@ -24,43 +27,64 @@ min(int a, int b) · 3 unmodified lines | ||
| 24 | 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 | + * */ | |
| 27 | 34 | static void |
| 28 | −drawlevel(Level *l, Tileset *ts, int width, int height) | |
| 35 | +drawlevel(Level *l, Tileset *ts, Rectangle r) | |
| 29 | 36 | { |
| 30 | 37 | int x, y, what; |
| 31 | − Point xy, tpos, cen; | |
| 38 | + Point xy, tpos; | |
| 32 | 39 | Tile *t; |
| 33 | 40 | |
| 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); | |
| 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 | 48 | tpos = xy; |
| 49 | + tpos = addpt(tpos, campos); | |
| 40 | 50 | tpos.x *= ts->width; |
| 41 | 51 | tpos.y *= ts->height; |
| 42 | − tpos = ctrans(&cam, tpos); | |
| 52 | + tpos = addpt(tpos, screen->r.min); | |
| 43 | 53 | 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 | + } | |
| 53 | 62 | } |
| 54 | 63 | } |
| 55 | 64 | } |
| 56 | 65 | |
| 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 | + | |
| 57 | 79 | void |
| 58 | 80 | threadmain(int argc, char *argv[]) |
| 59 | 81 | { |
| 60 | − Rune r; | |
| 82 | + Rune c; | |
| 61 | 83 | |
| 62 | 84 | /* of viewport, in size of tiles */ |
| 63 | 85 | int width, height; |
| 86 | + Point pt; | |
| 87 | + Rectangle r; | |
| 64 | 88 | |
| 65 | 89 | ARGBEGIN{ |
| 66 | 90 | }ARGEND |
| @@ -87,16 +111,18 @@ threadmain(int argc, char *argv[]) · 20 unmodified lines | ||
| 87 | 111 | |
| 88 | 112 | width = Dx(screen->r) / tiles->width; |
| 89 | 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))}; | |
| 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); | |
| 93 | 119 | flushimage(display, 1); |
| 94 | 120 | |
| 95 | 121 | enum { AMOUSE, ARESIZE, AKEYBOARD, AEND }; |
| 96 | 122 | Alt a[AEND+1] = { |
| 97 | 123 | { mc->c, nil, CHANRCV }, |
| 98 | 124 | { mc->resizec, nil, CHANRCV }, |
| 99 | − { kc->c, &r, CHANRCV }, | |
| 125 | + { kc->c, &c, CHANRCV }, | |
| 100 | 126 | { nil, nil, CHANEND }, |
| 101 | 127 | }; |
| 102 | 128 | |
| @@ -110,13 +136,14 @@ threadmain(int argc, char *argv[]) · 7 unmodified lines | ||
| 110 | 136 | width = Dx(screen->r) / tiles->width; |
| 111 | 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))}; | |
| 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); | |
| 116 | 143 | flushimage(display, 1); |
| 117 | 144 | break; |
| 118 | 145 | case AKEYBOARD: |
| 119 | − switch(r){ | |
| 146 | + switch(c){ | |
| 120 | 147 | case Kdel: |
| 121 | 148 | threadexitsall(nil); |
| 122 | 149 | break; |
| @@ -149,8 +176,10 @@ threadmain(int argc, char *argv[]) · 26 unmodified lines | ||
| 149 | 176 | } |
| 150 | 177 | break; |
| 151 | 178 | } |
| 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); | |
| 154 | 183 | flushimage(display, 1); |
| 155 | 184 | break; |
| 156 | 185 | } |