Advertisement
Guest User

OpenGL Test

a guest
May 24th, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. //The headers
  2. #include "SDL.h"
  3. #include "SDL_opengl.h"
  4.  
  5. //Screen attributes
  6. const int SCREEN_WIDTH = 640;
  7. const int SCREEN_HEIGHT = 480;
  8.  
  9. //Event handler
  10. SDL_Event event;
  11.  
  12. //The timer
  13. class Timer
  14. {
  15.     private:
  16.     //The clock time when the timer started
  17.     int startTicks;
  18.  
  19.     //The ticks stored when the timer was paused
  20.     int pausedTicks;
  21.  
  22.     //The timer status
  23.     bool paused;
  24.     bool started;
  25.  
  26.     public:
  27.     //Initializes variables
  28.     Timer();
  29.  
  30.     //The various clock actions
  31.     void start();
  32.     void stop();
  33.     void pause();
  34.     void unpause();
  35.  
  36.     //Gets the timer's time
  37.     int get_ticks();
  38.  
  39.     //Checks the status of the timer
  40.     bool is_started();
  41.     bool is_paused();
  42. };
  43.  
  44. bool init_GL()
  45. {
  46.     //Set clear color
  47.     glClearColor(0, 0, 0, 0);
  48.  
  49.     //Set projection
  50.     glMatrixMode(GL_PROJECTION);
  51.     glLoadIdentity();
  52.     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
  53.  
  54.     //Initialize modelview matrix
  55.     glMatrixMode(GL_MODELVIEW);
  56.     glEnable(GL_TEXTURE_2D);
  57.     glLoadIdentity();
  58.  
  59.     //If there was any errors
  60.     if (glGetError() != GL_NO_ERROR)
  61.     {
  62.         return false;
  63.     }
  64.  
  65.     //If everything initialized
  66.     return true;
  67. }
  68.  
  69. bool init()
  70. {
  71.     //Initialize SDL
  72.     if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
  73.     {
  74.         return false;
  75.     }
  76.  
  77.     //Create Window
  78.     if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL) == NULL)
  79.     {
  80.         return false;
  81.     }
  82.  
  83.     //Initialize OpenGL
  84.     if (init_GL() == false)
  85.     {
  86.         return false;
  87.     }
  88.  
  89.     //Set caption
  90.     SDL_WM_SetCaption("OpenGL Test", NULL);
  91.  
  92.     return true;
  93. }
  94.  
  95. Timer::Timer()
  96. {
  97.     //Initialize the variables
  98.     startTicks = 0;
  99. }
  100.  
  101. void Timer::start()
  102. {
  103.     //Get the current clock time
  104.     startTicks = SDL_GetTicks();
  105. }
  106.  
  107. int Timer::get_ticks()
  108. {
  109.     //Return the current time minus the start time
  110.     return SDL_GetTicks() - startTicks;
  111. }
  112.  
  113. int main (int argc, char *argv[])
  114. {
  115.     //Quit flag
  116.     bool quit = false;
  117.  
  118.     //Initialize
  119.     if (init() == false)
  120.     {
  121.         return 1;
  122.     }
  123.  
  124.     //The frame rate regulator
  125.     Timer fps;
  126.  
  127.     //Wait for user exit
  128.     while (quit == false)
  129.     {
  130.         //Start the frame timer
  131.         fps.start();
  132.  
  133.         //While there are events to handle
  134.         while (SDL_PollEvent(&event))
  135.         {
  136.             if (event.type == SDL_QUIT)
  137.             {
  138.                 quit = true;
  139.             }
  140.         }
  141.  
  142.         //Clear the screen
  143.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  144.  
  145.         //Render single quad on screen
  146.         glLoadIdentity();
  147.         glBegin(GL_QUADS);
  148.             glVertex3f(0, 0, 0);
  149.             glVertex3f(SCREEN_WIDTH, 0, 0);
  150.             glVertex3f(SCREEN_WIDTH, SCREEN_HEIGHT, 0);
  151.             glVertex3f(0, SCREEN_HEIGHT, 0);
  152.         glEnd();
  153.  
  154.         //Update screen
  155.         SDL_GL_SwapBuffers();
  156.  
  157.         //Cap the frame rate
  158.         if (fps.get_ticks() < 1000 / 60)
  159.         {
  160.             SDL_Delay((1000 / 60) - fps.get_ticks());
  161.         }
  162.     }
  163.  
  164.     //Clean up
  165.     SDL_Quit();
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement