Wednesday, March 31, 2010

GLTexture implementation

While using the template of OpenGL ES that comes bundle with xCode to develop OpenGL applications, I've started experimenting around with GLTexture class implementation to load images to the OpenGL ES system. Thanks to the help of 71squared Game Development Tutorials I was able to create an image texture to be display by my program.

#import "GLTexture.h"

First we imported the GLTexture header file for our EAGLView header file to find. Later on we created our texture image:


// Create texture image.

GLTexture *diamond;


This code is included in @private of @interface EAGLView : UIView. When we are done making the proper modifications in the header file we now go into the implementation file in our function -(id)initWithCoder:(NSCoder*)coder. In this case we are setting very important properties here; first, before we start drawing our GLTexture which we recently setup in our header file of EAGLView we must setup our OpenGL ES system as follows:


// Get the bounds of the main screen

CGRect rect = [[UIScreen mainScreen] bounds];

// Set up OpenGL projection matrix

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);

glMatrixMode(GL_MODELVIEW);

// Initialize OpenGL states

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);

glDisable(GL_DEPTH_TEST);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


The above source code should be familiar by now for those of you who know how to work around in the OpenGL System. We set these properties here to make our drawView function more organized, readable and understandable. Now on to our drawView function.

- (void)drawView {

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glViewport(0, 0, backingWidth, backingHeight);

// Init image

diamond = [[GLTexture alloc] initWithImage:[UIImage imageNamed:@"Sprite.png"]];

[diamond drawAtPoint:CGPointMake(160.0f, 240.0f)];

// Draw game scene

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}


As we can see our define diamond instance that we made of GLTexture is currently allocating memory to be loaded by using CG functions such as UIImage and imageNamed: @"Sprite.png" the name of our texture we are using right at this moment. Later on we simply use the function CGPointMake() to place the image where we want it located. When we compile and run the example application we get the exact image that we have in our resources folder.

Quite interesting how to load an image into the OpenGL ES system with this implementation GLTexture. I just wonder if we can rotate, translate, scale, etc... The current image we have available? 71squared decided to create and implement further functions that takes this image attach it to a OpenGL object to be display and manipulated in such ways. FYI 71squared is a YouTube user that is devoted in iPhone Game Development and has uploaded videos that explain these functionalities. Thanks 71squared!

Julio Cruz

Monday, March 29, 2010

Game State implementation.

Continuing where we left off from our last post after creating Game State and Game State Manager classes for our View-Based Application. Yet we have not discuss the auto-generated files App Delegate and View Controller .h & .m files. However we will discuss and work on these files later on when we are ready to work with them. We have not discuss Interface Builder either, which we will discuss later on when we have a good solid foundation to write meaningful source code that are use in our GUI for our app. In this particular case we are interested in writing and implementing the class functions for Game State and Game State Manager classes.

Our last post we talk about the header file for Game State where we describe and explain every line of the source code being presented. I have found this type of research and development to be a good method of study where I ask myself many questions as to how the particular peace of source code work in its smallest denominator. Finding and using the information provided by Apple Documentation is another great way to learn how to use iPhone SDK which are available for free. Quoting and citing specific information from these documentations will allows us to learn a bit about the why, how, and when to implement the particular peace of source code we are currently using. So, please do not be surprise if I use and quote the same information you would to found in Apple Documentation to explain a certain command. This way is how I learn how to further my programming knowledge about Cocoa programming and iPhone programming as well.

Enough blah, blah, blah... Let's get back to programming. To begin our Game State implementation:

#import "GameState.h"
#import "GameStateManager.h"

@implementation GameState

// GameState.m

-(id)initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager
{
if(self = [super initWithFrame: frame])
{
// Initialization code
m_pManager = pManager;
self. userInteractionEnabled = true;
}
return self;
}

-(void) Update
{

}

-(void) Render
{

}

-(void) drawRect:(CGRect)rect
{

}

@end

The first few lines of source code we imported our create header files from Game State and Game State Manager files into our Game State implementation source code. In out forth line we have @implementation Game State and our ending or closing @end line to tell the program this is the allocated area where we are going to create our implementation of the source code for our variables and functions we are going to create. Our first function we create is (id) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager, as mention in our last post according to Apple Documentation "Initializes and returns a newly allocated view object with the specified frame rectangle." we pass in a pointer variable into our function (GameStateManager*)pManager if you remember from our last post we have in our header file of GameState:

@interface GameState : UIView
{
GameStateManager * m_pManager;
}

Which we create a pointer variable that create an instance variable pointer of GameStateManager to switch between States. We also notice from last post that we also inherited from UIView properties which we discuss in our previous post. Now we have our if condition which test if the receiver frame would to initialized frame, if it can without a problem we then received the pManager pointer and assign it to our m_pManager we have create in our header file for Game State. The next line of code is self.userInteractionEnabled = True; as you can see it is a boolean variable pass to the receiver or to be more precise "a boolean value that determines whether user events are ignored and removed from the event queue...If NO, user events -- such as touch and keyboard -- are ignored and removed from the event queue. The default value is YES. To finish this function we return self; or we return the receiver.

The other functions we have are -(void) update, -(void) Render and -(void) drawRect: (CGRect) rect currently do not to be use and initialized for the moment we will later come back and describe them and how they fit into the overall planing of this game. This is all for the Game State implementation file if you have any suggestions, comments and questions about the overall implementation of the Game State files please email me at: julioykaly@hotmail.com.

Julio A. Cruz

Sunday, March 28, 2010

Framework for System Manager.

Starting our reading with the textbook of "iPhone Game Development" we start off by creating a simple application template in xCode View Based Application. In our development of the first application for 2D game development we created four different files for GameState and GameStateManager in xCode we have a .h or header file that holds our functions and variables declarations and .m file which has the implementations of the functions and variables we are currently creating and using. For our first step after creating these files we have to modified the GameState.h files to include the following source code:

-(void) doStateChange: (Class) state;

As the name of the function states this function changes the current state. In the source code GameState.h file we have the following:

#import

We implement the framework "Foundation" we need this framework to "...include classes for objects representing basic data types, collections, and operating-system services." according to apple documentation.

#include "GameStateManager.h"

We are going to need to call the GameStateManager to use it in our source code .m functions.

@interface GameState : UIView
{
GameStateManager* m_pManager;
}

An interface class GameState that inherits from UIView which creates a Game State Manager pointer to use with initWithFrame: function. UIView "provides common methods you use to create all types of views and access their properties...The UIView class provides a number of methods for managing the view hierarchy." For the moment we need the UIView to manage one view, later on we will continue discussing how UIView different subviews.

-(id) initWithFrame: (CGRect)frame andManager:(GameStateManager*)pManager;

-(id) according to apple documentation id "is a pointer to an instance of a class." Overall the documentation of -(id) initWithFrame: (CGRect) frame "initializes and returns a newly allocated view object with the specified frame rectangle." Later we just pass the newly created pointer for the GameStateManager to change states.

-(void) Render;
-(void) Update;

These functions declaration do not return a value and does not accepts any parameters or variables to be use. Simply call Render and Update function which we will discuss in our analysis in our main source code .m. Here is the complete GameState.h source code:

//

// GameState.h

// TropicalHeat

//

// Created by Julio Cruz. on 3/28/10.

// Copyright 2010 Red Fox Software. All rights reserved.

//


#import

#include "GameStateManager.h"


@interface GameState : UIView {

GameStateManager* m_pManager;

}


-(id) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager;

-(void) Render;

-(void) Update;


@end


Julio A. Cruz


Friday, March 26, 2010

Graphics Programming Black Book and iPhone Game Development

Many computer related savvy readers might remember hearing about the graphics programming textbook call "Graphics Programming Black Book by Michael Abrash", the author of other textbooks such as "Zen of Assembly Programming". Abrash focuses on optimization coding for C & C++, specialty in assembly language for 8086, 286, 386, 486, etc... I am quite impress of what Abrash had to say back then where computer limitation was a challenge compare to today microcomputer power.

Currently I am in chapter 20 of the textbook "Pentium Rules". This is still relevant to read in my opinion, not because we want to avoid the pitfalls of old CPU architectures, however we can not ignore possible improvements of implementation of algorithms and optimization habits, that will not go away anytime soon. It is true our microcomputers have become extremely powerful, with or without any, to no knowledge of optimization and algorithm we can implement and write systems that are very powerful and efficient. However it will become apparent that our computer programming skills needs to improve while we are in our search for developing video games.

This has become true for the iPhone development of a 2D video game for me. Although I might have taken the easy way out with Macromedia Flash development of a video game and later simply import it to iPhone I do think and believe I might have to still learn the iPhone architecture. Currently I am reading a textbook about 2D and 3D programming which writes a simple 2D framework and programs the behavior of game characters and objects plus objectives that needs to be completed. I will try out and see if I can use this framework to write my own 2D video game even if it ends up just being a simple puzzle game.

For the moment I do not have any source code to show off that is originally written by myself, however I was experimenting around with the example OpenGL ES template program that comes with xCode development. I did manage to draw and manipulate simple vertices and write a simple for...loop to draw a 32 x 32 pixels sprite to the OpenGL ES system however to write a visual screen I do believe it is much easier to write it in binary code the neccessary among of sprite I want to use for a 2D video game contrary with the .plist the textbook wants me to use. However using the .plist properties makes it a world of difference in simplicity for me the problem is the .plist might not work for further complex features later on.

My implementation of using vertices to create polygon images by brute force without using pointers and arrays to store it in binary files made the implementation impossible to work. According to the method I wanted to use I found a flaw of my current implementation accessing so many array elements that were dynamic instead of static which made it impossible to pass such a list to the "const GLFloat vertices[]" variable to the frame buffer of OpenGL ES with the command function that only took in a const pointer*.

Julio A. Cruz