/*
	Asteroids
    Copyright (C) 2006  Darragh Coy.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*##############################################################################

Module name: GLDraw.cpp
Purpose:     Handles drawing of the game: background, stars, asteroids etc...

##############################################################################*/

#include "GLDraw.h"

/*******************************************************************************
Private module prototypes: Not for outside use!
*******************************************************************************/

void GLDraw_DrawInit();         // Draw setup for the frame                                   

/*******************************************************************************
Variables
*******************************************************************************/

   // Constants
   
   const float CLEAR_COLOR[] = { 0.0 , 0.0 , 0.0 };   // Color for clearing the screen

/*******************************************************************************

Name:       GLDraw_Redraw
Purpose:    Redraws everything in the game 
Parameters: Pointer to the gamedata
Returns:    None

*******************************************************************************/

void GLDraw_Redraw ( GameData_t * gamedata )
{
     // Setup OpenGL for this frame: make all the settings we need to start
     // drawing in 2D
     
     GLDraw_DrawInit();
     
     // Draw the stars
     
     Stars_Draw();
     
     // Draw the asteroids
     
     Asteroid_Draw(gamedata);
     
     // Draw all bullets
     
     Bullet_Draw(gamedata);
     
     // Draw the player's ship: only if player not dead
     
     if ( gamedata->player.playerAlive ) Player_Draw();
     
     // Draw all particles
     
     Particle_Draw();
     
     // Set drawing color for text
     
     glColor3f(1.0f,1.0f,0.6f);
     
     // Draw the player's name
     
     GLDrawString_Draw(20,20,"Name: ");
     GLDrawString_Draw(90,20,playerName);     
     
     // Draw the player's current score
     
     char stringbuf[128];
     
     GLDrawString_Draw(320,20,"Score: ");
	 sprintf_s(stringbuf,32,"%u",gamedata->player.score);
     GLDrawString_Draw(410,20,stringbuf);
     
     // Draw the high score
     
     GLDrawString_Draw(560,20,"High: ");
	 sprintf_s(stringbuf,32,"%u",gamedata->player.scoreHighest);
     GLDrawString_Draw(630,20,stringbuf);  
     
     // Print the name of the player who got the best score of all time
     
     GLDrawString_Draw(20,580,"Record Holder: ");
     GLDrawString_Draw(220,580,highScoreName);
     
     // Print out the record score
     
     GLDrawString_Draw(420,580,"Record score: "); 
	 sprintf_s(stringbuf,32,"%u",highScore);
     GLDrawString_Draw(610,580,stringbuf);      
     
     // Draw performance info if enabled
     
     if ( showPerformanceInfo )
     {
       GLDrawString_Draw(20  ,520,"Game clocks: ");
	   sprintf_s(stringbuf,32,"%i",clocks_game);
       GLDrawString_Draw(200 ,520,stringbuf);
       GLDrawString_Draw(20  ,540,"Draw clocks: ");
	   sprintf_s(stringbuf,32,"%i",clocks_draw);
       GLDrawString_Draw(200 ,540,stringbuf);
     }
     
     // If game paused then draw paused text
     
     if ( gamePaused )
     {
          GLDrawString_Draw(160,400,"Game Paused. Press 'p' to resume."); 
     } 
     
     // If player entering name then draw enter name text and current name:
     
     if ( enteringName )
     {
          GLDrawString_Draw(60,420,"Enter name (Hit return to finish): ");           
          GLDrawString_Draw(540,420,playerName);           
     }

     // If game over then show game over message
     
     if ( ! gamedata->player.playerAlive )
     {
        GLDrawString_Draw(140,460,"Game Over! Press r to restart again."); 
     }
     
     // Swap the front and back buffers
     
     SwapBuffers( wglGetCurrentDC() );
}

/*******************************************************************************

Name:       GLDraw_DrawInit
Purpose:    Sets up the OpenGL state for drawing this frame
Parameters: None
Returns:    None

*******************************************************************************/

void GLDraw_DrawInit ( void )
{
     // Set current active matrix as the projection matrix
     
     glMatrixMode(GL_PROJECTION);
     
     // Load the identity
     
     glLoadIdentity();
     
     // Load this matrix with the orthographic (2d) transformation.
     // Depth has no effect in orthographic mode...
     
     glOrtho
        (
        0,         // Left  frustrum plane
        WINDOW_X,  // Right frustrum plane
        WINDOW_Y,  // Bottom frustrum plane
        0,         // Top frustrum plane
        0.0,       // Near frustrum plane
        1.0        // Far frustrum plane
        );
        
     // Set the current active matrix as the modelview matrix: this matrix
     // is designed to handle world transformations such as translate, rotate etc..
     // while the projection matrix handles projection...
     
     glMatrixMode(GL_MODELVIEW);
     
     // Load the identity matrix into the modelview matrix. No transforms will
     // be performed on stuff drawn. This is handled manually instead...
     
     glLoadIdentity();
     
     // Set the color for clearing the screen
     
     glClearColor( CLEAR_COLOR[0] , CLEAR_COLOR[1] , CLEAR_COLOR[2] , 0 );
     
     // Clear the screen
     
     glClear(GL_COLOR_BUFFER_BIT);
}
