DDraw - Problem creating surfaces, and locking.

Started by
5 comments, last by reaptide 23 years, 11 months ago
Hello everyone, I'm having some trouble with my DDraw program when it come to my surface creation function. I have the code listed below. The problem is that the surface does not seem to be created. I am unable to blit the surface or load and image into it. Can anyone figure out what is going on? The surface passed to the function is stored as a global. << START CODE >> // Create generic direct draw offscreen surface bool SSCreateSurface(LPDIRECTDRAWSURFACE lpDDS, int width, int height) { DDSURFACEDESC ddsd; HRESULT ddrval; ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS / DDSD_HEIGHT / DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwHeight = height; ddsd.dwWidth = width; ddrval = lpDD->CreateSurface(&ddsd, &lpDDS, NULL); if (ddrval != DD_OK) { OutputDebugString("Couldn't create offscreen surface in SSCreateSurface() - DXUtils.cpp\n"); MessageBox(hWnd,"Couldn't create offscreen surface.","ERROR",MB_OK/MB_ICONSTOP); return NULL; } return TRUE; } << END CODE >> As well I was wondering whether it is necessary to lock a surface when blitting another surface to it, or if locking is only necessary when directly manipulating the surface pixels. Thanks for any help, Derek Edited by - Reaptide on 4/28/00 1:31:35 PM
Advertisement
It is not necessary to Lock a surface when blitting ,think DD will do it for you automaticly.

Regarding the surfaceCreation question, your code seems ok,
the only thing I can think of is that ýou dont clear the DDSURFACEDESC structure. Try memset(&desc,0,sizeof(desc)) before loading it with data.

/ Tooon

It is not necessary to Lock a surface when blitting ,think DD will do it for you automaticly.

Regarding the surfaceCreation question, your code seems ok,
the only thing I can think of is that ýou dont clear the DDSURFACEDESC structure. Try memset(&desc,0,sizeof(desc)) before loading it with data.

/ Tooon



Maybe it has something to do with the pass by value on the Surface pointer. Try passing it by reference (with an ampersand), or passing in a pointer. Like:

bool SSCreateSurface(LPDIRECTDRAWSURFACE *lplpDDs, int width, int height);

Also, you should zero out your surface description with ZeroMemory or memset before using it.

Jesse Chounard
Hello again,

It works now. I don''t have a clue why, but when I rewote the code to like it now does below it worked. If anyone feels like explain this to me, please feel free to do so.
WOOHOO!!

<< CODE START >>

LPDIRECTDRAWSURFACE SSCreateSurface(int width, int height)
{
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE lpDDS;
HRESULT ddrval;

ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS / DDSD_HEIGHT / DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwHeight = height;
ddsd.dwWidth = width;

ddrval = lpDD->CreateSurface(&ddsd, &lpDDS, NULL);
if (ddrval != DD_OK)
{
OutputDebugString("Couldn''t create offscreen surface in SSCreateSurface() - DXUtils.cpp\n");
MessageBox(NULL,"Couldn''t create offscreen surface.","ERROR",MB_OK/MB_ICONSTOP);
return FALSE;
}

return lpDDS;
}

lpDDSTest = SSCreateSurface(640, 480);

<< END CODE >>

Thanks for the help guys,

Reaptide
It''s the ZeroMemory. You have to clear all the fields that you don''t want to fill out. So that''s why you have to call ZeroMemory. Just clear every structure before you pass it to DDraw.
Yoshi

The last truth is that there is no magic(Feist)
The last truth is that there is no magic(Feist)
It works because you are returning the surface. (The ZeroMemory is good too.) I believe it''s very similar to this situation:
void my_malloc(void *ptr, int size){    ptr=malloc(size);}

This doesn''t work, but this does:
void* my_malloc(int size){    return malloc(size);}

This has to do with passing by value vs passing by reference. The following code also works:
void my_malloc(void **ptr, int size){    *ptr=malloc(size);}


Does that make sense?

Jesse Chounard

This topic is closed to new replies.

Advertisement