• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: OpenGL Sprites...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OpenGL Sprites...


  • Subject: Re: OpenGL Sprites...
  • From: Markus Hitter <email@hidden>
  • Date: Wed, 26 Jun 2002 10:22:31 +0200

Am Montag den, 24. Juni 2002, um 18:21, schrieb Albert Atkinson:

I am trying to update one of my sprites to show another image when the up arrow key is pressed. Here is a snippet of code:

case upKey:
if(released) {
upKeyPressed = NO;
spriteImage = [[NSImage alloc] initWithContentsOfFile: fileName];
} else {
upKeyPressed = YES;
spriteImage = [[NSImage alloc] initWithContentsOfFile: fileNameThrust];
}
break;

This looks a lot like a Carbon-thinking approach. In Cocoa, you might prefer to subclass the view and override -keyDown and -keyUp etc. .


At the top of my code file is this snippet:

NSBundle *programBundle = [NSBundle bundleForClass:[self class]];

fileName = [programBundle pathForResource:@"Apple" ofType:@"tiff"];
fileNameThrust = [programBundle pathForResource:@"Apple Thrust" ofType:@"tiff"];
spriteImage = [[NSImage alloc] initWithContentsOfFile: fileName];

This is my snippet:
texturePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"grid.tiff"];
textureImage = [NSImageRep imageRepWithContentsOfFile:texturePath];

I load it once at startup and precalculate it using glGenTextures() etc.:

NSAssert([textureImage bitsPerPixel] == 24,
@"texture Image for platform does not have 24 bits/pixel");
glGenTextures(1, &platformTextureName);
glBindTexture(GL_TEXTURE_2D, platformTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
[textureImage pixelsHigh], [textureImage pixelsWide], 0,
GL_RGB, GL_UNSIGNED_BYTE, [textureImage bitmapData]);

You probably want to do the same since it's a huge computing overhead to load the image each time you press a key.

Btw., do you release your images any time?


My problem is that when I run the app it will work fine but the moment I push the up arrow I crash. Any suggestions?

Run with the debugger, look at the backtrace.

Replace the code with an NSLog(), showing the contents of your variables.

Enable NSZombies to find out about wether you access already released objects.


Have fun,
Markus

- - - - - - - - - - - - - - - - - - -
Dipl. Ing. Markus Hitter
http://www.jump-ing.de/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >OpenGL Sprites... (From: Albert Atkinson <email@hidden>)

  • Prev by Date: WYSIWYG Font Menu
  • Next by Date: Re: NSMutableArray: where is addObjectIfAbsent:?
  • Previous by thread: OpenGL Sprites...
  • Next by thread: Re: cocoa-dev digest, Vol 2 #831 - 15 msgs
  • Index(es):
    • Date
    • Thread