4 static struct level *lvl;
5 static struct cell *sel;
6 static int vpx, vpy, vpw, vph;
7 static float panx, pany, zoom;
8 static float cellsz; /* derived from zoom and level properties */
10 int init_lview(struct level *l)
20 void destroy_lview(void)
24 void lview_viewport(int x, int y, int xsz, int ysz)
30 zoom_lview(0); /* recalc cell size */
33 void pan_lview(float dx, float dy)
39 void zoom_lview(float dz)
44 xsz = zoom * vpw / lvl->width;
45 ysz = zoom * vph / lvl->height;
46 cellsz = xsz > ysz ? ysz : xsz;
49 static int bnstate[8];
51 void lview_mbutton(int bn, int press, int x, int y)
53 float hsz = cellsz / 2.0f;
54 sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0);
60 sel->type = CELL_WALK;
62 sel->type = CELL_SOLID;
67 void lview_mouse(int x, int y)
69 float hsz = cellsz / 2.0f;
70 if(!(sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0))) {
75 sel->type = CELL_WALK;
76 } else if(bnstate[2]) {
77 sel->type = CELL_SOLID;
82 static void draw_cell(struct cell *cell)
86 static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
90 cidx = cell - lvl->cells;
91 row = cidx / lvl->width;
92 col = cidx % lvl->width;
94 cell_to_pos(col, row, &x, &y);
97 glColor3f(0.4, 1.0f, 0.4);
99 glColor3f(0.5f, 0.5f, 0.5f);
101 glVertex2f(x - hsz, y - hsz);
102 glVertex2f(x + hsz, y - hsz);
103 glVertex2f(x + hsz, y + hsz);
104 glVertex2f(x - hsz, y + hsz);
108 hsz -= LTHICK * 2.0f;
110 glColor3fv(colors[cell->type]);
111 glVertex2f(x - hsz, y - hsz);
112 glVertex2f(x + hsz, y - hsz);
113 glVertex2f(x + hsz, y + hsz);
114 glVertex2f(x - hsz, y + hsz);
118 void draw_lview(void)
125 for(i=0; i<lvl->height; i++) {
126 for(j=0; j<lvl->width; j++) {
134 void cell_to_pos(int cx, int cy, float *px, float *py)
136 if(px) *px = (cx - lvl->width / 2.0f) * cellsz - panx + vpw / 2.0f;
137 if(py) *py = (cy - lvl->height / 2.0f) * cellsz - pany + vph / 2.0f;
140 struct cell *pos_to_cell(float px, float py, int *cx, int *cy)
144 col = (px + panx - vpw / 2.0f) / cellsz + lvl->width / 2.0f;
145 row = (py + pany - vph / 2.0f) / cellsz + lvl->height / 2.0f;
150 if(col >= 0 && col < lvl->width && row >= 0 && row < lvl->height) {
151 return lvl->cells + row * lvl->width + col;