Find the answer to your Linux question:
Results 1 to 2 of 2
So I was toying around with SDL, learning a lot in the process of making a small game. While my main game loop runs, my cpu peaks at 100% (understandably) ...
  1. #1
    Linux Newbie rudie_rage's Avatar
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    133

    CPU peaks

    So I was toying around with SDL, learning a lot in the process of making a small game.

    While my main game loop runs, my cpu peaks at 100% (understandably) and just stays there. The computer isnt slowed down, im assuming because the program has a very low priority, but I can imagine doing that for a length of time is rather hard on the cpu. It would at least create far more heat than needed.

    How do other programs that wait (like, all of them) stop the cpu from racing?

    Also, is there a way to set the priority on a program you make to tell it how to share cpu resources with other apps? I noticed that some programs hog all resources, while others will prioritize others before its own processing.

    This is a little more technical than I'm used to working with, and I think I'm missing a lot of info on the topic. Any enlightenment?
    Living the digital dream....
    Disclaimer: I may be wrong since I was once before.
    Breathe out so I can breathe you in ~~Everlong

  2. #2
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Maybe your game is hogging the CPU because you don't limit the frames per second (FPS) of your game. Say you want to display 25 frames per second. It means that you'd like to see a frame every 1000/25=40 microseconds. So for every frame you save the time with SDL_GetTicks function. Then you do the stuff necessary to display the frame and the time with SDL_GetTicks again. You subtract the two times, say it gives you 5ms. So to maintain 25 fps you need to wait 40-5=35ms before going on with the next frame. Call SDL_Delay(35) to sleep for the necessary time. This should reduce the CPU usage of your app. Also try to update only the part of the screen that you've changed, i.e. try using SDL_UpdateRect on the changed parts rather that SDL_Flip or single SDL_UpdateRect on the whole screen. This may reduce the usage depending on the specifics of you game.

    Your game is not low priority, it just has the priority of a normal program. So if another normal program want's some resources they'll have to share (more or less) equally. Given that most programs don't process data intensively and rather wait for some events, your game will be not have to share most of the time. In order to reschedule you can use the nice program to start your game like:
    nice -10 ./yourapp
    the lower the negative value the higher the priority of you app. positive values, i.e. +10 will mean low priority. see the man page for more details. To change priority of the running program see 'renice' command.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...