Archived

This topic is now archived and is closed to further replies.

mooboy

Timeing in C

Recommended Posts

mooboy    122
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).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
mooboy    122
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?

Share this post


Link to post
Share on other sites
Air    122
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

Share this post


Link to post
Share on other sites
RPGeezus    216
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

Share this post


Link to post
Share on other sites
Darrell L    122
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.

Share this post


Link to post
Share on other sites
mooboy    122
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

Share this post


Link to post
Share on other sites
Xanth    122
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...

Share this post


Link to post
Share on other sites
Xanth    122
>>>>>>

The timer-tick interrupt 1Ch is not a DOS service but is in fact set up by the system BIOS.

<<<<<<

Sorry I didn''t mean suggest that it was, I just meant if you are programming a DOS program(Real Mode) then you can get to that address. Protected Mode compilers won''t let you =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
This is what i''ve come up with, comments welcome.(My home comp is down so it''s untested). The work is done in the sub-function. a user sets the length at the command prompt and the programe cycles till the number of seconds have pasted.

dos.h
stuct dostime_t dtime;
stuct dostime_t etime;

void sleeper(int);

void main(){
int wsec;
_dos_gettime(&dtime);
printf("\nThe time is: %02u.%02u.%02u\n",dtime.min,dtime.sec,dtime.hsec);
printf("Set Sleep Timer(sec):";
scanf("%d",wsec);
sleeper(wsec);
_dos_gettime(&dtime);
printf("\nThe time is: %02u.%02u.%02u\n",dtime.min,dtime.sec,dtime.hsec);
}
{
void sleeper(wsec)
{
int count=0;
do{
_dos_gettime(&dtime);
do{
_dos_gettime(&etime);
while(dtime.sec==etime.sec);
count++;
while(count!=wsec);
}

Memoo

Share this post


Link to post
Share on other sites