7 extern int view_width, view_height;
8 extern float view_panx, view_pany, view_zoom;
10 extern int mousex, mousey, splitx;
13 static struct cell *selcell;
16 struct level *create_level(int xsz, int ysz)
20 if(!(lvl = malloc(sizeof *lvl))) {
23 if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) {
32 void free_level(struct level *lvl)
39 void cell_to_pos(struct level *lvl, int cx, int cy, float *px, float *py)
41 if(px) *px = (cx - lvl->width / 2.0f) * cellsz - view_panx + view_width / 2.0f;
42 if(py) *py = (cy - lvl->height / 2.0f) * cellsz - view_pany + view_height / 2.0f;
45 struct cell *pos_to_cell(struct level *lvl, float px, float py, int *cx, int *cy)
49 col = (px + view_panx - view_width / 2.0f) / cellsz + lvl->width / 2.0f;
50 row = (py + view_pany - view_height / 2.0f) / cellsz + lvl->height / 2.0f;
55 if(col >= 0 && col < lvl->width && row >= 0 && row < lvl->height) {
56 return lvl->cells + row * lvl->width + col;
62 static void draw_cell(struct level *lvl, struct cell *cell)
66 static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
70 cidx = cell - lvl->cells;
71 row = cidx / lvl->width;
72 col = cidx % lvl->width;
74 cell_to_pos(lvl, col, row, &x, &y);
75 /*printf("c->p: %d,%d -> %f,%f\n", col, row, x, y);
76 pos_to_cell(lvl, x, y, &col, &row);
77 printf("p->c: %f,%f -> %d,%d\n", x, y, col, row);*/
80 glColor3f(0.4, 1.0f, 0.4);
82 glColor3f(0.5f, 0.5f, 0.5f);
84 glVertex2f(x - hsz, y - hsz);
85 glVertex2f(x + hsz, y - hsz);
86 glVertex2f(x + hsz, y + hsz);
87 glVertex2f(x - hsz, y + hsz);
93 glColor3fv(colors[cell->type]);
94 glVertex2f(x - hsz, y - hsz);
95 glVertex2f(x + hsz, y - hsz);
96 glVertex2f(x + hsz, y + hsz);
97 glVertex2f(x - hsz, y + hsz);
100 void draw_level(struct level *lvl)
106 xsz = view_zoom * view_width / lvl->width;
107 ysz = view_zoom * view_height / lvl->height;
108 cellsz = xsz > ysz ? ysz : xsz;
111 selcell = pos_to_cell(lvl, mousex + hsz - splitx, view_height - mousey + hsz, 0, 0);
115 for(i=0; i<lvl->height; i++) {
116 for(j=0; j<lvl->width; j++) {
117 draw_cell(lvl, cell++);