Click here to Skip to main content
15,886,519 members
Articles / Desktop Programming / Win32

TetroGL: An OpenGL Game Tutorial in C++ for Win32 Platforms - Part 2

Rate me:
Please Sign up or sign in to vote.
4.95/5 (36 votes)
29 Mar 2009CPOL28 min read 202.4K   5.6K   95  
Learn how to load images, display them on the screen and manage them efficiently and how to display animations.
#include "TextureManager.h"
#include "Exception.h"
#include <IL/IL.h>

using namespace std;

CTextureManager::CTextureManager() : m_Textures()
{
	// Initialize DevIL
	ilInit();

	// Set the first loaded point to the 
	// upper-left corner.
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);
}

CTextureManager::~CTextureManager()
{
	TTextureMap::iterator iter = m_Textures.begin();
	for (iter; iter != m_Textures.end(); iter++)
	{
		if (iter->second)
			delete iter->second;
	}
}

CTextureManager* CTextureManager::GetInstance()
{
	// Returns the unique class instance.
	static CTextureManager Instance;
	return &Instance;
}

CTexture* CTextureManager::GetTexture(const string& strTextName)
{
	// Look in the map if the texture is already loaded.
	TTextureMap::const_iterator iter = m_Textures.find(strTextName);
	if (iter != m_Textures.end())
		return iter->second;

	// If it was not found, try to load it from file. If the load 
	// failed, delete the texture and throw an exception.
	CTexture* pNewText = NULL;
	try
	{
		pNewText = new CTexture(strTextName);
	}
	catch (CException& e)
	{
		delete pNewText;
		throw e;
	}

	// Store the newly loaded texture and return it.
	m_Textures[strTextName] = pNewText;
	return pNewText;
}

bool CTextureManager::ReleaseTexture(const std::string& strTextName)
{
	// Retrieve the texture from the map
	bool bFound = false;
	TTextureMap::iterator iter = m_Textures.find(strTextName);
	if (iter != m_Textures.end())
	{
		// If it was found, we delete it and remove the
		// pointer from the map.
		bFound = true;
		if (iter->second)
			delete iter->second;
		m_Textures.erase(iter);
	}

	return bFound;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Belgium Belgium
I am a 29 years old guy and I live with my girlfriend in Hoegaarden, little city from Belgium well known for its white beer Smile | :) .
I studied as an industrial engineer in electronics but I oriented myself more towards software development when I started to work.
Currently I am working in a research centre in mechatronica. I mainly develop in C++ but I also do a bit of Java.
When I have so spare time, I like to read (mainly fantasy) and play electric guitar.

Comments and Discussions