From 96d71270218c917f1bf189c5ed83dd094120f84f Mon Sep 17 00:00:00 2001 From: Rusty Striker Date: Wed, 5 Jul 2023 11:37:27 +0300 Subject: [PATCH] buttons can be drawn --- Makefile | 13 ++++++++++--- mmn_17.c | 22 +++++++++++++++++++++- ui.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ ui.h | 27 +++++++++++++++++++++++---- ui_button.h | 25 ------------------------- vec.c | 8 ++++++++ vec.h | 6 ++++-- 7 files changed, 113 insertions(+), 35 deletions(-) delete mode 100644 ui_button.h diff --git a/Makefile b/Makefile index ca6f58a..c1df8ca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,12 @@ -build: - gcc mmn_17.c -lglut -lGL -lGLU -lm -o mmn_17 +OBJ = ui.o vec.o +CFLAGS = -g -Wall + +build: $(OBJ) + gcc $(CFLAGS) mmn_17.c $(OBJ) -lglut -lGL -lGLU -lm -o mmn_17 + +%.o : %.c + gcc -c $(CFLAGS) $< -o $@ + clean: - rm mmn_17 + rm *.o mmn_17 diff --git a/mmn_17.c b/mmn_17.c index 546184a..c03dce7 100644 --- a/mmn_17.c +++ b/mmn_17.c @@ -1,13 +1,33 @@ #include #include #include +#include #include #include +#include "ui.h" +// Ui stuff +ui_button buttons[] = { + { + /* pos */ { { 0.5, 0.5, 0.0 }, { -50.0, -25.0, 0.0 } }, + /* size */ { 100.0, 50.0, 0.0 }, + /* onclick */ NULL, + /* text */ "Test :)" + } +}; void display(void) { // Clear screen glClearColor(0.0, 0.0, 0.0, 1.0); // Dark theme :) glClear(GL_COLOR_BUFFER_BIT); + // Draw buttons + for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) { + ui_button_draw(b); + } + int err = glGetError(); + if(err != 0) { + printf("opengl error %d: %s\n", err, gluErrorString(err)); + } + glutSwapBuffers(); glFlush(); } @@ -22,7 +42,7 @@ int main(int argc, char** argv) { glutInitWindowSize(800, 500); glutInitWindowPosition(450, 450); /* create window and set callbacks */ - glutCreateWindow("glutLeaveMainLoop is undefined if called from a nested loop"); + glutCreateWindow("World of Cow"); glutDisplayFunc(display); glutMouseFunc(mouseEvent); diff --git a/ui.c b/ui.c index e69de29..c27879f 100644 --- a/ui.c +++ b/ui.c @@ -0,0 +1,47 @@ +#include "ui.h" +#include "vec.h" +#include +#include +#include +#include +#include + +void ui_button_draw(ui_button* b) { + printf("drawing button %p\n", b); + int wWidth = glutGet(GLUT_WINDOW_WIDTH); + int wHeight = glutGet(GLUT_WINDOW_HEIGHT); + + vec3 pos = vec3_add(b->position.absolute, vec3_new(b->position.relative.x * wWidth, b->position.relative.y * wHeight, 0.0)); + vec3 ext = b->size; + glColor3f(1.0, 1.0, 1.0); + glBegin(GL_QUADS); + glVertexVec(pos); + glVertex3f(pos.x + ext.x, pos.y, pos.z); + glVertexVec(vec3_add(pos, ext)); + glVertex3f(pos.x, pos.y + ext.y, pos.z); + glEnd(); + // Draw text if needed + if(b->text != NULL) { + glColor3f(0.0, 0.0, 0.0); + // Get text size + int textWidth = glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, (unsigned char*)b->text); + int textHeight = glutBitmapHeight(GLUT_BITMAP_TIMES_ROMAN_24); + // set raster position - not gonna take into account multiple line text buttons + vec3 rPos = vec3_add(pos, vec3_mult(ext, 0.5)); + rPos.x -= textWidth * 0.5; + rPos.y -= textHeight * 0.25; + glRasterPos3f(rPos.x, rPos.y, rPos.z); + glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (unsigned char*)b->text); + } +} + +char ui_button_mouse_over(ui_button* b) { + return 0; +} + +void ui_slider_draw(ui_slider* s) { + +} +char ui_slider_mouse_over(ui_slider* s) { + return 0; +} \ No newline at end of file diff --git a/ui.h b/ui.h index b261947..3c6eca3 100644 --- a/ui.h +++ b/ui.h @@ -1,12 +1,31 @@ -#ifndef __UI_BUTTON -#define __UI_BUTTON +#ifndef __UI +#define __UI #include "vec.h" +typedef struct _ui_position { + vec3 relative; + vec3 absolute; +} ui_pos; + typedef struct _ui_button { - vec3 relative; // Position relative to screen size - vec3 absolute; // position in pixels(added to relative) + ui_pos position; + /* x,y - width,height, z - unused */ vec3 size; // size in pixels - if screen is too small i am not sure if it is a smart idea to make everything smaller void (*onClick); // instead of passing stuff in, im just gonna put every state variable in global scope to ensure everything lives... + char* text; } ui_button; +typedef struct _ui_slider { + ui_pos position; + /* x - width, y - height, z - middle circle radius */ + vec3 size; + float value; // Value will always range between 0 and 1 +} ui_slider; + +void ui_button_draw(ui_button* b); +char ui_button_mouse_over(ui_button* b); + +void ui_slider_draw(ui_slider* s); +char ui_slider_mouse_over(ui_slider* s); + #endif \ No newline at end of file diff --git a/ui_button.h b/ui_button.h deleted file mode 100644 index 8719d9a..0000000 --- a/ui_button.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef __UI -#define __UI -#include "vec.h" - -typedef struct _ui_position { - vec3 relative; - vec3 absolute; -} ui_pos; - -typedef struct _ui_button { - ui_pos position; - /* x,y - width,height, z - unused */ - vec3 size; // size in pixels - if screen is too small i am not sure if it is a smart idea to make everything smaller - void (*onClick); // instead of passing stuff in, im just gonna put every state variable in global scope to ensure everything lives... - char* text; -} ui_button; - -typedef struct _ui_slider { - ui_pos position; - /* x - width, y - height, z - middle circle radius */ - vec3 size; - float value; // Value will always range between 0 and 1 -} ui_slider; - -#endif \ No newline at end of file diff --git a/vec.c b/vec.c index 8438c4a..93c40d1 100644 --- a/vec.c +++ b/vec.c @@ -1,4 +1,5 @@ #include "vec.h" +#include vec3 vec3_new(float x, float y, float z) { vec3 r = { x, y, z }; @@ -26,4 +27,11 @@ float vec3_dot(vec3 a, vec3 b) { vec3 vec3_mult(vec3 a, float s) { vec3 r = { a.x * s, a.y * s, a.z * s }; return r; +} + +void glVertexVec(vec3 v) { + glVertex3f(v.x, v.y, v.z); +} +void glColorVec(vec3 v) { + glColor3f(v.x, v.y, v.z); } \ No newline at end of file diff --git a/vec.h b/vec.h index d558f03..6e4162f 100644 --- a/vec.h +++ b/vec.h @@ -7,8 +7,6 @@ typedef struct _vec3 { float z; } vec3; -const vec3 ZERO = { 0.0, 0.0, 0.0}; - vec3 vec3_new(float x, float y, float z); vec3 vec3_splat(float f); vec3 vec3_add(vec3 a, vec3 b); @@ -16,4 +14,8 @@ vec3 vec3_sub(vec3 a, vec3 b); float vec3_dot(vec3 a, vec3 b); vec3 vec3_mult(vec3 a, float s); +// Some opengl extensions to support these vectors +void glVertexVec(vec3 v); +void glColorVec(vec3 v); + #endif