follow my heart...
分类: C/C++
2006-11-24 22:21:18
SDL时间操作示例[转载]
//类似于在win32下使用SetTimer一样,但是在sdl中的addTimer用起来麻烦一点,几句关键代//码SDL_TimerID timeID=SDL_AddTimer(100,displaySprite,NULL);
//Uint32 displaySprite(Uint32 interval, void *param)
//也就是说回调函数中必须要参数,否则出错.
[WF-Cpptraining] SDL: Time
Wuzzeb wuzzeb at yahoo.com
Thu Apr 12 13:18:07 PDT 2001
Previous message: [WF-Cpptraining] SDL: Previous assignment?
Next message: [WF-Cpptraining] Beginners - Arrays
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--------------------------------------------------------------------------------
SDL has a few functions for dealing with time. The
manual pages are self explanitory, so I will just
touch on them with some examples.
First, the SDL_INIT_TIMER subsection must be
initilized somehow. (passed to SDL_Init or
SDL_InitSubSystem)
Functions:
- void SDL_Wait(Uint32 ms) will wait the specified
number of milliseconds.
- Uint32 SDL_GetTicks() will return the number of
milliseconds since SDL was initilized. This value
wraps if the program runs for ~49 days :)
This is the function I use because it allows input to
be handled between display updates. Think about
tetris. You can move and rotate blocks multiple times
between each move down. If SDL_Wait was used, all the
input would only be handled when a block moved down.
An example of this function
Uint32 count = SDL_GetTicks();
while (playing) {
// check/parse input
if (count + 80 > SDL_GetTicks()) {
count = SDL_GetTicks();
// move block down
}
}
moves a block down every 80 milliseconds.
This could also be accomplished with SDL_Timers.
First, the program sets up a timer with a function to
call and the interval between calls. The function
used to initilize a timer is SDL_AddTimer
- SDL_TimerID SDL_AddTimer(Uint32 interval,
SDL_NewTimerCallback callback, void *param);
The return value is a unique handle to this timer.
interval is the number of milliseconds between
function calls. The lowest resolution for calls is
10ms, so an interval of 27ms will run about every
30ms.
SDL_NewTimerCallback is declared as a pointer to a
function
typedef Uint32 (*SDL_NewTimerCallback)(Uint32
interval, void *param);
which is a function that returns a Uint32, and has a
Uint32 and void * as paramaters. This callback
function gets passed the interval for this timer and
the pointer specified on the SDL_AddTimer function
call. This allows a single function to be used in
multiple timers. This callback function returns the
interval for the timer, so it could be changed every
call.
param is the data to be passed to the callback
function.
- SDL_bool SDL_RemoveTimer(SDL_TimerID id) will delete
the timer, with the paramter being a handle for a
timer and returns a boolian value indicating success.
This example just shows timer functions and outputs
stuff to standard output. (Next lesson might look at
displaying text...) You might want to make the
intervals even bigger so the output is not so fast.
Notice that I use the interval to determin the
rectagle, but in our tetris example, I would probably
throw a rectangle and a SDL_Surface * into a structure
and pass in a pointer to that structure. Thus, the
callback function could update the screen while also
incrementing the value.
I have one callback function that recieves a pointer
to a SDL_Rect structure. I first have to cast it to a
SDL_Rect * from a void *. To exit the program, press
Control-C. Since I am not opeining a screen, I can't
switch focus to the SDL program rather than the
command line, and thus all keypresses would be
recieved with cin >> or scanf() or getc() or whatever
instead of through the event loop.
------------------------------
#i nclude
#i nclude
#define RED_INTERVAL 400
#define BLUE_INTERVAL 1000
SDL_TimerID red_timer, blue_timer;
SDL_Rect red, blue;
Uint32 callback(Uint32 interval, void *param)
{
SDL_Rect *image = (SDL_Rect *) param;
image->x += 1;
image->y += 3;
if (interval == RED_INTERVAL)
cout << "Red";
else
cout << "Blue";
cout << " Timer Update: x = " << image->x << " y = "
<< image->y << endl;
return interval;
}
int main()
{
SDL_Event event;
bool playing = true;
SDL_Init(SDL_INIT_TIMER);
blue.x = blue.y = 0;
blue.h = blue.w = 10;
red.x = red.y = 0;
red.h = red.w = 32;
red_timer = SDL_AddTimer(RED_INTERVAL, callback,
(void *)(&red));
blue_timer = SDL_AddTimer(BLUE_INTERVAL, callback,
(void *)(&blue));
while (playing) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
playing = false;
break;
}
}
}
SDL_RemoveTimer(red_timer);
SDL_RemoveTimer(blue_timer);
SDL_Quit();
}
__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
--------------------------------------------------------------------------------
Previous message: [WF-Cpptraining] SDL: Previous assignment?
Next message: [WF-Cpptraining] Beginners - Arrays
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--------------------------------------------------------------------------------
More information about the cpptraining mailing list