Jump to content
  • Advertisement
Sign in to follow this  
svnstrk

SDL Lag

This topic is 2562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Im trying to make a game with sdl. but the problem is as the scene gets more complicated (lots of objects), the input is lagging. I dont notice any fps drop, it runs smooth. but the input delays raised as i put more object. I use SDL for osx template from this website http://www.meandmark.com/sdlopenglpart1.html

This is the snipplet for the application related to the loop and event.


#include "GameApp.h"

using namespace std;

void GameApp::setOrthographicProjection() {



// switch to projection mode

glMatrixMode(GL_PROJECTION);



// save previous matrix which contains the

//settings for the perspective projection

glPushMatrix();



// reset matrix

glLoadIdentity();



// set a 2D orthographic projection

gluOrtho2D(0, width, height, 0);



// switch back to modelview mode

glMatrixMode(GL_MODELVIEW);

}




// Constructor

GameApp::GameApp(void)

{



done = false;

//drawContext = NULL;

}




// Destructor

GameApp::~GameApp(void)

{




}







// Initialization functions

void GameApp::InitApp(void)

{



width = 1280;

height = 720;



InitializeSDL();

InstallTimer();



}




void GameApp::InitializeSDL(void)

{



int error;

SDL_Surface* drawContext;



error = SDL_Init(SDL_INIT_EVERYTHING);



// Create a double-buffered draw context

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);



Uint32 flags;

flags = SDL_OPENGL;// | SDL_FULLSCREEN;

drawContext = SDL_SetVideoMode(width, height, 0, flags);



Control::leftButton.value = false;

Control::rightButton.value = false;

Control::upButton.value = false;

Control::downButton.value = false;

}




void GameApp::InstallTimer(void)

{

timer = SDL_AddTimer(10, GameLoopTimer, this);

}




Uint32 GameApp::GameLoopTimer(Uint32 interval, void* param)

{

// Create a user event to call the game loop.

SDL_Event event;



event.type = SDL_USEREVENT;

event.user.code = RUN_GAME_LOOP;

event.user.data1 = 0;

event.user.data2 = 0;



SDL_PushEvent(&event);



return interval;

}







// Cleanup functions

void GameApp::Cleanup(void)

{

SDL_bool success;

success = SDL_RemoveTimer(timer);

SDL_Quit();

}







// Event-related functions

void GameApp::EventLoop(void)

{

SDL_Event event;



while((!done) && (SDL_WaitEvent(&event))) {





if(event.type == SDL_MOUSEMOTION)

{

Control::mousePos.x = event.motion.x;//(float)value[0]*1600.0 - 800.0;

Control::mousePos.y = height - event.motion.y;//(float)value[1]*1600.0 - 800.0;

}



if (event.type == SDL_USEREVENT) {

HandleUserEvents(&event);

}



if (event.button.button == SDL_BUTTON_LEFT) {

if( event.type == SDL_MOUSEBUTTONDOWN )

{

Control::mouseLeftButton.value = true;

} else if (event.type == SDL_MOUSEBUTTONUP) {

Control::mouseLeftButton.value = false;

}

}



//If a key was pressed

if( event.type == SDL_KEYDOWN ) {

if ( event.key.keysym.sym == 'a') Control::leftButton = true;

if ( event.key.keysym.sym == 'd') Control::rightButton = true;

if ( event.key.keysym.sym == 'w') Control::upButton = true;

if ( event.key.keysym.sym == 's') Control::downButton = true;

} else if(event.type == SDL_KEYUP) {

if ( event.key.keysym.sym == 'a') Control::leftButton = false;

if ( event.key.keysym.sym == 'd') Control::rightButton = false;

if ( event.key.keysym.sym == 'w') Control::upButton = false;

if ( event.key.keysym.sym == 's') Control::downButton = false;

}

}

}




void GameApp::HandleUserEvents(SDL_Event* event)

{



switch (event->user.code) {

case RUN_GAME_LOOP:

GameLoop();

break;



default:

break;

}

}

// Game related functions

void GameApp::GameLoop(void)

{

Uint32 now = SDL_GetTicks();

float nowf = now*1.0/1000.0;



gm.update(nowf);



SDL_GL_SwapBuffers();

}





any1 notice whats wrong with my code?


Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!