I'm making a game in SDL, and when I run it it takes up 99% of my CPU time. I have it so you can only move once every 60 milliseconds, but the program still races the the game loop as fast as possible. Any suggestions?
//napKIN
Printable View
I'm making a game in SDL, and when I run it it takes up 99% of my CPU time. I have it so you can only move once every 60 milliseconds, but the program still races the the game loop as fast as possible. Any suggestions?
//napKIN
Without code I don't entirely know what you mean, but do you think you could only call the gameloop itself every 60ms? The downside of that is every single object in the game would only be able to move every 60ms, and I have no damn clue if you want that or not. But it would prevent it from 'racing through the game loop' or whatever.
It just goes through the loop as fast as possible. Is there a way to stop and wait for input? Maybe pause for a few milliseconds?Code:int gamedone=0;
while(gamedone!=1)
{
//lots of code
}
//napKIN
Quote:
Originally posted by napkin111
It just goes through the loop as fast as possible. Is there a way to stop and wait for input? Maybe pause for a few milliseconds?Code:int gamedone=0;
while(gamedone!=1)
{
//lots of code
}
//napKIN
sleep(1000); // pause for 1 second
Ah, thankyou Perspective. Now, during this sleep time, can it still accept input?
[edit]And what do you need to #include?[/edit]
[edit2] http://faq.cprogramming.com/cgi-bin/...&id=1043284392 The problem is that I want it to not use the CPU so much[/edit2]
//napKIN
yes and no. this sleep time is for the main thread your program is running on. the input that is recieved during this time should be waiting for you when your program wakes up (althoug this can cause a delay if your sleep time is too long). generally speaking, your pogram will stop at that point for the specified amount of time and then continiue on.Quote:
Originally posted by napkin111
Ah, thankyou Perspective. Now, during this sleep time, can it still accept input?
//napKIN
lmao that's exactly what i meant
Sleep(60); //sleep 60ms
Put this at the very end of the loop.
#include <windows.h>
no you cannot 'accept input while sleeping', the program does absolutely nothing for the number of milliseconds you pass to it, delaying every action in the game.
There are better and more professional ways of doing time based movement, but if this is just a single player thing it shouldn't matter.
>no you cannot 'accept input while sleeping', the program does absolutely nothing for the number of milliseconds you pass to it, delaying every action in the game.
yes, your right. what i meant was that the windows event handler should will pass the event to your program. if your program is sleeping, the event should remain accessable until it is handled. ( im not sure about this, i dont know to much about windows )
:D Ah this is great, thanks guys. The CPU is only at about 20% and it should run the same speed on all comps.
//napKIN
Cool, glad to hear it's working. I liked your break out thing on ur site napkin.
Heh, thanks silvercord. That took about 8 hours over 2 days, lost the source in a format tho :(
I have a little annoying problem. If you tap a button to move while it is sleeping, it doesn't move. Is there some way to sleep and accpept input? Or maybe tell the program to stop until input is recieved?
//napKIN
I've been doing homework and teaching my kid sister algebra, therefore I haven't been able to respond. I would instead of having your entire program sleep, only translate (move) the objects every 60 milliseconds.
Here's some code for this. I'm only including relevant parts. If anything seems incorrect, ambiguous, or just doesn't make any freaking sense, ask me questions.
Code:#include <windows.h> //for GetTickCount. You may also try timeGetTime() which is in <mmsystem.h>
...//more stuff
//our time variables, Im pretty sure DWORD is just an integer on a 32bit system
DWORD lasttime;
DWORD currentitme;
...//blah
lasttime = GetTickCount(); //store the time before going into the abyss of the game loop
while(!NapkinDead) {
AcceptInput();
CalculateOtherStuff();
currenttime = GetTickCount();
if((currenttime - lasttime) > 60) {
lasttime = currenttime;
MovePlayer();
}
}
Use code tags silver! Goddamnit! :p :p :p
Well, My thought is, When i write an OpenGL program, It never
takes up 99% CPU usages. Simply because it has to draw the
proccesed every 100th of a second, Leaving the program doing
nothing, So you might just want to flip on your VSync if it uses
99% all the time.
most large games dont even useup 99% cpu usage even when i play online
Pacman doesn't count pall.;)
bah lol like DArk ages of camelot may be laggy but doesnt use 99%
I think you'll find sleeping for 1-10ms will stop your program from hogging the CPU. 60ms is too long when you're waiting for input.
LD:
Compile and run, it will take 99% CPU time regardless of the processor.Code:int main()
{
int x=1;
while(0==0)
{
x=x*x-2*x*5/x/x/x/x/x*x*x*x*x*-x-x-x+x/x;
}
return 0;
SC: I had something similar to that, but it takes 99% and has the non moving problem.
TD: I'm using SDL, I'm not sure about VSync...
[edit]
Eibro: Just saw your post after I posted. Yeah, 60 ms is way to long, but if I go really low than you have to tap the button really fast otherwise it moves multiple times
[/edit]
//napKIN
Hogging 99% of the CPU time is much different from using 99% of the CPU's processing power.Quote:
Originally posted by LouDu
most large games dont even useup 99% cpu usage even when i play online
Napkins program is probably doing nothing 98.9% of the time that its hogging the CPU.
Correct...well, I guess it IS going through the loop really fast checking for input...Quote:
Originally posted by Eibro
Hogging 99% of the CPU time is much different from using 99% of the CPU's processing power.
Napkins program is probably doing nothing 98.9% of the time that its hogging the CPU.
//napKIN
Don't lose your other construct for timing your program. Combine the two like this:Quote:
[edit]
Eibro: Just saw your post after I posted. Yeah, 60 ms is way to long, but if I go really low than you have to tap the button really fast otherwise it moves multiple times
[/edit]
Let me know if that works, I didn't test it.Code:while (true)
{
if (lastupdate + 60 < SDL_GetTicks())
{
// Gamecode
lastupdate = SDL_GetTicks();
}
SDL_Delay(5/* 1 to 10 */);
}
Edit: Heh, 1000th post.
is the code orginzationn in codein really super inportant?
Very. It is extremely important when you go back to look at code.Quote:
Originally posted by LouDu
is the code orginzationn in codein really super inportant?
Oh, and its important if you want your program to work too heh.
Code:}
return 0;
int main()
cout<<"hello world!";
#include <iostream.h>
{
Thanks Eibro, through a bit of experimentation it works great and takes up an average of 60% CPU (on a 1.6 P4). I really appreciate all the help I receive on this board, thanks! :D
//napKIN
ohh ok thanksQuote:
Originally posted by Eibro
Hogging 99% of the CPU time is much different from using 99% of the CPU's processing power.
Napkins program is probably doing nothing 98.9% of the time that its hogging the CPU.
It is probably your main loop as the PeekMessage() is a CPU hog as it keeps pumping the msg que until there is a message (as opposed to GetMessage() which waits until there is a msg ).
>>I think you'll find sleeping for 1-10ms
without using a high resolution timer you must expect a +/-5ms delay in all timers. This is due to them being very low priority in the msg que and a bug in MS OS's (there is a patch available but don't think your user will have it)
Ah, thankyou. When I get home I will look into GetMessage(). That sound more like what I'm looking for.
//napKIN