From ecbd6ce6ca6f616169fb319ec86d963db4f84287 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 19 Aug 2021 05:55:00 +0300 Subject: [PATCH] cell select --- tools/dunger/src/level.c | 75 +++++++++++++++++++++++++++++++++++----------- tools/dunger/src/main.c | 8 +++-- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/tools/dunger/src/level.c b/tools/dunger/src/level.c index 6fb5706..1e88b5c 100644 --- a/tools/dunger/src/level.c +++ b/tools/dunger/src/level.c @@ -7,6 +7,11 @@ extern int view_width, view_height; extern float view_panx, view_pany, view_zoom; +extern int mousex, mousey, splitx; + +static int cellsz; +static struct cell *selcell; + struct level *create_level(int xsz, int ysz) { @@ -31,46 +36,80 @@ void free_level(struct level *lvl) free(lvl); } +void cell_to_pos(struct level *lvl, int cx, int cy, float *px, float *py) +{ + if(px) *px = (cx - lvl->width / 2.0f) * cellsz - view_panx + view_width / 2.0f; + if(py) *py = (cy - lvl->height / 2.0f) * cellsz - view_pany + view_height / 2.0f; +} + +struct cell *pos_to_cell(struct level *lvl, float px, float py, int *cx, int *cy) +{ + int col, row; + + col = (px + view_panx - view_width / 2.0f) / cellsz + lvl->width / 2.0f; + row = (py + view_pany - view_height / 2.0f) / cellsz + lvl->height / 2.0f; + + if(cx) *cx = col; + if(cy) *cy = row; + + if(col >= 0 && col < lvl->width && row >= 0 && row < lvl->height) { + return lvl->cells + row * lvl->width + col; + } + return 0; +} + #define LTHICK 0.5f static void draw_cell(struct level *lvl, struct cell *cell) { int cidx, row, col; - float x, y, xsz, ysz, sz; + float x, y, hsz; static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}}; - xsz = view_zoom * view_width / lvl->width; - ysz = view_zoom * view_height / lvl->height; - sz = xsz > ysz ? ysz : xsz; + hsz = cellsz * 0.5f; cidx = cell - lvl->cells; row = cidx / lvl->width; col = cidx % lvl->width; - x = col * sz - view_panx; - y = row * sz - view_pany; + cell_to_pos(lvl, col, row, &x, &y); + /*printf("c->p: %d,%d -> %f,%f\n", col, row, x, y); + pos_to_cell(lvl, x, y, &col, &row); + printf("p->c: %f,%f -> %d,%d\n", x, y, col, row);*/ - glColor3f(1, 1, 1); - glVertex2f(x, y); - glVertex2f(x + sz, y); - glVertex2f(x + sz, y + sz); - glVertex2f(x, y + sz); + if(selcell == cell) { + glColor3f(0.4, 1.0f, 0.4); + } else { + glColor3f(0.5f, 0.5f, 0.5f); + } + glVertex2f(x - hsz, y - hsz); + glVertex2f(x + hsz, y - hsz); + glVertex2f(x + hsz, y + hsz); + glVertex2f(x - hsz, y + hsz); - x += LTHICK; - y += LTHICK; - sz -= LTHICK * 2.0f; + x += LTHICK / 2.0f; + y += LTHICK / 2.0f; + hsz -= LTHICK * 2.0f; glColor3fv(colors[cell->type]); - glVertex2f(x, y); - glVertex2f(x + sz, y); - glVertex2f(x + sz, y + sz); - glVertex2f(x, y + sz); + glVertex2f(x - hsz, y - hsz); + glVertex2f(x + hsz, y - hsz); + glVertex2f(x + hsz, y + hsz); + glVertex2f(x - hsz, y + hsz); } void draw_level(struct level *lvl) { int i, j; + float xsz, ysz, hsz; struct cell *cell; + xsz = view_zoom * view_width / lvl->width; + ysz = view_zoom * view_height / lvl->height; + cellsz = xsz > ysz ? ysz : xsz; + hsz = cellsz / 2.0f; + + selcell = pos_to_cell(lvl, mousex + hsz - splitx, view_height - mousey + hsz, 0, 0); + glBegin(GL_QUADS); cell = lvl->cells; for(i=0; iheight; i++) { diff --git a/tools/dunger/src/main.c b/tools/dunger/src/main.c index 52074ba..bfc0d43 100644 --- a/tools/dunger/src/main.c +++ b/tools/dunger/src/main.c @@ -28,11 +28,11 @@ int view_width, view_height; float view_panx, view_pany, view_zoom = 1.0f; static int bnstate[8]; -static int mousex, mousey, clickx, clicky; +int mousex, mousey, clickx, clicky; static float uiscale = 1.0f; #define UISPLIT 150 -static int splitx; +int splitx; #define FONTSZ 16 static struct dtx_font *uifont; @@ -45,7 +45,7 @@ int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(1280, 800); - glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE); glutCreateWindow("dunger"); win_width = glutGet(GLUT_WINDOW_WIDTH); @@ -73,6 +73,8 @@ static int init(void) { utk_widget *win; + glEnable(GL_MULTISAMPLE); + glClearColor(0.15, 0.15, 0.15, 1); if(!(uifont = dtx_open_font("uifont.ttf", 0))) { -- 1.7.10.4