Timer Controls

Hi all,

I am trying to create a timer to control the display update etc.

Find below my code but it never calls anything under the event.type
SDL_TIMER_EVENT. If I place the draw code directly under the
switch(event.type) then it runs my display code (when moving the mouse) so I
know that the display does work, but I am confused to why the
SDL_TIMER_EVENT is never called. Have I forgotton something simple when
setting up the timer??

Any help would be appreciated.

I appologise for the poor coding etc, I am still learning :slight_smile:

/*
Main Game State Controller
*/
#include “string.h”
// The SDL include files
#include “SDL.h”
#include “SDL_image.h”
#include “SDL_mixer.h”

// Lua Headers
extern “C” {
#include “lua.h”
#include “lauxlib.h”
#include “lualib.h”
}
//Lua Luna Wrapper
#include “luna.h”
#include “engine.hpp”
#include “general.hpp”
#include “kyra.h”

extern CEngine engine;
#define SDL_TIMER_EVENT ( SDL_USEREVENT + 0 )
const int TIMER_INTERVAL = 80;
int timerInterval = TIMER_INTERVAL;

Uint32 TimerCallback(Uint32 interval)
{
SDL_Event event;
event.type = SDL_TIMER_EVENT;

SDL_PeepEvents( &event, 1, SDL_ADDEVENT, 0 );
return TIMER_INTERVAL;

}
/*
Initialise splash
*/
void initialiseSplashKyra()
{
…code
}

/*
Draw Function.
Draws all things to screen.
*/
void splashKyraDraw()
{
…code
}

/*
Key input handler.
*/
void splashKyraKeyInputHandler(SDL_KeyboardEvent event)
{
…code
}

/*
Main loop function.
*/
void mainSplashKyra()
{

if(engine.getDebug())
{
  fprintf(stdout, "KYRA SPLASH\n");
}   
if ( engine.splashKyraInitialised == 0 )
    initialiseSplashKyra();
Uint32 dt,td,td2,sdlgt;
SDL_Event event;
timerInterval = 80; 
SDL_SetTimer( timerInterval, TimerCallback );
U32 start = SDL_GetTicks();
   
while( SDL_WaitEvent(&event) && engine.running == STATE_SPLASH_KYRA )
{
	SDL_Event nextEvent;
	if (    SDL_PeepEvents( &nextEvent, 1, SDL_PEEKEVENT,

SDL_ALLEVENTS )
&& nextEvent.type == SDL_TIMER_EVENT )
{
if(engine.getDebug())
{
fprintf(stdout, “tock %i\n”,
engine.getFrameRate());
}
continue;
}
if ( event.type == SDL_QUIT )
break;

	switch(event.type)
	{
		case SDL_KEYDOWN:
		{  
		   splashKyraKeyInputHandler(event.key);
              }
              break;
		case SDL_TIMER_EVENT:
		{
		   splashKyraDraw(); 
		   engine.incFrameRate();
		}
		break;       
        } 
}
engine.splashKyraInitialised = 0;
delete engine.splash_kyra;
delete engine.splashKyraVault;

}

If required I can post all actual file if it’ll help

Regards Matthew

Hi all,

I am trying to create a timer to control the display update etc.

Find below my code but it never calls anything under the event.type
SDL_TIMER_EVENT. If I place the draw code directly under the
switch(event.type) then it runs my display code (when moving the mouse) so I
know that the display does work, but I am confused to why the
SDL_TIMER_EVENT is never called. Have I forgotton something simple when
setting up the timer??

Your peek call isn’t removing the message, so the next message is always next.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Thanks,

Its good to see my message appear after a reply :)…now that is fast
service :slight_smile:

I shall look into my peek call…

I also find out I missed something else the SDL_INIT_TIMER when initiating
SDL, sorry I shall go shoot myself for such a silly mistake…helps explains
why the timer didn’t work as well…

Cheers> ----- Original Message -----

From: Sam Lantinga [mailto:slouken@devolution.com]
Sent: 10 February 2004 17:42
To: sdl at libsdl.org
Subject: Re: [SDL] Timer Controls

Hi all,

I am trying to create a timer to control the display update etc.

Find below my code but it never calls anything under the event.type
SDL_TIMER_EVENT. If I place the draw code directly under the
switch(event.type) then it runs my display code (when moving the mouse) so
I
know that the display does work, but I am confused to why the
SDL_TIMER_EVENT is never called. Have I forgotton something simple when
setting up the timer??

Your peek call isn’t removing the message, so the next message is always
next.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl