nasty tearing when I scroll in openGL
When I scroll in my openGL game I get this tearing effect as if I can see the redraw in slo-mo but the FPS never change much. I have only really noticed this today, but I think its always been happening. I am running this on my G4/667 laptop.
You need to enable VBL synch. Someone recently posted some code that should work regardless of the API, a little searching should reveal it.
Edit: Here we go: http://www.idevgames.com/forum/showthread.php?t=6750
Edit: Here we go: http://www.idevgames.com/forum/showthread.php?t=6750
I tried the solution therin and it doesn't seem to work. It had no change on my issue. Both fullscreen and windowed have this problem. I am using SDL + OpenGL in C. Here's my main code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#if defined(__APPLE_CC__)
#include "SDL.h"
#include <OpenGL/OpenGL.h>
#include "SDL_opengl.h"
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include "SDL.h"
#endif
#ifndef PLATFORM_DEP
#include "platform_dep.h"
#endif
#ifndef CORE
#include "core.h"
#endif
#ifndef CAMERA
#include "camera.h"
#endif
#ifndef KEYBOARD
#include "keyboard.h"
#endif
#ifndef PREFS_H
#include "prefs.h"
#endif
#ifndef STARFIELD_H
#include "starfield.h"
#endif
#ifndef TEX_H
#include "tex.h"
#endif
#ifndef SPRITE_H
#include "sprite.h"
#endif
#ifndef LOAD_H
#include "load.h"
#endif
#ifndef CONSOLE_H
#include "console.h"
#endif
#ifndef TARENA_H
#include "tarena.h"
#endif
#ifndef MUSIC_H
#include "music.h"
#endif
#ifndef OBJ_H
#include "obj.h"
#endif
#ifndef LEVEL_H
#include "level.h"
#endif
#define MAX_FPS 1.0f/60.0f
extern int gameSegment;
int gameSegment=1;
int lastFrameTime = 0;
extern float elapsedTime;
float elapsedTime;
sprite textSprite;
float now;
void mainLoop()
{
elapsedTime=0;
while(elapsedTime<=MAX_FPS)
elapsedTime = (float)((float)SDL_GetTicks() - (float)lastFrameTime) / 1000.0f;
lastFrameTime = SDL_GetTicks();
if(consoleOn==1) elapsedTime=0;
//if (KeyboardDown[ prefVal(RIGHT_KEY)]) { camera[2] -= 64.0*elapsedTime; }
runCamera(); //do all the camera work
/*if (gameSegment==4){ //move to camera
glPushMatrix();
glTranslatef(-camera[0], -camera[1], 0.0f);
glPopMatrix();
}*/
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (gameSegment==1){
loadEverything();
}
if (gameSegment==0){
tarena(0);
}
if (gameSegment==2){
levelEditor();
}
console(); //run the console functions
if(showFPS==1)
{
glColor4f(1.0,1.0,0.5,0.5);
char ttbp[1024];
int tempint = 1/elapsedTime;
sprintf(ttbp, "%d FPS",tempint);
drawText(20.0,20.0,ttbp);
glColor4f(1.0,1.0,1.0,1.0);
}
SDL_GL_SwapBuffers( );
if(KeyboardDown[27].isDown==1) quit_game( 0 ); //the quit function
}
void process_events( void )
{
// Our SDL event placeholder
SDL_Event event;
// Grab all events off the cue
while( SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
handle_key_down( &event.key.keysym );
break;
case SDL_KEYUP:
handle_key_up( &event.key.keysym );
break;
case SDL_MOUSEBUTTONDOWN:
handle_mouse_down();
break;
case SDL_MOUSEBUTTONUP:
handle_mouse_up();
break;
case SDL_QUIT:
quit_game( 0 );
break;
default:
break;
}
}
}
void setup_opengl ( int width, int height )
{
glEnable(GL_ALPHA_TEST);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1024, 0, 768, -1024, 1024);
gluPerspective(45, width/height, width, width);
glMatrixMode(GL_MODELVIEW);
}
int main( int argc, char* argv[] )
{
#ifdef __APPLE__
long VBL = 1;
CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &VBL);
#endif
loadPlatformDep();
readPrefs();
// info on the current video settings
const SDL_VideoInfo* info = NULL;
// Dimensions of our window
int width=prefVal(SCREEN_WIDTH);
int height=prefVal(SCREEN_HEIGHT);
int bpp=32;
// lets init SDL's video subsystem
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 )
{
//failed, exit
fprintf( stderr, "video/audio init failed: %s\n", SDL_GetError() );
quit_game( 1 );
}
if( SDL_Init( SDL_INIT_TIMER ) < 0 )
{
//failed, exit
fprintf( stderr, "timer init failed: %s\n", SDL_GetError() );
quit_game( 1 );
}
info = SDL_GetVideoInfo();
if(!info)
{
fprintf( stderr, "Video query failed: %s\n", SDL_GetError() );
quit_game( 1 );
}
bpp = info->vfmt->BitsPerPixel;
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 32);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
if(prefVal(IS_FULLSCREEN)==1){
if (SDL_SetVideoMode(width, height, bpp, SDL_OPENGL | SDL_FULLSCREEN)==0)
{
fprintf( stderr, "Video mode change failed: %s\n", SDL_GetError() );
quit_game( 1 );
}
}else{
if (SDL_SetVideoMode(width, height, bpp, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RLEACCEL)==0)
{
fprintf( stderr, "Video mode change failed: %s\n", SDL_GetError() );
quit_game( 1 );
}
}
loadKeys();
setup_opengl(width,height);
hide_mouse();
while( 1 )
{
process_events( );
mainLoop();
}
return 1;
}
You can't set a GL context parameter without a GL context. Move CGLSetParameter to somewhere *after* you've created a window and a GL context. At the end of your setup_opengl function would be good.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Change scroll text using xcode | imaumac | 3 | 3,716 |
Nov 29, 2008 03:53 PM Last Post: imaumac |
|
| scroll text and images please. | imaumac | 13 | 6,260 |
Nov 6, 2008 07:42 AM Last Post: imaumac |
|
| Visual Tearing | Arjan B | 11 | 5,281 |
Nov 9, 2007 08:47 AM Last Post: Arjan B |
|
| 2d smooth scroll | Luzander | 3 | 3,279 |
Jan 9, 2006 03:42 PM Last Post: kelvin |
|
| Weird Windowed Mode GFX Tearing | nabobnick | 2 | 2,499 |
Jul 25, 2005 06:45 PM Last Post: nabobnick |
|

