概述:
SDL使用SDL_Surface和SDL_Texture这2种结构绘图到屏幕。SDL_Surface包含了一个像素集合(pixels成员),它使用软件渲染(非GPU);SDL_Textur可使用硬件加速器。
使用SDL_Texture的示例程序:
#include "SDL.h"
class Game2
{
public:
Game2():m_pWindow(NULL),m_pRenderer(NULL), m_bRunning(false){};
~Game2(){};
bool init(const char* title, int xpos, int ypos, int width, int height, int flags);
void render();
void update();
void handleEvents();
bool running() {return m_bRunning; }
void clean();
private:
SDL_Window *m_pWindow;
SDL_Renderer *m_pRenderer;
// new
SDL_Texture *m_pTexture;
SDL_Rect m_srcRect; // 源矩形
SDL_Rect m_dstRect; // 目标矩形
bool m_bRunning;
};
bool Game2::init(const char *title, int xpos, int ypos, int height, int width, int flags)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);
if (NULL == m_pWindow) return false;
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);
// new
SDL_Surface *pTmpSurface = SDL_LoadBMP("D:\\sample.bmp"); // 按照实际更改图片名称及路径
if (NULL == pTmpSurface) return false;
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTmpSurface);
if (NULL == m_pTexture) return false;
SDL_FreeSurface(pTmpSurface);
SDL_QueryTexture(m_pTexture,NULL, NULL, &m_srcRect.w, &m_srcRect.h);
m_dstRect.x = m_srcRect.x = 0;
m_dstRect.y = m_srcRect.y = 0;
// for debug
//m_srcRect.w /= 2;
//m_srcRect.h /= 2;
m_dstRect.w = m_srcRect.w;
m_dstRect.h = m_srcRect.h;
// for debug
//m_dstRect.w = m_srcRect.w/2;
//m_dstRect.h = m_srcRect.h/2;
//m_dstRect.x = 50;
//m_dstRect.y = 50;
//m_srcRect.x = 50;
//m_srcRect.y = 50;
m_bRunning = true;
return true;
}
void Game2::render()
{
SDL_RenderClear(m_pRenderer);
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_srcRect, &m_dstRect);
//SDL_RenderCopy(m_pRenderer, m_pTexture, NULL, NULL); // NULL means that use all texture( and renderer)
//SDL_RenderCopyEx(m_pRenderer, m_pTexture, &m_srcRect, &m_dstRect,0, 0, SDL_FLIP_HORIZONTAL);
//SDL_RenderCopyEx(m_pRenderer, m_pTexture, &m_srcRect, &m_dstRect,0, 0, SDL_FLIP_VERTICAL);
SDL_RenderPresent(m_pRenderer);
}
void Game2::clean()
{
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_DestroyTexture(m_pTexture);
SDL_Quit();
}
void Game2::handleEvents()
{
SDL_Event sdl_event;
if (SDL_PollEvent(&sdl_event))
{
switch(sdl_event.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game2::update()
{
static int count = 0;
Uint32 ticks = SDL_GetTicks();
if ((ticks % 1000) == 0)
{
count++;
m_dstRect.y = m_dstRect.h * (count%6);
}
}
/******************************************************************************
example03: use SDL_Texture to draw
******************************************************************************/
void example03()
{
Game2 game;
game.init("example03: drawing in SDL", 200, 200, 640, 480, 0);
while (game.running())
{
game.handleEvents();
game.update();
game.render();
}
game.clean();
}
源矩形和目标矩形:
源矩形用于指定要拷贝到目标图像的源图像起始位置及宽高参数,即可以只拷贝源图像的某一部分到目标图像。
目标矩形用于指定要拷贝到目标图像中的起始位置及宽高参数,即可以通过指定不同的目标矩形位置,让源图像拷贝到目标图像的不同位置。且目标矩形的宽高不用和源矩形一致,这可用于缩小或放大图像。
x,y为矩形的开始位置;w,h为矩形的宽高。
其余图片格式支持:
SDL2.0默认只支持.bmp图像(SDL_LoadBMP()),要想使用其它格式的图片,可在http://www.libsdl.org/projects/SDL_image页面下载SDL_image扩展组件,它支持BMP,GIF,JPEG,LBM,PCX,PNG,PNM,TGA,TIFF,WEBP,XCF,XPM,XV格式。
SDL_RenderCopyEx:
使用这个函数可实现水平和垂直翻转。