/* graphics.cpp
 */


#include <math.h>
#include "opengl.H"
#include "elevator.H"
#include "floor.H"
#include "events.H"
#include "sched.H"
#include "main.H"
#include "graphics.H"


/* Initialize the graphics system
 */

void 
Graphics::init( int num_elevators, int num_floors, ColourMode colour_mode )
{
    float w_size, d_size;

    if (!draw_graphics)
	return;

    mode = colour_mode;

    person_sep  = 0.2;
    person_width = 0.12;
    person_height = 0.5;

    // Find elevator dimensions to hold elev_capacity persons

    w_size = ceil( sqrt( elev_capacity ) );
    if (w_size * (w_size-1) >= elev_capacity)
	d_size = w_size-1;
    else
	d_size = w_size;

    elev_width  = person_sep * (1 + w_size);
    elev_depth  = person_sep * (1 + d_size);
    elev_height = person_height + 0.2;
    elev_sep    = elev_width;

    floor_depth = 1.4;
    floor_width = num_elevators * (elev_width + elev_sep);

    gl.init( 1.0 + num_elevators * (elev_width + elev_sep), // width
	     1.0 + num_floors * (elev_height * 2), // height
	     0.3, 0.2 );
}


/* Draw everything.
 */

void 
Graphics::draw_all() 
{
    int i;
    
    if (!draw_graphics)
	return;

    gl.clear();

    // Draw the elevators

    for (i=0; i<num_elevators; i++)
	elevators[i].draw( (elev_width + elev_sep) *
			   (i - (float)(num_elevators - 1)/2.0),
			   (elev_height * 2) * (elevators[i].floor_position()
						- (float)(num_floors-1) / 2.0),
			   0 );

    // Draw the floors

    for (i=0; i<num_floors; i++)
	floors[i].draw( 0, (elev_height * 2) *
			(i - (float)(num_floors-1) / 2.0), 0 );

    gl.swap();
}
