imtk

view test.c @ 2:3d661dd17af3

- initial textbox implementation added - made the test program slightly more interesting
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 Dec 2010 01:54:53 +0200
parents dfbd12d1f566
children 038e5577d527
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;
23 int main(int argc, char **argv)
24 {
25 float lpos[] = {-1, 1, 1, 0};
26 float white[] = {1, 1, 1, 1};
27 float color[] = {0.9, 0.8, 0.73, 1};
29 glutInitWindowSize(800, 600);
30 glutInit(&argc, argv);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
32 glutCreateWindow("imgui test");
34 glutDisplayFunc(disp);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyb);
37 glutKeyboardUpFunc(keyb_up);
38 glutSpecialFunc(skeyb);
39 glutSpecialUpFunc(skeyb_up);
40 glutMouseFunc(mouse);
41 glutMotionFunc(motion);
42 glutPassiveMotionFunc(motion);
44 glEnable(GL_DEPTH_TEST);
45 glEnable(GL_CULL_FACE);
46 glEnable(GL_LIGHTING);
47 glEnable(GL_LIGHT0);
48 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
50 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
51 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
52 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
54 glutMainLoop();
55 return 0;
56 }
58 void disp(void)
59 {
60 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62 glViewport(200, 0, xsz - 200, ysz);
64 glMatrixMode(GL_PROJECTION);
65 glLoadIdentity();
66 gluPerspective(45.0, (float)(xsz - 200) / (float)ysz, 1.0, 1000.0);
68 glMatrixMode(GL_MODELVIEW);
69 glLoadIdentity();
70 glTranslatef(0, 0, -8);
71 glRotatef(25, 1, 0, 0);
73 glFrontFace(GL_CW);
74 glutSolidTeapot(1.0);
75 glFrontFace(GL_CCW);
78 glViewport(0, 0, 200, ysz);
79 imtk_inp_reshape(200, ysz);
81 gui();
83 glutSwapBuffers();
84 assert(glGetError() == GL_NO_ERROR);
85 }
87 void gui(void)
88 {
89 static int bnshow;
90 static char textbuf[256];
91 static char textbuf2[256];
93 imtk_begin();
95 glBegin(GL_QUADS);
96 glColor3f(0.6, 0.6, 0.6);
97 glVertex2f(0, 0);
98 glVertex2f(200, 0);
99 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
100 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
101 glEnd();
103 if(imtk_button(IMUID, "red", 30, 50)) {
104 float color[] = {1, 0.4, 0.3, 1};
105 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
106 glutPostRedisplay();
107 }
108 if(imtk_button(IMUID, "blue", 30, 100)) {
109 float color[] = {0.3, 0.4, 1, 1};
110 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
111 glutPostRedisplay();
112 }
114 imtk_textbox(IMUID, textbuf, sizeof textbuf, 30, 200);
115 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, 30, 250);
117 if((bnshow = imtk_checkbox(IMUID, "show hidden button", 30, 300, bnshow))) {
118 if(imtk_button(IMUID, "yellow", 50, 340)) {
119 float color[] = {0.8, 0.75, 0.3, 1};
120 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
121 glutPostRedisplay();
122 }
123 }
125 if(imtk_button(IMUID, "Quit", 30, 500)) {
126 exit(0);
127 }
129 imtk_end();
130 }
132 void reshape(int x, int y)
133 {
134 xsz = x;
135 ysz = y;
137 glViewport(0, 0, x, y);
138 }
140 void keyb(unsigned char key, int x, int y)
141 {
142 switch(key) {
143 case 27:
144 exit(0);
146 default:
147 break;
148 }
150 imtk_inp_key(key, IMTK_DOWN);
151 }
153 void keyb_up(unsigned char key, int x, int y)
154 {
155 imtk_inp_key(key, IMTK_UP);
156 }
158 void skeyb(int key, int x, int y)
159 {
160 imtk_inp_key(key, IMTK_DOWN);
161 }
163 void skeyb_up(int key, int x, int y)
164 {
165 imtk_inp_key(key, IMTK_UP);
166 }
168 void mouse(int bn, int state, int x, int y)
169 {
170 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
171 }
173 void motion(int x, int y)
174 {
175 imtk_inp_motion(x, y);
176 }