Can someone tell me why my tile is drawing weird pixels?
It looks like this…
which my code is just creating a blank Texture2D…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace TileTest
{
public struct Tile
{
//Variables
private readonly GraphicsDevice Device;
private Texture2D Texture;
public int Width;
public int Height;
//Constructor
public Tile(GraphicsDevice device, int width, int height)
{
Device = device;
Width = width;
Height = height;
Texture = new Texture2D(Device, Width, Height);
}
//Methods
public bool IsValid()
{
return Texture != null;
}
public Color GetPixel(int x, int y)
{
if (!IsValid()) return Color.Black;
Color[] pixelData = new Color[Width * Height];
Texture.GetData(pixelData);
return pixelData[x + y * Width];
}
public void SetPixel(int x, int y, Color color)
{
if (!IsValid()) return;
Color[] pixelData = new Color[Width * Height];
Texture.GetData(pixelData);
pixelData[x + y * Width] = color;
Texture.SetData(pixelData);
}
public void Draw(SpriteBatch spriteBatch, Rectangle dest)
{
if (!IsValid()) return;
spriteBatch.Draw(Texture, dest, Color.White);
}
public void Draw(SpriteBatch spriteBatch, Rectangle dest, Color color)
{
if (!IsValid()) return;
spriteBatch.Draw(Texture, dest, color);
}
}
}
I actually am believe it or not.
I did it so pixels wouldn’t look blurry
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Forms.Controls;
using TileTest;
namespace TileTestForms
{
public class TileWindow : UpdateWindow
{
//Variables
private Tile tile;
public Color Color;
private readonly int width = 16;
private readonly int height = 16;
//Constructor
protected override void Initialize()
{
base.Initialize();
Click += OnClick;
tile = new Tile(Editor.graphics, width, height);
Color = new Color(0,0,0,255);
}
//Functions
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw()
{
base.Draw();
Editor.spriteBatch.Begin(samplerState: SamplerState.PointClamp);
Editor.graphics.Clear(Color.Black);
tile.Draw(Editor.spriteBatch, new Rectangle(0, 0, Width, Height));
Editor.spriteBatch.End();
}
//Events
protected void OnClick(object sender, EventArgs e)
{
MouseEventArgs m = (MouseEventArgs)e;
int tx = m.X / Width;
int ty = m.Y / Height;
int ti = tx + ty * Width;
tile.SetPixel(tx, ty, Color);
}
}
}
Just to clarify:
You are creating a Texture2D with just one color (green?) and getting additional different colors (orange?) when drawing this texture?
Also:
It seems that you are using MonoGame with WindowsForms. Please test your code in a new MonoGame project first and see if you are getting the same result.
You should draw this texture unscaled of course. Just make a screenshot and zoom it with an image editing program.
Just a heads up not related to your issue here. The way you use GetData and SetData is extremely inefficient. If I were you I’d have a color array that mirrors the texture data so you never have to call GetData and when setting data use the overload that lets you specify a region so you can only update the changed pixel.
Edit: actually regarding your issue. What size is the texture? Your OnClick code looks incorrect to me. Can you try to explain how your code should work? That helps people find the mistake (and often you’ll even figure out the mistake yourself!)
hi guys the problem was that I wasn’t setting the backcolor of the texture when creating it.
@BlizzCrafter I don’t know what was causing the problem but I guess the texture wasn’t being created with transparency.
I am using monogame.forms to draw it.
I am drawing it scaled to the size of the control.
@Jjagg
I am only using setData and getData for setting pixels when clicking on the monogame control.
the size of the texture is 16x16.