Advertisement

Timeing in C

Started by October 02, 2002 01:42 AM
10 comments, last by mooboy 22 years, 1 month ago
Hi All, I''m Memoo. I''ve writen a small ascii game in C. The game involves 2 ascii char(player1 & 2). 49 char spaces apart. A ramdom number is choosen and a bar of coloured dot extends from one char(player2) to the other(player1). the user (player1) must enter the matching number before the bar reach their char. There are 9 level each faster then before. on the 9th level the coloured bar should take around 1sec to cross the distance, on the 1st level the coloured bar should starts about 10sec. what i would like to know is how to have the timeing the same on different mechines(cpu).
Long Haired Lout.
Sleep(_milliseconds_) so put Sleep(30) within the game loop. If you have a really slow processor, this will have a reverse effect... but there shouldn''t be any worries.

It just makes the processor wait n milliseconds before continuing. Windows Game Programming For Dummies has that trick... and I have to go out and buy Tricks of The WGP Gurus 2nd ed. since I finally have money again YAY!
Advertisement
Sleep? in C?
Help>Command>Index>Sleep>not found
Help>Command>Index>Pause>not found
Help>Command>Index>Wait>not found

i''m currently attempting to write a sleep timer using dos.h _gettime but does anyone have a better way to do it?
and what doesWindows Game Programming have to do with writing pure C code?
Long Haired Lout.
It has everything to do with pure C code, if you happen to be writing a windows-based game. Pure C does not imply DOS. Pure C is available on many operating systems, like windows, unix, dos, etc. If you wanted to get picky about it and say that pure c involves only the use of ansi c libraries, then by using dos.h you''re no longer using pure c.

That said, I shall now try to help answer your question! Timing in DOS isn''t easy. You''re going to *have* to hook the DOS timer interrupt, no way around it. That means you''re going to have to write some assembly code (or something very near it, using dos.h). I used to do lots of DOS coding a few years back, but I don''t have much code handy to paste anymore. Anyways, it involves hooking either Interrupt 8 or Interrupt 1ch.

Hooking Int 1ch is the ''safe'' method... but the system timer in DOS runs 18.2 times a second by default, which might be too slow for you. If you want a smoother timer, then you have to hook Int 8 and set the timer speed manually. It gets pretty advanced pretty fast but, alas, that''s DOS programming.

- Air
This is an excellent question. Unfortunatley I''m not expert, so this may not be the best advice.

Windows has a few timing calls. The one that your probably most interested in is

*** GetTickCount(); ***

To make sure everything is running at the same speed, you''ll say something like:

Every 1 second Player A moves 10 units.

You do this by looking at the number of milliseconds that have passed between the last time you updated the characters position and the current time.

Here''s an example of the theory (that probably doesnt actually work)

long gLastTime;

// NOTE: You''ll have to initialze gLastTime before calling this function for the first time //
void DrawPlayer()
{
long dTime, time;

time = GetTickCount();

dTime = gLastTime - time;

// Increment playerX 5 units for each 100 miliseconds that have passed. //
playerX += (100 / dTime) * 5;

Draw();

gLastTime = time;
}


If I recall correctly there is a little more to it than this, but I don''t have any of my code on hand.

Either way, once you know how to get the system time you can come up with whatever solution works for you. Don''t be afraid to experiment.


Hope this helps,
Will
------------------http://www.nentari.com
If you''re using DOS, try using the clock function, as in:

void sleep( clock_t wait )
{
clock_t end;
end = wait + clock();
while( end > clock() )
;
}

You''ll need to include the time.h header.
Advertisement
MS QuickC, last version(1994 i think),is the complier i''ve been using and on a DOS6.22 mechine no less.
the rest of the programme uses stdio.h and ascci putchar codes for graphics
but as since so few people use such relic''s these day a timer that uses system time is needed.
Thanks for your help all. I don''t know exactly what you mean by ''hooking'' interrupts and my asm skills are unexsitance. The two code examples suggest i''m on the right track, i guess i''ll have to keep exexperimenting

-mooboy
Long Haired Lout.
If you''re coding for DOS you can get an internal clock at the memory address, 0000036Ch ...

Global Variable>>>>>

unsigned int far* clock = (unsigned int far*)0x0000036C; //18.2 ticks a second.

int main() {
unsigned int old;
old = *clock;

etc....

}

I think I picked that up from one of Andre Lamothe''s books...
"I thought Genius lived in bottles..." - Patrick Star
The timer-tick interrupt 1Ch is not a DOS service but is in fact set up by the system BIOS.
Good posts. I didn''t know about the timer address. Thanks.

Will
------------------http://www.nentari.com

This topic is closed to new replies.

Advertisement