1 // Computergraphik I
2 // Prof. Dr. Juergen Doellner
3 // Sommersemester 2001
4 //
5 // Rahmenprogramm fuer Aufgabenzettel 3
6
7 #include "cgapplication.h"
8
9 // Diese Variable enthaelt einen Zeiger auf die aktive Anwendung.
10 static CGApplication* activeApplication_ = 0;
11 static CGApplication::MouseButton activeMouseButton_ = CGApplication::LeftMouseButton;
12
13 CGApplication::CGApplication() {
14 assert(!activeApplication_); // Es ist nur eine aktive Anwendung erlaubt!!!
15 activeApplication_ = this;
16 }
17
18 CGApplication::~CGApplication() {
19 activeApplication_ = 0;
20 }
21
22 // Auf diese Ereignisse wird default-maessig nicht reagiert:
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 // glut-Callback fuer den Zeichenaufruf
29 static void displayFunc() {
30 assert(activeApplication_);
31 activeApplication_->onDraw();
32 }
33
34 // glut-Callback fuer die Aenderung der Fenstergroesse
35 static void reshapeFunc(int newWidth, int newHeight) {
36 assert(activeApplication_);
37 activeApplication_->onSize(newWidth, newHeight);
38 }
39
40 // glut-Callback fuer das Druecken einer Maustaste
41 static void mouseFunc(int button, int state, int x, int y) {
42 assert(activeApplication_);
43 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
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 // glut-Callback fuer das Ziehen bei gedrueckter Maustaste
55 static void motionFunc(int x, int y) {
56 assert(activeApplication_);
57 y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
58 activeApplication_->onMove(activeMouseButton_, x, y);
59 }
60
61 // glut-Callback fuer einen Tastendruck
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 // Initialisiert Glut
74 glutInit(&argc, argv);
75
76 // Fordert ein OpenGL-Fenster im RGB-Format mit oder ohne Double-Buffering an:
77 glutInitDisplayMode(GLUT_RGB | ((buffers & DoubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) );
78
79 // Legt die Fenstergroesse fest:
80 glutInitWindowSize(width, height);
81
82 // Erzeugt das OpenGL-Fenster mit dem entsprechenden Namen:
83 glutCreateWindow(windowTitle);
84
85 // Registriere die oben definierten Callbacks fuer die
86 // entsprechenden glut-Ereignisse:
87 glutDisplayFunc(displayFunc);
88 glutReshapeFunc(reshapeFunc);
89 glutMouseFunc(mouseFunc);
90 glutMotionFunc(motionFunc);
91 glutKeyboardFunc(keyboardFunc);
92
93 // Zeige das OpenGL-Fenster auf dem Bildschirm an
94 glutShowWindow();
95
96 // Rufe die Methode zur Initialisierung von OpenGL auf:
97 onInit();
98
99 // Starte die Verarbeitung der glut-Ereignisse:
100 glutMainLoop();
101 }
102
103 void CGApplication::swapBuffers() {
104 glutSwapBuffers();
105 }
106