OpenGL Sprites...

  • 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;

    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];

    And here is my header file:

    #define    upKey        126

    //Some code snipped out...

    BOOL        upKeyPressed;

    NSString    *fileName;
    NSString    *fileNameThrust;

    //Some code snipped out...

    - (id)init;
    - (void)dealloc;
    - (void)update;
    - (void)handleKey:(unsigned short)key keyUp:(BOOL)released;

    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?

    Thanks!

    Albert
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • 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 | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month june 2002 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today