99% CPU Usage...

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 ...

  1. #1
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Question 99% CPU Usage...

    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

  2. #2
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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.

  3. #3
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Code:
    int gamedone=0;
    while(gamedone!=1)
    {
    //lots of code
    }
    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?

    //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

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by napkin111
    Code:
    int gamedone=0;
    while(gamedone!=1)
    {
    //lots of code
    }
    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?

    //napKIN

    sleep(1000); // pause for 1 second

  5. #5
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    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

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by napkin111
    Ah, thankyou Perspective. Now, during this sleep time, can it still accept input?

    //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.

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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.

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >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 )

  9. #9
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    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

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Cool, glad to hear it's working. I liked your break out thing on ur site napkin.

  11. #11
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    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

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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();
    }
    
    }

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Use code tags silver! Goddamnit!

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    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.

  15. #15
    |<o>| <--cyclopse LouDu's Avatar
    Join Date
    Mar 2003
    Posts
    137
    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. */

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 02:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21