SDL:向窗口表面黑屏谜题发送BMP

时间:2019-01-27 18:30:21

标签: c++ visual-studio sdl sdl-2

我编写了以下代码以将BMP图像加载为表面,然后将该图像blit到窗口上

#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中工作)以生成并运行该程序时,创建的程序将运行,创建一个窗口,然后在程序运行时该窗口保持全黑。我没有收到来自V.S.,SDL_GetError()或Windows的错误消息。 看起来似乎没有问题,但是图像只是在某个地方丢失了。谁能帮助我?

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

the bmp I am trying to display

1 个答案:

答案 0 :(得分:2)

此代码无法执行您认为的操作:

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

您应该将其更改为

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

具有多个分配。甚至更好的方法是内联初始化SDL_Rect

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