Advertisement

SharpDX Drawing rectangle

Started by August 06, 2014 10:53 AM
3 comments, last by TariqoO 10 years, 5 months ago

Hi, i tried many times searched and used many methods

It did'nt work

iyCtmOp.png

The one on right is what i want to see

Full test code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDX.Direct3D9;
using System.Threading;
using SharpDX;

namespace GridDrawTest
{
    public partial class Form2 : Form
    {
        public struct CUSTOMVERTEX
        {
            public float x, y, z, rhw;
            public int color;
            public CUSTOMVERTEX(float x, float y, float z, float rhw, int c)
            {
                this.x = x;
                this.y = y;
                this.z = z;
                this.rhw = rhw;
                color = c;
            }
        }
        RenderManager Mgr;
        VertexBuffer bfr;
        public Form2()
        {
            InitializeComponent();
            pictureBox2.Image = new Bitmap(RenderWidth, RenderHeight);
            Mgr = new RenderManager(pictureBox1.Handle/*Handle*/,RenderWidth,RenderHeight);
            int numberoftiles = (RenderWidth / TileWidth) * (RenderHeight / TileHeight);
            bfr = new VertexBuffer(Mgr.Device, numberoftiles * 4, Usage.None, VertexFormat.PositionRhw | VertexFormat.Diffuse, Pool.Default);
            DataStream st = bfr.Lock(0, 0, LockFlags.None);
            Graphics myImageGraphics = Graphics.FromImage(pictureBox2.Image);
            for (int x = 0; x < RenderWidth / TileWidth; x++)
            {
                for (int y = 0; y < RenderHeight / TileHeight; y++)
                {
                    int tx = x * (TileWidth);
                    int ty = y * (TileHeight);
                    System.Drawing.Rectangle r = new System.Drawing.Rectangle(tx,ty,TileWidth,TileHeight);
                    myImageGraphics.DrawRectangle(new Pen(System.Drawing.Color.Red), r);
                    byte[] b = new byte[] { 0/*Blue*/, 0/*Green*/, 255/*Red*/ , 0/*useless*/ };
                    int bgr = BitConverter.ToInt32(b,0);
                    CUSTOMVERTEX[] cv = new CUSTOMVERTEX[] { 
                    new CUSTOMVERTEX(r.Right, r.Y, 0, 1f, bgr),
                    new CUSTOMVERTEX(r.X, r.Top, 0, 1f, bgr),
                    new CUSTOMVERTEX(r.Left, r.Y, 0, 1f, bgr),
                    new CUSTOMVERTEX(r.X, r.Bottom, 0, 1f, bgr),
                    };
                    st.WriteRange(cv);
                    primitiveCount += 4;
                }
            }
            myImageGraphics.Dispose();
            bfr.Unlock();
            Mgr.OnDraw += new SharpDX.Action(Mgr_OnDraw);
            Thread render = new Thread(new ThreadStart(delegate{
                while (true)
                {
                    if (ActiveForm == this)
                        Mgr.Render();
                }
            }));
            render.Start();
        }
        int primitiveCount = 0;
        void Mgr_OnDraw()
        {
            Mgr.Device.Clear(ClearFlags.Target, new ColorBGRA(this.BackColor.ToArgb()), 0f, 1);
            Mgr.Device.SetStreamSource(0, bfr, 0, 20/*sizeof(CUSTOMVERTEX)*/);
            Mgr.Device.VertexFormat = VertexFormat.Diffuse | VertexFormat.PositionRhw;
            Mgr.Device.DrawPrimitives(PrimitiveType.LineStrip, 0, primitiveCount);
        }
        int RenderWidth = 200;
        int RenderHeight = 200;
        int TileWidth = 32;
        int TileHeight = 32;

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}

You are using PrimitiveType.LineStrip which is as stated in the doc `Renders the vertices as a single polyline.` Instead you should use PrimitiveType.LineList which is "Renders the vertices as a list of isolated straight line segments.". You will have to change your points gen also that you have (p0-p1), (p1-p2), (p2-p3)....etc.

Advertisement

You are using PrimitiveType.LineStrip which is as stated in the doc `Renders the vertices as a single polyline.` Instead you should use PrimitiveType.LineList which is "Renders the vertices as a list of isolated straight line segments.". You will have to change your points gen also that you have (p0-p1), (p1-p2), (p2-p3)....etc.

Thanks .. didn't get that part? can you explain

After changing from LineStrip to LineList this is the result:

Tdw8aAL.png

Some lines missing maybe because i didn't do that?


Some lines missing maybe because i didn't do that?

You just need to add this case yes. You can also consider drawing this grid differently long horizontal/vertical lines, and a 4 long lines around to close the board. It all depends on your requirements regarding the grid (if you need to move a single point in the grid not aligned to the others or not)

@xoofx

Actually i'm planing to draw the tiles in different colors(depends on coordinate validity)

so using long lines is not an option

Thank you for you amazing library and you perfect help.

This topic is closed to new replies.

Advertisement