Timer Problams...

Working on a SwinGame and want to share your progress? Post up a link to your game and let everyone know.

Timer Problams...

Postby Miles on Mon Apr 28, 2008 8:07 pm

I have been trying for the last couple of days to get a simple timer to work in my c# project....
the swingame documentation has been fairly useless and all my attempts to figure it out myself have failed miserably.
any help on this would be MUCH appresiated.

if someone would be so kind as to post some code on how i would setup a timer and maybe have it write the framerate or something to the screen im sure i could figure it out from there.

cheers.
Member of Team : "Not Yet Named"
Working on a "Not Yet Named" Top Down Something or other....
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Re: Timer Problams...

Postby mitch on Mon Apr 28, 2008 8:25 pm

If you want to draw a framerate, the API provides that for you.

You just have to call SwinGame.Text.DrawFramerate() (Giving it the correct variables).

Starting a timer is a little bit more involved, but you do it like this.

Code: Select all
SwinGame.Timer _timeSinceLastCall = new SwinGame.Timer();
_timeSinceLastCall = SwinGame.Core.CreateTimer();


To get values.
Code: Select all
SwinGame.Core.GetTimerTicks(_timeSinceLastCall);


To restart it.
Code: Select all
SwinGame.Core.StopTimer(_timeSinceLastCall);
SwinGame.Core.StartTimer(_timeSinceLastCall);


Thats when you are using a timer that you want to manage.

It seems to work for me. If it doesn't let me know and I'll post another message.

Hope it helps :P
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Timer Problams...

Postby Miles on Mon Apr 28, 2008 8:49 pm

so, say i want to have an event occur every "tick" of the timer....

Pseudo code something like this.

When My Timer Ticks,
{
Msgbox("Success!");
}


how would i go about that?

thanks for the help,
Member of Team : "Not Yet Named"
Working on a "Not Yet Named" Top Down Something or other....
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Re: Timer Problams...

Postby mitch on Mon Apr 28, 2008 8:54 pm

What did you want to do with it? Because calling a event for every tick can get interesting. (This is in the milliseconds, remember. So thats 1000 calls a second...)

You could do something like checking it each loop and calling that event for whatever the delay was. But that in itself introduces a bit of delay... but if you really want to:

Code: Select all
uint ticks = SwinGame.Core.GetTimerTicks(_timer);
for(uint i = 1; i < ticks; i++) {

System.Console.WriteLine("Success!");

}


But, again: This could cause delays. That's how I would do it :)
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Timer Problams...

Postby Miles on Mon Apr 28, 2008 9:13 pm

Seems to work sorta..

drops from 140fps to 1 or 2 in about 10 secconds.

On the right track tho :mrgreen:
Member of Team : "Not Yet Named"
Working on a "Not Yet Named" Top Down Something or other....
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Re: Timer Problams...

Postby mitch on Mon Apr 28, 2008 9:17 pm

Miles wrote:Seems to work sorta..

drops from 140fps to 1 or 2 in about 10 secconds.

On the right track tho :mrgreen:


What are you using the timer for? As there may be another way of doing this... doing it this way it will just slow down, and eventually come to a complete stop.
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Timer Problams...

Postby Miles on Mon Apr 28, 2008 9:34 pm

The idea was to use it to time sprite animations, and character movement.

requires a timer that stays at a constant speed.
Member of Team : "Not Yet Named"
Working on a "Not Yet Named" Top Down Something or other....
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Re: Timer Problams...

Postby mitch on Mon Apr 28, 2008 9:38 pm

If you know how many frames you need for it to be correct, you could do something like a "time per frame" idea. Basically, if 400ms passes and you need the frame to update every 100ms, it would move 4 frames to keep up...

Much simpler than going through every tick :)
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Timer Problams...

Postby Miles on Mon Apr 28, 2008 9:41 pm

In theory,.... might be a trick to implement. but il give it a go and see what i can come up with.
thanks for all the help,
Member of Team : "Not Yet Named"
Working on a "Not Yet Named" Top Down Something or other....
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Re: Timer Problams...

Postby mitch on Mon Apr 28, 2008 10:11 pm

BTW: Doing movement updates is something like this.

Code: Select all
float moveX = distancePerSecond / (timeSinceLastFrame / 1000);


That part of it is relaitvely simple :)
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Timer Problems...

Postby acain on Tue Apr 29, 2008 8:24 am

The Timer in SwinGame is different to the Timer in Windows Forms. The Windows Forms timer allows you to register for an event that occurs at set time intervals, where as the SwinGame Timer just records the amount of time that has passed since the timer was created.

The difference in types of timers is based on how they are intended to be used. In Windows forms the program isn't doing much most of the time, and the timer allows it to update things periodically. On the other hand games usually run as fast as they can, updating the game from with a game loop rather than in response to events.

As each game loop updates the game and redraws the screen there is two ways you can approach this. Either you can limit the framerate, which means you now time things in frames, or you limit the updates and time things in milliseconds.

Framerate limit: This is the easiest to implement by far. Call Core.RefreshScreen(65), this will limit the framerate to around 65 frames per second. You now time your movement and animation in these frames, and you know that each time through the game loop is one frame.

Time limits: This is more complex, you need to use a Timer to record the amount of time that has passed since you last updated. You then use this value to update your game's state with all updates being relative to the amount of time that has passed. So if 10ms has passed and you move a unit 20 pixel per 100 ms then you will move that unit 20 / 100 * 10 = 2 pixels. Mitch's code shows the formula for this. The updating of the animation is a little more complicated. For this you need to keep a sum of the amount of time passed and then base the current image from the animation on this.

Hope this helps.
Cheers,

Andrew Cain
----------
Swinburne University of Technology
Lecturer of Software Development in PSD
User avatar
acain
Site Admin
 
Posts: 252
Joined: Wed Dec 05, 2007 2:35 pm
Location: Swinburne University - Hawthorn - Melbourne

Re: Timer Problams...

Postby mitch on Sat May 03, 2008 11:53 am

Also, if you are using a timer to time everything, I came across this problem (So to speak) if you use SwinGame.Timer... basically, since it records how much time has passed since it started, and you have lots of objects that are being drawn and are moving, the time it takes to draw them does have an impact on the time, so later objects in the draw queue are going to move further than an object at the start. Therefore, you would pause the timer before you start drawing the objects so that they move to the same time frame.

So, something like this (Assume that this is in a loop, and all that I am doing is setting the objects new movement speed.:
Code: Select all
SwinGame.Core.StopTimer(_timer);
SwinGame.Core.StartTimer(_timer);

// The move speed.
int moveSpd = 100; // Per second.

// Basically saying that I have 1000 objects to draw and move.
for(int i = 0; i < 1000; i++) {

   // Calculate new movement speed.
   _objArr[i] = moveSpd * (SwinGame.Core.GetTimerTicks(_timer) / 1000);

}


The problem with this is that if I have alot of complex calcluation which takes up 0.5 milliseconds, when I get to the last one, there will be a noticeable speed difference between the first and the last one. So a way of making it work would be to do something like this (I think).

Code: Select all
SwinGame.Core.StopTimer(_timer);
SwinGame.Core.StartTimer(_timer);

// The move speed.
int moveSpd = 100; // Per second.

// Basically saying that I have 1000 objects to draw and move.
SwinGame.Core.PauseTimer(_timer);
for(int i = 0; i < 1000; i++) {

   // Calculate new movement speed.
   _objArr[i] = moveSpd * (SwinGame.Core.GetTimerTicks(_timer) / 1000);

}
SwinGame.Core.UnpauseTimer(_timer);


That way, the difference is gone and they all move with respect to a constant timer speed, but I'm not sure if this is how you would go about it. But I think it is the right way.
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Timer Problams...

Postby acain on Mon May 05, 2008 9:35 am

You could also try this:

Code: Select all
// The move speed.
int moveSpd = 100; // Per second.
int currentMS = SwinGame.Core.GetTimerTicks(_timer);
int msPast = currentMS - lastMS

// Basically saying that I have 1000 objects to draw and move.
for(int i = 0; i < 1000; i++) {

   // Calculate new movement speed.
   _objArr[i] = moveSpd * (msPast / 1000);

}
lastMS = currentMS;


With this lastMS would need to be stored in the class itself, and initialised to 0 at the start of the program.

Give this a go and let us know if it works ok for you...
Cheers,

Andrew Cain
----------
Swinburne University of Technology
Lecturer of Software Development in PSD
User avatar
acain
Site Admin
 
Posts: 252
Joined: Wed Dec 05, 2007 2:35 pm
Location: Swinburne University - Hawthorn - Melbourne

Re: Timer Problams...

Postby Miles on Mon May 05, 2008 3:56 pm

In the end i just used a loop to run through the frames of the sprite using core.sleep() to time it.
its not the most efficient way of doing it but for the moment it works, once i get the core game mechanic down il go back and optimise it maybe trying to intergrate the timer function.
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Re: Timer Problams...

Postby Miles on Mon May 05, 2008 4:53 pm

any idea how to rotate sprites? ? ?
Member of Team : "Not Yet Named"
Working on a "Not Yet Named" Top Down Something or other....
User avatar
Miles
Journeyman
 
Posts: 11
Joined: Thu Apr 17, 2008 10:43 am
Location: Melbourne

Next

Return to Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron