This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Main.cpp

By: a guest on Jan 21st, 2011  |  syntax: C++  |  size: 5.85 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "BaseClass/Base.h"
  2.  
  3. #include "Defines.h"
  4.  
  5. Base GAME;
  6. bool done = false;
  7. int frame = 0;
  8. float averageFps;
  9. Uint32 pastTicks;
  10. Uint32 s_ticks;
  11. bool createWindow(int width, int height, int bpp, bool fullscreen, const char* title, bool three_dimensional)
  12. {
  13.     int w = width, h = height;
  14.     bool full = fullscreen;
  15.  
  16.     putenv("SDL_VIDEO_CENTERED=true");
  17.  
  18.     if(SDL_Init( SDL_INIT_EVERYTHING)<0)
  19.     {
  20.         std::cout<<"Failed to init\n";
  21.         return false;
  22.     }
  23.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  24.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  25.     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  26.     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  27.     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  28.     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  29.     std::cout<<"Setting video Mode:\n";
  30.     SDL_Surface* screen = SDL_SetVideoMode(w, h, 0, SDL_OPENGL|full*SDL_FULLSCREEN);
  31.     if(!screen)
  32.     {
  33.         std::cout<<"Screen creation failed. Aborting\n";
  34.         return false;
  35.     }
  36.     SDL_WM_SetCaption(title, NULL);
  37.  
  38.     if(three_dimensional)
  39.     {
  40.         double fov = 1000.0, znear = 1.0, zfar = 50000.0;
  41.         glViewport(0, 0, w, h);
  42.  
  43.         glMatrixMode(GL_PROJECTION);
  44.         glLoadIdentity();
  45.  
  46.         double fw, fh;
  47.  
  48.         fw = tan((fov/360.0)*pi)*znear;
  49.         fh = (float(h)/float(w))*fw;
  50.  
  51.         glFrustum(-fw, fw, -fh, fh, znear, zfar);
  52.  
  53.         glMatrixMode(GL_MODELVIEW);
  54.         glLoadIdentity();
  55.  
  56.         glEnable(GL_DEPTH_TEST);
  57.         glDepthFunc(GL_LEQUAL);
  58.  
  59.         //fog goes here
  60.  
  61.         glEnable( GL_BLEND );
  62.         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  63.     }else{
  64.         glViewport(0, 0, w, h);
  65.         glMatrixMode (GL_PROJECTION);
  66.         glLoadIdentity ();
  67.         glOrtho (0, w, 0,h, 0, 1);
  68.         glMatrixMode (GL_MODELVIEW);
  69.         glDisable(GL_DEPTH_TEST);
  70.         glEnable( GL_BLEND );
  71.         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  72.     }
  73.     SDL_ShowCursor(0);
  74.  
  75.     //fog test
  76.     /*GLfloat density = 0.05;
  77.     GLfloat fogColor[4] = {0.3,0.0,0.0,1.0};
  78.     glEnable(GL_FOG);
  79.     glFogi(GL_FOG_MODE,GL_EXP2);
  80.     glFogfv(GL_FOG_COLOR,fogColor);
  81.     glFogf(GL_FOG_DENSITY,density);
  82.     glHint(GL_FOG_HINT,GL_NICEST);*/
  83.  
  84.  
  85.     return true;
  86. }
  87. void ExitNicely(void)
  88. {
  89.     done = true;
  90. }
  91. void BeginPrint(void)
  92. {
  93.     std::cout<<"Comp3Project::log file "<< build <<"\n";
  94.     std::cout<<"built           : " << __DATE__ << " " << __TIME__ << "\n";
  95.     //std::cout<<"opengl vendor   : " << glGetString (GL_VENDOR)<<"\n";
  96.     //std::cout<<"opengl renderer : " << glGetString (GL_RENDERER)<<"\n";
  97.     //std::cout<<"opengl version  : " << glGetString (GL_VERSION)<<"\n\n";
  98.     //std::cout<<"screen open: GL extensions: " << glGetString (GL_EXTENSIONS)<<"\n";
  99. }
  100. void ChangeTitle(void)
  101. {
  102.     Uint32 currentTicks = SDL_GetTicks();
  103.     Uint32 diff = currentTicks-pastTicks;
  104.     pastTicks = currentTicks;
  105.     float fps=1000/float(diff);
  106.     averageFps += fps;
  107.     averageFps = averageFps/2;
  108.     std::stringstream cap;
  109.     cap<<"Comp3 Project; built: "<<__DATE__<<" "<<__TIME__<<" FPS:"<<fps;
  110.     SDL_WM_SetCaption(cap.str().c_str(),NULL);
  111. }
  112. void limitFps(void)
  113. {
  114.     for(Uint32 i = 0;i<=16;i=SDL_GetTicks()-s_ticks){}
  115.     s_ticks=SDL_GetTicks();
  116. }
  117. void dumpFrame(void)
  118. {
  119.      /*std::stringstream ss;
  120.         ss<<"Screenshots\\"<<"build "<<build<<" at "<<__DATE__<<" frame "<<frame<<".bmp";
  121.         SOIL_save_screenshot
  122.         (
  123.             ss.str().c_str(),
  124.             SOIL_SAVE_TYPE_BMP,
  125.             0, 0, w_width, w_height
  126.         );*/
  127. }
  128. int main ( int argc, char** argv )
  129. {
  130.     atexit(SDL_Quit);                                               //Register the exit even so that SDL cleans up correctly
  131.     createWindow(w_width,w_height,32,false,"tests", false);              //Create the window, with (width, height, bpp, windowed or not, the window's title)
  132.     BeginPrint();
  133.     GAME.Initialise();
  134.     GAME.INHANDLER.RegisterExitCallback(&ExitNicely);
  135.     GAME.INHANDLER.RegisterSSCallback(&dumpFrame);
  136.     //get initial fps;
  137.     averageFps =  500;
  138.     //game loop
  139.     //Create and initialise the while loop variable to false so that we can exit the while loops eaisily
  140.     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
  141.     while (!done)                                                   //Start the while loop
  142.     {
  143.         frame++;
  144.         if(frame == 5)
  145.         {
  146.             //USE FOR WHEN INCREMENTING BUILD NUMBER
  147.             //dumpFrame();
  148.         }
  149.         SDL_Event ev;                                               //Create the SDL_Event object to receive and process keyboard and mouse input
  150.         GAME.INHANDLER.PassEventHandler(ev);
  151.         std::cout<<"Main Loop\n";
  152.         glClearColor(0.09, 0.09, 0.09, 1.0);                           //Set the clear colour
  153.         std::cout<<"Main Loop 2\n";
  154.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);           //Clear the colour buffer and the depth buffer
  155.          std::cout<<"Main Loop 3\n";
  156.         glLoadIdentity();                                           //Load the projection matrix
  157.          std::cout<<"Main Loop 4\n";
  158.         //RENDER, TRANSLATE AND ROTATE
  159.         GAME.Render();
  160.          std::cout<<"Main Loop 5\n";                                //Calls the example render function
  161.         //FINISH RENDERING HERE
  162.         SDL_GL_SwapBuffers();
  163.          std::cout<<"Main Loop 6\n";                                   //Swap the buffers and thus display whatever was rendered
  164.         ChangeTitle();
  165.          std::cout<<"Main Loop 7\n";
  166.         limitFps();
  167.     }                                                               //--------------------
  168.     std::cout<<"\nAverage fps: "<<averageFps<<"\n";
  169.     printf("Exited cleanly\n");                                     //Log that there was a clean exit
  170.     std::cout.flush();
  171.     return 0;                                                       //Return 0 to show that nothing went wrong.
  172. }
clone this paste RAW Paste Data