5 static struct level *lvl;
6 static struct cell *sel;
7 static int vpx, vpy, vpw, vph;
8 static float panx, pany, zoom;
9 static float cellsz; /* derived from zoom and level properties */
11 int init_lview(struct level *l)
21 void destroy_lview(void)
25 void lview_viewport(int x, int y, int xsz, int ysz)
31 zoom_lview(0); /* recalc cell size */
34 void pan_lview(float dx, float dy)
40 void zoom_lview(float dz)
45 xsz = zoom * vpw / lvl->width;
46 ysz = zoom * vph / lvl->height;
47 cellsz = xsz > ysz ? ysz : xsz;
50 static int bnstate[8];
52 void lview_mbutton(int bn, int press, int x, int y)
54 float hsz = cellsz / 2.0f;
55 sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0);
61 sel->type = CELL_WALK;
63 sel->type = CELL_SOLID;
68 void lview_mouse(int x, int y)
70 float hsz = cellsz / 2.0f;
71 if(!(sel = pos_to_cell(x + hsz - vpx, vph - y + hsz - vpy, 0, 0))) {
76 sel->type = CELL_WALK;
77 } else if(bnstate[2]) {
78 sel->type = CELL_SOLID;
83 static void draw_cell(struct cell *cell)
87 static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
91 cidx = cell - lvl->cells;
92 row = cidx / lvl->width;
93 col = cidx % lvl->width;
95 cell_to_pos(col, row, &x, &y);
98 glColor3f(0.4, 1.0f, 0.4);
100 glColor3f(0.5f, 0.5f, 0.5f);
102 glVertex2f(x - hsz, y - hsz);
103 glVertex2f(x + hsz, y - hsz);
104 glVertex2f(x + hsz, y + hsz);
105 glVertex2f(x - hsz, y + hsz);
109 hsz -= LTHICK * 2.0f;
111 glColor3fv(colors[cell->type]);
112 glVertex2f(x - hsz, y - hsz);
113 glVertex2f(x + hsz, y - hsz);
114 glVertex2f(x + hsz, y + hsz);
115 glVertex2f(x - hsz, y + hsz);
119 void draw_lview(void)
126 for(i=0; i<lvl->height; i++) {
127 for(j=0; j<lvl->width; j++) {
134 int cidx = sel - lvl->cells;
135 int row = cidx / lvl->width;
136 int col = cidx % lvl->width;
138 glMatrixMode(GL_MODELVIEW);
140 glTranslatef(10, 10, 0);
143 dtx_printf("(%d, %d)", col, row);
150 void cell_to_pos(int cx, int cy, float *px, float *py)
152 if(px) *px = (cx - lvl->width / 2.0f) * cellsz - panx + vpw / 2.0f;
153 if(py) *py = (cy - lvl->height / 2.0f) * cellsz - pany + vph / 2.0f;
156 struct cell *pos_to_cell(float px, float py, int *cx, int *cy)
160 col = (px + panx - vpw / 2.0f) / cellsz + lvl->width / 2.0f;
161 row = (py + pany - vph / 2.0f) / cellsz + lvl->height / 2.0f;
166 if(col >= 0 && col < lvl->width && row >= 0 && row < lvl->height) {
167 return lvl->cells + row * lvl->width + col;