Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » my game uses 90% CPU

This thread is locked; no one can reply to it. rss feed Print
 1   2 
my game uses 90% CPU
GullRaDriel
Member #3,861
September 2003
avatar

I never have problems building themon solaris, linux (debian) , windows xp ( pthread pack from source.redhat.org )

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Birdeeoh
Member #6,862
February 2006
avatar

I've been using Boost::threads for months now. I THINK they use pthreads on Unix but native on Windows (correct me if I'm wrong, too lazy to go check)

but, regardless, it works great and easy, on either platform.

[url http://developer.berlios.de/projects/openlayer/]OpenLayer[/url is an OpenGL accelerated 2D library for fast and easy graphics development under Allegro

Milan Mimica
Member #3,877
September 2003
avatar

Quote:

1. Write a thread function for the timer thread, which sleeps for the desired amount of time, then sets an event.

Something like this?

while (true) {
    Sleep(20); //0.02 sec, 50 fps
    SetEvent(handle);
};

Is this more accurate than using allegro timer?

Birdeeoh
Member #6,862
February 2006
avatar

Is this more accurate than using allegro timer?

POSSIBLY/probably, but no guarantees. The allegro timers go for a 5ms resolution. Your snippet theoretically goes for 2ms resolution but, in reality, the documentation for all Sleep() style functions will say something like -

Sleep( int ms ) - Puts the process to sleep for AT LEAST ms milliseconds

Under a light load, you're likely to get pretty good results but there's no guarantee you will wake up right at 2ms everytime. Also, I wasn't following the ENTIRE event queue discussion, but asynchronous event delivery is also not usually guaranteed to work instantly.

[url http://developer.berlios.de/projects/openlayer/]OpenLayer[/url is an OpenGL accelerated 2D library for fast and easy graphics development under Allegro

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

Is this more accurate than using allegro timer?

No. In fact, that could lead to time drifting. It takes a variable amount of time to call those functions and do whatever they're gonna do. You'd have to find a way to make the OS put the thread to sleep and wake it up at specific intervals.

What you can try to do, is calculate the amount of time you need to sleep until the next tic should fire (using QPC/gettimeofday to track the current time vs. the next target time), sleep for that amount (via Sleep/usleep), wake up and do whatever, then repeat. This is basically what Allegro does for timers (though some jitter is introduced since all the timer functions run in the same thread/loop).

int ms_to_sleep_for = one_second / tics_per_second;
unsigned int current_time = get_time();
while(running)
{
   signed int diff = current_time - get_time();
   if(diff > 0)
      Sleep(diff);

   do_something();

   current_time += ms_to_sleep_for;
}

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

BAF
Member #2,981
December 2002
avatar

pthreads works fine on windows... I use it myself.

Thomas Harte
Member #33
April 2000
avatar

Quote:

What you can try to do, is calculate the amount of time you need to sleep until the next tic should fire (using QPC/gettimeofday to track the current time vs. the next target time)

Of course this is exactly the same solution as the code I gave (guess I found the thread sooner!), it just doesn't devolve time counting to an Allegro timer. So I guess follow on questions are whether your use of Sleep rather than rest is a non-platform neutral oversight and whether anybody has a useful table of which high precision time functions are available on which OSs?

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

So I guess follow on questions are whether your use of Sleep rather than rest is a non-platform neutral oversight and whether anybody has a useful table of which high precision time functions are available on which OSs?

Oversight, yes. I was going for psuedo-code and would've used sleep(), but that a real libc function.

As for high precision time functions, sane OSs have gettimeofday, Windows has QueryPerformanceFrequency/QueryPerformanceCounter. If neither of those exist/work, the system isn't worth worrying about.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Manfred D
Member #4,134
December 2003
avatar

I think it would be nice to have threads in Allegro. If I haven't misunderstood the universe, you need threads to fully enjoy the powers of dual core processors. And they are the future. ;)

CGamesPlay
Member #2,559
July 2002
avatar

I've picked up a timing method that I like very much, using Allegro timers:

1#include <semaphore.h>
2 
3sem_t ticks;
4void inc_timer()
5{
6 sem_post(&ticks);
7}
8 
9main()
10{
11 sem_init(&ticks, 0, 0);
12
13 install_int_ex(inc_timer, BPS_TO_TIMER(30));
14 
15 while(1)
16 {
17 if(redraw)
18 {
19 // Drawing
20 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
21 redraw = false;
22 }
23
24 sem_wait(&ticks);
25 
26 // Logic
27 redraw = true;
28 }
29 
30 remove_int(inc_timer);
31 sem_destroy(&ticks);
32}

The sem_wait causes the application to sleep for at least the right amount of time, but if it misses several ticks it simply won't sleep.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

 1   2 


Go to: