XNA Alpha Blending With Primitives and Sprites - xna

I've been using XNA for awhile now and I really enjoy using its interface. Right now we're trying to make a "fog of war" system. We're making an RTS and we're basically trying to obscure the map. Units on the map have influence, and show what's happening. They reveal a specific area on the map. Previously we used a tile based interface but we weren't happy with the appearance. Instead, we decided to draw a black mask on the screen, then reveal a transparent circle with DrawUserPrimitives. Here's the problem: When we draw a black mask on the screen, followed by the transparent circle, the screen is entirely black. Reversing the order doesn't change anything either. We've also tried every SpriteBatch BlendState we can find.
OUR CURRENT METHOD:
Right now, we're drawing a full black texture onto the screen, then drawing a transparent circle. The result is an entirely black screen.
OUR DESIRED OUTCOME:
Drawing the circle will reveal a section of the screen, but keep the rest obscured. We'd be able to draw multiple circles, all with their own radius, that would could be near each other without causing problems.
Basically, we're trying to draw a black mask, but reveal portions of the screen. We can't just make a circle texture and make the edges transparent, because we're trying to make the center of the circle transparent. We can't make the center transparent, because then when two such textures were near each other, their borders would show up. How would I go about making the mask, then revealing sections of it?
Regards
Ares

You could render the graphics normally to one render target, then paint the visible area in white on a black background in another render target, then render the second into the first with the Min BlendFunction.
Alternately, using only one render target, draw the graphics normally, then clear the alpha channel (by painting a rectangle over the whole screen with ColorWriteChannels = Alpha and alpha blend factors both 0), then draw the visible area with ColorWriteChannels = Alpha only.

Related

Make view stand out from the surroundings. Border layer with inverse color?

I have a view (a Button in this case) and the content behind/below it can move around (a ScrollView). The button is just plain white with black text and not bordered with anything. When the content behind the View is white it does not really stand out as I would like it to do. (I can fix this with a black border sure... but...)
I have this idea of having a border around the button that is the opposite color of the view (pixel) behind it. So the border would always contrast with the background and constantly change with the content behind it.
I have googled a bit and looked into visual effects layers and some more complicated (over the top of my head) graphics stuff I don't remember the terminology for.
If you have an idea of how to approach this please tell me. I just really want to see what it would look like.
and have a wonderful day!
I don't think you can create a border that uses colors based on the color of the pixel from the layer behind the current layer using CALayer and borders.
What I would suggest doing is to add a second CALayer to your view's CALayer, inset by -1 in both dimensions (made 1 pixel bigger.) Let's call that the surroundLayer. Then make the surroundLayer's borderColor be white and the view's layer.borderColor be black (or visa-versa.) You can make the surroundLayer's borderColor be 50% opaque so it just lightens/darkens the pixels under it without completely obscuring them, and that is enough to increase the contrast and make your view's border show up regardless of the contents under it.
I've used this technique before and it works well.
Edit:
Check out the project https://github.com/DuncanMC/MaskableImageView.git. The project demonstrates using an image as a mask layer to hide/reveal the contents of a view.
The class MaskableView in that project draws a circular "cursor" that shows where it is revealing/masking the contents of its subview (an image and a label, in the example app.) The cursor is yellow in the middle, with a partly transparent black outer circle around it. This gives good contrast regardless of the colors in the part of the image it is being drawn over.
The MaskableView class has properties that let the caller set the colors use for the "cursor" circle.
Below I posted a short animation of what the eraser tool with a yellow inner circle and an outer, 1/2 transparent black circle looks like.
Without the outer dark circle the yellow inner circle tends to get lost in brighter parts of an image. With the combination of a bright colored inner circle and a partly transparent, dark outer circle, it's easy to see on ANY background:

iOS Triangular Image view

so I'm making a game and pretty much when the player (which is a triangular shaped rocket) hits an object flying at you (a rock) the game ends. I have everything working well but my problem is the rocket is a triangle yet the image view its in is a rectangle. So if the edge of the image view touches the rock the game will end even though the actual rocket didn't touch the object. So basically how can I make the rock image view not recognize the parts of the rocket image view which are empty? Basically a triangular shaped image view.
Thank you for your help. Let me know if you need more info or want to see the code I have for them to collide.
You analytically present the triangle with 3 points and a rock with a center and radius then find and implement an algorithm checking a hit test between those 2 shapes. Or draw the two shapes onto some graphics context using an appropriate blending and check for overlapping pixels (for instance draw one as red and another as green and look if a pixel that is both red and green exists) you could actually do that with 2 image views having those colors and .5f alpha added on the 3rd invisible view but you would need to get the image from the view and then iterate through all the pixels. In any of the cases do this check only after the corresponding view frames overlap.

Cocos2d iOS: How to draw shadows underneath a CCNode

I'm building a simple card game and would like to draw shadows when users pick the cards up. Any quick solutions?
Example:
Quick solution? Draw a black shape (or whatever the shadow color is) on a layer behind your selected card. Just like in your example image you have a fuzzy gray square box behind, but yet slightly down and to the right, of another square that is on a layer on top of it. Or you can draw the shape underneath the card and move the card a little to give the illusion of it rising.

Making a UIImage in UITableViewCell look recessed

This is sort of a photoshop question, I suppose, but does anybody know how to make a nice looking heart that's recessed? Sort of like this one, except recessed:
There are a few layer styles which can compliment each other to give you that recessed look.
A light drop shadow beneath the sprite (1px white shadow directly below usually does the trick, but you can experiment with its softness/size)
A gradient which is darker at the top and lighter near the bottom
A dark inner shadow which is at the top of the sprite. (1px black, for example)
If you're doing this in Photoshop, make sure to have the light angle set to 90ยบ
A basic letterpress effect can be achieved by having a white (or light coloured) drop shadow 1 point below the image. This is what is used in iOS for the status bar images and text, for example.
You may also want an inner shadow inside the top edge, and possibly a subtle gradient (lighterdarker at the top - note I have changed my mind on the gradient, but not updated the image!) instead of a solid background colour.
Here's a 5 minute bodge job done in Pixelmator. The inner shadow at the top is particularly amateurish, but I prefer drawing things like this in code!

Alpha Blending for a Drawing App in cocos2d

Im using cocos2d to create a simple drawing app, I create a 32 bit texture in code in the shape of the brush i need (circle) with a simple hardness gradient (alpha = 1 in middle and alpha = 0 close to edge) The texture is obviously square so alpha is 0 outside the circle. The user touches the screen to draw and a the texture is repeated according to a separation constant.
i tried without alpha blending enabled and i get horrible result.
With (ccblendfunc){GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA} i get boundaries appearing between each instance of the texture. This effect does not happen if i manually create each point by tapping instead of dragging.
And with (ccblendfunc){GL_SRC_ALPHA,GL_ONE} i get good results but at the edges the colors are added together, blue and green = cyan, red and green = yellow and red and blue = purple
how can i simply create a blend mode that works like photoshop's standard????

Resources