【问题标题】:SDL: Blitting BMP to window surface black screen mysterySDL:将 BMP 传送到窗口表面黑屏之谜
【发布时间】:2019-01-27 18:30:20
【问题描述】:

我编写了以下代码来加载 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 的错误消息。 似乎没有问题,但图像似乎只是在某个地方丢失了。有人可以帮助我吗?

附:这是我要显示的 bmp:

【问题讨论】:

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


    【解决方案1】:

    这段代码并没有像你想象的那样做:

    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 };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多