summaryrefslogtreecommitdiffstats
path: root/display.cpp
blob: 64220305ba80a33fe2905bca0ec5ffea6bde3de9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
}