Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Gamers & Players
  • Search

Programming games to not use 100% CPU

Having problems getting your favorite Linux game to work? Want to discuss strategies? This is the place!
Post Reply
  • Print view
Advanced search
7 posts • Page 1 of 1
Author
Message
TheCoop
Veteran
Veteran
User avatar
Posts: 1814
Joined: Sat Jun 15, 2002 5:20 pm
Location: Where you least expect it
Contact:
Contact TheCoop
Website

Programming games to not use 100% CPU

  • Quote

Post by TheCoop » Wed Jun 20, 2007 9:53 pm

I've got a opengl/glut game that im touching up that always uses 100% of both CPUs whatever happens. How can I reduce the amount of CPU time the game uses?
95% of all computer errors occur between chair and keyboard (TM)

"One World, One web, One program" - Microsoft Promo ad.
"Ein Volk, Ein Reich, Ein Führer" - Adolf Hitler

Change the world - move a rock
Top
Bill Cosby
Guru
Guru
User avatar
Posts: 430
Joined: Sat Jan 22, 2005 9:24 am
Location: Aachen, Germany

Re: Programming games to not use 100% CPU

  • Quote

Post by Bill Cosby » Wed Jun 20, 2007 10:44 pm

TheCoop wrote:I've got a opengl/glut game that im touching up that always uses 100% of both CPUs whatever happens. How can I reduce the amount of CPU time the game uses?
Basically it is your game loop, if you run the loop unrestricted it will simply loop as fast and as often as possible, thus requiring the full CPU capabilities, so the idea is to run a check how often the game loop has been executed within a second or so, and define a frame rate (e.g. 30) and put the process (the loop) to sleep for the rest of the second, another advantage of this is that it will run on every PC with the same speed.
The Creature from Jekyll Island.
Top
TheCoop
Veteran
Veteran
User avatar
Posts: 1814
Joined: Sat Jun 15, 2002 5:20 pm
Location: Where you least expect it
Contact:
Contact TheCoop
Website

  • Quote

Post by TheCoop » Wed Jun 20, 2007 10:58 pm

wouldnt that cause a pause every second (or so) if the computer is very fast? What I've just put in is a timer that measures how long it takes to draw a frame, and calculates how long it should take to achieve a specific frame rate, and sleeps every frame for the difference between the two. Unfortunately, even with a high FPS (60) I still get slightly stuttery behaviour using this...
95% of all computer errors occur between chair and keyboard (TM)

"One World, One web, One program" - Microsoft Promo ad.
"Ein Volk, Ein Reich, Ein Führer" - Adolf Hitler

Change the world - move a rock
Top
Bill Cosby
Guru
Guru
User avatar
Posts: 430
Joined: Sat Jan 22, 2005 9:24 am
Location: Aachen, Germany

  • Quote

Post by Bill Cosby » Wed Jun 20, 2007 11:35 pm

TheCoop wrote:wouldnt that cause a pause every second (or so) if the computer is very fast?
The timer-offset- theory is a whole philosophy for itself :D I am not totally familiar with it, but you should be able to gather lots of information on this with google ;)
I remember reading about it in "Killer Game Programming in Java" from OReilly, if you can get your hands on it, it is interesting in this regard, but there surly are more sources for this information.
TheCoop wrote:What I've just put in is a timer that measures how long it takes to draw a frame, and calculates how long it should take to achieve a specific frame rate, and sleeps every frame for the difference between the two. Unfortunately, even with a high FPS (60) I still get slightly stuttery behaviour using this...
This can't be because of the sleep times though, it has to be the code that simply needs more time than it actually has, if you have 60 pictures per second which are periodical, the human eye can't see any stutter. As far as I remember your method to calculate the sleep times is right, another thing might be your timer, it might be that the timer gives inexact results, or better put not exact enough for your purpose :)
The Creature from Jekyll Island.
Top
o43
n00b
n00b
Posts: 8
Joined: Thu May 17, 2007 5:00 pm

  • Quote

Post by o43 » Fri Jun 29, 2007 12:34 pm

Bill Cosby wrote:
TheCoop wrote:wouldnt that cause a pause every second (or so) if the computer is very fast?
The timer-offset- theory is a whole philosophy for itself :D I am not totally familiar with it, but you should be able to gather lots of information on this with google ;)
I remember reading about it in "Killer Game Programming in Java" from OReilly, if you can get your hands on it, it is interesting in this regard, but there surly are more sources for this information.
TheCoop wrote:What I've just put in is a timer that measures how long it takes to draw a frame, and calculates how long it should take to achieve a specific frame rate, and sleeps every frame for the difference between the two. Unfortunately, even with a high FPS (60) I still get slightly stuttery behaviour using this...
This can't be because of the sleep times though, it has to be the code that simply needs more time than it actually has, if you have 60 pictures per second which are periodical, the human eye can't see any stutter. As far as I remember your method to calculate the sleep times is right, another thing might be your timer, it might be that the timer gives inexact results, or better put not exact enough for your purpose :)
Actually, it could be because of the sleep. When you sleep, your app tells the scheduler to let someone else execute code. It will only have the CPU back when the scheduler gives back the execution to your app. At that moment only, your app will test if the sleep is finished. That means that a sleep will last at least the minimal time that can be allocated by the scheduler to a process. I don't know the current value, but last time i checked, it was 50ms on one of my computers, which is 1/20 of a second!
Top
bobobo
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 122
Joined: Thu Nov 24, 2005 2:25 pm

  • Quote

Post by bobobo » Fri Jun 29, 2007 2:47 pm

Since you are programming in opengl/glut, there is a handy function called glutTimerFunc()

Typically, you'll want to compute how much time has elapsed since the last frame, and then call this function accordingly.
For example, if you want 50fps, each frame will last 20ms. Suppose that only 5ms have elapsed, then you need to wait 15ms. If your frame drawing/calculating function is called update(), then you'll call glutTimerFunc like this :
glutTimerFunc(15000, &update, 0)
This will make glut call your function update() after 15ms have elapsed.
Top
nitinchaurasia09
n00b
n00b
Posts: 1
Joined: Sat Jun 30, 2007 6:38 pm

SUbject

  • Quote

Post by nitinchaurasia09 » Sat Jun 30, 2007 6:52 pm

Hi this is nitin i will tell you a lot in gaming
Top
Post Reply
  • Print view

7 posts • Page 1 of 1

Return to “Gamers & Players”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic