XNA: Polygon Source from Texture2D - c#

I've got a problem. Using SpriteBatch, I can only draw a rectangle area from my source Texture2D.
Please, help me to find the way, how I can draw polygon or circle area from my source texture.
I'm creating 2d sprite game.
Thanks in advance,
Denis

You could construct this shapes with dynamic vertices, like building your own shapes [1]
But if you just want to draw any non rectangular shapes it would be much easier if you just use transparency. So you will still take a rectangular region from your texture but only the circle/polygon is visible.
This can be done easily by using png ord tga with baked in transparency. There are also a lot of qeustions dealing with this on SO:
[2] [3]

Related

Draw a one pixel line around square sprite

I have a 15 x 15 pixel box, that I draw several off in different colours using:
spriteBatch.Draw(texture, position, colour);
What I'd like to do is draw a one pixel line around the outside, in different colours, thus making it a 17 x 17 box, with (for example), a blue outline one pixel wide and a grey middle.
The only way I can think of doing it is to draw two boxes, one 17x17 in the outline colour, one 15x15 with the box colour, and layer them to give the appearance of an outline:
spriteBatch.Draw(texture17by17, position, outlineColour);
spriteBatch.Draw(texture15by15, position, boxColour);
Obviously the position vector would need to be modified but I think that gives a clear picture of the idea.
The question is: is there a better way?
You can draw lines and triangles using DrawUserIndexedPrimitives, see Drawing 3D Primitives using Lists or Strips on MSDN for more details. Other figures like rectangles and circles are constructed from lines, but you'll need to implement them yourself.
To render lines in 2D, just use orthographic projection which mirrors transformation matrix from SpriteBatch.
You can find a more complete example with the PrimitiveBatch class which encapsulates the logic of drawing in the example Primitives from XBox Live Indie Games.
Considering XNA can't draw "lines" like OpenGL immediate mode can, it is far more efficient to draw a spite with a pre-generated texture quad (2 triangles) than to draw additional geometry with dynamic texturing particularly when a single "line" each requiring 1 triangle; 2 triangles vs 4 respectfully. Less triangles and vertices in the former too.
So I would not try to draw a "thin" line using additional geometry that is trying to mimic lines around the outside of the other, instead continue with what you are doing - drawing 2 different sprites (each is a quad anyway)
Every object drawn in 3D is drawn using triangles. - Would you like to know more?

XNA Procedural Textures

I'm trying to make procedural textures in my XNA 4.0 game, mainly for buttons but for other textures as well. Here's an image describing what I want:
Hope you understand what I want to do, if you don't, heres some words:
I want to make objects in my game. These objects will all use the same texture, but can be resized, and their texture will not be resized so the pixels are "stretched", but procedurally placed.
The general way to do this is to have one texture for the middle, 4 for each corners and 4 for each edges. The vertical edges and middle would be stretched vertically, and the horizontal edges and middle would be stretched horizontally.
You could pack it into 1 texture for easy editing. You'd define the corners and edges implicitly with a border distance, which would define the parts of the texture that should not scale.
I would recommend you to split up the textures in 5 Textures. One for each side and one single-colored texture. You just stretch the one colored texture and draw the frame textures around your stretched colored texture.
I hope I could help you.

Render with multiple textures in managed Direct3D 9

I am pretty new Direct3D and have been looking for a solution to my problem for a couple of days. Most of the tutorials I have seen that cover textures only use one texture. For my program I have multiple textures that map to a specific collection of vertices that make up my mesh.
My question is how do I load multiple textures into my scene? and how do I map a collection of vertices to only one texture?
For example if I had a mesh of a car and I had a collection of textures like:
Tyres.dds
Body.dds
Cabin.dds
Given the car, how do I map the vertices that make up the tyre to the tyres.dds texture, body to body.dds and cabin to cabin.dds. All these textures have to render not just one.
Any help would be greatly appreciated,
Thank You
Usually this is done via submeshes. That means that the mesh consists of several parts that are represented as e.g. triangle lists. Each submesh is assigned a material. This material can be defined as you need. It may include diffuse color, roughness and the texture.
So when rendering the mesh, you would basically iterate every submesh, send the material parameters to the graphics card and then draw it.
Another possible solution in DirectX 10 would be to extend the vertex declaration by a TextureIndex variable. Or you could use 3d texture coordinates. This way, you can send all textures as a texture array to the graphics card and draw the mesh with one draw call. However, texture arrays are not suppported in DirectX 9. So you can either stick to method 1 or try to emulate a texture array.

How do I create an image and save it for later to draw as texture in XNA?

In my game I need to draw a circle made of squares the sizes of a game tile (circle is made of squares). I could just draw monochrome square textures in form of a jagged circle each frame, but it consumes a significant amount of resources. What I'd like to do is to draw it somewhere in the memory just once and save to draw each frame after that.
I could simply draw said circle myself and use it as a ready texture, but my circle is not always the same. It has different size throughout the game (and it's not really a circle half of the time, but I've got the algo that says where to draw), so it has to be drawn programmatically.
First you render the circle to a custom RenderTarget2D. You can set a custom render target like this:
GraphicsDevice.SetRenderTarget(renderTarget);
After rendering your circle to the render target cast it to a Texture2D like this:
texture = (Texture2D)renderTarget;
Read more: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Render_to_texture.php

C# MDX drawing transparent rectangle over a sprite

I want to make a "Pausemenu" in a Tetrisgame, when I hit Esc, the Menu pops up and that the user clearly is aware of that the game isnt running, I want to draw a transparent black rectangle over the whole game sprite, I´m using C# Managed DirectX 9.0c on .Net Framework 3.5. (I could achieve the same effect with a texture, but since in the settings the Board Width/Height can be changed, this would be an ugly solution)
Is there an easy way to achieve this?
You can simple create a small PNG, make it black and 75% opaque.
In your Game just use the Sprite Class (with alpha enabled) and scale it (with transform) to the dimensions of your viewport.
You could also use the Sprite Class to modify the Color of the Sprite when drawing, so you can make it fade in or out or give it another color.
Hope that helps!

Categories

Resources