Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 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