0

Every time I attempt to run my application it creates a window perfectly fine but when moving the mouse about it becomes obvious that it freezes briefly for a few seconds. I also had this issue in a previous SDL project but didn't fix it as it wasn't very vital but I could never find a solution for it.

I tried looking up my issue but couldn't find anything that matches the issue I'm facing, despite this I attempted a few things that I felt could work like slightly different implementations of an event loop.

I'm not 100% sure on what EVERY line of SDL related code does but I took it from a previous project so I knew what it did at one point but it's not too hard to work out.

    int width = 160;
    int height = 144;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Window Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width * 4, height * 4, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, width, height);

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    bool quit = false;
    while (!quit)
    {
        SDL_Event E;
        while (SDL_PollEvent(&E))
        {
            if (E.type == SDL_QUIT)
                quit = true;
        }
    }

I expect the window to display smoothly and not have this freezing occur every few seconds. I'm not sure if it only occurs when the mouse is moving or if it happens to freeze every few seconds despite this as the cursor is the only way I have to test it. I saw one post that talked about flooding the event queue but that solution didn't seem to work for me. Any help is appreciated.

RowanT
  • 11
  • Have you taken a look at your CPU usage when running this code. I would expect that tight loop you have there is the source of your troubles. – pstrjds Jul 20 '19 at 20:53
  • @pstrjds That's a good idea, it does seem quite high. Is there a better solution? I'm not too versed with SDL but I'm not sure if this usage is enough to cause this issue even if it is quite high. If there is a better solution for less CPU usage I would try that however. – RowanT Jul 20 '19 at 20:55
  • 1
    Use `SDL_WaitEvent`, it will block until an event is available. If you want to periodically do stuff, create a SDL timer that will create SDL timer events. – E. van Putten Jul 20 '19 at 20:57
  • I don't know much about SDL, just that nested while loop looked like it could cause this. This [tutorial](http://www.sdltutorials.com/huh-my-pong-clone-uses-100-percent-cpu-and-is-still-slow) page seems to be addressing this issue. – pstrjds Jul 20 '19 at 21:00
  • Also make sure you are building your code with optimizations enabled. – Jesper Juhl Jul 20 '19 at 21:01
  • Just followed the explanation shown in @pstrjds link and the CPU speed has dropped to showing 0% on task manager but the pausing still occurs every few seconds – RowanT Jul 20 '19 at 21:08
  • Quick note: It appears to be happening less frequently now which is a plus and when it does occur it may be less severe but it's still quite an irritating and noticeable pause. – RowanT Jul 20 '19 at 21:15
  • Sounds pretty similar to [this issue](https://stackoverflow.com/questions/53644628). Are you using SDL 2.0.9 on Windows? – genpfault Jul 21 '19 at 05:13
  • @genpfault yes I am that does sound very similar to my issue including the interval at which it happens. I will try the solution to that or perhaps even attempting to revert back a version but hopefully this should work. – RowanT Jul 21 '19 at 15:33
  • It seems to work for right now and I can't notice any freezing so I would consider this solved. Thanks for the help – RowanT Jul 21 '19 at 15:37

0 Answers0