Lueskelin kirjaa Focus on SDL ja testasin ajastimista tehtyä esimerkkiä.
Siirsin koodit omaan sätökseeni.
Mutta en oikein tajua miten saadaan esimerkiksi ajastimen avulla ulkopuolella eventhandlerin tehtyä ruudunpäivitystä 2 sekunnin ajan jolloin animaatiot toimisivat toisin kuin SDL_Delay funktiolla.
Pikaista vastausta toivoen ja mielellään esimerkkikoodia.
Kertokaa tosta koodista miten toi ajastin toimii.Metabolix hommiin....;)
//focus on sdl
//timer demo
//fosdl7_1.cpp
//23MAY2002
//ernest pazera
//requires static linkage to:
//sdl.lib, sdlmain.lib
//requires dynamic linkage to:
//sdl.dll
//include SDL stuff
#include "sdl.h"
//include ability to exit program
#include <stdlib.h>
//screen dimensions
const int SCREEN_WIDTH=640;
const int SCREEN_HEIGHT=480;
//display surface
SDL_Surface* g_pDisplaySurface = NULL;
//event structure
SDL_Event g_Event;
//rectangle
SDL_Rect g_FillRect;
//timer id
SDL_TimerID g_TimerID;
//global boolean flag
bool g_bFlag=false;
//timer callback
Uint32 FOSDL_TimerCallback(Uint32 interval, void *param);
//main function
int main(int argc, char* argv[])
{
//initialize SDL
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)==-1)
{
//error initializing SDL
//report the error
fprintf(stderr,"Could not initialize SDL!\n");
//end the program
exit(1);
}
else
{
//SDL initialized
//report success
fprintf(stdout,"SDL initialized properly!\n");
//set up to uninitialize SDL at exit
atexit(SDL_Quit);
}
//create windowed environment
g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_ANYFORMAT);
//error check
if (g_pDisplaySurface == NULL)
{
//report error
fprintf(stderr,"Could not set up display surface!\n");
//exit the program
exit(1);
}
//add the timer
g_TimerID=SDL_AddTimer(1000,FOSDL_TimerCallback,NULL);
//repeat forever
for(;;)
{
//wait for an event
if(SDL_PollEvent(&g_Event)==0)
{
//no event
//check for the flag
if(g_bFlag)
{
//set flag to false
g_bFlag=false;
//set up rectangle
g_FillRect.x=g_FillRect.y=0;
g_FillRect.w=SCREEN_WIDTH;
g_FillRect.h=SCREEN_HEIGHT;
SDL_FillRect(g_pDisplaySurface,&g_FillRect,SDL_MapRGB(g_pDisplaySurface->format,rand()%256,rand()%256,rand()%256));
}
//update the screen
SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
}
else
{
//event occurred, check for quit
if(g_Event.type==SDL_QUIT) break;
}
}
//remove the timer
SDL_RemoveTimer(g_TimerID);
//normal termination
fprintf(stdout,"Terminating normally.\n");
//return 0
return(0);
}
//timer callback
Uint32 FOSDL_TimerCallback(Uint32 interval, void *param)
{
//set boolean flag to true
g_bFlag=true;
//return same interval
return(interval)
}
[koodic]int main(void) {
printf("Morjens!");
}Siis tuossa luodaan ajastin, joka kutsuu "callback" funktiota noin sekunnin välein. Itse funktiossa taasen asetetaan g_bflag muuttuja todeksi, joka taasen silmukassa asetetaan epätodeksi. SDL:n omilla tutosivuilla on selitetty toimintaa vielä paremmin, jos vieras kieli taipuu suuhun. Sivu löytyy täältä
Aihe on jo aika vanha, joten et voi enää vastata siihen.