Advertisement
ChocoMan

Game

Apr 15th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include "game.h"
  2.  
  3. Game::Game()
  4. {  
  5.     _isRunning = true;
  6.     _xAngle = _yAngle = _zAngle = 0.0f;
  7.     _newPos = glm::vec3(0, 0, 20);
  8.  
  9.     m_pEffect = NULL;
  10.     m_scale = 0.f;
  11.     m_directionalLight.Color = glm::vec3(1.f, 1.f, 1.f);
  12.     m_directionalLight.AmbientIntensity = 0.25f;
  13.     m_directionalLight.DiffuseIntensity = 0.9f;
  14.     m_directionalLight.Direction = glm::vec3(1.f, 0.f, 0.f);
  15.  
  16.     m_persProjInfo.FOV = 60.f;
  17.     m_persProjInfo.Height = 600.0f;
  18.     m_persProjInfo.Width = 800.0f;
  19.     m_persProjInfo.zNear = 0.1f;
  20.     m_persProjInfo.zFar = 1000.0f;
  21. }
  22.  
  23.  
  24. Game::~Game()
  25. {
  26. }
  27.  
  28.  
  29. int Game::OnExecute()
  30. {
  31.     if (!OnInit())
  32.         return -1;
  33.  
  34.     SDL_Event Event;
  35.  
  36.     while (_isRunning)
  37.     {
  38.         while (SDL_PollEvent(&Event))
  39.         {
  40.             OnEvent(&Event);
  41.         }
  42.  
  43.         OnRender();
  44.         OnLoop();
  45.  
  46.         /* Swap our back buffer to the front */
  47.         SDL_GL_SwapWindow(_screen);
  48.     }
  49.  
  50.     OnCleanup();
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56. void Game::generalSetup()
  57. {
  58.     // Initialize SDL2
  59.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  60.         sdldie("Failed to initial SDL2.");
  61.     else
  62.     {  
  63.         /* Request OpenGL 3.3 */
  64.         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  65.         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  66.         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  67.         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  68.         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); // SDL_GL_CONTEXT_PROFILE_CORE
  69.  
  70.         // Create window
  71.         _screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  72.             800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  73.         SDL_GetRendererInfo(_render, &_renderInfo);
  74.  
  75.         // Create Context
  76.         _mainContext = SDL_GL_CreateContext(_screen);
  77.         //SDL_GL_MakeCurrent(_screen, _mainContext);
  78.  
  79.         // Create Surface
  80.         _surface = SDL_GetWindowSurface(_screen);
  81.         SDL_FillRect(_surface, NULL, SDL_MapRGB(_surface->format, 0xCC, 0x20, 0x20));
  82.         SDL_UpdateWindowSurface(_screen);
  83.  
  84.         /* swap synchronized */
  85.         SDL_GL_SetSwapInterval(1);
  86.  
  87.         // Initialize GLew 1.10
  88.         glewExperimental = GL_TRUE;
  89.         GLenum error = glewInit();
  90.  
  91.         if (error != GLEW_OK)
  92.             printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
  93.         else
  94.         {
  95.             std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
  96.             std::cout << "GLew Version: 1.10.0" << std::endl;
  97.         }
  98.            
  99.         // InitGLs
  100.         glClearColor(1.0, 0.0, 0.0, 1.0);
  101.         glMatrixMode(GL_PROJECTION);
  102.         glLoadIdentity();
  103.  
  104.         glShadeModel(GL_SMOOTH);
  105.         glEnable(GL_BLEND);
  106.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  107.         glEnable(GL_TEXTURE_2D);
  108.         glEnable(GL_NORMALIZE);
  109.         glEnable(GL_CULL_FACE);
  110.         glCullFace(GL_BACK);
  111.        
  112.         glEnable(GL_DEPTH_TEST);
  113.         glDepthFunc(GL_LESS);
  114.  
  115.         glm::vec3 Pos(3.0f, 7.0f, -20.0f);
  116.         //_cam = GCamera();
  117.         //_cam.setPosition(Pos);
  118.  
  119.         //gluPerspective(45.0, 800.0 / 600.0, 1.0, 500.0);
  120.         //gluLookAt(_cam.X(), _cam.Y(), _cam.Z(), 0, 0, 0, 0, 1, 0);
  121.         glMatrixMode(GL_MODELVIEW);
  122.     }
  123. }
  124.  
  125.  
  126. bool Game::OnInit()
  127. {
  128.     generalSetup();
  129.    
  130.     m_pEffect = new Lighting();
  131.  
  132.     if (!m_pEffect->Init())
  133.     {
  134.         std::cout << "Error initializing lighting" << std::endl;
  135.         return false;
  136.     }
  137.    
  138.     m_pEffect->Enable();
  139.     m_pEffect->SetColorTextureUnit(COLOR_TEXTURE_UNIT_INDEX);
  140.     m_pEffect->setDirectionalLight(m_directionalLight);
  141.     m_pEffect->setMatSpecularIntensity(0.0f);
  142.     m_pEffect->setMatSpecularPower(0);
  143.  
  144.     m_model = Model();
  145.  
  146.     if (!m_model.LoadMesh("earth02.dae"))
  147.     {
  148.         std::cout << "ERROR: model unable to laod" << std::endl;
  149.     }
  150.     else
  151.         std::cout << "Model loaded" << std::endl;
  152.  
  153.     return true;
  154. }
  155.  
  156.  
  157. void Game::OnEvent(SDL_Event* Event)
  158. {
  159.     if (Event->type == SDL_QUIT)
  160.         _isRunning = false;
  161. }
  162.  
  163.  
  164. void Game::OnLoop()
  165. {
  166.     displayFPS();  
  167. }
  168.  
  169.  
  170. void Game::OnRender()
  171. {
  172.     m_scale += 0.01f;
  173.    
  174.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  175.     //glViewport(0, 0, 800, 600);
  176.     //glLoadIdentity();
  177.    
  178.     m_pEffect->setEyeWorldPos(glm::vec3(3.0f, 7.0f, -20.0f));
  179.  
  180.     Pipeline p;
  181.     p.SetCamera(glm::vec3(3.0f, 7.0f, -20.0f), glm::vec3(0.0f, -0.2f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  182.     p.Rotate(0.0f, m_scale, 0.0f);
  183.     p.SetPerpsectiveProj(m_persProjInfo); // m_persProjInfo.FOV, m_persProjInfo.Width, m_persProjInfo.Height, m_persProjInfo.zNear, m_persProjInfo.zFar
  184.  
  185.     p.Scale(.1f, .1f, .1f);
  186.     p.WorldPos(0.0f, 0.0f, 10.f);
  187.     m_pEffect->SetWVP(p.GetWVPTrans());
  188.     m_pEffect->SetWorldMatrix(p.GetWorldTrans());
  189.     m_model.Render();
  190. }
  191.  
  192.  
  193. void Game::OnCleanup()
  194. {
  195.     SDL_GL_DeleteContext(_mainContext);
  196.     SDL_DestroyWindow(_screen);
  197.     SDL_Quit();
  198. }
  199.  
  200.  
  201. void Game::displayFPS()
  202. {
  203.     static long lastTime = SDL_GetTicks();
  204.     static long loops = 0;
  205.     static GLfloat fps = 0.0f;
  206.  
  207.     int newTime = SDL_GetTicks();
  208.     loops++;
  209.  
  210.     if (newTime - lastTime > 100)
  211.     {
  212.         float newFPS = (float) loops / float(newTime - lastTime) * 1000.0f;
  213.         fps = (fps + newFPS) / 2.0f;
  214.  
  215.         //std::cout << "FPS: " << (int)fps << std::endl;
  216.         lastTime = newTime;
  217.         loops = 0;
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement