![]() |
|
My game take too much CPU!! - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Graphics & Audio Programming (/forum-9.html) +--- Thread: My game take too much CPU!! (/thread-6595.html) |
My game take too much CPU!! - Taxxodium - Dec 10, 2003 12:41 PM Allright, I am working on my first finished, SDL based, game and I'm nearing completion but I have just found something. My game can take up to 80% CPU on a G5!! That's quite unacceptable. Ofcourse there is a bunch of for loops and so on, but I don't think that's why it takes so much CPU. I think I will need to write some time functions to prevent my game from running too fast on fast machines and too slow on slow machines. But I don't know how to do this in SDL. Can somebody help me out. You'll all be enlisted in beta testing ![]() Thanks My game take too much CPU!! - skyhawk - Dec 10, 2003 12:43 PM employ a timer that only runs at 60 or 120fps My game take too much CPU!! - Taxxodium - Dec 10, 2003 12:51 PM Would that be better than using the delay function? My game take too much CPU!! - arekkusu - Dec 10, 2003 01:35 PM Sync to VBL so your draws only occur as fast as they need to. Get real familiar with Shark to see what's taking up all the time. My game take too much CPU!! - henryj - Dec 10, 2003 01:54 PM What does your profiler say is using up all the cpu? My game take too much CPU!! - Taxxodium - Dec 10, 2003 01:57 PM I didn't use any profiler, I just used top to check the CPU %. If you wonder why i was using top, well I was tracking down a memory leak, which I then found with MallocDebug ![]() Isn't Shark one of the CHUD tools? I think I better get those. My game take too much CPU!! - Taxxodium - Dec 10, 2003 02:36 PM OK, after checking with Shark, it seems my game takes most of it's CPU power from the blitting functions. I have 3 of those to blit parts of the screen. In some of these function I use double buffering, could this be the problem? My game take too much CPU!! - MattDiamond - Dec 10, 2003 03:18 PM Sounds like you are making progress, but I will just point out that if a full-screen game uses 80% CPU, possibly noone will notice or mind. They are more likely to mind if it runs in a window. My game take too much CPU!! - Taxxodium - Dec 10, 2003 03:20 PM Hehe, currently, it runs in a window ![]() I might make it fullscreen though. My game take too much CPU!! - IBethune - Dec 11, 2003 01:16 AM Here are a few ideas that I have used in the past: 1) Run a timer, set to fire off at the framerate you want e.g. 50 fps which is 20ms between firings. Then each time your timer goes off, read events using SDL_PollEvents(), handle them, and redraw your screen. In the event that the computer can't execute the routine in 20ms, then you will get lower framerates, but on faster CPUs it will max out at 50fps, leaving you spare processor power. 2) Have the event loop run as fast as possible, checking for events etc. BUT - only redraw the screen once every 3 times round the loop. This means that you will get an more accurate physical response in your game world (as deltaT between cycles is smaller), and you save time by only blitting once every 3 cycles. The user will probably not notice this. I found that in a normal game loop, about 75% of the time was in drawing routines, if you only draw every 3 frames, your framerate will only drop to 66% of what it was, but you will be 3 times more responsive. Worth thinking about? - Iain My game take too much CPU!! - anarchie - Dec 11, 2003 04:49 AM Run the event loop as fast as possible, and check the time elapsed since the last frame drawn on each pass. Compare this to the interval for the desired framerate, and then call your drawing routine and update the last-frame time appropriately. This is probably the most CPU-hoggish solution of them all, since your event loop never goes to sleep, and then keeps bugging the OS about the current time. Course, this is just off the top of my head, so the one guy who actually tried it is going to come by and reply "OMG NO You'll crash your network stack!" My game take too much CPU!! - Skorche - Dec 12, 2003 01:11 AM I found this handy: SDL_GFX Among other things, it has a nice, easy and powerful framerate limiter. It compiles fine under Xcode. You'll have to fix all the filepaths however, the PB project provided has all absolute paths. (and I'm guessing that you don't have the same username) My game take too much CPU!! - Taxxodium - Dec 12, 2003 01:25 AM Cool, I'll check it out. My game take too much CPU!! - sealfin - Dec 12, 2003 06:47 AM If this hasn't already been suggested, I assume you call SDL_DisplayFormat? (or SDL_DisplayFormatAlpha?) My game take too much CPU!! - Taxxodium - Dec 12, 2003 07:16 AM Well, I use SDL_SetVideoMode for my main surface (the one which receives all the drawings). Then I have some surfaces which are indeed created with SDL_DisplayFormat. But for some classes, which have their own drawing functions, I simply send the main surface as an argument. Like so: Code: //*** main drawing function |