Skip to main content
gamedev.net
🔒 Locked

DrawRectangle - Question

Started by Kryptus 1 reply 400+ views
Original Post
Kryptus
Kryptus
I'm putting together a small engine just to play around with in Managed DirectX 9 for C# 2005, but this whole concept applies to DX in general. I've made a class dedicated to the output of 2D graphics on the screen such as fonts and shapes. My draw rectangle subroutine is displayed below:

private CustomVertex.TransformedColored[] vRectangle = new CustomVertex.TransformedColored[5];

public void DrawRectangle(float Left, float Top, float Width, float Height, Color Colour)
        {
            vRectangle[0].Position = new Vector4(Left, Top, 1.0f, 1.0f); vRectangle[0].Color = Colour.ToArgb();
            vRectangle[1].Position = new Vector4(Left + Width, Top, 1.0f, 1.0f); vRectangle[1].Color = Colour.ToArgb();
            vRectangle[2].Position = new Vector4(Left + Width, Top + Height, 1.0f, 1.0f); vRectangle[2].Color = Colour.ToArgb();
            vRectangle[3].Position = new Vector4(Left, Top + Height, 1.0f, 1.0f); vRectangle[3].Color = Colour.ToArgb();
            vRectangle[4].Position = new Vector4(Left, Top, 1.0f, 1.0f); vRectangle[4].Color = Colour.ToArgb();

            SWGraphics.D3DDev.VertexFormat = CustomVertex.TransformedColored.Format;
            SWGraphics.D3DDev.DrawUserPrimitives(PrimitiveType.LineStrip, 4, vRectangle);
        }


What I want to ask is this: This sub can be called once, twice or even three times from the same variable and still render to the screen, except for the fact that everytime it's rendered is sets the position again. I'm concerned about any decrease in framerate or CPU/RAM usage. What happens? Is it wise to do it this way? I'll show a small example of how it's used: So this code will draw 4 rectangles.

public CScreen Screen = null;

Screen.DrawRectangle(Blah Blah);
Screen.DrawRectangle(Blah Blah);
Screen.DrawRectangle(Blah Blah);
Screen.DrawRectangle(Blah Blah);


I would prefer it as the above way instead of:

// This way would mean I can set up the size, colour, etc. before I render,
// possibly increasing framerate? This way is too long though.
public CScreen Screen1 = null;
public CScreen Screen2 = null;
public CScreen Screen3 = null;
public CScreen Screen4 = null;

Screen1.DrawRectangle(Blah Blah);
Screen2.DrawRectangle(Blah Blah);
Screen3.DrawRectangle(Blah Blah);
Screen4.DrawRectangle(Blah Blah);


How should I do it?
aaron_ds
aaron_ds
I'll give this a shot.

Having multiple screens is a bit counter intuitive in this senario.

Try creating a CRectangle class.
This or something like it would go in the constructor
vRectangle[0].Position = new Vector4(Left, Top, 1.0f, 1.0f); vRectangle[0].Color = Colour.ToArgb();            vRectangle[1].Position = new Vector4(Left + Width, Top, 1.0f, 1.0f); vRectangle[1].Color = Colour.ToArgb();            vRectangle[2].Position = new Vector4(Left + Width, Top + Height, 1.0f, 1.0f); vRectangle[2].Color = Colour.ToArgb();            vRectangle[3].Position = new Vector4(Left, Top + Height, 1.0f, 1.0f); vRectangle[3].Color = Colour.ToArgb();            vRectangle[4].Position = new Vector4(Left, Top, 1.0f, 1.0f); vRectangle[4].Color = Colour.ToArgb();

and CRectangle would contain an array CustomVertex.TransformedColored

Then change CScreen's DrawREctangle to something like:
public void DrawRectangle(CRectangle rectangle)        {            SWGraphics.D3DDev.VertexFormat = CustomVertex.TransformedColored.Format;            SWGraphics.D3DDev.DrawUserPrimitives(PrimitiveType.LineStrip, 4, rectangle.getVertices());        }


Note:By no means is the above syntatically correct. I'm a C++ programmer, not a C# programmer, but the same principles apply.

If any of this doesn't make sense, I'd be happy to brush up on my C# and post something more compilable.

Kryptus
Kryptus
I'm not sure I understand. It appears this will do the exact same thing? Will it?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.