SDL: Blitting BMP to window 表面黑屏之谜

SDL: Blitting BMP to window surface black screen mystery

我编写了以下代码来加载 BMP 图像作为表面,然后将该图像 blit 到 window:

#include "stdafx.h"
#include "SDL.h"
#include <iostream>

int main(int argc, char *argv[])
{
    //init
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Playground", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, 0);
    std::cout << SDL_GetError() << std::endl;
    SDL_Surface* surface = SDL_GetWindowSurface(window);

    //load file and convert to texture
    SDL_Surface* bmp = SDL_LoadBMP("sample.bmp");
    std::cout << SDL_GetError() << std::endl;

    //render texture
    SDL_Rect area;
    area.x, area.y = 3;
    area.h, area.w = 25;
    SDL_BlitSurface(bmp, &area, surface, &area);
    std::cout << SDL_GetError() << std::endl;
    SDL_UpdateWindowSurface(window);
    std::cout << SDL_GetError() << std::endl;
    SDL_Delay(3000);

    //clean up
    SDL_FreeSurface(bmp);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

当我按 F5(我在 Visual Studio Express 2017 中工作)构建和 运行 程序时,程序创建了 运行s,创建了一个 window,然后 window 和程序 运行 一样保持全黑。我没有收到来自 V.S.、SDL_GetError() 或 Windows 的错误消息。 似乎没有问题,但图像似乎只是在某处丢失了。有人能帮我吗?

P.S。这是我要显示的 bmp:

此代码与您认为的不同:

area.x, area.y = 3;
area.h, area.w = 25;

你应该把它改成

area.x = area.y = 3;
area.h = area.w = 25;

有多项任务。或者甚至更好地初始化 SDL_Rect inline:

SDL_Rect area = { 3, 3, 25, 25 };