Sign in to follow this  
Decrius

[SDL] CPU 100%

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this