1

再次坚持一些我无法弄清楚如何工作的代码。我正在尝试创建一个 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 (width) 来验证它,这是正确的位图宽度。我没有收到任何错误消息(SDL_BlitSurface 返回 0),所以我不知道为什么窗口仍然是黑色的,没有显示图像。

4

1 回答 1

3

您需要调用SDL_Flip来更新屏幕后,您会看到它。

SDL_Flip(Window);

也阅读双缓冲

于 2013-02-28T17:55:05.450 回答