Randomize

by nik 17. May 2009 11:22

-(int)randomInRange:(NSRange)range except:(NSArray*)exceptionArray{

int r ;

BOOL isSame = TRUE;

while (isSame) {

isSame = FALSE;

r = rand() % range.length + range.location;

for (NSNumber *number in exceptionArray) {

if([number intValue] == r){

isSame = TRUE;

break;

}

}

}

return r;

}

 
//
 

[self randomInRange:NSMakeRange(1, 10) except:[NSArray arrayWithObjects:[NSNumber numberWithInt:2],[NSNumber numberWithInt:1], nil]];

 

 

Currently rated 3.1 by 8 people

  • Currently 3.125/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

General | General

Create openGL texture

by nik 13. May 2009 13:58

- (void)createGLTexture:(GLuint *)texName fromCGImage:(CGImageRef)img

{

GLubyte *spriteData = NULL;

CGContextRef spriteContext;

GLuint imgW, imgH, texW, texH;

imgW = CGImageGetWidth(img);

imgH = CGImageGetHeight(img);

// Find smallest possible powers of 2 for our texture dimensions

for (texW = 1; texW < imgW; texW *= 2) ;

for (texH = 1; texH < imgH; texH *= 2) ;

// Allocated memory needed for the bitmap context

spriteData = (GLubyte *) calloc(texH, texW * 4);

// Uses the bitmatp creation function provided by the Core Graphics framework. 

spriteContext = CGBitmapContextCreate(spriteData, texW, texH, 8, texW * 4, CGImageGetColorSpace(img), kCGImageAlphaPremultipliedLast);

// Translate and scale the context to draw the image upside-down (conflict in flipped-ness between GL textures and CG contexts)

CGContextTranslateCTM(spriteContext, 0., texH);

CGContextScaleCTM(spriteContext, 1., -1.);

// After you create the context, you can draw the sprite image to the context.

CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, imgW, imgH), img);

// You don't need the context at this point, so you need to release it to avoid memory leaks.

CGContextRelease(spriteContext);

// Use OpenGL ES to generate a name for the texture.

glGenTextures(1, texName);

// Bind the texture name. 

glBindTexture(GL_TEXTURE_2D, *texName);

// Speidfy a 2D texture image, provideing the a pointer to the image data in memory

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texW, texH, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

// Set the texture parameters to use a minifying filter and a linear filer (weighted average)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Enable use of the texture

glEnable(GL_TEXTURE_2D);

// Set a blending function to use

glBlendFunc(GL_SRC_ALPHA,GL_ONE);

// Enable blending

glEnable(GL_BLEND);

free(spriteData);

}

NSDictionary - read/write

by nik 13. May 2009 13:49

 

// you should add empty file "hiscores" in your xCode project

 - (void)save{

NSLog(@"saving...");

  NSDictionary *hiscores;

 

//adding object and keys;

//...

//...

 

NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"hiscores"]];

if (![hiscores writeToFile:writableFilePath atomically:YES]){

NSLog(@"WRITE ERROR");

 }

 

- (void)load{

NSLog(@"loading...");

NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"hiscores"]];

hiscores = [NSDictionary arrayWithContentsOfFile:writableFilePath]; 

 

 

 

 

- (NSString *)createEditableCopyOfFileIfNeeded:(NSString *)_filename {

    // First, test for existence.

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *writableFilePath = [documentsDirectory stringByAppendingPathComponent: _filename ];

    success = [fileManager fileExistsAtPath:writableFilePath];

    if (success) return writableFilePath;

// The writable file does not exist, so copy the default to the appropriate location.

NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: _filename ];

    success = [fileManager copyItemAtPath:defaultFilePath toPath:writableFilePath error:&error];

    if (!success) {

NSLog([error localizedDescription]);

        NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);

    }

return writableFilePath;

}

 

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

play mp3 sound

by nik 4. May 2009 04:55

//need to add: AVFoundation.framework

#import <AVFoundation/AVAudioPlayer.h> 

NSBundle *mainBundle = [NSBundle mainBundle];

NSURL *url = [NSURL fileURLWithPath:[mainBundle pathForResource:@"game" ofType:@"mp3"] isDirectory:NO];

 

AVAudioPlayer *gameSoundTrack = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

[gameSoundTrack prepareToPlay];

[gameSoundTrack play]; 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

General

Play Sound Effect

by nik 17. March 2009 07:02

#import "SoundEffect.h"

 

// init sound

NSBundle *mainBundle = [NSBundle mainBundle];

SoundEffect *tapSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"tap_snd" ofType:@"caf"]]; 

 

 // play sound

 [tapSound play];

 

SoundEffect.m (1.13 kb)

SoundEffect.h (263.00 bytes)

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Transformation in OpenGL (GL_MODELVIEW / GL_TEXTURE)

by nik 10. March 2009 10:24

glPushMatrix();

{

// - in MODEL MODE 

 

//translate

glTranslatef(0.0, 0.0, 0.0);

//scale 

glScalef(1.0, 1.0, 1.0);

//rotate 

glRotatef(0.0, 0.0, 0.0, 0.0);

glMatrixMode( GL_TEXTURE );

glPushMatrix();

{

// - in TEXTURE MODE

//translate

glTranslatef(0.0, 0.0, 0.0);

//scale 

glScalef(1.0, 1.0, 1.0);

//rotate 

glRotatef(0.0, 0.0, 0.0, 0.0);

//DRAW METHOD HERE;

}

glPopMatrix();

glMatrixMode( GL_MODELVIEW );

}

glPopMatrix();

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , ,

General

Animate fade in/out

by nik 10. March 2009 03:24

 

IN *.m file  

------------------------------ 

#pragma mark ANIMATION

//-------fade in

-(void)animateFadingIn{

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

[UIView setAnimationDelegate:self];

//set transformation

[UIView commitAnimations];

}

 

//-------fade out

- (void)animateFadingOut{

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

[UIView setAnimationDidStopSelector:@selector(fadeAnimationDidStop:finished:context:)];

[UIView setAnimationDelegate:self];

//set transformation

[UIView commitAnimations];

}

- (void)fadeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{

// push navigation controloller

}

 

 

IN *.h file

 

 -----------------------------

-(void)animateFadingIn;

-(void)animateFadingOut; 

 

 

////////////////////////////////////////////

//SAMPLE: 

logoBackdropView = [[UIImageView alloc] initWithFrame:CGRectMake(-36.0, 120.0, 479, 233)];

logoBackdropView.image = [UIImage imageNamed:@"logo_backdrop.png"];

[self.view addSubview:logoBackdropView]; 

 

 -(void)animateFadingIn{

 

  //state before animation

logoBackdropView.alpha = 0.0;

logoBackdropView.transform = CGAffineTransformMakeScale(2.002.00);

 

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

[UIView setAnimationDelegate:self];

 

//state after animation

logoBackdropView.alpha = 1.0;

logoBackdropView.transform = CGAffineTransformMakeScale(1.00, 1.00);

[UIView commitAnimations];

}

 

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

General

Do something when application terminated

by nik 2. March 2009 04:46

//in mainAppDelegate.m

 - (void)applicationDidFinishLaunching:(UIApplication *)application {

//

//init code

//

  atexit(applicationWillTerminate);

 

void applicationWillTerminate(){

//some stuff

 

 

//in mainAppDelegate.h

@interface mainAppDelegate : NSObject <UIApplicationDelegate> {

//.....

}

void applicationWillTerminate(); 

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

General

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen