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
: CGApplication::RightMouseButton);
46 activeApplication_->onButton(activeMouseButton_,
47 ((state == GLUT_DOWN)
48 ? CGApplication::MouseButtonDown
: CGApplication::MouseButtonUp),
49 x, y);
50 }
51
52
53 static void motionFunc(int x, int y) {
54 assert(activeApplication_);
55 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y;
56 activeApplication_->onMove(activeMouseButton_, x, y);
57 }
58
59
60 static void keyboardFunc(unsigned char key, int x, int y) {
61 assert(activeApplication_);
62 activeApplication_->onKey(key);
63 }
64
65 void CGApplication::start(int argc, char* argv[],
66 const char* windowTitle,
67 unsigned int buffers,
68 unsigned long width, unsigned long height) {
69 assert(buffers & ColorBuffer);
70
71
72 glutInit(&argc, argv);
73
74
75 glutInitDisplayMode(GLUT_RGB | ((buffers & DoubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) );
76
77
78 glutInitWindowSize(width, height);
79
80
81 glutCreateWindow(windowTitle);
82
83
84
85 glutDisplayFunc(displayFunc);
86 glutReshapeFunc(reshapeFunc);
87 glutMouseFunc(mouseFunc);
88 glutMotionFunc(motionFunc);
89 glutKeyboardFunc(keyboardFunc);
90
91
92 glutShowWindow();
93
94
95 onInit();
96
97
98 glutMainLoop();
99 }
100
101 void CGApplication::swapBuffers() {
102 glutSwapBuffers();
103 }
104
105