Solved

DirectDraw 7 Picture is being 'slanted' with most surface widths

Posted on 2003-11-15
5
272 Views
Last Modified: 2010-05-18
I have writen a method the takes a an apmatrix or colors and turns it into a DD7 Surface. It works great if the width of the suface is a multiple of 24 pixels. Otherwise the pisture is there, but slanted. If i do one pixel under a multiple of 24 then each line down the image is offset by one pixel to the right. I am running this with a screen color depth of 32 bpp. The height of the surface does not seem to matter.

Here is my method:
LPDIRECTDRAWSURFACE7 Color2Image(apmatrix<int> colors)
{
  int lX = 0, lY = 0;
  DDSURFACEDESC2 DDsd;
  LPDIRECTDRAWSURFACE7 Surf;
  HRESULT retval = 0;

  int SurfaceSize = 0;
  if (ColorDepth == 16)
    SurfaceSize = colors.numrows() * 2 * colors.numcols();
  else if (ColorDepth == 24)
    SurfaceSize = colors.numrows() * 3 * colors.numcols();
  else if (ColorDepth == 32)
    SurfaceSize = colors.numrows() * 4 * colors.numcols();
  else //Not an acceptable bit depth
    return NULL;
  unsigned char * yArray = new unsigned char[SurfaceSize];
  ZeroMemory(yArray, SurfaceSize);

  //Create the surface
  ZeroMemory(&DDsd, sizeof(DDsd));
  DDsd.dwSize = sizeof(DDsd);
  DDsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  DDsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  DDsd.dwHeight = colors.numcols();
  DDsd.dwWidth = colors.numrows();

  if (ColorDepth == 16) { //Transfer to a 16-bit surface
    //...
  } else if (ColorDepth == 24) { //Transfer to a 24-bit surface
    //...
  } else if (ColorDepth == 32) { //Transfer to a 32-bit surface
    for (lY = 0; lY < colors.numcols(); lY++) {
      for (lX = 0; lX < colors.numrows(); lX++) {
        yArray[lX * 4 + colors.numrows() * 4 * lY + 0] = GetBValue(colors[lX][lY]); //Transfer 'b' byte
        yArray[lX * 4 + colors.numrows() * 4 * lY + 1] = GetGValue(colors[lX][lY]); //Transfer 'g' byte
        yArray[lX * 4 + colors.numrows() * 4 * lY + 2] = GetRValue(colors[lX][lY]); //Transfer 'r' byte
        yArray[lX * 4 + colors.numrows() * 4 * lY + 3] = 0; //Skip every 4th byte (alpha channel)
      }
    }
  } else { //Not an acceptable bit depth
    delete[] yArray;
    return false;
  }
  retval = lpDD->CreateSurface(&DDsd, &Surf, NULL);
  retval = Surf->SetColorKey(DDCKEY_SRCBLT, &TransColorKey);
  //Draw the data onto the surface
  retval = Surf->Lock(NULL, &DDsd, DDLOCK_WAIT | DDLOCK_NOSYSLOCK | DDLOCK_WRITEONLY, 0);
  BYTE* pDDSurf = (BYTE*)DDsd.lpSurface;
  ZeroMemory(pDDSurf, SurfaceSize);
  memcpy(pDDSurf, yArray, SurfaceSize);
  retval = Surf->Unlock(NULL);
  delete[] yArray;
  return Surf;
}
0
Comment
Question by:thaimin
  • 2
  • 2
5 Comments
 
LVL 1

Author Comment

by:thaimin
Comment Utility
I solved the problem. It is because the pitch is forced to be a multiple of 24 (at least on my graphics card) and so you have to copy each row individually, only copying the width of the image each time, but to an area that is row * pitch on the surface.
Here is the code that replaces memcpy(pDDSurf, yArray, SurfaceSize);

BYTE* pDDSurf = (BYTE*)DDsd.lpSurface;
for (lY = 0; lY < colors.numcols(); lY++)
      memcpy(&pDDSurf[lY * DDsd.lPitch], &yArray[lY * bpr], bpr); //where bpr is bytes per row
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 1

Author Comment

by:thaimin
Comment Utility
I recommend PAQ with point refund.
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
thaimin

Mine was just a recommendation for the cleanup that will be done 7 days after my post.

As you are the asker of the question, I guess that it is you better than anyone who knows if the question is right answered or not, so it would be better that it is you the one who close it. Just keep in mind that my recommendation may differ from what you would like.

I would really appreciate if you could do this, as it is of great help for cleanup volunteers and moderators in general when users close their own questions.

If you need help in closing your questions, please check
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/help.jsp#hs5

If you still have any doubts feel free to ask me for it.

Thanks

Tincho

PS: Up to this moment, you have 2 open questions, 2 of which (including this one) could be considered as abandoned. Please, I would really appreciate if you could handle them as well.
In order to see which your open questions are check
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/QH_841659.html
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
Comment Utility
PAQed, with points refunded (100)

Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now