Topic:   My iPhone Game Tutorials for Beginners   (Read 362627 times)


0 Members and 1 Guest are viewing this topic.

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #105 on: July 21, 2010, 05:35:57 PM »
Gandolf you are a legend!!!!
I can hardly wait.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #106 on: July 23, 2010, 01:10:44 AM »
Quote
Gandolf you are a legend!!!!
No, not yet at least.

Here's a teaser of the Ultimate iPhone Template running in Xcode 4:
Teaser

Highlights:
*All code only needs to be in the App Delegate.
*The menu is made in Interface Builder. Has animations.
*OpenGl drawing.
*iOS 4 multitasking friendly.
*Made of awesome.


-Gan
P.S. Hurray for midnight screencast recording.
« Last Edit: July 23, 2010, 01:13:21 AM by Gandolf »

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: My iPhone Game Tutorials for Beginners
« Reply #107 on: July 23, 2010, 06:02:31 AM »
Wow, that looks really self explanatory! :o I'll have to get cracking on the rest of your tutorials when I free up some time.
I survived the spammage of 2007

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #108 on: July 29, 2010, 09:17:11 PM »
YES!!!! I can't wait!

But for now I am using the OpenGL template that Gandolf posted. I was wondering how to make the game so it doesn't automatically rotate when it starts? I want to make a game where you hold the device upright, but I don't know what code to change to make it rotate to an upright position.

Thanks in advance!

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #109 on: July 29, 2010, 10:52:44 PM »
To make it stay in portrait mode go to the left bar, go to the Resources folder, click the Info.plist file then change the UIInterfaceOrientation to UIInterfaceOrientationPortrait.

Then to make OpenGL draw for portrait mode you...
Replace the drawImage method with this:
Code: [Select]
- (void)drawImage:(Image*)image AtPoint:(CGPoint)point {
[image renderAtPoint:CGPointMake(point.x, 480-point.y - image.imageHeight) centerOfImage:NO];
}

Replace the 3 touches methods with this:
Code: [Select]
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray* allTouches = [touches allObjects];
NSArray* eventTouches = [[event allTouches] allObjects];

for (int i = 0; i < [eventTouches count]; i +=1) {
if ([eventTouches objectAtIndex:i] == [allTouches objectAtIndex:0]) {
//touchedScreenBegan1 = [[eventTouches objectAtIndex:i] locationInView:self];
//Rotated touch to landscape
CGPoint touch = CGPointMake([[eventTouches objectAtIndex:i] locationInView:self].x,[[eventTouches objectAtIndex:i] locationInView:self].y);
touchedScreenBegan1 = touch;
[self setNeedsDisplay];
}
if ([allTouches count] > 1) {
if ([eventTouches objectAtIndex:i] == [allTouches objectAtIndex:1]) {
//Rotated touch to landscape
CGPoint touch = CGPointMake([[eventTouches objectAtIndex:i] locationInView:self].x,[[eventTouches objectAtIndex:i] locationInView:self].y);
touchedScreenBegan2 = touch;
}
}
}
//CGPoint location = [touch locationInView:self];
// tell the view to redraw
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
NSArray* allTouches = [touches allObjects];
NSArray* eventTouches = [[event allTouches] allObjects];

for (int i = 0; i < [eventTouches count]; i +=1) {
if ([eventTouches objectAtIndex:i] == [allTouches objectAtIndex:0]) {
//Rotated touch to landscape
CGPoint touch = CGPointMake([[eventTouches objectAtIndex:i] locationInView:self].x,[[eventTouches objectAtIndex:i] locationInView:self].y);
touchedScreenMoved1 = touch;
}
if ([allTouches count] > 1) {
if ([eventTouches objectAtIndex:i] == [allTouches objectAtIndex:1]) {
//Rotated touch to landscape
CGPoint touch = CGPointMake([[eventTouches objectAtIndex:i] locationInView:self].x,[[eventTouches objectAtIndex:i] locationInView:self].y);
touchedScreenMoved2 = touch;
}
}
}
//CGPoint location = [touch locationInView:self];
// tell the view to redraw
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray* allTouches = [touches allObjects];
NSArray* eventTouches = [[event allTouches] allObjects];

for (int i = 0; i < [eventTouches count]; i +=1) {
if ([eventTouches objectAtIndex:i] == [allTouches objectAtIndex:0]) {
//Rotated touch to landscape
CGPoint touch = CGPointMake([[eventTouches objectAtIndex:i] locationInView:self].x,[[eventTouches objectAtIndex:i] locationInView:self].y);
touchedScreenEnded1 = touch;
}
if ([allTouches count] > 1) {
if ([eventTouches objectAtIndex:i] == [allTouches objectAtIndex:1]) {
//Rotated touch to landscape
CGPoint touch = CGPointMake([[eventTouches objectAtIndex:i] locationInView:self].x,[[eventTouches objectAtIndex:i] locationInView:self].y);
touchedScreenEnded2 = touch;
}
}
}
//CGPoint location = [touch locationInView:self];
// tell the view to redraw
}

And finally replace the initWithCoder method with this:

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #110 on: July 29, 2010, 10:53:01 PM »
Code: [Select]
//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder {
 Â   
 Â   if ((self = [super initWithCoder:coder])) {
 Â       // Get the layer
 Â       CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
 Â       
 Â       eaglLayer.opaque = YES;
 Â       eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
 Â                                       [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
 Â       
 Â       context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
 Â       
 Â       if (!context || ![EAGLContext setCurrentContext:context]) {
 Â           [self release];
 Â           return nil;
 Â       }

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);
glViewport(0, 0, rect.size.width, rect.size.height);


// Initialize OpenGL states
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);
glEnableClientState(GL_VERTEX_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

testSprite = [[Image alloc] initWithImage:[UIImage imageNamed:@"Npc 1.png"]];
movingThings = [NSMutableArray new];
for (int i = 0; i < 300; i ++) {
Sprite* toAdd = [[Sprite new] initWithImage:testSprite position:CGPointMake(rand()%320, rand()%480)];
[movingThings addObject:toAdd];
}


touchedScreen1 = CGPointMake(-1, -1);
touchedScreen2 = CGPointMake(-1, -1);
updateScreen = TRUE;

middleScreen = CGPointMake((177 - 46/2)*2,(160 - 46/2)*2);

gameTimer = [NSTimer scheduledTimerWithTimeInterval:(float)1/60 target:self selector:@selector(mainGameLoop) userInfo:nil repeats:YES];
screenDimensions = CGPointMake([self bounds].size.height, [self bounds].size.width);
}
 Â   return self;
}

Sorry about that, didn't stop to think of differently oriented games.


-Gan
« Last Edit: July 29, 2010, 10:53:42 PM by Gandolf »

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #111 on: July 29, 2010, 11:10:33 PM »
Thank you! Problem solved. Just one thing - in the initWithCoder method, screenDimensions was initialized slightly wrong:

screenDimensions = CGPointMake([self bounds].size.height, [self bounds].size.width);

height and width are mixed up. When I changed it to this, it worked:

screenDimensions = CGPointMake([self bounds].size.width, [self bounds].size.height);

Thanks for all the help! Hope you can release the Ultimate iPhone Game Template soon!

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #112 on: August 09, 2010, 06:16:19 PM »
Gandolf I was wondering, how does the Ultimate iPhone Template work with iPhone 4 if the iPhone 4 screen is 940x640(I think)? Does it scale everything, or leave black spots on the screen?

Please release it soon! Or at least a beta version!!

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #113 on: August 09, 2010, 07:49:06 PM »
Works like normal though if you display uiimages it just shows them in a higher definition.


-Gan

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #114 on: August 10, 2010, 01:06:29 PM »
Here's a beta:
Ultimate iPhone Template Beta

Should work perfectly. You design the menu in Interface Builder, all main code goes in the app delegate, supports multitasking, and it's compatible with iOS 3.1+. It's also fast, very fast.


-Gandolf

Gnome


  • GMG Extraordinaire

  • ***


  • Posts: 1073
Re: My iPhone Game Tutorials for Beginners
« Reply #115 on: August 10, 2010, 01:53:39 PM »
I have posted for the first time in a month to say:

[glb][size=18]YAY!!!??!!!111!!!one!!1[/size][/glb]
This Cannot be, NOOOOOOOO!!!!

-Gnomes Cry when the McRib was discontinued again.

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #116 on: August 10, 2010, 05:13:10 PM »
Thank you! It is AWESOME!!!!!!

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #117 on: August 10, 2010, 05:52:03 PM »
Thank you. I was wondering how, for the Ultimate iPhone Template, to make it rotate so that it is upright? Also, I noticed that when you ran the game, if you clicked the "play" button it would run fine, but then I made a button to go back to the main menu, and if you pressed this and then clicked "play" again the screen would be rotated strangely. I changed the code in the beginning of GameView.m to this and it seemed to fix the problem:

#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>

#import "GameView.h"
#import "AppDelegate.h"

#define USE_DEPTH_BUFFER 0


@implementation GameView

@synthesize context;


//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder {
    
    if ((self = [super initWithCoder:coder])) {
        // Get the layer
        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
        
        eaglLayer.opaque = YES;
        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
        
        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
        
        if (!context || ![EAGLContext setCurrentContext:context]) {
            [self release];
            return nil;
        }
            
            //CGRect rect = [[UIScreen mainScreen] bounds];
            
                  
            // Set up OpenGL projection matrix
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrthof(0, 480, 0, 320, -1, 1);
            glViewport(0, 0, 480, 320);
            //glTranslatef(-100, -320/2, 0.0f );
            glMatrixMode(GL_MODELVIEW);
            //glTranslatef(-0, -240.0f, 0.0f );
            //glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
            //glScalef(-1.0, 1.0, 1.0);
            
            
            // Initialize OpenGL states
            //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
            glDisable(GL_DEPTH_TEST);
            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);
            glEnableClientState(GL_VERTEX_ARRAY);
            glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            
            
            screenDimensions = CGPointMake([self bounds].size.width, [self bounds].size.height);
            //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
            rotationNeg = CGPointMake(1, -1);
            rotationOffset = CGPointMake(0, 320);
            glRotatef(-90.0, 0.0f, 0.0f, 1.0f);
            glTranslatef(-480.0, 0.0, 0.0f );
            //[self setTransform:CGAffineTransformMakeRotation(M_PI/2)];
            [self setFrame:CGRectMake(0, 0, 320, 480)];
            //[gameView setCenter:CGPointMake(320/4, 320/2)];
            // Set up OpenGL projection matrix
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrthof(0, 320, 0, 480, -1, 1);
            glViewport(0, 0, 320, 480);
            //glTranslatef(-100, -320/2, 0.0f );
            glMatrixMode(GL_MODELVIEW);
            //glTranslatef(-0, -240.0f, 0.0f );
            //glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
            //glScalef(-1.0, 1.0, 1.0);
            
            
            // Initialize OpenGL states
            //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
            glDisable(GL_DEPTH_TEST);
            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);
            glEnableClientState(GL_VERTEX_ARRAY);
            glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
      
      }
    return self;
      
}


- (void)renderScene {
// Make sure we are renderin to the frame buffer
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

// Clear the color buffer with the glClearColor which has been set
glClear(GL_COLOR_BUFFER_BIT);

AppDelegate* delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

// Save the current matrix to the stack
glPushMatrix();

// Set client states so that the Texture Coordinate Array will be used during rendering
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

// Enable Texture_2D
glEnable(GL_TEXTURE_2D);

// Enable blending as we want the transparent parts of the image to be transparent
glEnable(GL_BLEND);

[delegate drawGame];

// Now we are done drawing disable blending
glDisable(GL_BLEND);

// Disable as necessary
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

// Restore the saved matrix from the stack
glPopMatrix();

// Switch the render buffer and framebuffer so our scene is displayed on the screen
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}
« Last Edit: August 10, 2010, 05:54:22 PM by I-NEED-HELP »

I-NEED-HELP


  • GMG Newbie

  • *


  • Posts: 40

  • I love YaBB 1G - SP1!
Re: My iPhone Game Tutorials for Beginners
« Reply #118 on: August 10, 2010, 05:52:15 PM »
- (void)setOrientation:(UIInterfaceOrientation)interfaceOrientation {
/* if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
rotationNeg = CGPointMake(-1, 1);
rotationOffset = CGPointMake(480, 0);
glRotatef(90.0, 0.0f, 0.0f, 1.0f);
glTranslatef(0, -320.0, 0.0f );
//[self setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
[self setFrame:CGRectMake(0, 0, 320, 480)];
//[gameView setCenter:CGPointMake(320/4, 320/2)];
// Set up OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, 320, 0, 480, -1, 1);
glViewport(0, 0, 320, 480);
//glTranslatef(-100, -320/2, 0.0f );
glMatrixMode(GL_MODELVIEW);
//glTranslatef(-0, -240.0f, 0.0f );
//glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
//glScalef(-1.0, 1.0, 1.0);


// Initialize OpenGL states
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);
glEnableClientState(GL_VERTEX_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
rotationNeg = CGPointMake(1, -1);
rotationOffset = CGPointMake(0, 320);
glRotatef(-90.0, 0.0f, 0.0f, 1.0f);
glTranslatef(-480.0, 0.0, 0.0f );
//[self setTransform:CGAffineTransformMakeRotation(M_PI/2)];
[self setFrame:CGRectMake(0, 0, 320, 480)];
//[gameView setCenter:CGPointMake(320/4, 320/2)];
// Set up OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, 320, 0, 480, -1, 1);
glViewport(0, 0, 320, 480);
//glTranslatef(-100, -320/2, 0.0f );
glMatrixMode(GL_MODELVIEW);
//glTranslatef(-0, -240.0f, 0.0f );
//glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
//glScalef(-1.0, 1.0, 1.0);


// Initialize OpenGL states
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);
glEnableClientState(GL_VERTEX_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
} */
}
« Last Edit: August 10, 2010, 05:54:30 PM by I-NEED-HELP »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #119 on: August 10, 2010, 06:27:32 PM »
Sorry, I tend to forget people use portrait mode for games...
Ultimate iPhone Template Beta - Portrait


-Gan