2012-10-21 39 views
1

我不明白为什么我的SDL应用程序不会设置DOUBLE_BUF,因为我在问;SDL在HW_SURFACE上双缓存

这是一个简短的代码,在没有参数的情况下以32BBP模式打开一个全屏窗口1024 * 768。

#include <stdio.h> 
#include <stdlib.h> 
#include <SDL/SDL.h> 

#include "main.h" 

SDL_Surface *screen; 
Main mainstruct; 

void testModesInFormat(SDL_PixelFormat * format) 
{ 

    SDL_Rect **modes; 
    int i; 

    printf("Available hardware accelerated, fullscreen modes in %d bpp:\n", 
      format->BitsPerPixel); 

    modes = SDL_ListModes(format, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF); 

    // Check is there are any modes available 
    if(modes == (SDL_Rect **) 0) 
    { 
     printf("\tNo modes available!\n"); 
     return; 
    } 

    // Check if our resolution is restricted 
    if(modes == (SDL_Rect **) - 1) 
    { 
     printf("\tAll resolutions available.\n"); 
    } 
    else 
    { 
     // Print valid modes 
     for(i = 0; modes[i]; ++i) 
      printf("\t%d x %d\n", modes[i]->w, modes[i]->h); 
    } 

    free(modes); 

} 

void testModes() 
{ 
    SDL_PixelFormat format; 
    format.BitsPerPixel = 16; 
    testModesInFormat(&format); 
    format.BitsPerPixel = 24; 
    testModesInFormat(&format); 
    format.BitsPerPixel = 32; 
    testModesInFormat(&format); 
} 

int main(int argc, char *argv[]) 
{ 

    printf("Hello world!\n"); 
    Uint32 flags = SDL_DOUBLEBUF; 
    int w, h, bpp; 
    int i; 
    int hw_mem = 1; 
    w = 1024; 
    h = 768; 
    bpp = 32; 

    mainstruct.full_screen = 1; 

    if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) 
    { 
     fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); 
     exit(1); 
    } 
    atexit(SDL_Quit); 

    for(i = 0; i < argc; i++) 
    { 
     if(!strcmp(argv[i], "--window")) 
     { 
      mainstruct.full_screen = 0; 
     } 
     else if(!strcmp(argv[i], "--no-hardwarememory") || !strcmp(argv[i], "-nohw")) 
     { 
      hw_mem = 0; 
     } 
     else if(!strcmp(argv[i], "--test") || !strcmp(argv[i], "-t")) 
     { 
      testModes(); 
      exit(0); 
     } 
    } 

    if(hw_mem) 
    { 
     flags |= SDL_HWSURFACE; 
    } 
    else 
    { 
     flags |= SDL_SWSURFACE; 
    } 
    if(mainstruct.full_screen) 
    { 
     flags |= SDL_FULLSCREEN; 
    } 
    fprintf(stderr, "Attempting to set %dx%dx%d video mode.\n", w, h, bpp); 
    fflush(stderr); 
    screen = SDL_SetVideoMode(w, h, bpp, flags); 
    if(screen == NULL) 
    { 
     fprintf(stderr, "Unable to set %dx%dx%d video: %s\n", w, h, bpp, 
       SDL_GetError()); 
     exit(1); 
    } 
    fprintf(stderr, "Success:\n"); 
    fprintf(stderr, "\tSDL_HWSURFACE =%s\n", 
      (screen->flags & SDL_HWSURFACE ? "true" : "false")); 
    fprintf(stderr, "\tSDL_FULLSCREEN=%s\n", 
      (screen->flags & SDL_FULLSCREEN ? "true" : "false")); 
    fprintf(stderr, "\tSDL_DOUBLEBUF =%s\n", 
      (screen->flags & SDL_DOUBLEBUF ? "true" : "false")); 
    fprintf(stderr, "\tw=%d h=%d bpp=%d pitch=%d\n", screen->w, screen->h, 
      screen->format->BitsPerPixel, screen->pitch); 
    fflush(stderr); 
    return 0; 
} 

正如你所看到的,args是; --window到位SDL_HWSURFACE 的禁用全屏 --no-hardwarememory集SDL_SWSURFACE --test调用testModes()函数

因此,这里是我的输出;

没有参数(标志是“SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_FULLSCREEN”)我得到这个;

Attempting to set 1024x768x32 video mode. 
Success: 
    SDL_HWSURFACE =false 
    SDL_FULLSCREEN=true 
    SDL_DOUBLEBUF =false 
    w=1024 h=768 bpp=32 pitch=4096 

随着--test ARG,我得到这个:

Available hardware accelerated, fullscreen modes in 16 bpp: 
    1920 x 1080 
    1768 x 992 
    1680 x 1050 
    [...] 
    640 x 480 
Available hardware accelerated, fullscreen modes in 24 bpp: 
    No modes available! 
Available hardware accelerated, fullscreen modes in 32 bpp: 
    1920 x 1080 
    1768 x 992 
    1680 x 1050 
    [...] 
    640 x 480 

对于谁想要编译这个放入系统,这里是main.h

#ifndef MAIN_H 
#define MAIN_H 
#include "SDL.h" 
#include "SDL_thread.h" 

typedef struct _main { 
    int full_screen; 
} Main; 
extern Main mainstruct; 

#endif 

所以我想理解为什么DOUBLE_BUF不起作用,在32bpp全屏显示。 有些想法?

+0

很好暴露的问题! – makapuf

回答

5

如果你使用的是Windows,则问题可能是:

SDL 1.2.10 Release Notes 
... 
Windows Notes 
    The "windib" video driver is the default now, to prevent problems with 
    certain laptops, 64-bit Windows, and Windows Vista. The DirectX driver is 
    still available, and can be selected by setting the environment variable 
    SDL_VIDEODRIVER to "directx". 
... 

http://www.libsdl.org/release/changes.html

加入SDL_putenv("SDL_VIDEODRIVER=directx");到你的代码PROGRAMM打印出来后:

Attempting to set 1024x768x32 video mode. 
Success: 
    SDL_HWSURFACE =true 
    SDL_FULLSCREEN=true 
    SDL_DOUBLEBUF =true 
    w=1024 h=768 bpp=32 pitch=4096 
+0

是的! 谢谢! 我只是碰巧运行在完全相同的解决方案,而为什么我不能得到一个HWSURFACE:D –