Talk:DirectX:DirectDraw:Tutorials:VB:DX7:Efficient Line Drawing and Text Display

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Note: This method is actually slower than the Microsoft implementation - both involve locking surfaces but internally, the Microsoft inbuild API calls incur much less overhead... User:202.72.170.77

Actually, when I first created this tutorial, I tested this method and it was quite a bit faster than using the DirectX drawing routines (when more than a handful of line or text draw calls were made per frame). Have you tested it yourself? As explained in the tutorial, if you use the API method the surface is only locked once, which saves time if you can batch all of your drawing routines and execute them during a single lock. --Ryan Clark 11:50, 25 January 2006 (EST)

Efficient line and text drawing in DirectDraw are easily achieved.

The internal DrawLine and DrawText routines are slow, because they involve locking the surface and making GDI calls to place the line or the text on the surface. DrawText is fine for smaller operations, such as displaying the FPS or for debugging operations where you need to quickly and easily plot text onto a surface...perhaps to display the value of a variable. Of course, this would be normally be done to the backbuffer prior to a Flip() or Draw() call which updates the screen/window respectively.

If you need to plot a horizontal or vertical line, do not use DrawLine. Depending on your requirements and what your video driver supports, you could do one of the following - all of which are faster than the stock standard DirectDraw methods:

1. Using a drawing application like MS Paint, Paintshop Pro or Photoshop, create a 1x1 pixel bitmap. The color of this pixel will depend on the color of the line you wish to draw, assuming you already know what color it will be. To draw line 1 pixel thick/wide, all you need to do is use Blt or Draw (depending on whether you're using DirectDraw 7.0 or DirectDraw 9.0) to draw this pixel onto the desired surface but STRETCH it horizontally or vertically by changing the destination rectangle accordingly.

For example:

Dim Destination As New Rectangle(32,32,1,32) oBackbuffer.Draw(Destination, oSurfPixel, DrawFlags.Wait)

This will draw the pixel at 32,32 on the screen but will stretch it vertically so that it is 32 pixels high, creating a line.

Keep in mind that this CAN be done programatically during your applications setup to illeviate the need to load a bitmap externally. Simply create the surface and do a ColorFill operation to it and keep the pixel in memory during runtime for use when drawing a line of the desired color.

2. As above, but use a bitmap which contains the line of the desired height/width/thickness if this is already known. Using this method

   you could import "ready made" diagonal lines (if their characteristics were already known).  This also means that because you're drawing
   a line from a bitmap, the size of which is not going to change, you can use BltFast/DrawFast with their appropriate ColorKey flags to 
   specify transparency.  BltFast/DrawFast are slightly faster than Blt/Draw but do not allow stretching, which is why we're relying on 
   the bitmap to contain a line which is predefined.

3. Another method which is similar to the above but gives you greater control is to create a 1x1 pixel surface and colorfill it to the

   desired color.  By using BltFast/Drawfast, you can then plot that pixel to any location on the screen.  This is the beginnings of a 
   line drawing function. Through recursiveness, you can then draw another pixel across or down from the originator.. eventually
   creating a dynamic line of the desired length.  This method is most useful for situations like drawing tools writted with DirectDraw,
   where the user can do freehand drawing, pixel plotting or line drawing.  Additionally, by moving 1 or more pixels across/down, you
   can draw a diagonal line of any length.  BltFast/Drawfast directly to the Backbuffer, even with a 1x1 pixel, will always be faster
   than alternatives which rely on GDI!

I hope this helps. :)

User:202.72.170.77