先上代码:
#include <stdio.h>
#include "SDL.h"
int main(int argc, char* argv[])
{
// 初始化SDL视频子系统
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
// 如果SDL初始化失败,则直接退出程序
return -1;
}
// 获取视频驱动程序的数量
int numberOfDrivers = SDL_GetNumVideoDrivers();
printf("Number of drivers: %d\n", numberOfDrivers);
// 遍历并打印所有视频驱动程序的名称
for (int i = 0; i < numberOfDrivers; i++)
{
printf("Driver name: %s\n", SDL_GetVideoDriver(i));
}
//当前视频驱动程序
const char *current_driver = SDL_GetCurrentVideoDriver();
printf("Current Driver name : %s\n", current_driver);
printf("\n\n" );
// 获取显示器的数量
int numberOfDisplays = SDL_GetNumVideoDisplays();
printf("Number of displays: %d\n", numberOfDisplays);
// 遍历每个显示器并打印其支持的显示模式
for (int i = 0; i < numberOfDisplays; i++)
{
printf("\n------------------\n" );
// 获取当前显示器支持的显示模式数量
int numberOfModes = SDL_GetNumDisplayModes(i);
for (int j = 0; j < numberOfModes; j++)
{
// 获取并打印当前显示器的特定显示模式
SDL_DisplayMode mode;
SDL_GetDisplayMode(i,j,&mode);
printf("Display %d Mode %d: %d x %d\n", i, j, mode.w, mode.h);
}
printf("\n------------------\n" );
// 获取并打印当前显示器的桌面显示模式
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(i, &mode);
printf("Display %d DisplayMode: %d , %d \n", i ,mode.w ,mode.h );
}
// 退出SDL子系统
SDL_Quit();
return 0;
}
Windows下运行效果:
Number of drivers: 3
Driver name: windows
Driver name: offscreen
Driver name: dummy
Current Driver name : windows
Number of displays: 2
------------------
Display 0 Mode 0: 3840 x 2160
Display 0 Mode 1: 3840 x 2160
Display 0 Mode 2: 3840 x 1620
Display 0 Mode 3: 3840 x 1620
Display 0 Mode 4: 3200 x 1800
Display 0 Mode 5: 3200 x 1800
Display 0 Mode 6: 2560 x 1440
Display 0 Mode 7: 2560 x 1440
Display 0 Mode 8: 2560 x 1440
Display 0 Mode 9: 2048 x 1536
Display 0 Mode 10: 2048 x 1536
Display 0 Mode 11: 1920 x 1200
Display 0 Mode 12: 1920 x 1200
Display 0 Mode 13: 1920 x 1200
Display 0 Mode 14: 1920 x 1080
Display 0 Mode 15: 1920 x 1080
Display 0 Mode 16: 1920 x 1080
Display 0 Mode 17: 1920 x 1080
Display 0 Mode 18: 1920 x 1080
Display 0 Mode 19: 1920 x 1080
Display 0 Mode 20: 1680 x 1050
Display 0 Mode 21: 1680 x 1050
Display 0 Mode 22: 1680 x 1050
Display 0 Mode 23: 1600 x 1200
Display 0 Mode 24: 1600 x 1200
Display 0 Mode 25: 1600 x 1200
Display 0 Mode 26: 1600 x 900
Display 0 Mode 27: 1600 x 900
Display 0 Mode 28: 1600 x 900
Display 0 Mode 29: 1440 x 900
Display 0 Mode 30: 1440 x 900
Display 0 Mode 31: 1440 x 900
Display 0 Mode 32: 1366 x 768
Display 0 Mode 33: 1366 x 768
Display 0 Mode 34: 1366 x 768
Display 0 Mode 35: 1280 x 1024
Display 0 Mode 36: 1280 x 1024
Display 0 Mode 37: 1280 x 1024
Display 0 Mode 38: 1280 x 1024
Display 0 Mode 39: 1280 x 720
Display 0 Mode 40: 1280 x 720
Display 0 Mode 41: 1280 x 720
Display 0 Mode 42: 1280 x 720
Display 0 Mode 43: 1024 x 768
Display 0 Mode 44: 1024 x 768
Display 0 Mode 45: 1024 x 768
Display 0 Mode 46: 1024 x 768
Display 0 Mode 47: 800 x 600
Display 0 Mode 48: 800 x 600
Display 0 Mode 49: 800 x 600
Display 0 Mode 50: 800 x 600
Display 0 Mode 51: 720 x 576
Display 0 Mode 52: 720 x 576
Display 0 Mode 53: 720 x 576
Display 0 Mode 54: 720 x 576
Display 0 Mode 55: 720 x 480
Display 0 Mode 56: 720 x 480
Display 0 Mode 57: 720 x 480
Display 0 Mode 58: 720 x 480
Display 0 Mode 59: 720 x 400
Display 0 Mode 60: 720 x 400
Display 0 Mode 61: 720 x 400
Display 0 Mode 62: 720 x 400
Display 0 Mode 63: 640 x 480
Display 0 Mode 64: 640 x 480
Display 0 Mode 65: 640 x 480
Display 0 Mode 66: 640 x 480
Display 0 Mode 67: 640 x 480
------------------
Display 0 DisplayMode: 2048 , 1152
------------------
Display 1 Mode 0: 1920 x 1080
Display 1 Mode 1: 1920 x 1080
Display 1 Mode 2: 1680 x 1050
Display 1 Mode 3: 1680 x 1050
Display 1 Mode 4: 1600 x 900
Display 1 Mode 5: 1600 x 900
Display 1 Mode 6: 1366 x 768
Display 1 Mode 7: 1366 x 768
Display 1 Mode 8: 1280 x 1024
Display 1 Mode 9: 1280 x 1024
Display 1 Mode 10: 1280 x 720
Display 1 Mode 11: 1280 x 720
Display 1 Mode 12: 1024 x 768
Display 1 Mode 13: 1024 x 768
Display 1 Mode 14: 800 x 600
Display 1 Mode 15: 800 x 600
Display 1 Mode 16: 640 x 480
Display 1 Mode 17: 640 x 480
------------------
Display 1 DisplayMode: 1536 , 864
Linux下运行效果:
umber of drivers: 4
Driver name: x11
Driver name: wayland
Driver name: KMSDRM
Driver name: dummy
Current Driver name : x11
Number of displays: 1
------------------
Display 0 Mode 0: 3840 x 2400
Display 0 Mode 1: 3840 x 2160
Display 0 Mode 2: 2880 x 1800
Display 0 Mode 3: 2560 x 1600
Display 0 Mode 4: 2560 x 1440
Display 0 Mode 5: 2560 x 1365
Display 0 Mode 6: 1920 x 1440
Display 0 Mode 7: 1920 x 1200
Display 0 Mode 8: 1920 x 1080
Display 0 Mode 9: 1856 x 1392
Display 0 Mode 10: 1792 x 1344
Display 0 Mode 11: 1680 x 1050
Display 0 Mode 12: 1600 x 1200
Display 0 Mode 13: 1440 x 900
Display 0 Mode 14: 1400 x 1050
Display 0 Mode 15: 1360 x 768
Display 0 Mode 16: 1280 x 1024
Display 0 Mode 17: 1280 x 960
Display 0 Mode 18: 1280 x 800
Display 0 Mode 19: 1280 x 768
Display 0 Mode 20: 1280 x 720
Display 0 Mode 21: 1152 x 864
Display 0 Mode 22: 1024 x 768
Display 0 Mode 23: 800 x 600
Display 0 Mode 24: 640 x 480
------------------
Display 0 DisplayMode: 2560 , 1365