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

x
?
Solved

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

Posted on 2003-11-15
5
Medium Priority
?
280 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
[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
  • 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

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

722 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