[SDL] CPU 100%

Started by
7 comments, last by Kylotan 16 years, 2 months ago
I made a quick program which will view images with SDL_image, however when I run the program it takes up 100% of the CPU usage. This is caused by the loop I have, which draws every frame. Is there a way (and not by using SDL_Delay, because when the users moves its mouse in the corner, the image should scroll, with delay it will take too much time per frame) that I can refresh the screen ONLY when its modified? For example: when the screen is moved, resized, when the mouse button is pressed...than it should refresh the screen. Is there a way? With update rect or something? Thank you, Decrius
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
If you are using SDL_PollEvent, switch to SDL_WaitEvent.

PollEvent basically checks for events and, if there aren't any, returns control to your program, to go about its business. WaitEvent will give the OS control and wait patiently for events. It only returns when it gets one.

This change should reduce your cpu usage from ~100% to ~0% - unless you're frantically throwing the mouse around :)
Sounds like exactly what I need ;)

Thank you, I will try it out :).
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Yeah it works :), partly...

When I try to close the window, try to resize the window or when I click with my mouse, it doesn't stop waiting with the SDL_WaitEvent() function...

Is there anyway I can get around this problem?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
I know you've mentioned not wanting to do this but...

I'd continue to experiment with using SDL_PollEvent, and calling SDL_Delay once per frame. I personally don't like handing over control of my app to the OS for an indefinate period of time. And that's the crux of your current problem: you don't know WHEN SDL_WaitEvent will return.

Just calling SDL_Delay with 1 ms is enough to drop the CPU usage to nearly nothing, while not affecting performance.

But regarding SDL_Delay affecting your scrolling, from the sounds of it you're not using timed movement. If you're taking elapsed time into account when calculating how far to move, it should move at the same rate regardless of how much time has passed since the last frame.
Quote:Original post by gharen2
I personally don't like handing over control of my app to the OS for an indefinate period of time. And that's the crux of your current problem: you don't know WHEN SDL_WaitEvent will return.


I think the bug is elsewhere - you should certainly get an event when you resize the window or do anything with the mouse.
Quote:Original post by Decrius
Yeah it works :), partly...

When I try to close the window, try to resize the window or when I click with my mouse, it doesn't stop waiting with the SDL_WaitEvent() function...

Is there anyway I can get around this problem?


You should get an SDL_QUIT event when you close the window. So you just check for it and break out of the loop. As for resizing and mouse click, I don't quite understand the problem. Post your event loop code.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
SDL_QUIT didn't work, neither did anything in the event loop...very weird.

But I rewrote the code, and when I delay by 1 ms it gets to almost 0% CPU usage. But thanks for your help :), probably something time-consuming was going on of which I was not aware...still learning ;).
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
There was nothing time-consuming that was hidden - it's just that if you ask the CPU to do something repeatedly, no matter how simple that is, it will take 100% of the time. Personally I would go back to trying to get SDL_WaitEvent to work, but it's up to you.

This topic is closed to new replies.

Advertisement