//--------------------------------------------------------------------------------------------------
// file: tomprogs_opengl_tutorial_02.cpp
// description: Contains the whole code used in the OpenGL Tutorial 02 at www.tomprogs.at
//--------------------------------------------------------------------------------------------------
// Copyright (c) 2008 - 2010, Thomas Geymayer
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
//
//    * Redistributions of source code must retain the above copyright notice, this list of 
//      conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above copyright notice, this list of
//      conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//--------------------------------------------------------------------------------------------------

#include "SDL.h"
#include "SDL_opengl.h"

#include <iostream>
 
#include "application_window.h" // Hier ist die neue Klasse drinnen

using std::cout;    // Wir verwenden std::cout und
using std::endl;    // std::endl für die Konsolenausgaben


// Die neue Polling-Funktion
bool pollEvents()
{
  SDL_Event event;
 
  while( SDL_PollEvent(&event) ) // Nächste Nachricht holen
  {
    switch(event.type)
    {
      case SDL_QUIT:
        return false; // Programm sollte beendet werden
      default:
        break;
    }
  }
 
  return true;
}

int main(int argc, char **argv)
{
  if( SDL_Init( SDL_INIT_VIDEO ) != 0 ) // Initialisieren des SDL Video Subsystems
 
  {
 
    cout << "Die SDL konnte nicht initalisiert werden (" << SDL_GetError() << ")" << endl; 
    return 1;
 
  }
 
  // Ein Fenster mit 800*600 Pixeln bei 32 Bit Farbtiefe anlegen
  TomprogsTutorial_::ApplicationWindow *game_window =
    new TomprogsTutorial_::ApplicationWindow( 800, 600, 32 );
 
  // Den Fenstertitel setzen
  game_window->setCaption( "Tomprogs OpenGL Tutorial 02" );
 
  // Und schließlich das Fenster erzeugen lassen
  game_window->create();
 
  // Die Endlosschleife
  while(true)
  {
    //------------------------- 
    // Nachrichten verarbeiten:
    //-------------------------
   
    if( !pollEvents() ) break;
   
    //----------------------------------
    // Den aktuellen Bildbuffer löschen:
    //----------------------------------
   
    glClearColor( 1.0, 0.0, 0.0, 0.0 ); // Farbe zum Löschen setzen
    glClear( GL_COLOR_BUFFER_BIT );     // Aktuellen Bildbuffer löschen
   
    //--------------------------
    // Die Projektion festlegen:
    //--------------------------
   
    glMatrixMode( GL_PROJECTION ); // Stack für Projektionsmatrix als
                                   // aktiven Matrixstack setzen
    glLoadIdentity();              // Identitätsmatrix auf den Stack laden
   
    // Eine orthogonale Projektionsmatrix zum Stack
    // dazu multiplizieren.
    glOrtho( 0, 800, 600, 0, -1, 1 );
   
    //----------------------
    // Das Dreieck zeichnen:
    //----------------------
   
    glBegin( GL_TRIANGLES ); // Wir wollen ein Dreieck zeichnen
   
      glColor3f(1,0,0);      // Ab jetzt werden alle gezeichneten Punkte rot
      glVertex3f(400,100,0); // Der erste Eckpunkt ist mittig und 100 Pixel
                             // vom oberen Rand entfernt
   
      glColor3f(0,1,0);      // Ab jetzt werden alle gezeichneten Punkte grün
      glVertex3f(750,500,0); // Der zweite Eckpunkt ist 50 Pixel vom rechten
                             // und 100 Pixel vom unteren Rand entfernt
   
      glColor3f(0,0,1);      // Ab jetzt werden alle gezeichneten Punkte blau
      glVertex3f(50,500,0);  // Der dritte Eckpunkt ist 50 Pixel vom linken
                             // und 100 Pixel vom unteren Rand entfernt
   
    glEnd(); // Wir sind fertig mit dem Zeichnen
   
    SDL_GL_SwapBuffers(); // Bildbuffer vertauschen (als Letztes)
  }
 
  SDL_Quit();
 
  return 0;
}


