• Advertisement
Sign in to follow this  

problems with surfaces

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm currently reading the book "game programming all in one" and I've stuck on a problem. Although I did everything that was written in the book and although everything compiles right, I still do not get what I should get . With the following code I should get a certain grey pattern on the top left of my window : Code:
/* Initializes Direct3D */
HRESULT D3DWindow::SetupDirect3D(void)
{
   /* Create the Direct3D object */
   if (NULL == (m_pD3D = Direct3DCreate8(D3D_SDK_VERSION)))
   {
      return E_FAIL ;
   }

   /* Get the current display mode so we can know what bitdepth we are */
   D3DDISPLAYMODE d3ddm;
   if (FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))
   {
      return E_FAIL ;
   }

   /* Fill in the present parameters */
   D3DPRESENT_PARAMETERS d3dpp ;

   ZeroMemory(&d3dpp, sizeof(d3dpp)) ;
   /* We want windowed mode */
   d3dpp.Windowed = TRUE ;
   
   /* Discard this */
   d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ;
   /* Same format as the current format
   (we got this from g_pD3D->GetAdapterDisplayMode) */
   d3dpp.BackBufferFormat = d3ddm.Format ;

   /* Create the device */
   if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
                           m_hWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                           &d3dpp, &m_pD3DDevice)))
   {
      return E_FAIL ;
   }

   /* Create the surface */
   if (FAILED(m_pD3DDevice->CreateImageSurface(256, 256,
                           (D3DFORMAT) m_iD3DFormat, &m_pD3DSurface)))
   {
      return E_FAIL ;
   }

   /* Lock surface */
   D3DLOCKED_RECT kLockedRect ;
   m_pD3DSurface->LockRect(&kLockedRect, NULL, 0) ;

   /* Cast a pointer to point to the first pixel */
   DWORD *piSurfaceBuffer = (DWORD *)kLockedRect.pBits ;

   /* Fill surface */
   int iX, iY ;
   for (iY=0; iY<256; iY++)
   {
      for (iX=0; iX<256; iX++)
      {
         /* Form a pattern */
         int iPower = (int)(sin (iY * iX) * 128 + cos (iY * -iX) * 128) ;
         piSurfaceBuffer[iX + iY* (kLockedRect.Pitch >> 2)] =
                           D3DCOLOR_XRGB (iPower, iPower, iPower) ;
      }
   }

   /* Unlock */
   m_pD3DSurface->UnlockRect();

   return D3D_OK ;
}

mrBool32 D3DWindow::Frame(void)
{
   /* Clear the window to blue */
   m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                  D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0) ;

   /* Get back buffer */
   LPDIRECT3DSURFACE8   pBackBuffer ;
   m_pD3DDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer) ;

   /* Start rendering */
   m_pD3DDevice->BeginScene() ;
   
   /* Copy the surface to the screen */
   m_pD3DDevice->CopyRects(m_pD3DSurface, NULL, 0, pBackBuffer, NULL) ;

   m_pD3DDevice->EndScene() ;

   /* Present the rendered scene to the screen */
   m_pD3DDevice->Present(NULL, NULL, NULL, NULL) ;

   return mrTrue ;
}

The function setupDirect3D is (obviously) only runned once. The function frame is runned continueously. Everything compiles with no errors and D3D is initialized right, because I do get the blue background that I ask for in the frame() function. Does any1 see something wrong with this code?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by da_cobra
Hello,

I'm currently reading the book "game programming all in one"
and I've stuck on a problem.
Although I did everything that was written in the book and although everything compiles right, I still do not get what I should get .
With the following code I should get a certain grey pattern on the top left of my window :

Code:

*** Source Snippet Removed ***

The function setupDirect3D is (obviously) only runned once.
The function frame is runned continueously.
Everything compiles with no errors and D3D is initialized right, because I do get the blue background that I ask for in the frame() function.

Does any1 see something wrong with this code?


Where you have an camera and view setup ?

Share this post


Link to post
Share on other sites
Hmmmm, as you can see, there is no camera/view.
Could this be the part that's missing?

Share this post


Link to post
Share on other sites
Quote:
Original post by da_cobra
Hmmmm, as you can see, there is no camera/view.
Could this be the part that's missing?

No, this shouldn't be the issue - you're doing fairly simple 2D work, probably not even touching the GPU for any of it's processing power [smile]

If it's from a book, have you got some example code on a CD? That could be a useful reference point - I'd be surprised if they shipped a CD with broken code on it!

Secondly to that, check it against the debug runtimes (link in my signiture below) and see if it's spitting out any messages. You might be lucky and find that it's giving your warnings/errors that'll lead you to your mistake...

hth
Jack

Share this post


Link to post
Share on other sites
thx jollyjeffers,

I used debugged my application (through your link in your signature)
and I got these errors :

Direct3D8: (ERROR) :This format is not supported for CreateImageSurface
Direct3D8: (ERROR) :Failure trying to create image surface
Direct3D8: (ERROR) :NULL surface interface specified. CopyRect fails

So what exactly is wrong? And how can I fix it?

Share this post


Link to post
Share on other sites
Right, perfect - thats exactly the sort of error message we wanted. Tells you exactly what the problem is [grin]

Direct3D8: (ERROR) :This format is not supported for CreateImageSurface

This means that the (D3DFORMAT) m_iD3DFormat part in your CreateImageSurface() call contains an invalid value. Now, if you're getting this code from a book it is quite possible that what worked for the author's hardware won't work for your hardware.

Direct3D8: (ERROR) :NULL surface interface specified. CopyRect fails

Because of the above error message, you never actually created a surface, so when you try to display it in the main loop it obviously fails - as there isn't anything to show. Fix the first error and this one will go away.


Okay, so the trick for solving the first problem is to pick a suitable format. BUT, because you're manually locking your surface and writing data to it you'll have to be careful. If you pick the wrong format, then the subsequent lock/write code will, at best, not work. Seems like a 32bit integer format will be required.

You need to check out either the "DX Caps Viewer" tool in the SDK for a list of your supported formats, or use the IDirect3D8::CheckDeviceFormat() function...

hth
Jack

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement