SDL2.0 drawing with SDL


Overview:
the SDL SDL_Surface using two types of structures and SDL_Texture graphics to the screen. SDL_Surface contains a set of pixels (pixels members), which uses software rendering (non GPU); SDL_Textur can use hardware accelerators. Use SDL_Texture sample program:

#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(); }

Source rectangle and destination rectangle:
the source rectangle is used to specify the source image to be copied to the start position of the target image and the width and height parameters, i.e., only one copy can be part of the source image to the destination image. Specifies the destination rectangle to be copied into the target image and the start position of the width and height parameters, i.e. by the position specifying different target rectangle, so the source image to a different position of the copy target image. And the target rectangle and the width and height of the source rectangle not consistent, which can be used to reduce or enlarge the image.
x, y rectangular start position; w, h is the width and height of the rectangle.


The remaining image formats supported:
SDL2.0 default only supports .bmp image (SDL_LoadBMP ()), in order to use pictures in other formats can be downloaded SDL_image extension component in http://www.libsdl.org/projects/SDL_image page, it supports BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, XV format.


SDL_RenderCopyEx:
Use this function may be implemented flipped horizontally or vertically.
----------------
Disclaimer: This article is CSDN blogger "green farming husband" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/finewind/article/details/38541193

Guess you like

Origin www.cnblogs.com/lvdongjie/p/12455185.html