使用SDL - c ++将图像添加到窗口

时间:2013-06-07 11:14:54

标签: c++ xcode sdl

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

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

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

1 个答案:

答案 0 :(得分:0)

你的代码很好。

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

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