[Add] Loading texture from sprite class.
[Add] Resource management!
This commit is contained in:
parent
96a2929c08
commit
b2fb84dcee
@ -98,6 +98,7 @@
|
||||
<ClInclude Include="..\..\src\Math\Vec2.h" />
|
||||
<ClInclude Include="..\..\src\Sprite\Sprite.h" />
|
||||
<ClInclude Include="..\..\src\System\Debug.h" />
|
||||
<ClInclude Include="..\..\src\System\ResourceManager.h" />
|
||||
<ClInclude Include="..\..\src\Texture\Texture.h" />
|
||||
<ClInclude Include="..\..\src\TMXParser\base64.h" />
|
||||
<ClInclude Include="..\..\src\TMXParser\Tmx.h" />
|
||||
|
@ -126,6 +126,9 @@
|
||||
<ClInclude Include="..\..\src\TMXParser\TmxObject.h">
|
||||
<Filter>TMXParser</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\System\ResourceManager.h">
|
||||
<Filter>System</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\src\Main\main.cpp">
|
||||
|
@ -6,17 +6,12 @@ Player::Player(void) {
|
||||
}
|
||||
|
||||
Player::~Player(void) {
|
||||
delete _player->GetTexture();
|
||||
delete _player;
|
||||
}
|
||||
|
||||
void Player::Prepare(void) {
|
||||
_player = new Sprite();
|
||||
_playerTexture = new Texture();
|
||||
_playerTexture->Load("../Data/Img/Player.png");
|
||||
_player->SetTexture(_playerTexture);
|
||||
_player->SetPosition(Vec2(800/2, 600/2));
|
||||
_player->SetScale(Vec2(4.5f, 4.5f));
|
||||
_player->LoadSprite("../Data/Img/Player.png");
|
||||
}
|
||||
|
||||
void Player::Render(void) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "../Texture/Texture.h"
|
||||
#pragma once
|
||||
#include "../Sprite/Sprite.h"
|
||||
#include "../Global/Globals.h"
|
||||
#include "../System/Debug.h"
|
||||
@ -19,6 +19,5 @@ public:
|
||||
private:
|
||||
float PLAYER_SPEED;
|
||||
Sprite* _player;
|
||||
Texture* _playerTexture;
|
||||
float _rotationAngle;
|
||||
};
|
||||
|
@ -10,6 +10,10 @@ Sprite::Sprite() {
|
||||
}
|
||||
|
||||
Sprite::~Sprite() {
|
||||
if(texture) {
|
||||
textureManager.Destroy(texture);
|
||||
texture = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void Sprite::Update(float dt) {
|
||||
@ -20,10 +24,10 @@ void Sprite::Draw() const {
|
||||
}
|
||||
|
||||
void Sprite::DrawRegion(const Rect& src) const {
|
||||
const float uvX = src.x / (float)texture->GetWidth();
|
||||
const float uvY = src.y / (float)texture->GetHeight();
|
||||
const float uvW = src.w / (float)texture->GetWidth();
|
||||
const float uvH = src.h / (float)texture->GetHeight();
|
||||
const float uvX = src.x / (float)texture->GetWidth();
|
||||
const float uvY = src.y / (float)texture->GetHeight();
|
||||
const float uvW = src.w / (float)texture->GetWidth();
|
||||
const float uvH = src.h / (float)texture->GetHeight();
|
||||
|
||||
// Awesome artwork to describe this:
|
||||
// 0---------1
|
||||
@ -74,6 +78,20 @@ void Sprite::DrawRegion(const Rect& src) const {
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
bool Sprite::LoadSprite(const std::string& filename) {
|
||||
Texture* newTexture = textureManager.Load(filename);
|
||||
if(newTexture) {
|
||||
if(texture) {
|
||||
textureManager.Destroy(texture);
|
||||
}
|
||||
size.x = (float)newTexture->GetWidth();
|
||||
size.y = (float)newTexture->GetHeight();
|
||||
texture = newTexture;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Sprite::SetTexture(Texture* texture) {
|
||||
this->texture = texture;
|
||||
this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight());
|
||||
|
@ -14,10 +14,14 @@ public:
|
||||
virtual void Draw() const;
|
||||
virtual void DrawRegion(const Rect& src) const;
|
||||
|
||||
virtual bool LoadSprite(const std::string& filename);
|
||||
|
||||
const Vec2& GetPosition() const { return position; }
|
||||
float GetX(void) { return position.x; }
|
||||
float GetY(void) { return position.y; }
|
||||
const Vec2& GetSize() const { return size; }
|
||||
float GetWidth() const { return size.x; }
|
||||
float GetHeight() const { return size.y; }
|
||||
const Vec2& GetScale() const { return scale; }
|
||||
float GetRotation() const { return rotation; }
|
||||
Texture* GetTexture() { return texture; }
|
||||
|
96
src/System/ResourceManager.h
Normal file
96
src/System/ResourceManager.h
Normal file
@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
class Resource {
|
||||
public:
|
||||
virtual bool Load(const std::string& filename) = 0;
|
||||
|
||||
protected:
|
||||
Resource() : refs(0) {}
|
||||
int refs;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class ResourceManager
|
||||
{
|
||||
public:
|
||||
ResourceManager() { }
|
||||
|
||||
typedef std::map<std::string, T*> ResourceMap;
|
||||
|
||||
T* Load(const std::string& name) {
|
||||
ResourceMap::iterator i = m_resources.find(name);
|
||||
if(i == m_resources.end()) {
|
||||
T* resource = new T();
|
||||
|
||||
if(!resource->Load(name)) {
|
||||
delete resource;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
resource->refs = 1;
|
||||
|
||||
m_resources.insert(std::pair<std::string, T*>(name, resource));
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
i->second->refs++;
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
T* Find(const std::string& name) {
|
||||
ResourceMap::iterator i = m_resources.find(name);
|
||||
if(i != m_resources.end()) {
|
||||
return i->second;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Add(const std::string& name, T* resource) {
|
||||
ResourceMap::iterator i = m_resources.find(name);
|
||||
if(i == m_resources.end()) {
|
||||
resource->refs++;
|
||||
m_resources.insert(std::pair<std::string, T*>(name, resource));
|
||||
} else {
|
||||
i->second->refs++;
|
||||
}
|
||||
}
|
||||
|
||||
void Destroy(const std::string& name) {
|
||||
if(name.empty())
|
||||
return;
|
||||
|
||||
ResourceMap::iterator i = m_resources.find(name);
|
||||
if(i != m_resources.end()) {
|
||||
T* resource = i->second;
|
||||
|
||||
resource->refs--;
|
||||
|
||||
if(resource->m_refs < 1) {
|
||||
delete resource;
|
||||
m_resources.erase(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Destroy(T* resource) {
|
||||
ResourceMap::iterator i;
|
||||
for(i = m_resources.begin(); i != m_resources.end(); ++i) {
|
||||
if(i->second == resource){
|
||||
resource->refs--;
|
||||
if(resource->refs < 1) {
|
||||
m_resources.erase(i);
|
||||
delete resource;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ResourceMap m_resources;
|
||||
};
|
@ -3,6 +3,8 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
ResourceManager<Texture> textureManager;
|
||||
|
||||
static GLuint boundTexture = 0;
|
||||
|
||||
static bool IsBGR(SDL_Surface* surf) {
|
||||
@ -239,8 +241,8 @@ Texture::~Texture() {
|
||||
}
|
||||
}
|
||||
|
||||
bool Texture::Load(const char* filename) {
|
||||
if(BuildTexture(filename, &texID, GL_CLAMP, false)) {
|
||||
bool Texture::Load(const std::string& filename) {
|
||||
if(BuildTexture(filename.c_str(), &texID, GL_CLAMP, false)) {
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
|
||||
return true;
|
||||
|
@ -25,12 +25,16 @@ int LoadTGAFile(const char* filename, TGAFILE* tgaFile);
|
||||
int WriteTGAFile(const char* filename, short int width, short int height, unsigned char* textureData);
|
||||
void BindTexture(GLuint texID);
|
||||
|
||||
class Texture {
|
||||
#include "../System/ResourceManager.h"
|
||||
|
||||
class Texture : public Resource {
|
||||
template<class T> friend class ResourceManager;
|
||||
|
||||
public:
|
||||
Texture();
|
||||
~Texture();
|
||||
|
||||
bool Load(const char* filename);
|
||||
bool Load(const std::string& filename);
|
||||
|
||||
GLuint GetTexID() const { return texID; }
|
||||
int GetWidth() const { return width; }
|
||||
@ -41,3 +45,5 @@ private:
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
extern ResourceManager<Texture> textureManager;
|
||||
|
Loading…
Reference in New Issue
Block a user