From c91209078ac6e61e8543f506b504bca92b7f1a74 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 18 Aug 2021 06:41:14 +0300 Subject: [PATCH] cell grid and UI split --- tools/dunger/src/level.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dunger/src/level.h | 26 +++++++++++++++ tools/dunger/src/main.c | 62 +++++++++++++++++++++++++++++----- 3 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 tools/dunger/src/level.c create mode 100644 tools/dunger/src/level.h diff --git a/tools/dunger/src/level.c b/tools/dunger/src/level.c new file mode 100644 index 0000000..f61f3f6 --- /dev/null +++ b/tools/dunger/src/level.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include "level.h" + +extern int view_width, view_height; + +struct level *create_level(int xsz, int ysz) +{ + struct level *lvl; + + if(!(lvl = malloc(sizeof *lvl))) { + return 0; + } + if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) { + free(lvl); + return 0; + } + lvl->width = xsz; + lvl->height = ysz; + return lvl; +} + +void free_level(struct level *lvl) +{ + if(!lvl) return; + free(lvl->cells); + free(lvl); +} + +static void draw_cell(float x, float y, float sz, struct cell *cell) +{ + static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}}; + + if(cell) { + glColor3fv(colors[cell->type]); + } else { + glColor3f(1, 1, 1); + } + glVertex2f(x, y); + glVertex2f(x + sz, y); + glVertex2f(x + sz, y + sz); + glVertex2f(x, y + sz); +} + +void draw_level(struct level *lvl) +{ + int i, j; + float x, y, dx, dy, cellsz; + struct cell *cell; + + dx = view_width / lvl->width; + dy = view_height / lvl->height; + cellsz = dx > dy ? dy : dx; + + glBegin(GL_QUADS); + cell = lvl->cells; + y = 0; + for(i=0; iheight; i++) { + x = 0; + for(j=0; jwidth; j++) { + draw_cell(x, y, cellsz, cell++); + x += cellsz; + } + y += cellsz; + } + glEnd(); + + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glBegin(GL_QUADS); + y = 0; + for(i=0; iheight; i++) { + x = 0; + for(j=0; jwidth; j++) { + draw_cell(x, y, cellsz, 0); + x += cellsz; + } + y += cellsz; + } + glEnd(); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +} diff --git a/tools/dunger/src/level.h b/tools/dunger/src/level.h new file mode 100644 index 0000000..93fc08e --- /dev/null +++ b/tools/dunger/src/level.h @@ -0,0 +1,26 @@ +#ifndef LEVEL_H_ +#define LEVEL_H_ + +enum { + CELL_SOLID, + CELL_WALK, + CELL_BLOCKED +}; + +struct cell { + int type; + int wall[4]; + int floor, ceil; +}; + +struct level { + int width, height; + struct cell *cells; +}; + +struct level *create_level(int xsz, int ysz); +void free_level(struct level *lvl); + +void draw_level(struct level *lvl); + +#endif /* LEVEL_H_ */ diff --git a/tools/dunger/src/main.c b/tools/dunger/src/main.c index 4c0fe7f..7c022c1 100644 --- a/tools/dunger/src/main.c +++ b/tools/dunger/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include "level.h" static int init(void); static void cleanup(void); @@ -22,13 +23,18 @@ static void utext(int x, int y, const char *txt, int sz); static int utextspacing(void); static int utextwidth(const char *txt, int sz); -static int win_width, win_height; +int win_width, win_height; +int view_width, view_height; + static float uiscale = 1.0f; +#define UISPLIT 150 #define FONTSZ 16 static struct dtx_font *uifont; static utk_widget *uiroot; +static struct level *lvl; + int main(int argc, char **argv) { @@ -84,27 +90,72 @@ static int init(void) utk_set_text_spacing_func(utextspacing); utk_set_text_width_func(utextwidth); - win = utk_window(uiroot, 20, 20, 100, 100, "window"); - utk_show(win); + win = utk_vbox(uiroot, 0, UTK_DEF_SPACING); + utk_set_pos(win, 15, 15); + utk_button(win, "hello", 0, 0, 0, 0); + utk_button(win, "button 2", 0, 0, 0, 0); + utk_button(win, "button 3", 0, 0, 0, 0); + + if(!(lvl = create_level(32, 32))) { + fprintf(stderr, "failed to create level\n"); + return -1; + } return 0; } static void cleanup(void) { + free_level(lvl); dtx_close_font(uifont); utk_close(uiroot); } static void display(void) { + int splitx = UISPLIT * uiscale; + + view_width = win_width - splitx; + view_height = win_height; + glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + /* draw UI */ + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, splitx, win_height, 0, -1, 1); + glViewport(0, 0, splitx, win_height); + + glBegin(GL_QUADS); + glColor3f(0.25, 0.25, 0.25); + glVertex2f(0, 0); + glVertex2f(splitx, 0); + glVertex2f(splitx, win_height); + glVertex2f(0, win_height); + glEnd(); utk_draw(uiroot); + /* draw view */ + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, view_width, 0, view_height, -1, 1); + glViewport(splitx, 0, view_width, view_height); + + glBegin(GL_QUADS); + glColor3f(0.1, 0.1, 0.1); + glVertex2f(0, 0); + glVertex2f(view_width, 0); + glVertex2f(view_width, view_height); + glVertex2f(0, view_height); + glEnd(); + + draw_level(lvl); + glutSwapBuffers(); } @@ -113,11 +164,6 @@ static void reshape(int x, int y) win_width = x; win_height = y; - glViewport(0, 0, x, y); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, x, y, 0, -1, 1); - if(uiroot) { utk_set_size(uiroot, x / uiscale, y / uiscale); } -- 1.7.10.4