Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The headers
- #include "SDL.h"
- #include "SDL_opengl.h"
- //Screen attributes
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- //Event handler
- SDL_Event event;
- //The timer
- class Timer
- {
- private:
- //The clock time when the timer started
- int startTicks;
- //The ticks stored when the timer was paused
- int pausedTicks;
- //The timer status
- bool paused;
- bool started;
- public:
- //Initializes variables
- Timer();
- //The various clock actions
- void start();
- void stop();
- void pause();
- void unpause();
- //Gets the timer's time
- int get_ticks();
- //Checks the status of the timer
- bool is_started();
- bool is_paused();
- };
- bool init_GL()
- {
- //Set clear color
- glClearColor(0, 0, 0, 0);
- //Set projection
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
- //Initialize modelview matrix
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_TEXTURE_2D);
- glLoadIdentity();
- //If there was any errors
- if (glGetError() != GL_NO_ERROR)
- {
- return false;
- }
- //If everything initialized
- return true;
- }
- bool init()
- {
- //Initialize SDL
- if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
- {
- return false;
- }
- //Create Window
- if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL) == NULL)
- {
- return false;
- }
- //Initialize OpenGL
- if (init_GL() == false)
- {
- return false;
- }
- //Set caption
- SDL_WM_SetCaption("OpenGL Test", NULL);
- return true;
- }
- Timer::Timer()
- {
- //Initialize the variables
- startTicks = 0;
- }
- void Timer::start()
- {
- //Get the current clock time
- startTicks = SDL_GetTicks();
- }
- int Timer::get_ticks()
- {
- //Return the current time minus the start time
- return SDL_GetTicks() - startTicks;
- }
- int main (int argc, char *argv[])
- {
- //Quit flag
- bool quit = false;
- //Initialize
- if (init() == false)
- {
- return 1;
- }
- //The frame rate regulator
- Timer fps;
- //Wait for user exit
- while (quit == false)
- {
- //Start the frame timer
- fps.start();
- //While there are events to handle
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_QUIT)
- {
- quit = true;
- }
- }
- //Clear the screen
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //Render single quad on screen
- glLoadIdentity();
- glBegin(GL_QUADS);
- glVertex3f(0, 0, 0);
- glVertex3f(SCREEN_WIDTH, 0, 0);
- glVertex3f(SCREEN_WIDTH, SCREEN_HEIGHT, 0);
- glVertex3f(0, SCREEN_HEIGHT, 0);
- glEnd();
- //Update screen
- SDL_GL_SwapBuffers();
- //Cap the frame rate
- if (fps.get_ticks() < 1000 / 60)
- {
- SDL_Delay((1000 / 60) - fps.get_ticks());
- }
- }
- //Clean up
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement