/*
	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:   Stars.cpp
Purpose:  Star drawing, movement and initialisation

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

#include "Stars.h"

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

   void Stars_CheckOffscreen();                          

/*******************************************************************************
Variables
*******************************************************************************/
   
   // Array of syar structurs
   
   Star_t stars[STAR_NUM_STARS];

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

Name:       Star_Init()
Purpose:    Initialises stars with random positions and intensities
Parameters: none
Returns:    none

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

void Stars_Init ( void )
{
     // Initialise all stars
     
     for ( int i = 0; i < STAR_NUM_STARS ; i++ )
     {
         // Set position in random place inside window
         
         stars[i].x = float( rand() % WINDOW_X );
         stars[i].y = float( rand() % WINDOW_Y );
         
         // Get random float from 1.0 to 0.0
         
         float r = (float)( rand() % 0xFFFF ) / (65535.0f);
         
         // Use this random float to decide on brightness. It is a percentage
         // between the minimum and maximum brightness
         
         stars[i].brightness = STAR_MIN_BRIGHTNESS * ( 1.0f - r );
         stars[i].brightness = STAR_MAX_BRIGHTNESS * r;
     }
     
}

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

Name:       Stars_Shift( float x , float y)
Purpose:    Shifts the stars by the translation amount. Called when the player is
            moving
Parameters: Amount to shift in x and y directions
Returns:    none

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

void Stars_Shift ( float x , float y )
{
     // Ponter to current star
     
     Star_t * pStar = stars;   
     
     // Move all stars by translation amount
     
     for ( int i = 0 ; i < STAR_NUM_STARS ; i++ )
     {  
        // Move this star    
     
        pStar->x += x;
        pStar->y += y; 
        
        // Move onto the next star
        
        pStar++;   
     }
     
     // Check if the stars are offscreen
     
     Stars_CheckOffscreen();
}

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

Name:       Stars_CheckOffscreen()
Purpose:    Checks if any of the stars are off the side of the screen and moves
            them back onto the other side if they are
Parameters: none
Returns:    none

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

void Stars_CheckOffscreen ( void )
{
     // Ponter to current star
     
     Star_t * pStar = stars;   
     
     // Move all stars NE by scroll speed amount
     
     for ( int i = 0 ; i < STAR_NUM_STARS ; i++ )
     {  
         // Make sure pStar isn't fully offscreen. If so then make reappear
         // at the other side of the screen
         
         if ( pStar->x < - STAR_RADIUS )
         {
              // Off left side of screen. move back to right
              
              pStar->x = WINDOW_X + STAR_RADIUS;
         }         
         else if ( pStar->x > WINDOW_X + STAR_RADIUS )
         {
              // Off right side of screen. move back to left
              
              pStar->x = - STAR_RADIUS;
         }         
         
         if ( pStar->y < - STAR_RADIUS )
         {
              // Off top side of screen. move back to bottom
              
              pStar->y = WINDOW_Y + STAR_RADIUS;
         }   
         else if ( pStar->y > WINDOW_Y + STAR_RADIUS )
         {
              // Off bottom side of the screen- move back to the top
              
              pStar->y = - STAR_RADIUS;
         }
         
         // Move pointer onto next pStar
         
         pStar++;        
     }
}

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

Name:       Star_Draw()
Purpose:    Draws all of the stars
Parameters: none
Returns:    none

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

void Stars_Draw ( void )
{
     // Set point size to star radius
     
     glPointSize(STAR_RADIUS);
     
     // Draw each star
      
     glBegin(GL_POINTS);

     for ( int i = 0 ; i < STAR_NUM_STARS ; i++ )
     {         
        // Star Color
            
            glColor3f
              (
              stars[i].brightness,   // r
              stars[i].brightness,   // g
              stars[i].brightness    // b 
              );
              
         // Star Vertex
            
         glVertex2f( stars[i].x , stars[i].y );
     }
     
     //End drawing
     
     glEnd();
     
}    
