为什么SDL程序不显示BMP图片?

时间:2012-05-24 15:29:21

标签: c++ sdl

我从Lazy获得了以下代码:

#include <iostream>
#include "SDL/SDL.h"

using namespace std;

int main()
{
    //Start SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Surface *hello = NULL;
    SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

    hello = SDL_LoadBMP("hello.bmp");
    SDL_BlitSurface(hello, NULL, screen, NULL);
    SDL_Flip(screen);
    SDL_Delay(3000);
    SDL_FreeSurface(hello);

    //Quit SDL
    SDL_Quit();
    return 0;
}

有时会显示图片,但大部分时间它只是一个黑色的窗口(带有这个图片的细长字符串)。我在同一目录中有名为“hello.bmp”的BMP文件。 PS.I have ArchLinux。

1 个答案:

答案 0 :(得分:2)

在显示图像之前,您应将其转换为与所选视频模式兼容的格式。 因此,你应该实现这样的东西:

SDL_Surface *imagef;
imagef = SDL_DisplayFormat(image);

在点击您的BMP之前,使用imagef进行所有操作。

相关问题