Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
MarauderIIC
Respected Programmer
Posts: 3406 Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA
Post
by MarauderIIC » Thu Jan 29, 2009 2:32 pm
If you're developing something that needs to continuously loop but also doesn't need a framerate limiter (ie, a server) and you don't want it to eat all your CPU, try implementing your heartbeat like this:
Code: Select all
while (run) {
doGameLogic();
sendOutput();
#ifdef __WIN32
Sleep(0); //windows
#else
sleep(0); //UNIX
#endif
}
Of course you only need a particular #ifdef block for your OS (unless you're developing multiplatform). Sleep(0) essentially tells the OS that your program can be interrupted (Sleep(milliseconds) pauses execution for milliseconds' worth of time). I had a server at one point (MrAdventure) that was eating 99-100% CPU without this, but went down to something reasonable (I think like 20-40%?) after doing this.
If you're using SDL, I think SDL_Delay() is multiplatform, but I'm not sure if it works in the same fashion.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
LeonBlade
Chaos Rift Demigod
Posts: 1314 Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY
Post
by LeonBlade » Thu Jan 29, 2009 3:39 pm
What does sleep do?
What do you pass in?
There's no place like ~/
M_D_K
Chaos Rift Demigod
Posts: 1087 Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK
Post
by M_D_K » Thu Jan 29, 2009 3:45 pm
MarauderIIC wrote:
Sleep(0) essentially tells the OS that your program can be interrupted.
Thats what it does. It stops program execution for the amount of time you tell it in milliseconds(5000 = 5 seconds).
Gyro Sheen wrote: you pour their inventory onto my life
IRC wrote:
<sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
LeonBlade
Chaos Rift Demigod
Posts: 1314 Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY
Post
by LeonBlade » Thu Jan 29, 2009 3:47 pm
Gotcha... thanks for the tip
There's no place like ~/
dandymcgee
ES Beta Backer
Posts: 4709 Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:
Post
by dandymcgee » Thu Jan 29, 2009 6:49 pm
Interesting heh I've always assumed such a parameter would simply cause a syntax error.
Falco Girgis wrote: It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Arce
Jealous Self-Righteous Prick
Posts: 2153 Joined: Mon Jul 10, 2006 9:29 pm
Post
by Arce » Sat Jan 31, 2009 9:05 am
BP is the same, and wou1d use the 'de1ay' command w/ parameter of O.
<qpHalcy0n > decided to paint the office, now i'm high and my hands hurt
eatcomics
ES Beta Backer
Posts: 2528 Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois
Post
by eatcomics » Sat Jan 31, 2009 1:26 pm
Arce wrote: BP is the same, and wou1d use the 'de1ay' command w/ parameter of O.
Arce, maybe you need an ipod touch, you wouldn't be missing any buttons...
dandymcgee
ES Beta Backer
Posts: 4709 Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:
Post
by dandymcgee » Mon Feb 02, 2009 2:15 pm
Arce wrote: My L, J, and 0 keys are broken.
I was wondering why the quality of your posts has been slowly decreasing lately. Get a new keyboard!
Falco Girgis wrote: It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
trufun202
Game Developer
Posts: 1105 Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:
Post
by trufun202 » Mon Feb 02, 2009 3:35 pm
I have to do the same thing in .NET with a Windows Service. If I have a delegate that's processing a request, I first do a Thead.Sleep(0) to avoid hangups.
MarauderIIC
Respected Programmer
Posts: 3406 Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA
Post
by MarauderIIC » Mon Feb 02, 2009 4:15 pm
dandymcgee wrote: Arce wrote: My L, J, and 0 keys are broken.
:lol: I was wondering why the quality of your posts has been slowly decreasing lately. Get a new keyboard!
I wrote that. I exercised my administrative privilege.
Glad to know it's used in industry, trufun, thanks :D
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Arce
Jealous Self-Righteous Prick
Posts: 2153 Joined: Mon Jul 10, 2006 9:29 pm
Post
by Arce » Tue Feb 03, 2009 11:32 am
trufun202 wrote: I have to do the same thing in .NET with a Windows Service. If I have a delegate that's processing a request, I first do a Thead.Sleep(0) to avoid hangups.
Huh, wow...I thought I was just being a cheap cunt? Didn't know that was actually what you're 'supposed' to do. xD
<qpHalcy0n > decided to paint the office, now i'm high and my hands hurt
LuciDreamTheater
Chaos Rift Newbie
Posts: 39 Joined: Tue Jan 20, 2009 2:18 am
Location: Southern CA
Contact:
Post
by LuciDreamTheater » Tue Feb 03, 2009 12:12 pm
dandymcgee wrote: Interesting heh I've always assumed such a parameter would simply cause a syntax error.
It's a function that takes an integer as its only parameter, so it wouldn't cause a syntax error. This is true for all functions in C++ that take an integer, unless I'm mistaking.
I suppose that if you forced the compiler to treat warnings as errors, then there might be an signed/unsigned mismatch. Who knows...
Ginto8
ES Beta Backer
Posts: 1064 Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java
Post
by Ginto8 » Tue Feb 03, 2009 1:42 pm
anothrguitarist wrote: dandymcgee wrote: I suppose that if you forced the compiler to treat warnings as errors, then there might be an signed/unsigned mismatch. Who knows...
It shouldn't... 0 isn't negative, and the range of a normal unsigned int is from 0 to 4,294,967,295, not 1 to 4,294,967,295...
Quit procrastinating and make something awesome.
Ducky wrote: Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
dandymcgee
ES Beta Backer
Posts: 4709 Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:
Post
by dandymcgee » Wed Feb 04, 2009 7:37 pm
I suppose what I was referring to is technically a run-time error not a syntax error.. either way I didn't know you could pass zero
Falco Girgis wrote: It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!