imtk

view test.c @ 4:00a4ea4ee6dc

attempt at slightly more complex widgets
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Mar 2011 09:25:27 +0200
parents 038e5577d527
children 6d35e6c7b2ca
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #ifndef __APPLE__
5 #include <GL/glut.h>
6 #else
7 #include <GLUT/glut.h>
8 #endif
9 #include "imtk.h"
11 void disp(void);
12 void gui(void);
13 void reshape(int x, int y);
14 void keyb(unsigned char key, int x, int y);
15 void keyb_up(unsigned char key, int x, int y);
16 void skeyb(int key, int x, int y);
17 void skeyb_up(int key, int x, int y);
18 void mouse(int bn, int state, int x, int y);
19 void motion(int x, int y);
21 int xsz, ysz;
22 float angle;
23 int objsel;
25 int main(int argc, char **argv)
26 {
27 float lpos[] = {-1, 1, 1, 0};
28 float white[] = {1, 1, 1, 1};
29 float color[] = {0.9, 0.8, 0.73, 1};
31 glutInitWindowSize(800, 600);
32 glutInit(&argc, argv);
33 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
34 glutCreateWindow("imgui test");
36 glutDisplayFunc(disp);
37 glutReshapeFunc(reshape);
38 glutKeyboardFunc(keyb);
39 glutKeyboardUpFunc(keyb_up);
40 glutSpecialFunc(skeyb);
41 glutSpecialUpFunc(skeyb_up);
42 glutMouseFunc(mouse);
43 glutMotionFunc(motion);
44 glutPassiveMotionFunc(motion);
46 glEnable(GL_DEPTH_TEST);
47 glEnable(GL_CULL_FACE);
48 glEnable(GL_LIGHTING);
49 glEnable(GL_LIGHT0);
50 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
52 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
53 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
54 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
56 glutMainLoop();
57 return 0;
58 }
60 void disp(void)
61 {
62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64 glViewport(200, 0, xsz - 200, ysz);
66 glMatrixMode(GL_PROJECTION);
67 glLoadIdentity();
68 gluPerspective(45.0, (float)(xsz - 200) / (float)ysz, 1.0, 1000.0);
70 glMatrixMode(GL_MODELVIEW);
71 glLoadIdentity();
72 glTranslatef(0, 0, -8);
73 glRotatef(25, 1, 0, 0);
74 glRotatef(angle, 0, 1, 0);
76 switch(objsel) {
77 case 0:
78 glFrontFace(GL_CW);
79 glutSolidTeapot(1.0);
80 glFrontFace(GL_CCW);
81 break;
83 case 1:
84 glutSolidTorus(0.5, 1, 12, 24);
85 break;
87 case 2:
88 glutSolidSphere(1.0, 24, 12);
89 break;
91 default:
92 break;
93 }
96 glViewport(0, 0, 200, ysz);
97 imtk_inp_reshape(200, ysz);
99 gui();
101 glutSwapBuffers();
102 assert(glGetError() == GL_NO_ERROR);
103 }
105 void gui(void)
106 {
107 static int bnshow;
108 static char textbuf[256];
109 static char textbuf2[256];
110 static float val;
111 static int prev_sel;
112 char *itemlist;
114 imtk_begin();
116 glBegin(GL_QUADS);
117 glColor3f(0.6, 0.6, 0.6);
118 glVertex2f(0, 0);
119 glVertex2f(200, 0);
120 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
121 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
122 glEnd();
124 if(imtk_button(IMUID, "red", 30, 50)) {
125 float color[] = {1, 0.4, 0.3, 1};
126 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
127 glutPostRedisplay();
128 }
129 if(imtk_button(IMUID, "blue", 30, 80)) {
130 float color[] = {0.3, 0.4, 1, 1};
131 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
132 glutPostRedisplay();
133 }
135 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
136 if((objsel = imtk_listbox(IMUID, itemlist, prev_sel, 30, 120)) != prev_sel) {
137 prev_sel = objsel;
138 glutPostRedisplay();
139 }
140 imtk_free_list(itemlist);
142 imtk_textbox(IMUID, textbuf, sizeof textbuf, 30, 200);
143 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, 30, 250);
145 if((bnshow = imtk_checkbox(IMUID, "show hidden button", 30, 300, bnshow))) {
146 if(imtk_button(IMUID, "yellow", 50, 340)) {
147 float color[] = {0.8, 0.75, 0.3, 1};
148 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
149 glutPostRedisplay();
150 }
151 }
153 val = imtk_slider(IMUID, angle, 0.0, 360.0, 30, 390);
154 if(val != angle) {
155 angle = val;
156 glutPostRedisplay();
157 }
159 imtk_progress(IMUID, val / 360.0, 30, 420);
161 if(imtk_button(IMUID, "Quit", 30, 500)) {
162 exit(0);
163 }
165 imtk_end();
166 }
168 void reshape(int x, int y)
169 {
170 xsz = x;
171 ysz = y;
173 glViewport(0, 0, x, y);
174 }
176 void keyb(unsigned char key, int x, int y)
177 {
178 switch(key) {
179 case 27:
180 exit(0);
182 default:
183 break;
184 }
186 imtk_inp_key(key, IMTK_DOWN);
187 }
189 void keyb_up(unsigned char key, int x, int y)
190 {
191 imtk_inp_key(key, IMTK_UP);
192 }
194 void skeyb(int key, int x, int y)
195 {
196 imtk_inp_key(key, IMTK_DOWN);
197 }
199 void skeyb_up(int key, int x, int y)
200 {
201 imtk_inp_key(key, IMTK_UP);
202 }
204 void mouse(int bn, int state, int x, int y)
205 {
206 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
207 }
209 void motion(int x, int y)
210 {
211 imtk_inp_motion(x, y);
212 }