Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DrawLine in DirectDraw

Posted on 2001-08-28
4
Medium Priority
?
792 Views
Last Modified: 2010-05-18
Can someone give me an example of drawing simple lines in DirectDraw and C++. (2D)
0
Comment
Question by:franceskinm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6431502
Small example of using DirectDraw you can find in
http://www.theparticle.com/pgraph.html
In http://www.cs.mcgill.ca/~kshaikh/ComputerGraphicsTutorial.html
Press softvare, lesson 3, display code, you can see, how
display line .
0
 
LVL 2

Author Comment

by:franceskinm
ID: 6431707
Thank you, but the examples you gave me use DrawPrimitive that goes trough 3D engine (if I understood right). What I wanted was an example of drawing in 2D directly to DirectDraw surface.
0
 

Expert Comment

by:Mithander
ID: 6432782
The way I've always done it is to lock a DC.  Use the windows api to draw on it, and release the DC.  I'm not at home right now, so I can't put up some code.  But if you look in the DirectX help for LockDC... Or something like that :)  you should be ok.  This is a pretty slow way to do it, but I don't think anything else is supported.
0
 
LVL 5

Accepted Solution

by:
FengYuan earned 800 total points
ID: 6435205
From my book (www.fengyuan.com)

void KLockedSurface::Line(int x0, int y0, int x1, int y1, DWORD color)
{
     int bps       = (bpp+7) / 8;                           // bytes-per-pixel
     BYTE * pPixel = pSurface + pitch * y0 + bps * x0; // first pixel address

     int error;                              // error
     int d_pixel_pos, d_error_pos;   // adjustment to pixel address and error when error>=0
     int d_pixel_neg, d_error_neg;     // adjustment to pixel address and error when error<0
     int dots;                              // number of dots to draw

     {
          int dx, dy, inc_x, inc_y;

          if ( x1 > x0 )
          {      dx = x1 - x0; inc_x =  bps; }
          else
          {     dx = x0 - x1; inc_x = -bps; }

          if ( y1 > y0 )
          {      dy = y1 - y0; inc_y =  pitch; }
          else
          {     dy = y0 - y1; inc_y = -pitch; }

          d_pixel_pos = inc_x + inc_y;    // move x and y
          d_error_pos = (dy - dx) * 2;
     
          if ( d_error_pos < 0 ) // x dominant
          {
               dots        = dx;
               error       = dy*2 - dx;
               d_pixel_neg = inc_x;          // move x only
               d_error_neg = dy * 2;
          }
          else
          {
               dots        = dy;
               error       = dx*2 - dy;
               d_error_pos = - d_error_pos;
               d_pixel_neg = inc_y;          // move y only
               d_error_neg = dx * 2;
          }
     }

     switch ( bps )
     {
          case 1:
               for (; dots>=0; dots--)               // 8-bpp pixel loop
               {
                    pPixel[0] = (BYTE) color;     // draw 8-bpp pixel
               
                    if ( error>=0 )
                    {  pPixel += d_pixel_pos; error  += d_error_pos; }
                    else
                    {  pPixel += d_pixel_neg; error  += d_error_neg; }
               }
               break;

          case 2:
               for (; dots>=0; dots--)               // 16-bpp pixel loop
               {
                    * (WORD *) pPixel = (WORD) color;     // draw 16-bpp pixel
               
                    if ( error>=0 )
                    {  pPixel += d_pixel_pos; error  += d_error_pos; }
                    else
                    {  pPixel += d_pixel_neg; error  += d_error_neg; }
               }
               break;

          case 3:
               for (; dots>=0; dots--)               // 24-bpp pixel loop
               {
                    * (RGBTRIPLE *) pPixel = * (RGBTRIPLE *) & color;     // draw 24-bpp pixel
               
                    if ( error>=0 )
                    {  pPixel += d_pixel_pos; error  += d_error_pos; }
                    else
                    {  pPixel += d_pixel_neg; error  += d_error_neg; }
               }
               break;

          case 4:
               for (; dots>=0; dots--)               // 32-bpp pixel loop
               {
                    * (DWORD *) pPixel = color;     // draw 32-bpp pixel
               
                    if ( error>=0 )
                    {  pPixel += d_pixel_pos; error  += d_error_pos; }
                    else
                    {  pPixel += d_pixel_neg; error  += d_error_neg; }
               }
               break;
     }
}

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question