SDL2改动比较大,随便玩玩高手勿喷。此代码是官方wiki给出的。
地址:http://wiki.libsdl.org/SDL_CreateRenderer
#include "SDL.h"
int main(int argc, char *argv[]) {
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *bitmapTex = NULL;
SDL_Surface *bitmapSurface = NULL;
int posX = 100, posY = 100, width = 320, height = 240;
SDL_Init(SDL_INIT_VIDEO);
win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
bitmapSurface = SDL_LoadBMP("img/hello.bmp");
bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
SDL_FreeSurface(bitmapSurface);
while (1) {
SDL_Event e;
if (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(bitmapTex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
下面看看SDL Game Development给出的代码,我摘录了一小段
if(m_pRenderer != 0)
{
cout<<"renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer,0,0,0,255);
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,pTempSurface);
SDL_FreeSurface(pTempSurface);
SDL_QueryTexture(m_pTexture,NULL,NULL,&m_sourceRectangle.w,&m_sourceRectangle.h);
m_sourceRectangle.w=50;
m_sourceRectangle.h=50;
m_destinationRectangle.x=m_sourceRectangle.x=0;
m_destinationRectangle.y=m_sourceRectangle.y=0;
m_destinationRectangle.x=100;
m_destinationRectangle.y=100;
m_sourceRectangle.x=50;
m_sourceRectangle.y=50;
m_destinationRectangle.w=m_sourceRectangle.w;
m_destinationRectangle.h=m_sourceRectangle.h;
}
1 void Game::render()
2 {
3 SDL_RenderClear(m_pRenderer);
4 //SDL_RenderCopy(m_pRenderer,m_pTexture,&m_sourceRectangle,&m_destinationRectangle);
5 SDL_RenderCopy(m_pRenderer,m_pTexture,NULL,NULL);
6 SDL_RenderPresent(m_pRenderer);
7 }
截图如下: