Subversion Repositories gelsvn

Rev

Rev 561 | Details | Compare with Previous | Last modification | View Log | RSS feed

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