Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
680 janba 1
#include <GEL/GL/glew.h>
2
#ifdef __APPLE__
3
#include <GLUT/glut.h>
4
#else
5
#include <GL/glut.h>
6
#endif
7
#include <GEL/GLGraphics/Console.h>
8
#include <GEL/CGLA/Vec3f.h>
671 khor 9
 
10
GLGraphics::Console console;
11
 
12
int width, height;
13
bool console_visible = true;
14
 
15
static void display()
16
{
17
    glClearColor(0.4f, 0.4f, 0.4f, 1.f);
18
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
19
 
20
    if (console_visible)
21
    {
22
        //draw console in upper half of screen
23
        glPushAttrib(GL_VIEWPORT_BIT);
24
        glViewport(0, height-height/2,
25
                   width, height/2);
26
        console.display();
27
        glPopAttrib();
28
    }
29
 
30
    assert(glGetError() == GL_NO_ERROR);
31
    glutSwapBuffers();
32
}
33
 
34
static void reshape(int w, int h)
35
{
36
    width = w;
37
    height = h;
38
 
39
    glViewport( 0, 0, width, height);
40
}
41
 
42
static void keyboard(unsigned char key, int x, int y)
43
{
44
    //toggle console with ESC
45
    if (key == 27)
46
    {
47
        console_visible = !console_visible;
48
        glutPostRedisplay();
49
        return;
50
    }
51
 
52
    if (console_visible)
53
    {
54
        console.keyboard(key);
55
        glutPostRedisplay();
56
        return;
57
    }
58
 
59
    //switch (key)
60
    //{
61
    //}
62
 
63
    glutPostRedisplay();
64
}
65
 
66
static void special(int key, int x, int y)
67
{
68
    if (console_visible)
680 janba 69
        switch (key) {
70
            case GLUT_KEY_UP:
71
                console.key_up();
72
                break;
73
            case GLUT_KEY_DOWN:
74
                console.key_down();
75
                break;
76
            case GLUT_KEY_LEFT:
77
                console.key_left();
78
                break;
79
            case GLUT_KEY_RIGHT:
80
                console.key_right();
81
                break;
82
            case GLUT_KEY_HOME:
83
                console.key_home();
84
                break;
85
            case GLUT_KEY_END:
86
                console.key_end();
87
                break;
88
 
89
            default:
90
                break;
91
        }
671 khor 92
    glutPostRedisplay();
93
}
94
 
95
void vararg_test(const std::vector<std::string>& args)
96
{
97
    console.printf("Number of arguments: %i", int(args.size()));
98
    for (int i=0; i<int(args.size()); ++i)
99
        console.printf("  arg %i: %s", i, args[i].c_str());
100
}
101
 
102
int main( int argc, char *argv[] )
103
{
104
  glutInit(&argc, argv);
105
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
106
  glutInitWindowSize(768, 768);
107
  glutInitWindowPosition(256, 256);
108
  glutCreateWindow(argv[0]);
109
  glutReshapeFunc(reshape);
110
  glutKeyboardFunc(keyboard);
111
  glutSpecialFunc(special);
112
  glutDisplayFunc(display);
113
 
114
  console.printf("GLGraphics console test.");
115
  console.newline();
116
 
117
  //some examples of use:
118
 
119
  console.reg_cmd0("quit",
120
      std::bind(&std::exit, EXIT_SUCCESS), "Exit application.");
121
 
122
  console.reg_cmd1<int>("quit",
123
      std::bind(&std::exit, std::placeholders::_1),
124
      "Exit application with specified exit code.");
125
 
126
  console.reg_cmd0("fullscreen", std::bind(&glutFullScreen), "Switch to fullscreen.");
127
 
128
#if 1
129
  //needs lambda
130
  console.reg_cmd0("window_pos", [&]{
131
      console.printf("window_pos = %i %i", glutGet(GLUT_WINDOW_X),
132
          glutGet(GLUT_WINDOW_Y));
133
  }, "Show window position.");
134
  console.reg_cmd0("window_size", [&]{
135
      console.printf("window_size = %i %i", glutGet(GLUT_WINDOW_WIDTH),
136
          glutGet(GLUT_WINDOW_HEIGHT));
137
  }, "Show window position.");
138
#endif
139
 
140
  console.reg_cmd2<int,int>("window_pos", std::bind(&glutPositionWindow,
141
      std::placeholders::_1, std::placeholders::_2),
142
      "Set the window position.");
143
 
144
  console.executef("window_pos %i %i", 384, 256);
145
 
146
  console.reg_cmd2<int,int>("window_size", std::bind(&glutReshapeWindow,
147
      std::placeholders::_1, std::placeholders::_2),
148
      "Set the window size.");
149
 
150
  console.reg_cmdN("vararg_test", vararg_test, "Test of variable number of arguments.");
151
 
152
  using namespace GLGraphics;
153
 
154
  Console::variable<int> test_int(42);
155
  test_int.reg(console, "test_int", "Some clever help string..");
156
  console.execute("test_int");
157
  console.execute("test_int 167");
158
 
159
  Console::variable<float> test_float(3.14f);
160
  test_float.reg(console, "test_float", "Well..");
161
  console.execute("test_float");
162
  console.execute("test_float 2.71");
163
 
164
  Console::variable<std::string> test_string("Hello, world!");
165
  test_string.reg(console, "test_string", "Well..");
166
  console.execute("test_string");
167
  console.execute("test_string \"some other string with spaces in\"");
168
 
169
  Console::variable<CGLA::Vec3f> test_Vec3f(CGLA::Vec3f(1,2,3));
170
  test_Vec3f.reg(console, "test_Vec3f", "Well..");
171
  console.execute("test_Vec3f");
172
  console.execute("test_Vec3f [ 12 1234 15]");
173
 
174
  CGLA::Vec3f in(0,1,2);
175
  std::stringstream ss;
176
  ss << in;
177
  CGLA::Vec3f out;
178
  ss >> out;
179
  assert(in == out);
180
 
181
  glutMainLoop();
182
  return 0;
183
}
184
 
185
 
186
 
187
 
188