1

我是 SDL_image 的新手,我正在尝试在 C 文件中使用它来加载 BMP 图像。为此,我编写了以下代码:

#include "SDL/SDL.h"
#include "SDL_image.h"

SDL_RWops *rwop;
rwop = SDL_RWFromFile("sample.bmp", "rb");

但是,由于某种原因,虽然rwop在执行这些行之后NULL不再IMG_isBMP(rwop)是 0。

知道有什么问题吗?

4

1 回答 1

2

也许更好的例子。这(可能)会产生更多信息,也许如果支持 BMP:

https://www.libsdl.org/projects/SDL_image/docs/SDL_image_32.html

您也可以尝试IMG_LoadBMP_RW在此处按照示例使用:

https://www.libsdl.org/projects/SDL_image/docs/SDL_image_16.html#SEC16

#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

int main(int argc, char *argv[])
{
    const char *fn = "sample.bmp";
    SDL_Surface *surf;

    if (argc > 1)
        fn = argv[1];

    if ((surf = SDL_LoadBMP(fn)) == NULL) {
        printf("SDL_loadBMP failed: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    printf("%s is bmp\n", fn);

    SDL_FreeSurface(surf);
    SDL_Quit();

    return 0;
}

老答案:


测试和验证:

#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

int main(int argc, char *argv[])
{
    const char *fn = "sample.bmp";
    int v;
    SDL_RWops *rwop;

    if (argc > 1)
        fn = argv[1];

    if ((rwop = SDL_RWFromFile(fn, "rb")) == NULL) {
        printf("SDL_RWFromFile failed: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    v = IMG_isBMP(rwop);

    printf("%s is bmp = %d\n", fn, v);

    SDL_FreeRW(rwop);
    SDL_Quit();

    return 0;
}

编译:

gcc -Wall -Wextra -pedantic -o sdl sdl.c `sdl-config --libs` -lSDL_image

BMP 图像的产量,例如:

$ ./sdltest lena.bmp 
lena.bmp is bmp = 1
于 2015-01-14T11:12:46.573 回答