Sign in to follow this  

[.net] [XNA] Buffering

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My game is currently in C# and GDI+. To get good performances, I'm using several buffers. The map is static (ie not animated), and changes only when the player scrolls. The units are animated. So my idea was to build the map, create a bitmap buffer for it, and in the render loop, to start from this already created bitmap, and paint on top of it the units. So I don't have to rerender the whole map everytime. To do this, I've done something like // Creation of the map buffer bitmap Bitmap mapBuffer = new Bitmap(pictureBoxPreview.Width, pictureBoxPreview.Height); // Creation of a graphics from this bitmap (not from a window!) Graphics graphicsBuffer = Graphics.FromImage(backBuffer); // Creation if the map from the tiles for (int i=0; i < NbLine; i++) { for (int j=0; j < NbColonne; i++) { // Return the correct tile for this line and colums Bitmap Tile= Tiles.GetTile(i,j); graphicsBuffer.DrawImage(frame, X, Y, frame.Width, frame.Height); } } // And at the end GraphicsPictureBox.DrawImageUnscaled(backBuffer, 0, 0); Where GraphicsPictureBox is the Graphics associated to the picture box. The important point is that in the loop, the DrawImage are used to draw a bitmap into another bitmap, not directly in the window. Is it possible to do something similar in XNA? Like set the render target to be a Texture2D, or any other kind of buffer, and not directly the Window?

Share this post


Link to post
Share on other sites
Many many thanks, it seems to be exactly what I needed!

Perhaps you'll be able to answer another question.

Is it possible to create a Texture2D which is 256 colour index, and then access the palette and change it?

That's what I used for player coloring in GDI+.

I had some 256 colour graphics, and the first index was the player color. So when I changed player, I simpply accessed the palette of the bitmap, change the color of the first index, and then applied it.

Share this post


Link to post
Share on other sites
Today’s graphics chips don’t support palette textures anymore.

Showing player colors is done different today. One solution is having a texture set per player (You have much more texture space today) or solve this problem with a small shader program that add the color at the right places.

Share this post


Link to post
Share on other sites
I'm making a 2D games based on sprites, so I cannot use several textures for each player.

I need to do some colour substitution.

I think that's what the hint is for in XNA and Texture2D.

But I guess there is a small problem with that.

Currently, I can have blue as player color, and somewhere in the graphics the same shade of blue.

But as they are not at the same index, if I use palette change, I can change the first blue to red (supposing red is the new player color), but keep the other blue blue.

I'm not sure if it can be done easily with XNA?

Share this post


Link to post
Share on other sites
You can do what Demirug suggested. When creating the sprite, make the parts of the character that you want the colors to change a certain known color. Now don't use that color for anything else. If you need the same color, just change it a bit. With 16/32 bit color, there are lots of similar colors.

Now when you render the sprite, in the pixel shader, check the color of the texel to be drawn. If it is the known color (the color chosen earlier), change the color to the color desired.

There is a sample on the XNA website that uses shaders with SpriteBatch. Take a sneak peek at that to see how to get it to work.

Hope this helped

Edit: Ok, not sure if there are conditional operations in pixel shaders. Sorry, don't know too much about them.

Share this post


Link to post
Share on other sites
Another possibility, though perhaps it is too much for a simple game, is to have your sprites with the player specific color not coloured in or whatever.
Then have a separate sprite which specifies which areas need to change color. Then, in the shader, just multiply the two textures. That way, you can have different shades of a color without much effort.

For example, you have a horse and a person on it. Horse's saddle color changes depending on the current player, and the person-on-the-horse shirt color changes depending on the current player. In addition, there is a shadow from the person, so that it covers part of the saddle, making it darker. Well, for you sprite, you just do the shadow, and in the second texture, you just show which color changes.
Now, if your player is, say, Blue color, multiplying these two textures would give you a horse with a blue saddle, plus it would retain the shadow dropping from a person on the horse.

/me hopes that was understandable

Share this post


Link to post
Share on other sites

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this