gpt4 book ai didi

c++ - SDL: Blitting BMP to window surface 黑屏之谜

转载 作者:行者123 更新时间:2023-11-28 01:24:43 24 4
gpt4 key购买 nike

我编写了以下代码来加载 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:

the bmp I am trying to display

最佳答案

这段代码并不像你想象的那样:

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

关于c++ - SDL: Blitting BMP to window surface 黑屏之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54391502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com