Tearing

Started by
0 comments, last by wildex999 16 years, 5 months ago
Hey all, I'm using SDL and OpenGL together, making a start at a 2D game. I've got a nice textured quad moving around on the screen but a whole lot of tearing occurs when I move it.. I've got a double buffer and I've set swap control so I'm not sure how that is possible.. Here's the code:
Quote:

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/openglut.h>
#include <GL/glext.h>

class Sprite
{
  public:
    int x, y;
    int w, h;
    float r;
    GLuint texture;
};

void Display(Sprite);
void InitScreen();
Sprite LoadSprite(char*, int, int, float);

Sprite smiley;

int main(int argc, char* argv[])
{
  SDL_Init(SDL_INIT_VIDEO);
  SDL_WM_SetCaption("OpenGL test", NULL);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
  SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_FULLSCREEN);
  
  bool keepdoing = true;
  SDL_Event event;
  
  InitScreen();
  
  smiley = LoadSprite("smiley.bmp", 80, 80, 0);
  Uint8* KeyStates;
  
  while(keepdoing)
  {
    while(SDL_PollEvent(&event))
    {
      if(event.type == SDL_QUIT){ keepdoing = false; }
    }
    
    KeyStates = SDL_GetKeyState(NULL);
    
    if(KeyStates[SDLK_UP]){ smiley.y -= 4; }
    if(KeyStates[SDLK_DOWN]){ smiley.y += 4; }
    if(KeyStates[SDLK_LEFT]){ smiley.x -= 4; }
    if(KeyStates[SDLK_RIGHT]){ smiley.x += 4; }
    if(KeyStates[SDLK_r]){ smiley.r += 2; }
    if(KeyStates[SDLK_ESCAPE]){ keepdoing = false; }
    
    glClear(GL_COLOR_BUFFER_BIT);
    
    Display(smiley);
    
    SDL_GL_SwapBuffers();
  }
  
  SDL_Quit();
  
  return 0;
}

void Display(Sprite sprite)
{
  float tX = sprite.w/2;
  float tY = sprite.h/2;
  
  glPushMatrix();
  
  glTranslatef(sprite.x, sprite.y, 0);
  glRotatef(sprite.r, 0, 0, 1);
  
  glBindTexture(GL_TEXTURE_2D, sprite.texture);
  
  glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex2d(-tX, -tY);
    glTexCoord2i(1,0); glVertex2d(tX, -tY);
    glTexCoord2i(1,1); glVertex2d(tX, tY);
    glTexCoord2i(0,1); glVertex2d(-tX, tY);
  glEnd();
  
  glPopMatrix();
}

void InitScreen()
{
  glClearColor(0,0,0,0);
  glViewport(0, 0, 640, 480);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, 640, 480, 0, -100, 100);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glEnable(GL_TEXTURE_2D);
  glDisable(GL_CULL_FACE);
}

Sprite LoadSprite(char* file, int x, int y, float r)
{
  Sprite sprite;
  SDL_Surface* surface = SDL_LoadBMP(file);
  GLuint tex;
  
  glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, surface->w, surface->h , 0, GL_BGR, 
               GL_UNSIGNED_BYTE, surface->pixels);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  sprite.w = surface->w;
  sprite.h = surface->h;
  sprite.texture = tex;
  sprite.x = x;
  sprite.y = y;
  sprite.r = r;
  
  SDL_FreeSurface(surface);
  
  return sprite;
}
Can anyone see why that tearing occurs? Maybe it's my video card? (nVidia GeForce 7300 GT) Or maybe someone has a simple framework that shows how to have smoothly moving textured quads using SDL with OpenGL? Thank you in advance ^^
Advertisement
I have somewhat the same problem, would be nice if someone could answer.
I get a blurry shadow of the last movement when moving a large texture.(ATI card here if it should matter)

Programming=Creating,Your fantasy is the only limit!

This topic is closed to new replies.

Advertisement