Sprites blurry when enlarged - c#

In XNA I am trying to create a game using old style Super Mario sprites, but if I try to make them bigger, they get very blurry. I have tried saving the PNG sprites as nearest neighbor, bicubic, and bilinear in photoshop, but they all appear equally blurry. I have also tried compressing the PNG online, which also didn't help.
My knowledge of XNA is somewhat basic, so unless your answer is code I can simply copy paste into my 'main' class, please explain how to use it.

In your draw-function you should only need to change one row of code.
Where you call spritebatch.Begin(); instead call
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
This will set your "GraphicsDevice" to render textures without interpolating the colors between whole pixels when the sprites get zoomed.
One other thing that you could do is to use a rendertarget with the resolution you want the game to "emulate" , draw everything on the rendertarget and finally draw that to the screen.
This is a bit out of the scope of this question but it is ideal if you want to create a genuine oldschool experience.

Related

OpenTK/C# - 2D text rendering works, how about 3D?

Edit: Bad angle picture was due to Depth Testing. Still need the 'right' way to do 3d text rendering though!
I got 2D text rendering in OpenTK working. It's very simple, I use .NET Graphics class methods to draw a string to Bitmap, which I can load into GPU via OpenTK. Ok great, but how about 3D?
I'm thinking about this in terms of a cylinder. What is a cylinder? It's just a circle stretched out over a certain height. That's EXACTLY what I want to do here! I researched a bunch...but surprisingly there isn't all that much info readily available IMO for such a basic task.
Here's what I've tried:
1) Rendering the bitmap 100 times from Z = 0.0f to 1.0f. This actually works pretty well! For certain rotations anyway.
2) Drawing 16x16x16 Voxels (well, I think I'm drawing voxels). Basically the idea is, use the typical GL.TexCoord3 and GL.Vertex3 methods for drawing the SURFACE of a cube, but because we are drawing so freakin many of them, I figured it would actually give depth to my text. It sort of does, but the results are actually worse than attempt 1.
I want to get this working with a really simple solution, if one exists. I'm using Immediate mode, and if possible I'd like to keep using that.
This is what solution 1 looks like at a good angle:
Bad Angle:
I know that my method is inherently flawed because these bitmaps dont actually have a depth when I draw them, which is why at some critical angles either the text becomes flat looking, or disappears from view.

Depth Buffer not working

In my XNA 3D game, for some reason, the depth buffer is off and ignored - even though I've done everything I can find to enable it (which admittedly isn't much, but it's supposed to be simple... not to mention default)
Before the models are rendered:
global.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
Somewhere earlier:
graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
graphics.ApplyChanges();
the models are rendered using a Vertex/Index list with device.DrawUserIndexedPrimitives and a BasicEffect.
It comes out like this:
The purple object is very far from the camera, and is drawn 1st.
The gray object is very near the camera, and is drawn 2nd.
The blue object is medium-distance to the camera and is drawn 3rd.
The gray object is rendering behind the blue object - that is correct if you're going off the draw order, but I want it to organize by distance from the camera (Using the depth buffer), in which case the gray object should draw in front of the blue object.
(And, no, just quickly sorting them manually, while it may provide a temporary solution, is not the way to fix this problem)
Update: This is directly related to the fact that I'm rendering onto a RenderTarget2D instead of straight onto the screen. (If I render on-screen instead of to the rendertarget, the depth is calculated correctly. The rendertarget is needed for other parts of the program... or an equivalent system.)
Well I found what I missed on my own after searching randomly:
A RenderTarget2D has its DepthBuffer disabled by default.
I just had to set the DepthFormat on the rendertarget I was drawing too.
Knew I missed something obvious...
(I'll green-check-mark-this in 4 hours when the site lets me.)
ED: that is, unless, in that time, somebody posts a nice list of everywhere XNA depth buffer data could/should be set, to better help people who might find this topic by google.

Optimal 2D Tile rendering in C#

So I'm making a simple 2D Sidescrolling game in C# however I've found that using Graphics.drawImage doesn't particularly allow me to update the tiles as I wish. For example, I tell it to draw the image and it stays where I tell it to be. I want to be able to move the entire scene left to right. This would be easier if I had to use a for loop or something and define it's position every time it draws the image.
This may be confusing and I'm certain there's a way of doing it, I just don't know how.
So my question to you is: How can I control the positioning of each rectangle drew on a form so that I can scroll the entire scene to the left when I wish?
I'm certainly no game/graphics expert, so take this with a grain of salt.
A couple things spring to mind.
First, you could pre-render the entire level as one bitmap, then just paint the relevent portion into your picturebox.
Second, the Graphics class has a Transforms property (I may have the name wrong). You could add a linear transformation to the graphics object so the painting coordinates of your tiles wouldnt change, but the graphics object would slide, or I think the graphics term is "translate" the output en-masse.

Draw a Text in 3D

I'm trying to figure out how to draw text in 3D.
Right now I have two ways to display stuff onto the screen. One that uses usual 3d space to render the world, and additional render to render the HUD that does it in pretransformed way.
So, ideally I would like a way to do the same for text, to both render a string in 3D (kind of like "billboarding" way) and pretransformed way (as if I was using 2d mode).
The only thing that I was able to find is to use "Mesh.FromText", but it doesn't seem to be what I need. Oh, and also using spritebatch to draw text to a texture, and then using this texture in 3D with alpha blending, but again it seems like a crappy way to do it...
Any ideas?
You can draw with SpriteBatch in 3D space by using a custom effect. This blog post has the details.
It's as simple as passing an effect (like BasicEffect) with the correct transformations set on it to the appropriate overload of SpriteBatch.Begin (this one).
(The text is made up of transparent sprites - so all the usual caveats with drawing with transparency in 3D apply: sort and draw over the opaque, Z-buffered scene, or use alpha-testing.)

Erase part of RenderTarget when drawing primitives?

I'm creating a paint like application using XNA.
I have a render target which acts as a canvas. When the user draws something I draw corresponding triangles using DrawUserPrimitives and triangle strips to make lines and other curves.
I want to implement an eraser in the application, so that the user can erase the triangles from the texture. I've used OpenGL in the past and there I would just use a blend function like so: glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
How would I do this in XNA? I tried setting the GraphicsDevice blend mode to AlphaBlend, Additive, etc.. but it did not work. Any ideas?
If you change your code to use a Texture2D instead of a RenderTarget2D, you can use RenderTarget2D.GetTexture() to get the pixel data from the RenderTarget2D.
Of course, there will probably be a performance hit, but if you can optimize the code in a way that cuts down on the number of times this is done, like temporarily drawing the background color instead of actually erasing, and then iterating through the pixels after the mouse is released, you could make it work.
There may be a better way, but I couldn't find it.

Categories

Resources