imtk

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