Solved

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

Posted on 2003-11-15
5
274 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
simple way to manipulate chinese characters in vb6 application. 13 279
Embarcadero C++ Builder XE2 TDateTime 8 61
FMX TCameraComponent Problem 2 52
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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. …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

895 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

12 Experts available now in Live!

Get 1:1 Help Now