How to save CPU usage when starting an SDL program?

I made a very main window with SDL and want it to work until I click on window X.

#include "SDL.h" const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int main(int argc, char **argv) { SDL_Init( SDL_INIT_VIDEO ); SDL_Surface* screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF ); SDL_WM_SetCaption( "SDL Test", 0 ); SDL_Event event; bool quit = false; while (quit != false) { if (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { quit = true; } } SDL_Delay(80); } SDL_Quit(); return 0; } 

I tried adding SDL_Delay() at the end of the while clause, and it worked pretty well.

However, 80 ms seemed to be the highest value that I could use to run the program smoothly, and even then CPU usage was around 15-20%.

Is this the best way to do this, and I just need to live with the fact that he eats this many processor already at this point?

+4
source share
4 answers

I would definitely experiment with completely blocking functions (e.g. SDL_WaitEvent ). I have an OpenGL application in Qt, and I noticed that CPU usage ranges from 0% to 1%. It accumulates up to 4% during "use" (moving the camera and / or creating animations).

I am working on my own windowing tools. I noticed that I can use a similar CPU usage when using lock event cycles. This will complicate any timers you can rely on, but it is not so difficult to implement timers with this new approach.

+8
source

I just figured out how to reduce the processor load in my game from 50% to <10%. Your program is much simpler, and just using SDL_Delay() should be enough.

What I did: Use SDL_DisplayFormat() when loading images so blitting is faster. This has reduced CPU usage by up to 30%.

So, I found out that the sparkle of the background of the game (a large whole .png file) most of all absorbs my processor. I searched the Internet for a solution, but all I found was the same answer - just use SDL_Delay() . Finally, I found out that the problem was awkwardly simple - SDL_DisplayFormat() converted my 24-bit images to 32-bit. Thus, I set my BPP indicator to 24, which led to a CPU load of ~ 20%. Minimizing up to 16 bits helped solve the problem, and CPU usage is now less than 10%.

Of course, this means the loss of color detail, but since my game is a simplified 2D game with not too detailed graphics, everything was in order.

+7
source

To really understand this, you need to understand the flows. In a multi-threaded application, the program runs until it waits for something, and then tells the operating system that something else might work. Essentially, you do this with the SDL_Delay . If there were no delays at all, I suspect that your program will run at about 100% speed.

The time you have to put in the delay statement is only relevant if other commands take a considerable amount of time. In general, I would put the delay on the same amount of time that it takes to check the polling command, but no more than, say, 10 ms. What will happen is that the OS will wait at least as long as it will allow other applications to run in the background.

As for what you can do to improve this, well, it looks like you can't do much. However, keep in mind that if you were running another process that required a significant amount of processor power, your program resource would decrease.

+3
source

I know this is an older post, but I myself just stumbled upon this issue with the SDL when starting a small demo project. As noted by thebuzzsaw, the best solution is to use SDL_WaitEvent to reduce CPU usage in the event loop.

Here is how it will look in your example for those who are looking for a quick solution for it in the future. Hope this helps!

 #include "SDL.h" const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int main(int argc, char **argv) { SDL_Init( SDL_INIT_VIDEO ); SDL_Surface* screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF ); SDL_WM_SetCaption( "SDL Test", 0 ); SDL_Event event; bool quit = false; while (quit != false) { if (SDL_WaitEvent(&event) != 0) { switch (event.type) { case SDL_QUIT: quit = true; break; } } } SDL_Quit(); return 0; } 
+3
source

Source: https://habr.com/ru/post/1438268/


All Articles