Subversion Repositories gelsvn

Rev

Rev 561 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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