For this question I would like to reffer to my previous thread: about my game project for school: http://www.daniweb.com/forums/thread102003.html.

I would like to execute a function every few miliseconds. Say every 5 miliseconds.

This part should call every 5 miliseconds.

bool Render() {
   // Render all the 20 fishes
   for(int i=0; i<20; i++)
        DrawTheFish(fish[i].x, fish[i].y);
    // Render the hook
    DrawTheHook(hook.x, hook.y);
}

This is the function where you render to the screen. This fuction should be call in every 10 miliseconds or 5 miliseconds (up to you)

The problem is that I can't figger out how to run a function every 5 miliseconds...
I tryed some different idees. Those are the most promosing:

1) This one starts, takes some time, prints 5 lines at once.

void main() {
	int i=0;
	unsigned long timer=0, sw_timer;
                printf("test test \n---------\n");

  while(i<5){
	timer=clock()/CLK_TCK;

	if(timer==2) {
		printf("do something\n");
		timer = 0;
		i++;
	}

  }
}

2) This is beter. its a little "flashing" but it gets the job done. But I can't use this cause the game dosn't have a fix timelimit.

void main() {
	int i=0;
	unsigned long timer=0, sw_timer;

  while(i<5){
	timer=clock()/CLK_TCK;
	sw_timer=timer*10;

	switch(sw_timer){
		case 0:  printf("   X   \n\n"); clrscr(); break;
		case 10: printf("X      \n\n"); clrscr(); break;
		case 20: printf("   X   \n\n"); clrscr(); break;
		case 30: printf("      X\n\n"); clrscr(); break;
		case 40: printf("   X   \n\n"); clrscr(); break;
		case 50: printf("X      \n\n"); clrscr(); break;
		default: printf(""); clrscr();
	} 
  }
}

any ideas? To bad I have to do this in C and not in C++ pffft
(teacher only accepts projects written in C)

AbberLine

> Say every 5 miliseconds.
Does your monitor refresh at 200Hz ?

If there is no way for you to observe the effects of such a high resolution event, then what would be the point of trying it?

Besides, once you get below the normal scheduling frequency of the OS (which is usually 10ms or 20ms IIRC), then accurate timings in user code become pretty tricky.

Oh, and main returns int, not void.

I didn't know about the effect of refreshing so quickly. Thanks for you advise. Thats one issue solved.

But it still leaves my problem unsolved. I will reform the question to:
How can I execute a function every five seconds?

(can I change the title of this thread?)

Sorry about the wrong return of main. Its indeed a bad thing to do. But i got sloppy and copyed some code snippets from my teammate.

> Say every 5 miliseconds.
Does your monitor refresh at 200Hz ?

If there is no way for you to observe the effects of such a high resolution event, then what would be the point of trying it?

Besides, once you get below the normal scheduling frequency of the OS (which is usually 10ms or 20ms IIRC), then accurate timings in user code become pretty tricky.

Oh, and main returns int, not void.

(Sorry for my misunderstanding, I though 1 second = 100milliseconds. Actually, 1 second = 1000milliseconds.)

Abber: http://www.winprog.org/tutorial/animation.html , Check this tutorial to learn how to use timer. (you can refresh it every 100miliseconds which is 10 FPS, if you think 50miliseconds is too unusal. The more FPS your program proform, the smoother your animation is)

Can't you use a variable to keep a running count of the frames, then render certain things after so many frames. You're not going to notice times smaller than the time for a frame anyway.

while(1)
{
    frameCounter++;
    if  (frameCounter == 10)
    render1(); //  draw something after 10 frames
   
    render2(); // draw something every frame
}

Abber: http://www.winprog.org/tutorial/animation.html , Check this tutorial to learn how to use timer.

Thanks invisal, thats a very nice tutorial and a very handy website. To bad I can't use that C++ code there (stupid C assignment) but it gives me a better understanding of the way it is suppost to be done.

Colin Mac, that looks like a good idee there I'll give it a try.

Thank you all for helping me with my program/project. Every post was helpfull in one way are an other. Special thanks to invisal for the hyperlink.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.