Sunday, April 4, 2010

Sprite animation and GLTexture usage

In our last post we have talk about GLTexture files to load to our OpenGL ES screen our sprite images we have borrow for our test program. In this post we are going to try and animate our sprites we have currently available. In this case I have decided to borrow the sprites from The Legend of Zelda A Link to the Past looking, up, down, left and right. We store these image files in our Resources folder in our xCode project we have called "Test". In our last post we have modified our header file for EAGLView for OpenGL:

//

// EAGLView.h

// Test

//

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

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

//



#import

#import

#import

#import

#import "GLTexture.h"


/*

This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.

The view content is basically an EAGL surface you render your OpenGL scene into.

Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.

*/

@interface EAGLView : UIView {

@private

/* The pixel dimensions of the backbuffer */

GLint backingWidth;

GLint backingHeight;

EAGLContext *context;

/* OpenGL names for the renderbuffer and framebuffers used to render to this view */

GLuint viewRenderbuffer, viewFramebuffer;

/* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */

GLuint depthRenderbuffer;

NSTimer *animationTimer;

NSTimeInterval animationInterval;

// Create texture image.

GLTexture *diamond[6];

}


@property NSTimeInterval animationInterval;


- (void)startAnimation;

- (void)stopAnimation;

- (void)drawView;


@end


Nothing new in this source file except for GLTexture *diamond[6]; in this case we have a GLTexture array that will take about five unique images to be store in each individual array we have. After we have modified our EAGLView header file we are now really to write the functions to display the images and determine the time frame of each image to be display. In this case in our implementation file of EAGLView we have:

// Define the space to hold the global variables for the frame counter

unsigned int frameCounter;

double totalRenderingTime;


We are going to determine the first frame we have display on our OpenGL screen image and get the current time for our program later on we must determine the starting time and end time to determine our total rendering time. For example source code:


- (void)drawView {

#ifdef DEBUG

double startTime = CFAbsoluteTimeGetCurrent();

#endif

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glViewport(0, 0, backingWidth, backingHeight);

// Draw game scene

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];


#ifdef DEBUG

double endTime = CFAbsoluteTimeGetCurrent();

totalRenderingTime += (endTime - startTime);

#endif

}


With CFAbsoluteTimeGetCurrent() function we can obviously determine the current time or our start time. After the game draws the current frame we must store our ending time frame. Later on we must determine the render time or the total frames that we have taken to draw to the screen. This sounds fun and easy however we must now determine when our user wants to tap our screen to see the sprites to animate. For this type of functionality we have to write a touch function:


// Detect touches of the user and animate sprite image.


#pragma mark -

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView: self];

if(point.x >= 0 && point.x <= 160)

{

if(point.y >= 0 && point.y <= 480)

{

// Init images

switch (frameCounter) {

case 1:

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

[diamond[frameCounter] drawAtPoint:CGPointMake(diamond[frameCounter].contentSize.width, diamond[frameCounter].contentSize.height)];

NSLog(@"Frame Counter: %d", frameCounter);

break;

case 2:

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

[diamond[frameCounter] drawAtPoint:CGPointMake(diamond[frameCounter].contentSize.width, diamond[frameCounter].contentSize.height)];

NSLog(@"Frame Counter: %d", frameCounter);

break;

case 3:

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

[diamond[frameCounter] drawAtPoint:CGPointMake(diamond[frameCounter].contentSize.width, diamond[frameCounter].contentSize.height)];

NSLog(@"Frame Counter: %d", frameCounter);

break;

case 4:

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

[diamond[frameCounter] drawAtPoint:CGPointMake(diamond[frameCounter].contentSize.width, diamond[frameCounter].contentSize.height)];

NSLog(@"Frame Counter: %d", frameCounter);

break;

case 5:

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

[diamond[frameCounter] drawAtPoint:CGPointMake(diamond[frameCounter].contentSize.width, diamond[frameCounter].contentSize.height)];

NSLog(@"Frame Counter: %d", frameCounter);

break;

case 6:

frameCounter = 0;

NSLog(@"Frame Counter: %d", frameCounter);

break;

default:

break;

}

}

frameCounter++;

NSLog(@"Frame Counter: %d",frameCounter);

}

}


In this function we have done several important things:

1) Touch function to detect our users taps on the screen.

2) We have specificed in what view: self.

3) We have determine the point in our view. (Left side tap.).

4) Switch statement for frameCounter.

5) If we have any frame we draw and allocate memory for this specific image being call for this frame.

6) If frame equals 6 frameCounter equals 0 to start at the first frame.

Several disadvantages I see from this implementation is that switch statement that takes 6 calls to switch statement to create a new image. Another disadvantage from this implementation function is that we have to allocate memory each time they are being call. This is not good in the perspective that when we want to animate our character we will have a different quad object for OpenGL to draw our images instead of using one quad object to draw all of our image we need to fully animate our sprite. The sprite animation will be in our second part video for next week.

Julio A. Cruz

Friday, April 2, 2010

iPad pricing.

According to Yahoo.com news internet articles the new and improve apps for the iPad would to cost double the amount at minimum from $0.99 dollars to $1.99 dollars for the minimum pricing. Personally it is not a big deal to make the necessary modifications to the apps and even build up new applications for iPad. What really justified just a jacking up of pricing from the small price $0.99 dollars to $1.99 dollars for minimum pricing? I personally think that depending on the magnitude of the application for the iPhone and iPod touch it should be price. Such as my first app that I created for the iPhone and iPod touch which was Dante the Inferno digital textbook which I priced at the lowest price of $0.99 dollars. Would I dare to bump up the pricing just for the reason of the iPad came around or should there be more to the pricing then meets the eye.

Yahoo News Article: http://news.yahoo.com/s/ytech_wguy/20100331/tc_ytech_wguy/ytech_wguy_tc1397_1

Julio A. Cruz

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

Monday, January 25, 2010

"Gold Farming" in MMORPGs games!

This was a recent post from the college course Modification and Level Design where I discuss and answer the question of "Gold Farming" the pros and cons in terms of some game players taking advantage of Auto-Bots doing the "Gold Farming" for them.

"Gold Farming"

I have heard from my oldest brother, Carlos, that at his workplace there have been some few young engineers who buy very expensive online items for their avatars for WOW for example and this is real money exchange. I wonder how much of those items were just a simple "gold farming" and robbing other ignocent players who want to just get a good item. However I have heard about players who pay other players to play for them and level up for their avatars.

The only negative I see about these things whether it is fare for other players to be doing such a thing or this particular player when he or she wants to join a clan for example the clan will know this player is not as good as he should be or at least at the level that supposedly be. This could be very dangerous as well because if many people are doing these types of tricks or simply looking for a easier way to advance in this MMO they will unbalance the game for many others.

I have heard of a MMORPG that many players started doing similar things "gold farming" and getting very expensive items for the appropriate price however they themselves were starting to sell these items double the amount that these cost. This unbalance gameplay for many players who do not want to pay so much money, however what if other players could make money transfer and purchase the item and the leveling up what hopes would a new player have facing bad ass players that level-up unfairly.

By the way that particular MMORPG has been suffering ever since. I personally could not get into MMORPG yet, however I was interested in WOW for a while, however I have heard that there are many kids going there and not hard core gamers or at least not as much."

Julio A. Cruz

Saturday, January 23, 2010

Dante The Inferno V. 2.0 Sales and Torque Game Engine

Hello again its been a while since my last post. Sorry but I have been so busy with college work lately taking a class about Modification and Level Design. Currently doing quite well with the software I am using, it is call Torque Game Engine. You can find more information about Torque Game Engine at link: http://www.torquepowered.com/ . With the software version of the SDK 1.5.2 of TGE, I have been learning how to create terrains and how to modified them in its height, textures, place static and movable objects and much more.

Other features TGE has is the Torque-Script which is use for game play mechanics, create and instantiate objects and programs its behaviors among other great features I have not been able to learn as of yet. For this week assignment I had to learn how to place textures appropriately with the seamless feature or repeated tiles and textures. This takes the user from the immersion of the video game and takes out the realism. Another objective in TGE I needed to learn was how to use the console window of the TGE to translate, rotate, and scale objects in the world editor.

Later I use the Torque-Script by using the software Torsion which is an editor for TGE for scripting purposes, please note this is not the only editor that could do this for TGE. In this editor I learn to program the executable to accept my script and execute them instead of calling the console itself and using the line command. However I have also learn how to do this through console window of TGE world editor. Other objectives I needed to complete was to include lighting features, items that could pick up, sounds or music, etc... I needed to only complete two task, however I have decided to include a simple feature which is weather.

Raining atmosphere with the texture of the terrain to be an icy roam which it since to the player that it is snowing hard. By increasing the fog tensity I have made a scary atmosphere as I have planned to do. By the end of this modification of this particular level I will hope to accomplish at least by the end of Modification and Level Design a Mod level based on a FPS (First Person Shooter) of a Zombies outbreak out in the snowy mountains of Alaska. Oh, I almost forgot to tell you about the history behind the mod. I guess I will leave that out for now until I have finish and later I will post on youtube a video sample of the mod.

In the other hand I have recently receive email from Apple Developer Center that Dante The Inferno V. 2.0 application for the iPhone and iPod Touch has been purchase. I would like a moment to thank all of you who have downloaded and hopefully enjoy Dante's poem about the Inferno. I do also hope the commentaries included in the beginning of each Cantos will help out in giving out the big picture of the particular Cantos the reader is about to read. We at Red Fox Software thank you all our clients and avid readers out there for their purchased and if you have any need of help with out software please drop us a line at julioykaly@hotmail.com.

Julio A. Cruz