May 05

From my previous example, I’ve refactored the texture class. I’ve moved some of the common code into a new method.

/*
 *  mcaTexture.h
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
 
#ifndef  __MCA_TEXTURE__
#define  __MCA_TEXTURE__
 
#ifdef TARGET_OS_IPHONE
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#endif
 
class mcaTexture {
 
protected:
 
	GLuint m_texture;	// OpenGL name for the  texture
 
	void initTexture( size_t w, size_t h, int iType, GLvoid *pImage );
 
public:
 
	GLuint getTexture();
 
	void initWithChecks();
 
	void initFromImage( NSString *location);
 
};
 
#endif
/*
 *  mcaTexture.mm
 *  mcaGL1
 *
 *  Created by Mitchell Allen on 4/24/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
 
#include "mcaTexture.h"
 
#define checkImageWidth	64
#define checkImageHeight 64
static GLubyte checkImage[ checkImageHeight][checkImageWidth ][ 4 ];
 
GLuint mcaTexture::getTexture()
{
	return m_texture;
}
 
void mcaTexture::initTexture( size_t w, size_t h, int iType, GLvoid *pImage )
{
	NSLog( @"START: mcaTexture::initTexture( '...' )" );
 
	GLint					saveName;
 
	// Now use OpenGL ES to generate a name for the texture.
	// Pass by reference so that our texture variable gets set.
 
	glGenTextures(1, &m_texture);
 
	glGetIntegerv(GL_TEXTURE_BINDING_2D, &saveName);
 
	// Bind the texture name. 
	glBindTexture(GL_TEXTURE_2D, m_texture);
 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
 
	// Specify a 2D texture image, providing a pointer to the image data in memory
 
	switch( iType ) {
		case 1:
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImage);
			break;
		case 2:
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pImage);
			break;
		case 3:
			glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pImage);
			break;
		default:
			break;
	}
 
	glBindTexture(GL_TEXTURE_2D, saveName);
 
	NSLog( @"END: mcaTexture::initTexture()" );
}
 
void mcaTexture::initWithChecks() 
{
	NSLog( @"START: mcaTexture::initWithChecks( '...' )" );
 
	int i, j, c;
 
	for( i = 0; i < checkImageHeight; i++ ) {
		for( j = 0; j < checkImageWidth; j++ ) {
			c = ((((i&0x8)==0)^((j&0x8))==0))*255;
			checkImage[i][j][0] = (GLubyte) c;
			checkImage[i][j][1] = (GLubyte) c;
			checkImage[i][j][2] = (GLubyte) c;
			checkImage[i][j][3] = (GLubyte) 255;  // For ghost effect ( 255 / 2 )
		}
	}
 
	initTexture( checkImageWidth, checkImageHeight, 1, checkImage );
 
	NSLog( @"END: mcaTexture::initWithChecks( '...' )" );
}
 
 
void mcaTexture::initFromImage( NSString *location)
{
	NSLog( @"START: mcaTexture::initFromImage( '...' )" );
 
	// Creates a Core Graphics image from an image file using our location.
	CGImageRef spriteImage = [UIImage imageNamed:location].CGImage;
 
	// Get the width and height of the image.
	size_t w = CGImageGetWidth(spriteImage);
	size_t h = CGImageGetHeight(spriteImage);
 
	//TODO - resize the width and the height to the nearest power of 2.
 
	//Only create a sprite if we were able to properly load the CG image.
	if(spriteImage)
	{
		NSLog( @"... image loaded successfully ..." );
 
		// Allocated memory needed for the bitmap context
		GLubyte *spriteData = (GLubyte *) malloc(w * h * 4);
 
		// Use the bitmap creation function provided by the Core Graphics framework. 
		CGContextRef spriteContext = CGBitmapContextCreate(spriteData, w, h, 8, w * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
 
		// After we create the context, we can draw the sprite image to the context.
		CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)w, (CGFloat)h), spriteImage);
 
		// We don't need the context at this point, so we need to release it to avoid memory leaks.
		CGContextRelease(spriteContext);
 
		initTexture( w, h, 1, spriteData );
 
		// Release the image data, which is now unused.
		free(spriteData);
	}
 
	GLenum	errGL = glGetError();
 
	NSLog( @"... glGetError = %1 ...", errGL );
 
	NSLog( @"END: mcaTexture::initFromImage( '...' )" );
}

Tags: ,

One Response to “Refactored Texture Class”

  1. Experimental Texture Class Says:

    [...] Mixing C++ and Objective-C Refactored Texture Class May [...]

Leave a Reply