glamtk

changeset 0:b04d49e4599c

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Dec 2010 05:22:14 +0200
parents
children dfbd12d1f566
files Makefile src/imtk.c src/imtk.h test.c
diffstat 4 files changed, 313 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Thu Dec 30 05:22:14 2010 +0200
     1.3 @@ -0,0 +1,20 @@
     1.4 +src = $(wildcard *.c src/*.c)
     1.5 +obj = $(src:.c=.o)
     1.6 +bin = test
     1.7 +
     1.8 +CC = gcc
     1.9 +CFLAGS = -pedantic -Wall -g -Isrc
    1.10 +LDFLAGS = $(libgl)
    1.11 +
    1.12 +ifeq ($(shell uname -s), Darwin)
    1.13 +	libgl = -framework OpenGL -framework GLUT
    1.14 +else
    1.15 +	libgl = -lGL -lglut
    1.16 +endif
    1.17 +
    1.18 +$(bin): $(obj)
    1.19 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.20 +
    1.21 +.PHONY: clean
    1.22 +clean:
    1.23 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/imtk.c	Thu Dec 30 05:22:14 2010 +0200
     2.3 @@ -0,0 +1,161 @@
     2.4 +#include <stdio.h>
     2.5 +#include <assert.h>
     2.6 +#ifndef __APPLE__
     2.7 +#include <GL/glut.h>
     2.8 +#else
     2.9 +#include <GLUT/glut.h>
    2.10 +#endif
    2.11 +#include "imtk.h"
    2.12 +
    2.13 +static int scr_width = 1, scr_height = 1;
    2.14 +static int mousex, mousey, mouse_bnmask;
    2.15 +static int active = -1, hot = -1;
    2.16 +
    2.17 +static void set_active(int id);
    2.18 +static int set_hot(int id);
    2.19 +static int hit_test(int x, int y, int w, int h);
    2.20 +
    2.21 +static void draw_button(int id, int x, int y, const char *label, int focused);
    2.22 +static void calc_button_size(const char *label, int *wret, int *hret);
    2.23 +static void draw_string(int x, int y, const char *str);
    2.24 +static int string_size(const char *str);
    2.25 +
    2.26 +void imtk_inp_key(int key, int state)
    2.27 +{
    2.28 +	glutPostRedisplay();
    2.29 +}
    2.30 +
    2.31 +void imtk_inp_mouse(int bn, int state)
    2.32 +{
    2.33 +	if(state == IMTK_DOWN) {
    2.34 +		mouse_bnmask |= 1 << bn;
    2.35 +	} else {
    2.36 +		mouse_bnmask &= ~(1 << bn);
    2.37 +	}
    2.38 +	glutPostRedisplay();
    2.39 +}
    2.40 +
    2.41 +void imtk_inp_motion(int x, int y)
    2.42 +{
    2.43 +	mousex = x;
    2.44 +	mousey = y;
    2.45 +
    2.46 +	glutPostRedisplay();
    2.47 +}
    2.48 +
    2.49 +void imtk_inp_reshape(int x, int y)
    2.50 +{
    2.51 +	scr_width = x;
    2.52 +	scr_height = y;
    2.53 +}
    2.54 +
    2.55 +void imtk_begin(void)
    2.56 +{
    2.57 +	glMatrixMode(GL_PROJECTION);
    2.58 +	glPushMatrix();
    2.59 +	glLoadIdentity();
    2.60 +	glTranslatef(-1, 1, 0);
    2.61 +	glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0);
    2.62 +}
    2.63 +
    2.64 +void imtk_end(void)
    2.65 +{
    2.66 +	glMatrixMode(GL_PROJECTION);
    2.67 +	glPopMatrix();
    2.68 +}
    2.69 +
    2.70 +int imtk_button(int id, const char *label, int x, int y)
    2.71 +{
    2.72 +	int w, h, res = 0;
    2.73 +	int over = 0;
    2.74 +
    2.75 +	assert(id >= 0);
    2.76 +
    2.77 +	calc_button_size(label, &w, &h);
    2.78 +
    2.79 +	if(hit_test(x, y, w, h)) {
    2.80 +		if(set_hot(id)) {
    2.81 +			glutPostRedisplay();
    2.82 +		}
    2.83 +		over = 1;
    2.84 +	}
    2.85 +
    2.86 +
    2.87 +	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
    2.88 +		if(over) {
    2.89 +			set_active(id);
    2.90 +		}
    2.91 +	} else { /* mouse button up */
    2.92 +		if(active == id) {
    2.93 +			set_active(-1);
    2.94 +			if(hot == id && over) {
    2.95 +				res = 1;
    2.96 +			}
    2.97 +		}
    2.98 +	}
    2.99 +
   2.100 +	draw_button(id, x, y, label, over);
   2.101 +	return res;
   2.102 +}
   2.103 +
   2.104 +static void set_active(int id)
   2.105 +{
   2.106 +	active = id;
   2.107 +}
   2.108 +
   2.109 +static int set_hot(int id)
   2.110 +{
   2.111 +	if(active == -1) {
   2.112 +		hot = id;
   2.113 +		return 1;
   2.114 +	}
   2.115 +	return 0;
   2.116 +}
   2.117 +
   2.118 +static int hit_test(int x, int y, int w, int h)
   2.119 +{
   2.120 +	return mousex >= x && mousex < (x + w) &&
   2.121 +		mousey >= y && mousey < (y + h);
   2.122 +}
   2.123 +
   2.124 +static void draw_button(int id, int x, int y, const char *label, int focused)
   2.125 +{
   2.126 +	int width, height;
   2.127 +
   2.128 +	calc_button_size(label, &width, &height);
   2.129 +
   2.130 +	glBegin(GL_QUADS);
   2.131 +	if(focused) {
   2.132 +		glColor3f(0.85, 0.85, 0.85);
   2.133 +	} else {
   2.134 +		glColor3f(0.7, 0.7, 0.7);
   2.135 +	}
   2.136 +	glVertex2f(x, y);
   2.137 +	glVertex2f(x + width, y);
   2.138 +	glVertex2f(x + width, y + height);
   2.139 +	glVertex2f(x, y + height);
   2.140 +	glEnd();
   2.141 +
   2.142 +	glColor3f(0, 0, 0);
   2.143 +	draw_string(x + 20, y + 15, label);
   2.144 +}
   2.145 +
   2.146 +static void calc_button_size(const char *label, int *wret, int *hret)
   2.147 +{
   2.148 +	int strsz = string_size(label);
   2.149 +	if(wret) *wret = strsz + 40;
   2.150 +	if(hret) *hret = 20;
   2.151 +}
   2.152 +
   2.153 +static void draw_string(int x, int y, const char *str)
   2.154 +{
   2.155 +	glRasterPos2i(x, y);
   2.156 +	while(*str) {
   2.157 +		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *str++);
   2.158 +	}
   2.159 +}
   2.160 +
   2.161 +static int string_size(const char *str)
   2.162 +{
   2.163 +	return glutBitmapLength(GLUT_BITMAP_HELVETICA_10, str);
   2.164 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/imtk.h	Thu Dec 30 05:22:14 2010 +0200
     3.3 @@ -0,0 +1,26 @@
     3.4 +#ifndef IMTK_H_
     3.5 +#define IMTK_H_
     3.6 +
     3.7 +/* key/button state enum */
     3.8 +enum {
     3.9 +	IMTK_UP,
    3.10 +	IMTK_DOWN
    3.11 +};
    3.12 +
    3.13 +enum {
    3.14 +	IMTK_LEFT_BUTTON,
    3.15 +	IMTK_MIDDLE_BUTTON,
    3.16 +	IMTK_RIGHT_BUTTON
    3.17 +};
    3.18 +
    3.19 +void imtk_inp_key(int key, int state);
    3.20 +void imtk_inp_mouse(int bn, int state);
    3.21 +void imtk_inp_motion(int x, int y);
    3.22 +void imtk_inp_reshape(int x, int y);
    3.23 +
    3.24 +void imtk_begin(void);
    3.25 +void imtk_end(void);
    3.26 +
    3.27 +int imtk_button(int id, const char *label, int x, int y);
    3.28 +
    3.29 +#endif	/* IMTK_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test.c	Thu Dec 30 05:22:14 2010 +0200
     4.3 @@ -0,0 +1,106 @@
     4.4 +#include <stdio.h>
     4.5 +#include <stdlib.h>
     4.6 +#ifndef __APPLE__
     4.7 +#include <GL/glut.h>
     4.8 +#else
     4.9 +#include <GLUT/glut.h>
    4.10 +#endif
    4.11 +#include "imtk.h"
    4.12 +
    4.13 +void disp(void);
    4.14 +void reshape(int x, int y);
    4.15 +void keyb(unsigned char key, int x, int y);
    4.16 +void keyb_up(unsigned char key, int x, int y);
    4.17 +void skeyb(int key, int x, int y);
    4.18 +void skeyb_up(int key, int x, int y);
    4.19 +void mouse(int bn, int state, int x, int y);
    4.20 +void motion(int x, int y);
    4.21 +
    4.22 +
    4.23 +int main(int argc, char **argv)
    4.24 +{
    4.25 +	glutInitWindowSize(800, 600);
    4.26 +	glutInit(&argc, argv);
    4.27 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    4.28 +	glutCreateWindow("imgui test");
    4.29 +
    4.30 +	glutDisplayFunc(disp);
    4.31 +	glutReshapeFunc(reshape);
    4.32 +	glutKeyboardFunc(keyb);
    4.33 +	glutKeyboardUpFunc(keyb_up);
    4.34 +	glutSpecialFunc(skeyb);
    4.35 +	glutSpecialUpFunc(skeyb_up);
    4.36 +	glutMouseFunc(mouse);
    4.37 +	glutMotionFunc(motion);
    4.38 +	glutPassiveMotionFunc(motion);
    4.39 +
    4.40 +	glutMainLoop();
    4.41 +	return 0;
    4.42 +}
    4.43 +
    4.44 +void disp(void)
    4.45 +{
    4.46 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    4.47 +
    4.48 +	imtk_begin();
    4.49 +
    4.50 +	if(imtk_button(0, "foobar", 100, 100)) {
    4.51 +		printf("clicked button 0\n");
    4.52 +	}
    4.53 +	if(imtk_button(1, "xyzzy", 100, 200)) {
    4.54 +		printf("clicked button 1\n");
    4.55 +	}
    4.56 +
    4.57 +	imtk_end();
    4.58 +
    4.59 +	glutSwapBuffers();
    4.60 +}
    4.61 +
    4.62 +void reshape(int x, int y)
    4.63 +{
    4.64 +	glViewport(0, 0, x, y);
    4.65 +	imtk_inp_reshape(x, y);
    4.66 +
    4.67 +	glMatrixMode(GL_PROJECTION);
    4.68 +	glLoadIdentity();
    4.69 +	glTranslatef(-1, -1, 0);
    4.70 +	glScalef(2.0 / x, 2.0 / y, 1.0);
    4.71 +}
    4.72 +
    4.73 +void keyb(unsigned char key, int x, int y)
    4.74 +{
    4.75 +	switch(key) {
    4.76 +	case 27:
    4.77 +		exit(0);
    4.78 +
    4.79 +	default:
    4.80 +		break;
    4.81 +	}
    4.82 +
    4.83 +	imtk_inp_key(key, IMTK_DOWN);
    4.84 +}
    4.85 +
    4.86 +void keyb_up(unsigned char key, int x, int y)
    4.87 +{
    4.88 +	imtk_inp_key(key, IMTK_UP);
    4.89 +}
    4.90 +
    4.91 +void skeyb(int key, int x, int y)
    4.92 +{
    4.93 +	imtk_inp_key(key, IMTK_DOWN);
    4.94 +}
    4.95 +
    4.96 +void skeyb_up(int key, int x, int y)
    4.97 +{
    4.98 +	imtk_inp_key(key, IMTK_UP);
    4.99 +}
   4.100 +
   4.101 +void mouse(int bn, int state, int x, int y)
   4.102 +{
   4.103 +	imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
   4.104 +}
   4.105 +
   4.106 +void motion(int x, int y)
   4.107 +{
   4.108 +	imtk_inp_motion(x, y);
   4.109 +}