SDL_BlitSurface不起作用 - 我无法在表面上看到图像

时间:2013-02-28 17:46:37

标签: c++ image window sdl

再次坚持使用一些我不能弄清楚如何工作的代码。我正在尝试创建SDL表面,加载位图图像并显示它。我的代码是这样的:

struct Image {
    string       Name;
    SDL_Surface* Surface;
};

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, PSTR lpCmdL, INT nCmdS) {
    // Will store our bitmap
    Image NewImage;

    // Initialization
    SDL_Init(SDL_INIT_EVERYTHING);

    // Create main window
    SDL_Surface *Window = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
    SDL_WM_SetCaption("MyApp", "MyApp");

    // Load image and make it compatible
    SDL_Surface* Surf_Temp = NULL;
    if((Surf_Temp = SDL_LoadBMP("myimage.bmp")) != NULL) {
        NewImage.Name = "MyImage";
        NewImage.Surface = SDL_DisplayFormat(Surf_Temp);
        SDL_FreeSurface(Surf_Temp);
    }

    // Draw image over the window
    if (NewImage.Surface != NULL && Window  != NULL) {
        SDL_Rect DestR;
                 DestR.x = 0;
                 DestR.y = 0;
        if (SDL_BlitSurface(NewImage.Surface, NULL, Window, &DestR) == -1) {
            MessageBox(NULL, "Error", "Alert", MB_OK);
        }
    }

    // Run
    SDL_Event Event;
    while(!End) {
        while(SDL_PollEvent(&Event)) {
            EventsHandler(&Event);
        }
    }
    return 0;
}

好吧,图像正确加载。我通过获取NewImage.Surface-> w(宽度)来验证它,这是正确的位图宽度。我没有收到任何错误信息(SDL_BlitSurface返回0),所以我不知道为什么窗口仍然是黑色,没有显示图像。

1 个答案:

答案 0 :(得分:3)

你需要调用SDL_Flip来更新屏幕。

SDL_Flip(Window);

同时阅读double buffering