Click here to Skip to main content
15,892,480 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.9K   5.6K   95  
Learn how to load images, display them on the screen and manage them efficiently and how to display animations.
#include "Image.h"
#include "Exception.h"
#include "TextureManager.h"

using namespace std;

CImage::CImage(const string& strFileName) 
  : m_pTexture(NULL), m_rectTextCoord()
{
	// This line will throw an exception if the texture is not found. 
	m_pTexture = CTextureManager::GetInstance()->GetTexture(strFileName); 
	m_pTexture->AddReference();

	// Set the texture coordinate to the full texture
	m_rectTextCoord.m_Top = m_rectTextCoord.m_Left = 0;
	m_rectTextCoord.m_Bottom = m_pTexture->GetHeight();
	m_rectTextCoord.m_Right = m_pTexture->GetWidth();
}

CImage::CImage(const string& strFileName, const TRectanglei& textCoord)
  : m_pTexture(NULL), m_rectTextCoord(textCoord)
{
	// This line will throw an exception if the texture is not found. 
	m_pTexture = CTextureManager::GetInstance()->GetTexture(strFileName); 
	m_pTexture->AddReference();
}

TImagePtr CImage::CreateImage(const string& strFileName)
{
	TImagePtr imgPtr(new CImage(strFileName));
	return imgPtr;
}

TImagePtr CImage::CreateImage(const string& strFileName, const TRectanglei& textCoord)
{
	TImagePtr imgPtr(new CImage(strFileName,textCoord));
	return imgPtr;
}


CImage::~CImage()
{
	if (m_pTexture)
		m_pTexture->ReleaseReference();
}

void CImage::BlitImage(int iXOffset, int iYOffset) const
{
	if (m_pTexture)
	{
		m_pTexture->Bind();

		// Get the coordinates of the image in the texture, expressed
		// as a value from 0 to 1.
		float Top	 = ((float)m_rectTextCoord.m_Top)/m_pTexture->GetHeight();
		float Bottom = ((float)m_rectTextCoord.m_Bottom)/m_pTexture->GetHeight();
		float Left   = ((float)m_rectTextCoord.m_Left)/m_pTexture->GetWidth();
		float Right  = ((float)m_rectTextCoord.m_Right)/m_pTexture->GetWidth();

		// Draw the textured rectangle.
		glBegin(GL_QUADS);
		glTexCoord2f(Left,Top);		glVertex3i(iXOffset,iYOffset,0);
		glTexCoord2f(Left,Bottom);	glVertex3i(iXOffset,iYOffset+m_rectTextCoord.GetHeight(),0);
		glTexCoord2f(Right,Bottom);	glVertex3i(iXOffset+m_rectTextCoord.GetWidth(),iYOffset+m_rectTextCoord.GetHeight(),0);
		glTexCoord2f(Right,Top);	glVertex3i(iXOffset+m_rectTextCoord.GetWidth(),iYOffset,0);
		glEnd();
	}
}


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