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
This is a discussion on 99% CPU Usage... within the Game Programming forums, part of the General Programming Boards category; I'm making a game in SDL, and when I run it it takes up 99% of my CPU time. I ...
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
"The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
-John Carmack
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
Last edited by napkin111; 03-02-2003 at 03:47 PM.
"The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
-John Carmack
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.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 )
Ah this is great, thanks guys. The CPU is only at about 20% and it should run the same speed on all comps.
//napKIN
"The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
-John Carmack
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
"The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
-John Carmack
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!![]()
![]()
![]()
--
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
Languages <> C++, HTML
/*The Ledgend of Ludlow Coming to a PC Near
You intro Cinenmatic Needed email me of
Reply to one of my threads if you can make
one, thats decent. */