studies/grafik2/Computergrafik-Code5/cgapplication.cpp
⇒
download file
1
2
3
4
5
6
7
8
9 #include "cgapplication.h"
10
11
12 static CGApplication* activeApplication_ = 0;
13
14 CGApplication::CGApplication() {
15 assert(!activeApplication_);
16 activeApplication_ = this;
17 }
18
19 CGApplication::~CGApplication() {
20 activeApplication_ = 0;
21 }
22
23
24 void CGApplication::onInit() { }
25 void CGApplication::onButton(MouseButton button, int x, int y) { }
26 void CGApplication::onMove(int x, int y) { }
27 void CGApplication::onKey(unsigned char key) { }
28 void CGApplication::onIdle() { }
29
30
31 static void displayFunc() {
32 assert(activeApplication_);
33 activeApplication_->onDraw();
34 }
35
36
37 static void reshapeFunc(int newWidth, int newHeight) {
38 assert(activeApplication_);
39 activeApplication_->onSize(newWidth, newHeight);
40 }
41
42
43 static void mouseFunc(int button, int state, int x, int y) {
44 assert(activeApplication_);
45 if(state == GLUT_DOWN) {
46 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y;
47 if(button == GLUT_LEFT_BUTTON) {
48 activeApplication_->onButton(CGApplication::LeftMouseButton, x, y);
49 } else {
50 activeApplication_->onButton(CGApplication::RightMouseButton, x, y);
51 }
52 }
53 }
54
55
56 static void motionFunc(int x, int y) {
57 assert(activeApplication_);
58 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y;
59 activeApplication_->onMove(x, y);
60 }
61
62
63 static void keyboardFunc(unsigned char key, int x, int y) {
64 assert(activeApplication_);
65 activeApplication_->onKey(key);
66 }
67
68
69
70 static void idleFunc() {
71 assert(activeApplication_);
72 activeApplication_->onIdle();
73 }
74 void CGApplication::start(const char* windowTitle, bool doubleBuffering,
75 unsigned long width, unsigned long height) {
76
77 glutInitDisplayMode(GLUT_RGB | (doubleBuffering ? 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 glutIdleFunc(idleFunc);
93
94
95 glutShowWindow();
96
97
98 onInit();
99
100
101 glutMainLoop();
102 }
103
104 void CGApplication::swapBuffers() {
105 glutSwapBuffers();
106 }
107
108