如何在SDL中自动探测显卡(显示器相关)的分辨率

在SDL中要探测显卡的分辨率的话,可以用下面的enumerate_sdl_modes函数。

void fatal (int ret, char *format, ...)
{
  va_list ap;

  fprintf (stderr, "fatal error: ");
  va_start (ap, format);
  vfprintf (stderr, format, ap);
  va_end (ap);
  if (ret == 1)
    fprintf (stderr, "Wrong parameters!/n");
  exit (ret);
}



int enumerate_sdl_modes (int *width, int *height, FILE *out)
{
  SDL_Rect **modes;
  int w = -1;
  int h = -1;
  int m = -1;
  int i;

  modes = SDL_ListModes (NULL, SDL_HWSURFACE | SDL_FULLSCREEN);

  if (! modes)
    fatal (2, "no SDL display modes available/n");

  if (modes == (SDL_Rect **) -1)
    fatal (2, "all SDL resolutions available (on hardware?)/n");

  for (i = 0; modes [i]; i++)
  {
    if (out)
        fprintf (out, "  mode %d: %d x %d/n", i, modes [i]->w, modes [i]->h);
    // find the largest resolution for the screen
    if ((modes [i]->w >= w) && (modes [i]->h >= h))    {
        w = modes [i]->w;
        h = modes [i]->h;
        m = i;
    }
    
  }

  if (m < 0)
    fatal (2, "no matching SDL video modes found/n");

  *width = w;
  *height = h;
  return (m);
}


在主程序中,应该这样调用:
    SDL_Init(SDL_INIT_VIDEO);
    enumerate_sdl_modes( &W_WIDTH, &W_HEIGHT, stderr );
    video = SDL_SetVideoMode(W_WIDTH,W_HEIGHT, 32, SDL_FULLSCREEN|SDL_ANYFORMAT);  //SDL_HWSURFACE
    atexit(SDL_Quit);


基本格式也就这样了,遇到新的情况可以依照这种方法灵活运用就是了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值