Skip to content

Instantly share code, notes, and snippets.

@DanielGibson
Created July 4, 2019 15:42
Show Gist options
  • Save DanielGibson/6cf1a153c3058a91585488843d4ee5d6 to your computer and use it in GitHub Desktop.
Save DanielGibson/6cf1a153c3058a91585488843d4ee5d6 to your computer and use it in GitHub Desktop.
// can be built with:
// $ gcc -o sdl2test `sdl2-config --cflags` sdl_fullscreentest.c `sdl2-config --libs`
// will try to create a fullscreen window with 1920x1080 by default,
// but accepts commandline arguments to use a different resolution, like
// $ ./sdl2test 2560 1440
// will create testlog.txt and write some info there
#include <SDL.h>
#include <unistd.h>
int main(int argc, char** argv)
{
//FILE* out = fopen("testlog.txt", "wt");
FILE* out = stdout;
SDL_Init(SDL_INIT_VIDEO);
int winWidth = 1920;
int winHeight = 1080;
if(argc >= 3)
{
winWidth = atoi(argv[1]);
winHeight = atoi(argv[2]);
}
int winFlags = SDL_WINDOW_FULLSCREEN;
SDL_version ver;
SDL_GetVersion(&ver);
fprintf(out, "SDL version %d.%d.%d revision %s\n",
(int)ver.major, (int)ver.minor, (int)ver.patch, SDL_GetRevision());
fprintf(out, "Creating a Window with resolution %d x %d\n", winWidth, winHeight);
SDL_Window* win = SDL_CreateWindow("Test Fullscreen", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
winWidth, winHeight, winFlags);
if(win != NULL)
{
int winW, winH;
SDL_GetWindowSize(win, &winW, &winH);
fprintf(out, "Creating Window successful, apparently (not NULL), size: %d %d\n", winW, winH);
if(winW != winWidth || winH != winHeight)
{
fprintf(out, "wrong size, we got: %d %d\n", winW, winH);
SDL_DisplayMode realMode = {};
realMode.w = winWidth;
realMode.h = winHeight;
fprintf(out, "trying to really switch to %d x %d now\n", winWidth, winHeight);
if(SDL_SetWindowDisplayMode(win, &realMode) == 0)
{
fprintf(out, "swiching succeeded, according to SDL\n");
}
else
fprintf(out, "swiching FAILED, according to SDL\n");
//sleep(1);
SDL_GetWindowSize(win, &winW, &winH);
fprintf(out, "after switching we have size: %d %d\n", winW, winH);
SDL_SetWindowSize(win, winWidth, winHeight);
fprintf(out, "after setwindowsize we have size: %d %d\n", winW, winH);
}
int winIdx = SDL_GetWindowDisplayIndex(win);
if(winIdx >= 0)
{
SDL_DisplayMode mode;
if(SDL_GetCurrentDisplayMode(winIdx, &mode) == 0)
{
fprintf(out, "we're on display %d with mode %d x %d\n", winIdx, mode.w, mode.h);
}
else
fprintf(out, "Failed to get SDL Display mode: %s\n", SDL_GetError());
/*int numDisplayModes = SDL_GetNumDisplayModes(winIdx);
fprintf(out, "This display supports %d modes:\n", numDisplayModes);
for(int i=0; i<numDisplayModes; ++i)
{
if (SDL_GetDisplayMode(winIdx, i, &mode) == 0)
fprintf(out, " %d: %d x %d @ %d Hz\n", i, mode.w, mode.h, mode.refresh_rate);
}*/
}
else
fprintf(out, "Failed to get SDL Display index: %s\n", SDL_GetError());
sleep(1);
SDL_DestroyWindow(win);
}
else
fprintf(out, "Failed to create SDL window: %s\n", SDL_GetError());
SDL_Quit();
fflush(out);
fclose(out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment