Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Having problems drawing tile

$
0
0

@Shadowblitz16 wrote:

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);
        }

    }
}

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles