How do I use a faster timer for simple 2D animations?

How do I use a faster timer for simple 2D animations?

Post by Federico Hansse » Sun, 06 Aug 2000 04:00:00



I am new to programming in windows and I am coding a really simple game
where a 300 x 250 bitmap moves to one side and you try to avoid
obstacles. Its extremely simple but I cant speed up the animation. I
tried slicing the image with CopyRect and also redrawing it entirely by
using an array with all the data every time, but the timer ticks (55mS)
seem to be very slow for getting an acceptable rate.
Is there any way of speeding it up with another kind of timer? I have
John Mianos How-To, where he mentions using a multimedia timer....
Please  any help...
 
 
 

How do I use a faster timer for simple 2D animations?

Post by Davi » Sun, 06 Aug 2000 04:00:00


Have you tried sending your processing to the OnIdle event routine?
Look up OnIdle in help files for specific details on how to implement
this in code.  Part of the processing may include something like:

#include <time.h>
void __fastcall TForm1::MyIdleHandler(TObject *Sender, bool &Done)
{
   double timeclick;

   timeclick = (float)clock()/CLOCKS_PER_SEC;
   Done = false;  //Set this so that OnIdle is continuously recalled...

   //relocate your bitmap based off of the timeclick variable...
   .
   .
   .

Quote:}

Good luck, and happy programming.
David

> I am new to programming in windows and I am coding a really simple game
> where a 300 x 250 bitmap moves to one side and you try to avoid
> obstacles. Its extremely simple but I cant speed up the animation. I
> tried slicing the image with CopyRect and also redrawing it entirely by
> using an array with all the data every time, but the timer ticks (55mS)
> seem to be very slow for getting an acceptable rate.
> Is there any way of speeding it up with another kind of timer? I have
> John Mianos How-To, where he mentions using a multimedia timer....
> Please  any help...


 
 
 

How do I use a faster timer for simple 2D animations?

Post by Michael F?tsc » Sun, 06 Aug 2000 04:00:00



> but the timer ticks (55mS)
> seem to be very slow for getting an acceptable rate.

Using timeGetTime() would be straight-forward. It gives you the time in
milliseconds (1/1000 secs). You'd have to include <mmsystem.h>, and animate
your sprite like this:

DWORD OldTime = timeGetTime();
...
TimePassed = timeGetTime() - OldTime;
Sprite.Position.x += TimePassed * PixelPerMs;
...

There are also other ways, but this one is very easy to implement (and gives
you good results).
You might also wish to go to www.mr-gamemaker.com. They have a tutorial on
using timers for games.

M.F.

 
 
 

How do I use a faster timer for simple 2D animations?

Post by Scott Heima » Sun, 06 Aug 2000 04:00:00


Look at the timeSetEvent function in the Windows API help.

This API function will call a static function at a periodic rate.  The
minimum time between events is 1 millisecond.

--
Regards,
Scott
http://www.gnt.net/~heiman
heiman at gnt dot net (don't ya just hate spam)

 
 
 

How do I use a faster timer for simple 2D animations?

Post by Federico Hansse » Sun, 06 Aug 2000 04:00:00


Michael,
Thank you for the answer, based on your explanation I will make the rate
"system-independent". Nevertheless, I cant speed up the animation. Could it be
a limitation on the speed of the windows GUI? I have even tried to put the code
in a  for{ } loop (so no timer would be necessary, just to test the maximum
possible speed of the animation) and it is still slow...
Is there another method for having faster graphics?

Thanks in advance
Federico



> > but the timer ticks (55mS)
> > seem to be very slow for getting an acceptable rate.

> Using timeGetTime() would be straight-forward. It gives you the time in
> milliseconds (1/1000 secs). You'd have to include <mmsystem.h>, and animate
> your sprite like this:

> DWORD OldTime = timeGetTime();
> ...
> TimePassed = timeGetTime() - OldTime;
> Sprite.Position.x += TimePassed * PixelPerMs;
> ...

> There are also other ways, but this one is very easy to implement (and gives
> you good results).
> You might also wish to go to www.mr-gamemaker.com. They have a tutorial on
> using timers for games.

> M.F.

 
 
 

How do I use a faster timer for simple 2D animations?

Post by Evan Da » Sun, 06 Aug 2000 04:00:00



> I am new to programming in windows and I am coding a really simple game
> where a 300 x 250 bitmap moves to one side and you try to avoid
> obstacles. Its extremely simple but I cant speed up the animation. I
> tried slicing the image with CopyRect and also redrawing it entirely by
> using an array with all the data every time, but the timer ticks (55mS)
> seem to be very slow for getting an acceptable rate.
> Is there any way of speeding it up with another kind of timer? I have
> John Mianos How-To, where he mentions using a multimedia timer....
> Please  any help...

Another response mentioned timeGetTime(), but newer systems include a
"Performance" timer.  Look up QueryPerformanceFrequency() and
QueryPerformanceCounter() (from the Windows SDK).  The Performance
Counter is system dependent, but by using the above functions you'll be
able to get a consistent timer on any machine that supports it.

-Evan

 
 
 

How do I use a faster timer for simple 2D animations?

Post by Michael F?tsc » Mon, 07 Aug 2000 04:00:00



> Is there another method for having faster graphics?

You could benefit a lot from using DirectDraw. Even in its simplest form (DX
3 on NT systems), it supports hardware-accelerated blits (CopyRects). As you
can imagine, this will be a lot faster than using the CPU to copy bitmaps.
DirectDraw is quite straight-forward to initialize for simply blitting
(especially when using VCL Forms). See the DDraw samples that come in the
BCB Examples folder.

Regards,
M.F.