#include "BaseClass/Base.h"
#include "Defines.h"
Base GAME;
bool done = false;
int frame = 0;
float averageFps;
Uint32 pastTicks;
Uint32 s_ticks;
bool createWindow(int width, int height, int bpp, bool fullscreen, const char* title, bool three_dimensional)
{
int w = width, h = height;
bool full = fullscreen;
putenv("SDL_VIDEO_CENTERED=true");
if(SDL_Init( SDL_INIT_EVERYTHING)<0)
{
std::cout<<"Failed to init\n";
return false;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
std::cout<<"Setting video Mode:\n";
SDL_Surface* screen = SDL_SetVideoMode(w, h, 0, SDL_OPENGL|full*SDL_FULLSCREEN);
if(!screen)
{
std::cout<<"Screen creation failed. Aborting\n";
return false;
}
SDL_WM_SetCaption(title, NULL);
if(three_dimensional)
{
double fov = 1000.0, znear = 1.0, zfar = 50000.0;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double fw, fh;
fw = tan((fov/360.0)*pi)*znear;
fh = (float(h)/float(w))*fw;
glFrustum(-fw, fw, -fh, fh, znear, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//fog goes here
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
}else{
glViewport(0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, w, 0,h, 0, 1);
glMatrixMode (GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
}
SDL_ShowCursor(0);
//fog test
/*GLfloat density = 0.05;
GLfloat fogColor[4] = {0.3,0.0,0.0,1.0};
glEnable(GL_FOG);
glFogi(GL_FOG_MODE,GL_EXP2);
glFogfv(GL_FOG_COLOR,fogColor);
glFogf(GL_FOG_DENSITY,density);
glHint(GL_FOG_HINT,GL_NICEST);*/
return true;
}
void ExitNicely(void)
{
done = true;
}
void BeginPrint(void)
{
std::cout<<"Comp3Project::log file "<< build <<"\n";
std::cout<<"built : " << __DATE__ << " " << __TIME__ << "\n";
//std::cout<<"opengl vendor : " << glGetString (GL_VENDOR)<<"\n";
//std::cout<<"opengl renderer : " << glGetString (GL_RENDERER)<<"\n";
//std::cout<<"opengl version : " << glGetString (GL_VERSION)<<"\n\n";
//std::cout<<"screen open: GL extensions: " << glGetString (GL_EXTENSIONS)<<"\n";
}
void ChangeTitle(void)
{
Uint32 currentTicks = SDL_GetTicks();
Uint32 diff = currentTicks-pastTicks;
pastTicks = currentTicks;
float fps=1000/float(diff);
averageFps += fps;
averageFps = averageFps/2;
std::stringstream cap;
cap<<"Comp3 Project; built: "<<__DATE__<<" "<<__TIME__<<" FPS:"<<fps;
SDL_WM_SetCaption(cap.str().c_str(),NULL);
}
void limitFps(void)
{
for(Uint32 i = 0;i<=16;i=SDL_GetTicks()-s_ticks){}
s_ticks=SDL_GetTicks();
}
void dumpFrame(void)
{
/*std::stringstream ss;
ss<<"Screenshots\\"<<"build "<<build<<" at "<<__DATE__<<" frame "<<frame<<".bmp";
SOIL_save_screenshot
(
ss.str().c_str(),
SOIL_SAVE_TYPE_BMP,
0, 0, w_width, w_height
);*/
}
int main ( int argc, char** argv )
{
atexit(SDL_Quit); //Register the exit even so that SDL cleans up correctly
createWindow(w_width,w_height,32,false,"tests", false); //Create the window, with (width, height, bpp, windowed or not, the window's title)
BeginPrint();
GAME.Initialise();
GAME.INHANDLER.RegisterExitCallback(&ExitNicely);
GAME.INHANDLER.RegisterSSCallback(&dumpFrame);
//get initial fps;
averageFps = 500;
//game loop
//Create and initialise the while loop variable to false so that we can exit the while loops eaisily
for(int i=0;i<999;i++){} //Pausing for a certain amount of time, to allow the current ticks not to be the past ticks
while (!done) //Start the while loop
{
frame++;
if(frame == 5)
{
//USE FOR WHEN INCREMENTING BUILD NUMBER
//dumpFrame();
}
SDL_Event ev; //Create the SDL_Event object to receive and process keyboard and mouse input
GAME.INHANDLER.PassEventHandler(ev);
std::cout<<"Main Loop\n";
glClearColor(0.09, 0.09, 0.09, 1.0); //Set the clear colour
std::cout<<"Main Loop 2\n";
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //Clear the colour buffer and the depth buffer
std::cout<<"Main Loop 3\n";
glLoadIdentity(); //Load the projection matrix
std::cout<<"Main Loop 4\n";
//RENDER, TRANSLATE AND ROTATE
GAME.Render();
std::cout<<"Main Loop 5\n"; //Calls the example render function
//FINISH RENDERING HERE
SDL_GL_SwapBuffers();
std::cout<<"Main Loop 6\n"; //Swap the buffers and thus display whatever was rendered
ChangeTitle();
std::cout<<"Main Loop 7\n";
limitFps();
} //--------------------
std::cout<<"\nAverage fps: "<<averageFps<<"\n";
printf("Exited cleanly\n"); //Log that there was a clean exit
std::cout.flush();
return 0; //Return 0 to show that nothing went wrong.
}