使用PNG图片
使用PNG图片
我们之前使用的图片都是bmp类型的图片,而市面上的大多数图片都是PNG格式或者JDP格式的图片,这将导致转换图片格式会变得非常麻烦。似乎SDL也想到了这方面的不足,为我们提供了SDL_image库,它允许我们直接访问PNG格式的图片
主要任务
下面我们来学习直接在SDL中载入其他格式的图片,我们直接以PNG格式图片为例。
源码讲解
同样的,以中文代码形式讲解,英文代码为已经讲解过的部分,不加以阐述。
在文末划重点。
/*This source code copyrighted by Lazy Foo' Productions (2004-2020)
and may not be redistributed without written permission.*/
//Using SDL, SDL_image, standard IO, and strings
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string.h>
#include "struct_type.h"
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image
SDL_Surface* loadSurface(char path[]);
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//Current displayed PNG image
SDL_Surface* gPNGSurface = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//这里需要我们初始化IMG_Init,切记切记
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//这里引入png资源
gPNGSurface = loadSurface("C:\\Users\\Lixin\\Desktop\\dog.png");
if (gPNGSurface == NULL)
{
printf("Failed to load PNG image!\n");
success = false;
}
return success;
}
void close()
{
//Free loaded image
SDL_FreeSurface(gPNGSurface);
gPNGSurface = NULL;
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
SDL_Surface* loadSurface(char path[])
{
//The final optimized image
SDL_Surface* optimizedSurface = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path);
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError());
}
else
{
//Convert surface to screen format
optimizedSurface = SDL_ConvertSurface(loadedSurface, gScreenSurface->format, 0);
if (optimizedSurface == NULL)
{
printf("Unable to optimize image %s! SDL Error: %s\n", path, SDL_GetError());
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
return optimizedSurface;
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
//Load media
if (!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
}
//Apply the PNG image
SDL_BlitSurface(gPNGSurface, NULL, gScreenSurface, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindow);
}
}
}
//Free resources and close SDL
close();
return 0;
}
解释说明
- 别给我发电子邮件告诉我打电话给IMG_Init是BUG!记住,没有事先初始化当然会错。
- IMG_INIT_PNG的值为2,这个值是有库函数自身定义的一个枚举类型,就是2!!
- 我们的图像加载功能和以前几乎一样,只是现在它使用IMG_Load而不是SDL_LoadBMP。IMG_Load可以加载许多不同类型的格式,您可以在SDL_image文档中找到这些格式。与IMG_Init一样,当IMG_Load出现错误时,我们调用IMG_GetError来获取错误字符串。

浙公网安备 33010602011771号