imtk

view test.c @ 16:1cf212375db3

textbox cursor handling
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 19 Apr 2011 08:24:39 +0300
parents df2bc9406561
children 737e9047d9c9
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 glMatrixMode(GL_MODELVIEW);
65 glLoadIdentity();
66 glMatrixMode(GL_PROJECTION);
67 glPushMatrix();
68 glLoadIdentity();
70 glPushAttrib(GL_ENABLE_BIT);
71 glDisable(GL_LIGHTING);
72 glDisable(GL_DEPTH_TEST);
74 glBegin(GL_QUADS);
75 glColor3f(0.3, 0.4, 0.8);
76 glVertex2f(-1, -1);
77 glVertex2f(1, -1);
78 glColor3f(0.7, 0.3, 0.2);
79 glVertex2f(1, 1);
80 glVertex2f(-1, 1);
81 glEnd();
83 glPopAttrib();
84 glPopMatrix();
86 glMatrixMode(GL_MODELVIEW);
87 glLoadIdentity();
88 glTranslatef(0, 0, -4);
89 glRotatef(25, 1, 0, 0);
90 glRotatef(angle, 0, 1, 0);
92 switch(objsel) {
93 case 0:
94 glFrontFace(GL_CW);
95 glutSolidTeapot(1.0);
96 glFrontFace(GL_CCW);
97 break;
99 case 1:
100 glutSolidTorus(0.5, 1, 12, 24);
101 break;
103 case 2:
104 glutSolidSphere(1.0, 24, 12);
105 break;
107 default:
108 break;
109 }
112 gui();
114 glutSwapBuffers();
115 assert(glGetError() == GL_NO_ERROR);
116 }
118 void gui(void)
119 {
120 static int bnshow;
121 static char textbuf[256];
122 static char textbuf2[256];
123 static float val;
124 static int prev_sel = 0;
125 char *itemlist;
127 imtk_begin();
129 /*glBegin(GL_QUADS);
130 glColor3f(0.6, 0.6, 0.6);
131 glVertex2f(0, 0);
132 glVertex2f(200, 0);
133 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
134 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
135 glEnd();*/
137 if(imtk_button(IMUID, "red", 30, 50)) {
138 float color[] = {1, 0.4, 0.3, 1};
139 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
140 glutPostRedisplay();
141 }
142 if(imtk_button(IMUID, "blue", 30, 80)) {
143 float color[] = {0.3, 0.4, 1, 1};
144 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
145 glutPostRedisplay();
146 }
148 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
149 if((objsel = imtk_listbox(IMUID, itemlist, prev_sel, 30, 120)) != prev_sel) {
150 prev_sel = objsel;
151 glutPostRedisplay();
152 }
153 imtk_free_list(itemlist);
155 imtk_textbox(IMUID, textbuf, sizeof textbuf, 30, 200);
156 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, 30, 250);
158 if((bnshow = imtk_checkbox(IMUID, "show hidden button", 30, 300, bnshow))) {
159 if(imtk_button(IMUID, "yellow", 50, 340)) {
160 float color[] = {0.8, 0.75, 0.3, 1};
161 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
162 glutPostRedisplay();
163 }
164 }
166 val = imtk_slider(IMUID, angle, 0.0, 360.0, 30, 390);
167 if(val != angle) {
168 angle = val;
169 glutPostRedisplay();
170 }
172 imtk_progress(IMUID, val / 360.0, 30, 420);
174 imtk_label("alpha:", 24, 473);
175 imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, 60, 470));
177 if(imtk_button(IMUID, "Quit", 30, 500)) {
178 exit(0);
179 }
181 imtk_end();
182 }
184 void reshape(int x, int y)
185 {
186 xsz = x;
187 ysz = y;
189 glViewport(0, 0, x, y);
190 imtk_set_viewport(x, y);
192 glMatrixMode(GL_PROJECTION);
193 glLoadIdentity();
194 gluPerspective(45.0, (float)xsz / (float)ysz, 1.0, 1000.0);
195 }
197 void keyb(unsigned char key, int x, int y)
198 {
199 switch(key) {
200 case 27:
201 exit(0);
203 default:
204 break;
205 }
207 imtk_inp_key(key, IMTK_DOWN);
208 }
210 void keyb_up(unsigned char key, int x, int y)
211 {
212 imtk_inp_key(key, IMTK_UP);
213 }
215 void skeyb(int key, int x, int y)
216 {
217 imtk_inp_key(key | 0x100, IMTK_DOWN);
218 }
220 void skeyb_up(int key, int x, int y)
221 {
222 imtk_inp_key(key | 0x100, IMTK_UP);
223 }
225 void mouse(int bn, int state, int x, int y)
226 {
227 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
228 }
230 void motion(int x, int y)
231 {
232 imtk_inp_motion(x, y);
233 }