sources:


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code3/Aufgabe8/cgapplication.cpp
download file

  1 //
  2 // Computergraphik II
  3 // Prof. Dr. Juergen Doellner
  4 // Wintersemester 2001/2002
  5 //
  6 // Rahmenprogramm fuer Aufgabenzettel 3
  7 //
  8 // Autoren: Florian Kirsch (kirsch@hpi.uni-potsdam.de)
  9 // Marc Nienhaus (nienhaus@hpi.uni-potsdam.de)
 10 // Juergen Doellner (doellner@hpi.uni-potsdam.de)
 11 //
 12
 13 #include "cgapplication.h"
 14
 15 // Diese Variable enthaelt einen Zeiger auf die aktive Anwendung.
 16 static CGApplication* activeApplication_ = 0;
 17
 18 CGApplication::CGApplication() {
 19     assert(!activeApplication_); // Es ist nur eine aktive Anwendung erlaubt!!!
 20     activeApplication_ = this;
 21 }
 22
 23 CGApplication::~CGApplication() {
 24     activeApplication_ = 0;
 25 }
 26
 27 // Auf diese Ereignisse wird default-maessig nicht reagiert:
 28 void CGApplication::onInit() { }
 29 void CGApplication::onButton(MouseButton button, int x, int y) { }
 30 void CGApplication::onMove(int x, int y) { }
 31 void CGApplication::onKey(unsigned char key) { }
 32 void CGApplication::onIdle() { }
 33
 34 // glut-Callback fuer den Zeichenaufruf
 35 static void displayFunc() {
 36     assert(activeApplication_);
 37     activeApplication_->onDraw();
 38 }
 39
 40 // glut-Callback fuer die Aenderung der Fenstergroesse
 41 static void reshapeFunc(int newWidth, int newHeight) {
 42     assert(activeApplication_);
 43     activeApplication_->onSize(newWidth, newHeight);
 44 }
 45
 46 // glut-Callback fuer das Druecken einer Maustaste
 47 static void mouseFunc(int button, int state, int x, int y) {
 48     assert(activeApplication_);
 49     if(state == GLUT_DOWN) {
 50         y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 51         if(button == GLUT_LEFT_BUTTON) {
 52             activeApplication_->onButton(CGApplication::LeftMouseButton, x, y);
 53         } else {
 54             activeApplication_->onButton(CGApplication::RightMouseButton, x, y);
 55         }
 56     }
 57 }
 58
 59 // glut-Callback fuer das Ziehen bei gedrueckter Maustaste
 60 static void motionFunc(int x, int y) {
 61     assert(activeApplication_);
 62     y = glutGet(GLenum(GLUT_WINDOW_HEIGHT)) - y; // Koordinaten-Ursprung: links unten!!!
 63     activeApplication_->onMove(x, y);
 64 }
 65
 66 // glut-Callback fuer einen Tastendruck
 67 static void keyboardFunc(unsigned char key, int x, int y) {
 68     assert(activeApplication_);
 69     activeApplication_->onKey(key);
 70 }
 71
 72 static void idleFunc() {
 73     assert(activeApplication_);
 74     activeApplication_->onIdle();
 75 }
 76 void CGApplication::start(const char* windowTitle, bool doubleBuffering,
 77                           unsigned long width, unsigned long height)
{
 78     // Fordert ein OpenGL-Fenster im RGB-Format mit oder ohne Double-Buffering an:
 79     glutInitDisplayMode(GLUT_RGB | (doubleBuffering ? GLUT_DOUBLE : GLUT_SINGLE));
 80
 81     // Legt die Fenstergroesse fest:
 82     glutInitWindowSize(width, height);
 83
 84     // Erzeugt das OpenGL-Fenster mit dem entsprechenden Namen:
 85     hMainWindow = glutCreateWindow(windowTitle);
 86
 87     // Registriere die oben definierten Callbacks fuer die
 88     // entsprechenden glut-Ereignisse:
 89     glutDisplayFunc(displayFunc);
 90     glutReshapeFunc(reshapeFunc);
 91     glutMouseFunc(mouseFunc);
 92     glutMotionFunc(motionFunc);
 93     glutKeyboardFunc(keyboardFunc);
 94     glutIdleFunc(idleFunc);
 95
 96     // Zeige das OpenGL-Fenster auf dem Bildschirm an
 97     glutShowWindow();
 98
 99     // Rufe die Methode zur Initialisierung von OpenGL auf:
100     onInit();
101
102     // Starte die Verarbeitung der glut-Ereignisse:
103     glutMainLoop();
104 }
105
106 void CGApplication::swapBuffers() {
107     glutSwapBuffers();
108 }
109
110