Solved

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

Posted on 2003-11-15
5
275 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
ID: 9917716
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
ID: 10249250
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
ID: 10249713
I recommend PAQ with point refund.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249721
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
ID: 10263211
PAQed, with points refunded (100)

Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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