From 823a82611d37e618a4734ea480fab4c60b73f5b7 Mon Sep 17 00:00:00 2001 From: piernov Date: Thu, 18 Aug 2016 14:13:40 +0200 Subject: Initial Import --- display.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 display.cpp (limited to 'display.cpp') diff --git a/display.cpp b/display.cpp new file mode 100755 index 0000000..6422030 --- /dev/null +++ b/display.cpp @@ -0,0 +1,55 @@ +#include "logging.h" +#include "display.h" + +display screen; + +/** +* Loads a BMP image into a texture on the rendering device +* @param file The BMP image file to load +* @param ren The renderer to load the texture onto +* @return the loaded texture, or nullptr if something went wrong. +*/ +SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ + SDL_Log("loadTexture %s", file.c_str()); + //Initialize to nullptr to avoid dangling pointer issues + SDL_Texture *texture = nullptr; + //Load the image + SDL_Surface *loadedImage = IMG_Load(file.c_str()); + //If the loading went ok, convert to texture and return the texture + if (loadedImage != nullptr){ + texture = SDL_CreateTextureFromSurface(ren, loadedImage); + SDL_FreeSurface(loadedImage); + //Make sure converting went ok too + if (texture == nullptr) + logSDLError("CreateTextureFromSurface"); + } + else + logSDLError("IMG_Load"); + + return texture; +} + +/** +* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving +* the texture's width and height +* @param tex The source texture we want to draw +* @param ren The renderer we want to draw too +* @param x The x coordinate to draw too +* @param y The y coordinate to draw too +* @param angle The angle of rotation +*/ +void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, float x, float y, double angle){ + //Setup the destination rectangle to be at the position we want + SDL_Rect dst; + dst.x = x*screen.width; + dst.y = y*screen.height; + //Query the texture to get its width and height to use + SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h); + SDL_RenderCopyEx(ren, tex, NULL, &dst, angle, NULL, SDL_FLIP_NONE); +} + +void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, float x, float y){ + renderTexture(tex, ren, x, y, 0); +} + + -- cgit v1.2.3-54-g00ecf