studies/grafik/Computergrafik-Code3/cgapplication.cpp
⇒
download file
1
2
3
4
5
6
7 #include "cgapplication.h"
8
9
10 static CGApplication* activeApplication_ = 0;
11 static CGApplication::MouseButton activeMouseButton_ = CGApplication::LeftMouseButton;
12
13 CGApplication::CGApplication() {
14 assert(!activeApplication_);
15 activeApplication_ = this;
16 }
17
18 CGApplication::~CGApplication() {
19 activeApplication_ = 0;
20 }
21
22
23 void CGApplication::onInit() { }
24 void CGApplication::onButton(MouseButton button, MouseButtonEvent event, int x, int y) { }
25 void CGApplication::onMove(MouseButton buttoin, int x, int y) { }
26 void CGApplication::onKey(unsigned char key) { }
27
28
29 static void displayFunc() {
30 assert(activeApplication_);
31 activeApplication_->onDraw();
32 }
33
34
35 static void reshapeFunc(int newWidth, int newHeight) {
36 assert(activeApplication_);
37 activeApplication_->onSize(newWidth, newHeight);
38 }
39
40
41 static void mouseFunc(int button, int state, int x, int y) {
42 assert(activeApplication_);
43 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y;
44 activeMouseButton_ = ((button == GLUT_LEFT_BUTTON)
45 ? CGApplication::LeftMouseButton
46 : CGApplication::RightMouseButton);
47 activeApplication_->onButton(activeMouseButton_,
48 ((state == GLUT_DOWN)
49 ? CGApplication::MouseButtonDown
50 : CGApplication::MouseButtonUp),
51 x, y);
52 }
53
54
55 static void motionFunc(int x, int y) {
56 assert(activeApplication_);
57 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y;
58 activeApplication_->onMove(activeMouseButton_, x, y);
59 }
60
61
62 static void keyboardFunc(unsigned char key, int x, int y) {
63 assert(activeApplication_);
64 activeApplication_->onKey(key);
65 }
66
67 void CGApplication::start(int argc, char* argv[],
68 const char* windowTitle,
69 unsigned int buffers,
70 unsigned long width, unsigned long height) {
71 assert(buffers & ColorBuffer);
72
73
74 glutInit(&argc, argv);
75
76
77 glutInitDisplayMode(GLUT_RGB | ((buffers & DoubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) );
78
79
80 glutInitWindowSize(width, height);
81
82
83 glutCreateWindow(windowTitle);
84
85
86
87 glutDisplayFunc(displayFunc);
88 glutReshapeFunc(reshapeFunc);
89 glutMouseFunc(mouseFunc);
90 glutMotionFunc(motionFunc);
91 glutKeyboardFunc(keyboardFunc);
92
93
94 glutShowWindow();
95
96
97 onInit();
98
99
100 glutMainLoop();
101 }
102
103 void CGApplication::swapBuffers() {
104 glutSwapBuffers();
105 }
106