-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureManager.cpp
109 lines (90 loc) · 2.96 KB
/
TextureManager.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// TextureManager.cpp
// SDL Game Programming Book
//
// Created by shaun mitchell on 31/12/2012.
// Copyright (c) 2012 shaun mitchell. All rights reserved.
//
#include "TextureManager.h"
#include <SDL_image.h>
#include <SDL.h>
#include "Game.h"
#include <stdexcept>
TextureManager* TextureManager::s_pInstance = 0;
static SDL_Renderer* getRenderer(SDL_Renderer* pRenderer)
{
if (pRenderer)
return pRenderer;
else
return TheGame::Instance()->getRenderer();
}
void TextureManager::load(std::string const &fileName, std::string const &id, SDL_Renderer* pRenderer)
{
pRenderer = getRenderer(pRenderer);
SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
if(pTempSurface == 0)
{
throw std::runtime_error(IMG_GetError());
}
SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
if(pTexture != 0)
{
m_textureMap[id] = pTexture;
}
else
throw std::runtime_error("Error loading " + fileName + ": " + SDL_GetError());
}
void TextureManager::draw(std::string const &id, int x, int y, int width, int height, SDL_Renderer* pRenderer,
double angle, int alpha, double scale, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = 0;
srcRect.y = 0;
//@todo get size automatic when 0
srcRect.w = width;
destRect.w = width * scale;
srcRect.h = height;
destRect.h = height * scale;
destRect.x = x;
destRect.y = y;
SDL_SetTextureAlphaMod(m_textureMap[id], alpha);
SDL_RenderCopyEx(getRenderer(pRenderer), m_textureMap[id], &srcRect, &destRect, angle, 0, flip);
}
void TextureManager::drawFrame(const std::string &id, int x, int y, int width, int height, int currentRow, int currentFrame,
SDL_Renderer *pRenderer, double angle, int alpha, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = width * currentFrame;
srcRect.y = height * currentRow;
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_SetTextureAlphaMod(m_textureMap[id], alpha);
SDL_RenderCopyEx(getRenderer(pRenderer), m_textureMap[id], &srcRect, &destRect, angle, 0, flip);
}
void TextureManager::drawTile(std::string const &id, int margin, int spacing, int x, int y, int width, int height,
int currentRow, int currentFrame, SDL_Renderer *pRenderer)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = margin + (spacing + width) * currentFrame;
srcRect.y = margin + (spacing + height) * currentRow;
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_SetTextureAlphaMod(m_textureMap[id], 255);
SDL_RenderCopyEx(getRenderer(pRenderer), m_textureMap[id], &srcRect, &destRect, 0, 0, SDL_FLIP_NONE);
}
void TextureManager::clearTextureMap()
{
m_textureMap.clear();
}
void TextureManager::clearFromTextureMap(std::string const &id)
{
m_textureMap.erase(id);
}