# HG changeset patch # User John Tsiombikas # Date 1293679334 -7200 # Node ID b04d49e4599c4ddb0ec3ec10fa564d4502b57460 foo diff -r 000000000000 -r b04d49e4599c Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Thu Dec 30 05:22:14 2010 +0200 @@ -0,0 +1,20 @@ +src = $(wildcard *.c src/*.c) +obj = $(src:.c=.o) +bin = test + +CC = gcc +CFLAGS = -pedantic -Wall -g -Isrc +LDFLAGS = $(libgl) + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -framework GLUT +else + libgl = -lGL -lglut +endif + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 000000000000 -r b04d49e4599c src/imtk.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/imtk.c Thu Dec 30 05:22:14 2010 +0200 @@ -0,0 +1,161 @@ +#include +#include +#ifndef __APPLE__ +#include +#else +#include +#endif +#include "imtk.h" + +static int scr_width = 1, scr_height = 1; +static int mousex, mousey, mouse_bnmask; +static int active = -1, hot = -1; + +static void set_active(int id); +static int set_hot(int id); +static int hit_test(int x, int y, int w, int h); + +static void draw_button(int id, int x, int y, const char *label, int focused); +static void calc_button_size(const char *label, int *wret, int *hret); +static void draw_string(int x, int y, const char *str); +static int string_size(const char *str); + +void imtk_inp_key(int key, int state) +{ + glutPostRedisplay(); +} + +void imtk_inp_mouse(int bn, int state) +{ + if(state == IMTK_DOWN) { + mouse_bnmask |= 1 << bn; + } else { + mouse_bnmask &= ~(1 << bn); + } + glutPostRedisplay(); +} + +void imtk_inp_motion(int x, int y) +{ + mousex = x; + mousey = y; + + glutPostRedisplay(); +} + +void imtk_inp_reshape(int x, int y) +{ + scr_width = x; + scr_height = y; +} + +void imtk_begin(void) +{ + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glTranslatef(-1, 1, 0); + glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0); +} + +void imtk_end(void) +{ + glMatrixMode(GL_PROJECTION); + glPopMatrix(); +} + +int imtk_button(int id, const char *label, int x, int y) +{ + int w, h, res = 0; + int over = 0; + + assert(id >= 0); + + calc_button_size(label, &w, &h); + + if(hit_test(x, y, w, h)) { + if(set_hot(id)) { + glutPostRedisplay(); + } + over = 1; + } + + + if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) { + if(over) { + set_active(id); + } + } else { /* mouse button up */ + if(active == id) { + set_active(-1); + if(hot == id && over) { + res = 1; + } + } + } + + draw_button(id, x, y, label, over); + return res; +} + +static void set_active(int id) +{ + active = id; +} + +static int set_hot(int id) +{ + if(active == -1) { + hot = id; + return 1; + } + return 0; +} + +static int hit_test(int x, int y, int w, int h) +{ + return mousex >= x && mousex < (x + w) && + mousey >= y && mousey < (y + h); +} + +static void draw_button(int id, int x, int y, const char *label, int focused) +{ + int width, height; + + calc_button_size(label, &width, &height); + + glBegin(GL_QUADS); + if(focused) { + glColor3f(0.85, 0.85, 0.85); + } else { + glColor3f(0.7, 0.7, 0.7); + } + glVertex2f(x, y); + glVertex2f(x + width, y); + glVertex2f(x + width, y + height); + glVertex2f(x, y + height); + glEnd(); + + glColor3f(0, 0, 0); + draw_string(x + 20, y + 15, label); +} + +static void calc_button_size(const char *label, int *wret, int *hret) +{ + int strsz = string_size(label); + if(wret) *wret = strsz + 40; + if(hret) *hret = 20; +} + +static void draw_string(int x, int y, const char *str) +{ + glRasterPos2i(x, y); + while(*str) { + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *str++); + } +} + +static int string_size(const char *str) +{ + return glutBitmapLength(GLUT_BITMAP_HELVETICA_10, str); +} diff -r 000000000000 -r b04d49e4599c src/imtk.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/imtk.h Thu Dec 30 05:22:14 2010 +0200 @@ -0,0 +1,26 @@ +#ifndef IMTK_H_ +#define IMTK_H_ + +/* key/button state enum */ +enum { + IMTK_UP, + IMTK_DOWN +}; + +enum { + IMTK_LEFT_BUTTON, + IMTK_MIDDLE_BUTTON, + IMTK_RIGHT_BUTTON +}; + +void imtk_inp_key(int key, int state); +void imtk_inp_mouse(int bn, int state); +void imtk_inp_motion(int x, int y); +void imtk_inp_reshape(int x, int y); + +void imtk_begin(void); +void imtk_end(void); + +int imtk_button(int id, const char *label, int x, int y); + +#endif /* IMTK_H_ */ diff -r 000000000000 -r b04d49e4599c test.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.c Thu Dec 30 05:22:14 2010 +0200 @@ -0,0 +1,106 @@ +#include +#include +#ifndef __APPLE__ +#include +#else +#include +#endif +#include "imtk.h" + +void disp(void); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); +void keyb_up(unsigned char key, int x, int y); +void skeyb(int key, int x, int y); +void skeyb_up(int key, int x, int y); +void mouse(int bn, int state, int x, int y); +void motion(int x, int y); + + +int main(int argc, char **argv) +{ + glutInitWindowSize(800, 600); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("imgui test"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + glutKeyboardUpFunc(keyb_up); + glutSpecialFunc(skeyb); + glutSpecialUpFunc(skeyb_up); + glutMouseFunc(mouse); + glutMotionFunc(motion); + glutPassiveMotionFunc(motion); + + glutMainLoop(); + return 0; +} + +void disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + imtk_begin(); + + if(imtk_button(0, "foobar", 100, 100)) { + printf("clicked button 0\n"); + } + if(imtk_button(1, "xyzzy", 100, 200)) { + printf("clicked button 1\n"); + } + + imtk_end(); + + glutSwapBuffers(); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + imtk_inp_reshape(x, y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glTranslatef(-1, -1, 0); + glScalef(2.0 / x, 2.0 / y, 1.0); +} + +void keyb(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + + default: + break; + } + + imtk_inp_key(key, IMTK_DOWN); +} + +void keyb_up(unsigned char key, int x, int y) +{ + imtk_inp_key(key, IMTK_UP); +} + +void skeyb(int key, int x, int y) +{ + imtk_inp_key(key, IMTK_DOWN); +} + +void skeyb_up(int key, int x, int y) +{ + imtk_inp_key(key, IMTK_UP); +} + +void mouse(int bn, int state, int x, int y) +{ + imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP); +} + +void motion(int x, int y) +{ + imtk_inp_motion(x, y); +}