[Add] Loading texture from sprite class.

[Add] Resource management!
This commit is contained in:
Tamir Atias 2012-04-12 01:28:26 +03:00
parent 96a2929c08
commit b2fb84dcee
9 changed files with 140 additions and 16 deletions

View File

@ -98,6 +98,7 @@
<ClInclude Include="..\..\src\Math\Vec2.h" /> <ClInclude Include="..\..\src\Math\Vec2.h" />
<ClInclude Include="..\..\src\Sprite\Sprite.h" /> <ClInclude Include="..\..\src\Sprite\Sprite.h" />
<ClInclude Include="..\..\src\System\Debug.h" /> <ClInclude Include="..\..\src\System\Debug.h" />
<ClInclude Include="..\..\src\System\ResourceManager.h" />
<ClInclude Include="..\..\src\Texture\Texture.h" /> <ClInclude Include="..\..\src\Texture\Texture.h" />
<ClInclude Include="..\..\src\TMXParser\base64.h" /> <ClInclude Include="..\..\src\TMXParser\base64.h" />
<ClInclude Include="..\..\src\TMXParser\Tmx.h" /> <ClInclude Include="..\..\src\TMXParser\Tmx.h" />

View File

@ -126,6 +126,9 @@
<ClInclude Include="..\..\src\TMXParser\TmxObject.h"> <ClInclude Include="..\..\src\TMXParser\TmxObject.h">
<Filter>TMXParser</Filter> <Filter>TMXParser</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\System\ResourceManager.h">
<Filter>System</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Main\main.cpp"> <ClCompile Include="..\..\src\Main\main.cpp">

View File

@ -6,17 +6,12 @@ Player::Player(void) {
} }
Player::~Player(void) { Player::~Player(void) {
delete _player->GetTexture();
delete _player; delete _player;
} }
void Player::Prepare(void) { void Player::Prepare(void) {
_player = new Sprite(); _player = new Sprite();
_playerTexture = new Texture(); _player->LoadSprite("../Data/Img/Player.png");
_playerTexture->Load("../Data/Img/Player.png");
_player->SetTexture(_playerTexture);
_player->SetPosition(Vec2(800/2, 600/2));
_player->SetScale(Vec2(4.5f, 4.5f));
} }
void Player::Render(void) { void Player::Render(void) {

View File

@ -1,4 +1,4 @@
#include "../Texture/Texture.h" #pragma once
#include "../Sprite/Sprite.h" #include "../Sprite/Sprite.h"
#include "../Global/Globals.h" #include "../Global/Globals.h"
#include "../System/Debug.h" #include "../System/Debug.h"
@ -19,6 +19,5 @@ public:
private: private:
float PLAYER_SPEED; float PLAYER_SPEED;
Sprite* _player; Sprite* _player;
Texture* _playerTexture;
float _rotationAngle; float _rotationAngle;
}; };

View File

@ -10,6 +10,10 @@ Sprite::Sprite() {
} }
Sprite::~Sprite() { Sprite::~Sprite() {
if(texture) {
textureManager.Destroy(texture);
texture = NULL;
}
} }
void Sprite::Update(float dt) { void Sprite::Update(float dt) {
@ -74,6 +78,20 @@ void Sprite::DrawRegion(const Rect& src) const {
glPopMatrix(); 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) { void Sprite::SetTexture(Texture* texture) {
this->texture = texture; this->texture = texture;
this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight()); this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight());

View File

@ -14,10 +14,14 @@ public:
virtual void Draw() const; virtual void Draw() const;
virtual void DrawRegion(const Rect& src) const; virtual void DrawRegion(const Rect& src) const;
virtual bool LoadSprite(const std::string& filename);
const Vec2& GetPosition() const { return position; } const Vec2& GetPosition() const { return position; }
float GetX(void) { return position.x; } float GetX(void) { return position.x; }
float GetY(void) { return position.y; } float GetY(void) { return position.y; }
const Vec2& GetSize() const { return size; } const Vec2& GetSize() const { return size; }
float GetWidth() const { return size.x; }
float GetHeight() const { return size.y; }
const Vec2& GetScale() const { return scale; } const Vec2& GetScale() const { return scale; }
float GetRotation() const { return rotation; } float GetRotation() const { return rotation; }
Texture* GetTexture() { return texture; } Texture* GetTexture() { return texture; }

View 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;
};

View File

@ -3,6 +3,8 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
ResourceManager<Texture> textureManager;
static GLuint boundTexture = 0; static GLuint boundTexture = 0;
static bool IsBGR(SDL_Surface* surf) { static bool IsBGR(SDL_Surface* surf) {
@ -239,8 +241,8 @@ Texture::~Texture() {
} }
} }
bool Texture::Load(const char* filename) { bool Texture::Load(const std::string& filename) {
if(BuildTexture(filename, &texID, GL_CLAMP, false)) { 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_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
return true; return true;

View File

@ -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); int WriteTGAFile(const char* filename, short int width, short int height, unsigned char* textureData);
void BindTexture(GLuint texID); void BindTexture(GLuint texID);
class Texture { #include "../System/ResourceManager.h"
class Texture : public Resource {
template<class T> friend class ResourceManager;
public: public:
Texture(); Texture();
~Texture(); ~Texture();
bool Load(const char* filename); bool Load(const std::string& filename);
GLuint GetTexID() const { return texID; } GLuint GetTexID() const { return texID; }
int GetWidth() const { return width; } int GetWidth() const { return width; }
@ -41,3 +45,5 @@ private:
int width; int width;
int height; int height;
}; };
extern ResourceManager<Texture> textureManager;