Accessing Pixel Information

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I have an NSImage which has my map loaded. How can I get access to the pixel data so that I can say "if this pixel == 0x0000ff, that space is water"? I've been looking at NSBitmapImageRepresentation but I can't figure it out. Is there any support for this or am I going to have to figure it out on my own?

As a side question, does this seem like a good idea for making maps for a simple adventure, rpg, or rts game?
Quote this message in a reply
Member
Posts: 168
Joined: 2004.10
Post: #2
I believe SDL and SDL_image helps in this situation. If there is a way to do it like you say, I would also like to know it Smile
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
I figured SDL_image could do it, and I probably have code in my other projects that would help me do it, but I wanted to try and stick with just using Cocoa (partially for learning purposes and partially for elegance and ease).
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #4
I haven't got time for a detailed response at the moment (I'm off to catch a bus), but check this out:
http://developer.apple.com/documentation...lanesCOLON
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #5
Check out the cocoa implementation of: http://nehe.gamedev.net/data/lessons/les...?lesson=34

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #6
Nick Wrote:As a side question, does this seem like a good idea for making maps for a simple adventure, rpg, or rts game?
You can do it this way if you want to (I did it a few times in my first few programming years), but it's a really naïve way of handling map data.

The strength of this technique is you can just use a paint program to make your maps. There are several weaknesses. For example, there's no way of having extra data for each space on the map; you can only have a tile index. Also, if you want to have tiles which handle the various ways water could meet land, you'll have to paint in a lot of different colour indices to get the effect you want, and the map in your paint program won't look anything like the finished result.

A much better solution is to have a 2D (or whatever) array of structs, where the structs hold the tile index and any other useful information you might need. You'd want to store this in some custom file format, and you'd need to write a level editor to create the maps, so there's a certain amount of extra work involved, but the result will be more more flexible and easier to use in the long run.

One other thing: you don't necessarily need to store all data for a tile in the struct with the tile index. You could instead use a look-up table. In this case, the tile number would index into a second (probably 1D) array of additional tile data, and any information which pertains to all tiles of a particular type can be stored in this data. You can even use this idea in conjunction with a pixel-based map if you want to get a little more flexibility out of it.

Just a few ideas....

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #7
now that I have the time, here's some sample code:

Code:
NSImage *image = [NSImage imageNamed:@"test.jpg"];
    
NSBitmapImageRep *imageRep = [image bestRepresentationForDevice:nil];
        
NSColor *colorOfPixel = [imageRep colorAtX:20 y:10];
        
float red, green, blue, alpha;
    
[colorOfPixel getRed:&red
               green:&green
                blue:&blue
               alpha:&alpha];

NSLog(@"R: %f  G: %f  B: %f", red, green, blue);
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #8
Oh sweet. I couldn't figure out how to do that. Thanks a lot. That's awesome.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
By the way, is this how one would go about doing pixel collision detection? I want to use that for my game but I'm not sure how to start.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #10
Nick Wrote:By the way, is this how one would go about doing pixel collision detection? I want to use that for my game but I'm not sure how to start.

Sure, it's one way. You could add up the alpha values of two different sprites, then see if the pass a certain threshold (assuming they're antialiased).
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #11
So would I just do a quick bounding-box check followed by a for loop to check the alpha values of all the pixels or would that be too inefficient? That sounds like it would be too slow. Maybe break up the image into like 4 bounding boxes and then do a for loop.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #12
Nick Wrote:So would I just do a quick bounding-box check followed by a for loop to check the alpha values of all the pixels or would that be too inefficient? That sounds like it would be too slow. Maybe break up the image into like 4 bounding boxes and then do a for loop.

Well, the alpha values thing would only be if you wanted to detect collisions very accurately (the exact contour of your sprites). If you're going to use bounding boxes, you don't even need to look at pixels. Just use the width, height, and x-y position of your sprite to determine if it's touching (or overlapping) anything.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SQLite3 question: accessing by column instead of index? Toontingy 3 4,670 Apr 8, 2010 06:19 PM
Last Post: OneSadCookie
  Noob: Accessing Structures from Cocoa Classes MikeC 15 7,444 Oct 19, 2007 02:42 PM
Last Post: MikeC
  Accessing an inherited class's variables Tobs_ 22 10,225 Feb 28, 2007 05:26 PM
Last Post: mac_girl
  How to access the game memory to withdraw information utopianbl 3 2,955 Dec 6, 2006 04:08 PM
Last Post: Blacktiger
  Accessing the Datafork in Carbon Muffinking 3 4,503 May 15, 2002 10:06 AM
Last Post: wadesworld