Hi there… i’ve some probs with the following code.
SDL_GL_SwapBuffers() doesn’t sync with the display refresh.
Are there any necessary tests that need to be done before setting up
sdl/gl, or did I miss something in my init function?
thanks in advance
–hendrik
OS: win2k
HW: Radeon 7000
DevC++
#include
#include <stdlib.h>
#include <SDL/SDL.h>
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;
SDL_Surface * screen = NULL;
int init_sdl()
{
GLint gl_doublebuf;
gl_doublebuf = SDL_DOUBLEBUF;
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, gl_doublebuf);
screen = SDL_SetVideoMode(640,480,16,SDL_DOUBLEBUF | SDL_OPENGL | SDL_FULLSCREEN);
if(!screen)
{
fprintf(stderr,"failed to setup sdl");
return -1;
}
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glViewport(0, 0, screen->w, screen->h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, 0.0f);
}
void render(int x,int y)
{
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
glVertex2f(x, y);
glVertex2f(x+50, y);
glVertex2f(x+50, y+50);
glVertex2f(x, y+50);
glEnd();
}
int main(int argc, char *argv[])
{
int quit = 0;
int i=0;
SDL_Event event;
Uint8 *keys;
int x,y,xs,ys;
int st,et,fc;
init_sdl();
x = 0; y = 0; xs = 2; ys = 1;
keys = SDL_GetKeyState(&i);
st = SDL_GetTicks();
fc=0;
while(!quit)
{
while(SDL_PollEvent(&event))
{
if(keys[SDLK_ESCAPE])
{
quit = 1;
break;
}
}
x=x+xs;
y=y+ys;
if(x<0 || x>640-50) xs=-xs;
if(y<0 || y>480-50) ys=-ys;
render(x,y);
SDL_GL_SwapBuffers();
fc++;
}
et = (SDL_GetTicks() - st)/1000;
fprintf(stdout, "fps: %.2f | time: %d | frames: %d\n", (float)((float)fc/(float)et), et, fc);
return 0;
}