在《 SDL点、线、面及图像加载 》一文中自己用SDL显示JPEG失败,现在有了解决方案。
关键函数:
SDL_rwops.h:
extern DECLSPEC SDL_RWops * SDLCALL SDL_RWFromFile(const char *file, const char *mode);
extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops *area);
SDL_image.h:
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src);
查看IMG_jpg.c文件,发现需要预先声明LOAD_JPG才能使用函数IMG_LoadJPG_RW(SDL_RWops *src);
#include "SDL_image.h"
#ifdef LOAD_JPG
#include <jpeglib.h>
/* Define this for fast loading and not as good image quality */
/*#define FAST_JPEG*/
代码文件:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
int main(){
char *file = "/home/edemon/Pictures/sun.jpg";
SDL_Surface *screen;
SDL_Surface *picture;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf(stderr,"SDL_INIT_VIDEO error %s\n",SDL_GetError());
exit(1);
}
//extern DECLSPEC SDL_RWops * SDLCALL SDL_RWFromFile(const char *file, const char *mode);
SDL_RWops *rwops = SDL_RWFromFile(file,"rb");
//extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src);
if(rwops == NULL){
fprintf(stderr,