Reputation: 97
1 ) Hey. I need help with pixels... How can i draw pixels with less lagg in draw function? While i am drawing them then my processor usage is 40%, it's too much for that.
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(grasspixel, screenpos, Color.Red);
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawGround(spriteBatch, 1024, 768, screenpos, grasspixel);
spriteBatch.End();
base.Draw(gameTime);
}
Here is DrawGround function:
public void DrawGround(SpriteBatch spriteBatch, int screen_X, int screen_Y, Vector2 screenPosition, Texture2D texture)
{
Vector2 tempVector = Vector2.Zero;
for (int i = 1; i <= screen_X; i++)
{
for (int j = 1; j <= screen_Y; j++)
{
if (earth[i, j] == 1)
{
tempVector.X = i;
tempVector.Y = j;
spriteBatch.Draw(texture, tempVector, Color.Blue);
}
}
}
}
In DrawGround I get pixel locations from an array and then return them to Vector2.
2 ) Do I need to draw my map every frame?
Is there a special buffer or something like that to do this? Sorry if I ask something stupid, but I'm new to XNA. Sorry for my bad english.
Upvotes: 0
Views: 232
Reputation: 3745
The problem is that you are drawing 786432 (1024*768) sprites per frame. Each sprite consists of 2 triangles, that need to be streched and positioned, not to forget all the texture sampling. SpriteBatch is fast, but not that fast.
Normally you would draw your grass texture over a larger area, using a sprite that is more than 1*1 pixels in size. Drawing one sprite for every pixel on screen really defeats the whole purpose of it.
So combine your grass pixels in an image editing program to a texture and use a few large sprites to draw your ground. That should bring your FPS to a reasonable rate.
If you really need to write data to a texture on a per pixel basis, use the Texture2D.SetData
method.
Upvotes: 2