6 static struct level *lvl;
7 static struct cell *sel;
8 static int vpx, vpy, vpw, vph;
9 static float panx, pany, zoom;
10 static float cellsz; /* derived from zoom and level properties */
12 int init_lview(struct level *l)
23 void destroy_lview(void)
27 void lview_viewport(int x, int y, int xsz, int ysz)
33 zoom_lview(0); /* recalc cell size */
36 void pan_lview(float dx, float dy)
42 void zoom_lview(float dz)
47 xsz = zoom * vpw / lvl->width;
48 ysz = zoom * vph / lvl->height;
49 cellsz = xsz > ysz ? ysz : xsz;
52 static int bnstate[8];
54 void lview_mbutton(int bn, int press, int x, int y)
57 float hsz = cellsz / 2.0f;
58 sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0);
67 sel->type = CELL_WALK;
69 sel->type = CELL_SOLID;
74 cell_coords(sel, &cx, &cy);
76 if(sel->type == CELL_WALK) {
81 if(lvl->px == cx && lvl->py == cy) {
82 lvl->px = lvl->py = -1;
90 void lview_mouse(int x, int y)
92 float hsz = cellsz / 2.0f;
93 if(!(sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0))) {
100 sel->type = CELL_WALK;
101 } else if(bnstate[2]) {
102 sel->type = CELL_SOLID;
109 static void draw_cell(struct cell *cell)
113 static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
117 cell_coords(cell, &col, &row);
118 cell_to_pos(col, row, &x, &y);
123 if(col == lvl->px && row == lvl->py) {
126 glColor3f(0.5f, 0.5f, 0.5f);
129 glVertex2f(x - hsz, y - hsz);
130 glVertex2f(x + hsz, y - hsz);
131 glVertex2f(x + hsz, y + hsz);
132 glVertex2f(x - hsz, y + hsz);
136 hsz -= LTHICK * 2.0f;
138 glColor3fv(colors[cell->type]);
139 glVertex2f(x - hsz, y - hsz);
140 glVertex2f(x + hsz, y - hsz);
141 glVertex2f(x + hsz, y + hsz);
142 glVertex2f(x - hsz, y + hsz);
146 void draw_lview(void)
153 for(i=0; i<lvl->height; i++) {
154 for(j=0; j<lvl->width; j++) {
161 cell_coords(sel, &col, &row);
163 glMatrixMode(GL_MODELVIEW);
165 glTranslatef(10, 10, 0);
168 dtx_printf("(%d, %d)", col, row);
175 void cell_to_pos(int cx, int cy, float *px, float *py)
177 if(px) *px = (cx - lvl->width / 2.0f) * cellsz - panx + vpw / 2.0f;
178 if(py) *py = (cy - lvl->height / 2.0f) * cellsz - pany + vph / 2.0f;
181 struct cell *pos_to_cell(float px, float py, int *cx, int *cy)
185 col = (px + panx - vpw / 2.0f) / cellsz + lvl->width / 2.0f;
186 row = (py + pany - vph / 2.0f) / cellsz + lvl->height / 2.0f;
191 if(col >= 0 && col < lvl->width && row >= 0 && row < lvl->height) {
192 return lvl->cells + row * lvl->width + col;
197 void cell_coords(struct cell *cell, int *col, int *row)
199 int cidx = cell - lvl->cells;
200 *row = cidx / lvl->width;
201 *col = cidx % lvl->width;