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 void lview_mouse(int x, int y)
51 float hsz = cellsz / 2.0f;
52 sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0);
56 static void draw_cell(struct cell *cell)
60 static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
64 cidx = cell - lvl->cells;
65 row = cidx / lvl->width;
66 col = cidx % lvl->width;
68 cell_to_pos(col, row, &x, &y);
71 glColor3f(0.4, 1.0f, 0.4);
73 glColor3f(0.5f, 0.5f, 0.5f);
75 glVertex2f(x - hsz, y - hsz);
76 glVertex2f(x + hsz, y - hsz);
77 glVertex2f(x + hsz, y + hsz);
78 glVertex2f(x - hsz, y + hsz);
84 glColor3fv(colors[cell->type]);
85 glVertex2f(x - hsz, y - hsz);
86 glVertex2f(x + hsz, y - hsz);
87 glVertex2f(x + hsz, y + hsz);
88 glVertex2f(x - hsz, y + hsz);
99 for(i=0; i<lvl->height; i++) {
100 for(j=0; j<lvl->width; j++) {
108 void cell_to_pos(int cx, int cy, float *px, float *py)
110 if(px) *px = (cx - lvl->width / 2.0f) * cellsz - panx + vpw / 2.0f;
111 if(py) *py = (cy - lvl->height / 2.0f) * cellsz - pany + vph / 2.0f;
114 struct cell *pos_to_cell(float px, float py, int *cx, int *cy)
118 col = (px + panx - vpw / 2.0f) / cellsz + lvl->width / 2.0f;
119 row = (py + pany - vph / 2.0f) / cellsz + lvl->height / 2.0f;
124 if(col >= 0 && col < lvl->width && row >= 0 && row < lvl->height) {
125 return lvl->cells + row * lvl->width + col;