How to check the color of a certain part of an image? - objective-c

I am making a simple game that uses "AI players" (they aren't really AI players). I need to find out if a certain part of the "map" I am using has certain colors, so I can make the "AI players" do certain things. Is it possible to do this?
I don't know if this will help, but a game called "Warcraft 3" uses a very similar thing to determine certain things, such as movement. If you know of this game, it should be a lot easier to understand this question.
I think this may be possible if I put the image into a custom NSView subclass, but I have not yet learned how to check colors there either.

The best way to do this would be not to bother checking the colors of the actual image (which can be an expensive operation if you're checking a lot of individual pixels), but to indicate in your map's data structure the characteristics you want to have, and then take both the color and player behavior from that.
In pseudocode:
// Draw Map
foreach currSquare in listOfSquares:
if map[currSquare].hasPropertyX():
drawSquare(currSquare, blue)
else if map[currSquare].hasPropertyY():
drawSquare(currSquare, red)
// Move pieces
foreach currPlayer in listOfPlayers:
squareIAmStandingOn = currPlayer.square
if map[squareIAmStandingOn].hasPropertyX():
currPlayer.takeActionX()
else if map[squareIAmStandingOn].hasPropertyY():
currPlayer.takeActionY()

Create a NSBitmapImageRep from the NSImage and use colorAtX:y: to get the color.
Check:
NSBitmapImageRep Class Reference

Related

Object Oriented Graphics And Painters

When creating an object that will be graphically represented but also have data and functionality independent of that representation (i.e., a card in a card game will have a graphic, but also know its value, suit, maybe be able to flip over), from a best practices approach, should that object know its own image and position?
If not, how should it be handled? I understand that at the very least another class should be responsible for drawing said object, and it appeals to me that the class shouldn't need to be concerned with its graphics at all: the program should be able to change the design and look of the cards without impacting the class itself - a seven of spades is a seven of spades no matter how you draw it - but I'm finding it difficult to think up a solution to having the 'drawer' class know the image and location of the card.
My present solution is to have a sprite class, and the card contains a sprite object, which is constructed along with the card - the sprite simply contains an image and vector (location), but I feel that I could break this down.
Any design patterns or common sense solutions I'm missing? Or am I just thinking incorrectly that this should be separated?
Since this is a matter of opinion: I believe it would be best for the object not to know its own image. It should know its own position. When a card is drawn, like you mentioned, a Seven of Spades is a Seven of Spades. Leave it to a different handler to draw the image based on what type of card it is, rather than asking the card for its image.
When you draw a card, you could make a method call, ex: drawCardImage(cardObject.type). The method could then check, using if statements, "if card == 'sevenofspades': draw("/images/sevenspades.png")

UISegmentedControl custom images for each segment

I'm trying to customize a UISegmentedControl to use a custom image for each segment. I've done a lot of searching, but haven't had any luck with the solutions I've tried so far. This is the most recent post I can find, which is still fairly out of date now, and seems pretty hacky yet. Are there any better or more recent guides on how to do this?
Thanks
Unfortunately, UISegmentedControl doesn’t make it easy to set separate background images for each segment separately. If your control is always a known width, you might be able to make a full-size background image with the three segments drawn in, like this: (yellow][green][red) (where parentheses represent rounded corners), and then use -[UISegmentedControl setBackgroundImage:forState:barMetrics:] to set your image.
However, that solution isn’t very flexible if you want to resize the control later. You might be better off faking it with three adjacent UIButtons, or even subclassing UIControl to make a custom segmented control which can have a separate image for each segment.

OpenglES - Transparent texture blocking objects behind

I have some quads that have a texture with transparency and some objects behind these quads. However, these don't seem to be shown. I know it's something about GL_BLEND but I can't manage to make the objects behind show.
I've tried with:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
but still not working. What I basically have is:
// I paint the object
draw_ac3d_file([actualObject getCurrentObject3d]);
// I paint the quad
paintQuadWithAlphaTexture();
There are two common scenarios that create this situation, and it is difficult to tell which one your program is doing, if either at all.
Draw Order
First, make sure you are drawing your objects in the correct order. You must draw from back-to-front or else the models will not be blended properly.
http://www.opengl.org/wiki/Transparency_Sorting
note as Arne Bergene Fossaa pointed out, front-to-back is the proper way to render objects that are not transparent from a performance stand point. Because of this, most renderers first draw all the models that have no transparency front-to-back, and then they go back and render all models that have transparency back-to-front. This is covered in most 3D-graphic texts out there.
back-to-front
front-to-back
image credit to Geoff Leach at RMIT University
Lighting
The second most common issue is improper use of lighting. Normally in this case if you were using the fixed-function pipeline, people would advise you to simply call glDisable(GL_LIGHTING);
Now this should work (if it is the cause at all) but what if you want lighting? Then you would either have to employ custom shaders or set up proper material settings for the models.
A discussion of using the material properties can be found at http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=285889

Translate Colors to Image?

Im not sure how else I should approach it, but if I was to (in my mac application) have a grid of NSViews, which the user can change the colour of each, is it possible to then translate this, so now I have been given a colour for each pixel by the user, make this into an exportable image?
I honestly can't think of how else to do this. I don't want to go ahead an realise I have taken a rather foolish path.
The idea is I will have a grid of squares which the user can paint, a colour in each square, a square representing a pixel in the final image. So they paint with like a paint bucket filling each one, then export it into an actual image file.
Any help much appreciated, thanks.
A grid of NSViews sounds really heavy for what you're doing. Why not write one single custom view that checks the mouse position and modifies the data appropriately? Then you'd write a custom drawing method to fill the custom view, and you could use the same exact draw method to write to an NSImage which you could export.
You'll need to do a bit o' math. For each "pixel", call -set on the appropriate NSColor, then use NSBezierPath's -fillRect method. It may help you to get out a pencil & paper to figure out the math for the rect origins & sizes.
Check http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html for help if you've never done custom drawing before. It's really not that bad, just takes a little reading. :)

Which pixels did that drawmesh operation just draw to?

Ok, it's a relatively simple problem, I want to know where, in screen space, a particular mesh was just drawn. I plan on then storing that information in a data store of some kind so that when I interact with something in screen space, I can lookup in the register and find the object, i.e, click on the spaceship drawn on the screen and then select target etc.
I can't find any way of finding out which pixels the mesh was drawn to though...
Alternatively, if I'm missing something obvious regarding what it is that I Want to do, please let me know!
There is no easy way to do that. But you can use another texture as render target and render those meshes with unique colors.
So for example you give #FF0000 to your mesh A and draw it also to your second render target with that color. Now when you select a pixel from 2nd render target and look at that color, if it is #FF0000 you can understand that, the pixel is a part of mesh A. Thus you can easily pick the mesh drawn on a certain pixel when you click one of those pixels.
Why dont you Unproject your screen space coords into 3D space? The only complication I had was the fact that I'd be left with a plane, I could check if a Mesh intersected with that plane but I often had multiple candidates for 'picking'.
Check out Google for DirectX Unproject and there are various articles discussing it. It's sometimes complicated for some to implement but done well it's actually pretty nifty; don't get put off by the people online who say it doesn't work, it does work!