buttons can be drawn
This commit is contained in:
parent
cedf7cac74
commit
96d7127021
7 changed files with 113 additions and 35 deletions
13
Makefile
13
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
|
||||
|
|
22
mmn_17.c
22
mmn_17.c
|
@ -1,13 +1,33 @@
|
|||
#include <GL/freeglut.h>
|
||||
#include <GL/freeglut_std.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#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);
|
||||
|
||||
|
|
47
ui.c
47
ui.c
|
@ -0,0 +1,47 @@
|
|||
#include "ui.h"
|
||||
#include "vec.h"
|
||||
#include <GL/freeglut_std.h>
|
||||
#include <GL/freeglut_ext.h>
|
||||
#include <GL/glut.h>
|
||||
#include <GL/gl.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
27
ui.h
27
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
|
25
ui_button.h
25
ui_button.h
|
@ -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
|
8
vec.c
8
vec.c
|
@ -1,4 +1,5 @@
|
|||
#include "vec.h"
|
||||
#include <GL/gl.h>
|
||||
|
||||
vec3 vec3_new(float x, float y, float z) {
|
||||
vec3 r = { x, y, z };
|
||||
|
@ -27,3 +28,10 @@ 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);
|
||||
}
|
6
vec.h
6
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
|
||||
|
|
Loading…
Reference in a new issue