imtk

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