Ch03 在SDL中繪製圖片

SDL 默認只支持BMP圖片格式,加載圖片到SDL_Texture*中,使用SDL_LoadBMP函數便可。函數原型以下。ios

SDL_Surface* SDL_LoadBMP(const char* file)

按照SDL官方文檔,舊的SDL_Surface已經建議儘可能不要使用了,由於SDL_Surface是加載到RAM中處理,而使用SDL_Texture,數據是在GPU中處理,可以使用到硬件加速,速度會快不少。可是SDL_Surface並未被廢棄,在加載大圖片(如地圖,背景等圖片),一般仍是使用SDL_Surface, 對於遊戲精靈等圖片,推薦使用SDL_Texture.ide

將BMP圖片加載到SDL_Texture中,能夠使用以下代碼。函數

SDL_Surface* pTempSurface = SDL_LoadBMP("assets/rider.bmp");
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,
pTempSurface);
SDL_FreeSurface(pTempSurface);

當前遊戲中所涉及的圖片資源都是JPG或PNG格式,這個時候能夠將SDL_Image庫引用到工程中,獲取更多的圖片格式支持。SDL_Image支持 BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, XV 文件格式。ui

SDL_Texture* pTexture=IMG_LoadTexture(pRenderer,"xxx.png");

圖片加載後,就能夠調用SDL_RenderCopy 或SDL_RenderCopyEx函數 進行繪製。spa

int SDL_RenderCopy(SDL_Renderer*   renderer,
                   SDL_Texture*    texture,
                   const SDL_Rect* srcrect,
                   const SDL_Rect* dstrect);

 

SDL_RenderCopy 中srcrect 是源圖片的矩形圖片,賦值爲NULL便是繪製整張圖片,取圖片的一部分則只繪製部分圖片。dstrect是將srcrect內容繪製的目的矩形,賦值爲NULL是繪製當前render所在的整個區間。調整這兩個參數能夠實現圖片的部分繪製,圖片的縮放。code

若是要實現圖片的水平或垂直反轉,圖片的旋轉,須要調用SDL_RenderCopyEx。orm

int SDL_RenderCopyEx(SDL_Renderer*          renderer,
                     SDL_Texture*           texture,
                     const SDL_Rect*        srcrect,
                     const SDL_Rect*        dstrect,
                     const double           angle,
                     const SDL_Point*       center,
                     const SDL_RendererFlip flip);

本篇開頭所示的圖片依次示範,背景圖片繪製(冰山),圖片原樣輸出,放大2倍,縮小1倍,水平反轉,垂直反轉,沿順時針方向旋轉45度,沿逆時針旋轉45度。遊戲

完整示例代碼以下:圖片

#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
using namespace std;
SDL_Window* window = 0;
SDL_Renderer* renderer = 0;
void loadAssets();
void draw();
void cleanup();
void render();
void update();
SDL_Texture *texBackground = 0;
SDL_Texture *texTux = 0;
const SDL_Rect rc[] = {
	{0,0,64,80},  //normal
	{64,0,64*2,80*2}, //scale *2
	{190,0,64/2,80/2}, //scale* 1/2
	{260,0,64,80},//H
	{330,0,64,80 },//V
	{400,0,64,80 },//rotate
	{470,0,64,80 }//rotate

};
int main(int argc, char** argv)
{
	
	SDL_Event event;
	bool running = true;
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_SHOWN, &window, &renderer);
	SDL_SetWindowTitle(window, "Draw Image in SDL");
	loadAssets();
	while (running)
	{
		while (SDL_PollEvent(&event))
		{
			if (event.type == SDL_QUIT)
			{
				running = false;
				break;
			}
			update();
			render();
		}
	}
	
	cleanup();
	return 0;
}
void render()
{
	SDL_RenderClear(renderer);
	draw();
	SDL_RenderPresent(renderer);
}
void loadAssets()
{
	texBackground = IMG_LoadTexture(renderer, "assets/bluemountain-middle.png");
	texTux = IMG_LoadTexture(renderer, "assets/stand-0.png");
}
void cleanup()
{
	SDL_DestroyTexture(texBackground);
	SDL_DestroyTexture(texTux);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
}
void update()
{

}
void draw()
{
	SDL_RenderCopy(renderer, texBackground, NULL, NULL);
	SDL_RenderCopy(renderer, texTux, NULL, &rc[0]);
	SDL_RenderCopy(renderer, texTux, NULL, &rc[1]);
	SDL_RenderCopy(renderer, texTux, NULL, &rc[2]);
	SDL_RenderCopyEx(renderer, texTux, NULL, &rc[3], 0, 0, SDL_FLIP_HORIZONTAL);
	SDL_RenderCopyEx(renderer, texTux, NULL, &rc[4], 0, 0, SDL_FLIP_VERTICAL);
	SDL_RenderCopyEx(renderer, texTux, NULL, &rc[5], 45, 0, SDL_FLIP_NONE);
	SDL_RenderCopyEx(renderer, texTux, NULL, &rc[6], -45, 0, SDL_FLIP_NONE);
}
相關文章
相關標籤/搜索