//	Creator: Mark Dehus
//	Description: MilkShape 3D ASCII Model loader class
//	Copyright (C) 2004 Mark Dehus

//	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 2
//	of the License, or (at your option) any later version.

//	For more information about this project and gpl see readme.htm

//	TODO:	Figure out a better way to store the index data
//			Fix the problem with quotes in the model data

//	MODIFICATIONS:
//			3/21/2004 Mark Dehus - Cleaned up code,
//				fixed file not found bugs, implemented
//				display lists.

#include "model.h"

int drawModel = 0;
GLfloat moveBody = 0.0;

bool initSDL(int w, int h, int bpp)
{
	// Initalize libsdl and exit if there is a problem
	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
		return false;

	// Clears out SDL on program termination
	atexit(SDL_Quit);

	if (SDL_SetVideoMode(w, h, bpp, SDL_OPENGL) == 0)
		return false;

	return true;
}

bool initGL(int w, int h, int bpp)
{
	// Set our color and buffer settings
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, bpp / 3);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, bpp / 3);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, bpp / 3);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, bpp);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	// Setup inital clear color and clear the color buffer
	glClearColor(0.0, 0.0, 0.0, 0);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	// Create some lights so we can see
	GLfloat light_ambient[]  = 	{0.0, 0.0, 0.0, 1.0};
	GLfloat light_diffuse[]	 =	{1.0, 1.0, 1.0, 1.0};
	GLfloat light_position[] =	{1.0, 1.0, 0.0, 1.0};

	glLightfv	(GL_LIGHT1, GL_AMBIENT,  light_ambient);
	glLightfv	(GL_LIGHT1, GL_DIFFUSE,  light_diffuse);
	glLightfv	(GL_LIGHT1, GL_POSITION, light_position);
	glLightfv	(GL_LIGHT2, GL_AMBIENT,  light_ambient);
	glLightfv	(GL_LIGHT2, GL_DIFFUSE,  light_diffuse);
	glLightfv	(GL_LIGHT2, GL_POSITION, light_position);
	// And god said.. let there be light!
	glEnable	(GL_LIGHT0);
	glEnable	(GL_LIGHT1);
	glEnable	(GL_LIGHT2);
	glEnable	(GL_LIGHTING);

	return true;
}

void resize(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(h<1) h=1;
	gluPerspective(80.0, (double)w/(double)h, 1.0, 200.0);
	glMatrixMode(GL_MODELVIEW);
}

int handleKeyDown(SDL_keysym* keysym)
{
    switch( keysym->sym ) 
	{
		case SDLK_ESCAPE:
			return 0;
			break;
		
		case SDLK_UP:
			if (drawModel < 1)
				drawModel++;
			else
				drawModel = 0;
			return 1;
			break;

		case SDLK_RIGHT:
			moveBody ++;
			break;

		case SDLK_LEFT:
			moveBody --;
			break;

		default:
			return 1;
    }

}

int processEvents()
{
    SDL_Event event;

    while(SDL_PollEvent(&event)) 
	{
        switch(event.type) 
		{
			case SDL_KEYDOWN:
				return handleKeyDown(&event.key.keysym);
				break;

			case SDL_QUIT:
	            return 0;
		        break;
        }
    }

	return 1;
}

int main (int argc, char **argv)
{
	initSDL(1024, 768, 32);
	initGL(1024, 768, 32);
	resize(1024, 768);

	GLuint playerTank = glGenLists(1);
	
	model tank08;
	tank08.load("tank04.twm");

	glNewList(playerTank, GL_COMPILE);

	glEndList();

	GLfloat angle1 = 0.0;
	moveBody = false;

	while (processEvents())
	{
		angle1 += 0.2f;		
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		glTranslatef(0.0, -20.0, -60.0);
		glPushMatrix();
			//glRotatef(10.0, 1.0, 0.0, 0.0);
			//glRotatef(120.0, 0.0, 1.0, 0.0);
			glPushMatrix();
				glTranslatef(moveBody, 0.0, 0.0);
				tank08.render(0);
			glPopMatrix();
			glPushMatrix();
				//glRotatef(angle1, 0.0, 1.0, 0.0);
				glPushMatrix();
					tank08.render(1);
				glPopMatrix();
			glPopMatrix();
		glPopMatrix();
		glPopMatrix();
		SDL_GL_SwapBuffers();
	}

    return 0;
}
