Hello! I’ve recently started to learn SDL2 (using windows 7) and it’s been going great so far, however I’ve encountered an issue which I can’t seem to be able to fix.
Basically my application is a very simple tile drawing system (for now). There’s a tileset and tiles are drawn onto the screen from a map data. This works great with a solid 60 fps all the time. I want to now implement a fullscreen system, but using SDL_WINDOW_FULLSCREEN doesn’t cut it since it streches the pixel art. I want to maintain the aspect ratio of the original. I’ve already established a proper scale up and down system which allows the window to be resized with the pixel art resizing as well and this works great in windowed mode.
The problem is that as soon as I make my application go fullscreen using SDL_WINDOW_FULLSCREEN_DESKTOP the framerate drops significantly. I tried to see what could be going wrong, but I don’t really see any problems in my code or at least I can’t really spot them. The same does not happen with SDL_WINDOW_FULLSCREEN as that runs smooth without issues.
Instead of posting the full code here’s a brief rundown of what happens
pre loop
- SDL, SDL_Image, SDL_TTF and SDL_Mixer are initialized
- window (SDL_Window) is created
- text variables are initialized (for debug display)
- room data is loaded
- tileset image is loaded
main program loop
- handle SDL events
- react to inputs (resizing and switching fullscreen happens here)
- update debug text
- render room
- render debug text
- delay the loop to achieve 60 fps
To activate fullscreen I use the following
fullscreen = true;
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
fullscreen_x = (display_mode.w - sc_w) / 2;
fullscreen_y = (display_mode.h - sc_h) / 2;
fullscreen is just a bool that stores the state of the program, window is the main SDL_Window, fullscreen_x and fullscreen_y are used as offsets for drawing the map in the center of the screen and display_mode is an SDL_DisplayMode variable for the monitor size.
sc_w and sc_h are the width and height of the screen.
I thought that maybe it’s the map being drawn that causes the lag, but even if I completely remove the map from the program and only leave the debug info the fps drops. I’m regulating fps using a timer that counts CPU ticks elapsed in each loop and delays each loop to adjust to 60 fps.
I also had a black border being drawn to hide the things that are “off screen”, but they don’t seem to affect the framerate. I currently have them removed.
I am using SDL_RenderSetScale to upscale the tileset, but that doesn’t seem to be it either since the lag is still there when this is removed.
So I then tried to create an application which only has 2 things, the window and an fps counter / limiter and then letting it enter fullscreen. Guess what… it still slows down to about 30 fps.
Then I tried removing everything so that nothing is being rendered and only fps is being reported to the console which yielded the following
60
59
59
59
59
enter fullscreen
41
33
33
33
33
33
leave fullscreen
43
59
59
59 is close enough to 60 to be good, but why does it lower to 33?
I’m at a loss here… Has anyone had similiar issues with SDL_WINDOW_FULLSCREEN_DESKTOP or am I doing something wrong?
EDIT : Forgot to mention I’m programming in c++