【问题标题】:Add image to window with SDL — c++使用 SDL 将图像添加到窗口 - c++
【发布时间】:2013-06-07 11:14:53
【问题描述】:

我想将图像添加到窗口。这是我的代码:

SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp");

// Part of the bitmap that we want to draw
SDL_Rect source;
source.x = 24;
source.y = 63;
source.w = 65;
source.h = 44;

// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = 100;
destination.y = 100;
destination.w = 65;
destination.h = 44;

SDL_Event event;
bool gameRunning = true;

while (gameRunning)
{
    if (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            gameRunning = false;
        }
    }

    SDL_BlitSurface(bitmap, &source, screen, &destination);

    SDL_Flip(screen);
}

当窗口加载时它会变黑并且图像不会出现。怎么了?

【问题讨论】:

  • 你可以查看SDL_BlitSurface的返回值看看是否成功。 libsdl.org/docs/html/sdlblitsurface.html
  • 它返回 -1。这是什么意思?
  • @WilhelmMichaelsen 您是否阅读过 PureW 发布的文档?

标签: c++ xcode sdl


【解决方案1】:

你的代码很好。

您是否检查了SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp"); 是否返回了正确的地址?
加载图片失败会返回NULL。

SDL_Surface screen 也可能存在问题,但代码中没有显示。

【讨论】:

  • 这是屏幕:SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_SWSURFACE);
  • 而窗口事物是常量,值为640和480
猜你喜欢
  • 2019-09-16
  • 2019-11-16
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2021-03-06
相关资源
最近更新 更多