DDraw Init Problem : CreateSurface FAILED

Started by
3 comments, last by srink 23 years, 7 months ago
Hi, could you help me with this? It''s from the book DirectX7 in 24 hours(a bit modified). The error message is as follows: "CreateSurface FAILED" COMPLETE CODE: #include #include #define INITGUID #include LPDIRECTDRAW7 lpDD=NULL; LPDIRECTDRAWSURFACE7 lpDDSPrimary=NULL; LPDIRECTDRAWSURFACE7 lpDDSBack=NULL; int cur_image=0; #define IMAGE_COUNT 7 char file_names[IMAGE_COUNT][256]={ "slide001.bmp", "slide002.bmp", "slide003.bmp", "slide004.bmp", "slide005.bmp", "slide006.bmp", "slide007.bmp" }; LPDIRECTDRAWSURFACE7 lpSlides[IMAGE_COUNT]; static char szClass[]="XmplHr3Class"; static char szCaption[]="Example - Hour 3"; const char *ErrStr=NULL; const char Err_Reg_Class[]="Error Registering Window Class"; const char Err_Create_Win[]="Error Creating Window"; const char Err_DirectDrawCreate[]="DirectDrawCreate FAILED"; const char Err_Query[]="Query Interface FAILED"; const char Err_Coop[]="SetCooperativeLevel FAILED"; const char Err_CreateClip[]="CreateClip FAILED"; const char Err_CreateSurf[]="CreateSurface FAILED"; const char Err_LoadBMP[]="Error Loading Image"; const char Err_SetD[]="SetDisplay FAILED"; #define SafeRelease(x) if (x) {x->Release(); x=NULL;} void Cleanup(void) { for (int i=0;i); SafeRelease(lpDDSBack); SafeRelease(lpDDSPrimary); SafeRelease(lpDD); if(ErrStr) MessageBox(NULL, ErrStr, szCaption, MB_OK); } LPDIRECTDRAWSURFACE7 bitmap_surface(LPCTSTR file_name) { HDC hdc; HBITMAP bit; LPDIRECTDRAWSURFACE7 surf; bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE); if (!bit) return NULL; BITMAP bitmap; GetObject(bit, sizeof(BITMAP), &bitmap); int surf_width=bitmap.bmWidth; int surf_height=bitmap.bmHeight; HRESULT ddrval; DDSURFACEDESC2 ddsd; ZeroMemory(&ddsd,sizeof(ddsd)); ddsd.dwSize=sizeof(DDSURFACEDESC2); ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT; ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth=surf_width; ddsd.dwHeight=surf_height; ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL); if(ddrval!=DD_OK) { DeleteObject(bit); return NULL; } else { surf->GetDC(&hdc); HDC bit_dc=CreateCompatibleDC(hdc); SelectObject(bit_dc,bit); BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY); surf->ReleaseDC(hdc); DeleteDC(bit_dc); } DeleteObject(bit); return surf; } void draw_slide() { if (!lpSlides[cur_image]) { lpSlides[cur_image]=bitmap_surface(file_names[cur_image]); if (!lpSlides[cur_image]) return; } lpDDSBack->BltFast(0,0,lpSlides[cur_image],NULL,DDBLTFAST_WAIT); HDC hdc; if (DD_OK==lpDDSBack->GetDC(&hdc)) { SetTextColor(hdc,0x00ff7f00); SetBkColor(hdc,0x000000); TextOut(hdc,20,400,"<- Previous Slide",16); TextOut(hdc,540,400,"Next Slide ->",13); SetTextColor(hdc,0x0000ffff); TextOut(hdc,235,440,"Press Arrow Keys to Change Slides",33); lpDDSBack->ReleaseDC(hdc); } lpDDSPrimary->Flip(0,DDFLIP_WAIT); int next_slide=(cur_image>=IMAGE_COUNT-1)?0:cur_image+1; if (!lpSlides[next_slide]) lpSlides[next_slide]=bitmap_surface(file_names[next_slide]); int prev_slide=(cur_image<1)? IMAGE_COUNT-1:cur_image-1; if (!lpSlides[prev_slide]) lpSlides[prev_slide]=bitmap_surface(file_names[next_slide]); return; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_KEYDOWN: switch (wParam) { case VK_LEFT: cur_image--; if (cur_image<0) cur_image=IMAGE_COUNT-1; draw_slide(); break; case VK_RIGHT: cur_image++; if(cur_image>IMAGE_COUNT-1) cur_image=0; draw_slide(); break; case VK_ESCAPE: DestroyWindow(hWnd); break; default: break; } case WM_DESTROY: Cleanup(); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0L; } static BOOL Init(HINSTANCE hInstance, int nCmdShow) { WNDCLASS wc; HRESULT hRet; DDSURFACEDESC2 ddsd; DDSCAPS2 ddscaps; wc.style=CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc=(WNDPROC) WindowProc; wc.cbClsExtra=0; wc.cbWndExtra=sizeof(DWORD); wc.hInstance=hInstance; wc.hIcon=NULL; wc.hCursor=LoadCursor(NULL, IDC_ARROW); wc.hbrBackground=(HBRUSH) GetStockObject(BLACK_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName=szClass; if (!RegisterClass(&wc)) { ErrStr=Err_Reg_Class; return FALSE; } int ScreenWidth=GetSystemMetrics(SM_CXSCREEN); int ScreenHeight=GetSystemMetrics(SM_CYSCREEN); HWND hWnd; hWnd=CreateWindow(szClass, szCaption, WS_VISIBLE|WS_POPUP, 0,0,ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL); if (!hWnd) { ErrStr=Err_Create_Win; return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); ZeroMemory(lpSlides,sizeof(lpSlides)); hRet=DirectDrawCreateEx(NULL,(LPVOID*)&lpDD,IID_IDirectDraw7,NULL); if (hRet !=DD_OK) { ErrStr=Err_DirectDrawCreate; return FALSE; } hRet=lpDD->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT); if (hRet!=DD_OK) { ErrStr=Err_Coop; return FALSE; } hRet=lpDD->SetDisplayMode(800,600,16,0,0); if (hRet!=DD_OK) { ErrStr=Err_SetD; return FALSE; } ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize=sizeof(ddsd); ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX; ddsd.dwBackBufferCount=1; hRet=lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL); if (hRet!=DD_OK) { ErrStr=Err_CreateSurf; return FALSE; } ddscaps.dwCaps=DDSCAPS_BACKBUFFER; hRet=lpDDSPrimary->GetAttachedSurface(&ddscaps,&lpDDSBack); if (hRet!=DD_OK) { ErrStr=Err_CreateSurf; return FALSE; } lpSlides[0]=bitmap_surface(file_names[0]); if (!lpSlides[0]) return FALSE; draw_slide(); return TRUE; } int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; if (!Init(hInstance, nCmdShow)) { Cleanup(); return FALSE; } while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); }
Advertisement
I cant tell you what the problem is. It would be alot easier if you knew what error CreateSurface() returned. Here is how you could do that:

    const char*GetErrorString(HRESULT p_hRes){	switch(p_hRes)	{	case DDERR_ALREADYINITIALIZED:           return "DDERR_ALREADYINITIALIZED";	case DDERR_CANNOTATTACHSURFACE:          return "DDERR_CANNOTATTACHSURFACE";	case DDERR_CANNOTDETACHSURFACE:          return "DDERR_CANNOTDETACHSURFACE";	case DDERR_CURRENTLYNOTAVAIL:            return "DDERR_CURRENTLYNOTAVAIL";	case DDERR_EXCEPTION:                    return "DDERR_EXCEPTION";	case DDERR_GENERIC:                      return "DDERR_GENERIC";	case DDERR_HEIGHTALIGN:                  return "DDERR_HEIGHTALIGN";	case DDERR_INCOMPATIBLEPRIMARY:          return "DDERR_INCOMPATIBLEPRIMARY";	case DDERR_INVALIDCAPS:                  return "DDERR_INVALIDCAPS";	case DDERR_INVALIDCLIPLIST:              return "DDERR_INVALIDCLIPLIST";	case DDERR_INVALIDMODE:                  return "DDERR_INVALIDMODE";	case DDERR_INVALIDOBJECT:                return "DDERR_INVALIDOBJECT";	case DDERR_INVALIDPARAMS:                return "DDERR_INVALIDPARAMS";	case DDERR_INVALIDPIXELFORMAT:           return "DDERR_INVALIDPIXELFORMAT";	case DDERR_INVALIDRECT:                  return "DDERR_INVALIDRECT";	case DDERR_LOCKEDSURFACES:               return "DDERR_LOCKEDSURFACES";	case DDERR_NO3D:                         return "DDERR_NO3D";	case DDERR_NOALPHAHW:                    return "DDERR_NOALPHAHW";	case DDERR_NOCLIPLIST:                   return "DDERR_NOCLIPLIST";	case DDERR_NOCOLORCONVHW:                return "DDERR_NOCOLORCONVHW";	case DDERR_NOCOOPERATIVELEVELSET:        return "DDERR_NOCOOPERATIVELEVELSET";	case DDERR_NOCOLORKEY:                   return "DDERR_NOCOLORKEY";	case DDERR_NOCOLORKEYHW:                 return "DDERR_NOCOLORKEYHW";	case DDERR_NODIRECTDRAWSUPPORT:          return "DDERR_NODIRECTDRAWSUPPORT";	case DDERR_NOEXCLUSIVEMODE:              return "DDERR_NOEXCLUSIVEMODE";	case DDERR_NOFLIPHW:                     return "DDERR_NOFLIPHW";	case DDERR_NOGDI:                        return "DDERR_NOGDI";	case DDERR_NOMIRRORHW:                   return "DDERR_NOMIRRORHW";	case DDERR_NOTFOUND:                     return "DDERR_NOTFOUND";	case DDERR_NOOVERLAYHW:                  return "DDERR_NOOVERLAYHW";	case DDERR_NORASTEROPHW:                 return "DDERR_NORASTEROPHW";	case DDERR_NOROTATIONHW:                 return "DDERR_NOROTATIONHW";	case DDERR_NOSTRETCHHW:                  return "DDERR_NOSTRETCHHW";	case DDERR_NOT4BITCOLOR:                 return "DDERR_NOT4BITCOLOR";	case DDERR_NOT4BITCOLORINDEX:            return "DDERR_NOT4BITCOLORINDEX";	case DDERR_NOT8BITCOLOR:                 return "DDERR_NOT8BITCOLOR";	case DDERR_NOTEXTUREHW:                  return "DDERR_NOTEXTUREHW";	case DDERR_NOVSYNCHW:                    return "DDERR_NOVSYNCHW";	case DDERR_NOZBUFFERHW:                  return "DDERR_NOZBUFFERHW";	case DDERR_NOZOVERLAYHW:                 return "DDERR_NOZOVERLAYHW";	case DDERR_OUTOFCAPS:                    return "DDERR_OUTOFCAPS";	case DDERR_OUTOFMEMORY:                  return "DDERR_OUTOFMEMORY";	case DDERR_OUTOFVIDEOMEMORY:             return "DDERR_OUTOFVIDEOMEMORY";	case DDERR_OVERLAYCANTCLIP:              return "DDERR_OVERLAYCANTCLIP";	case DDERR_OVERLAYCOLORKEYONLYONEACTIVE: return "DDERR_OVERLAYCOLORKEYONLYONEACTIVE";	case DDERR_PALETTEBUSY:                  return "DDERR_PALETTEBUSY";	case DDERR_COLORKEYNOTSET:               return "DDERR_COLORKEYNOTSET";	case DDERR_SURFACEALREADYATTACHED:       return "DDERR_SURFACEALREADYATTACHED";	case DDERR_SURFACEALREADYDEPENDENT:      return "DDERR_SURFACEALREADYDEPENDENT";	case DDERR_SURFACEBUSY:                  return "DDERR_SURFACEBUSY";	case DDERR_CANTLOCKSURFACE:              return "DDERR_CANTLOCKSURFACE";	case DDERR_SURFACEISOBSCURED:            return "DDERR_SURFACEISOBSCURED";	case DDERR_SURFACELOST:                  return "DDERR_SURFACELOST";	case DDERR_SURFACENOTATTACHED:           return "DDERR_SURFACENOTATTACHED";	case DDERR_TOOBIGHEIGHT:                 return "DDERR_TOOBIGHEIGHT";	case DDERR_TOOBIGSIZE:                   return "DDERR_TOOBIGSIZE";	case DDERR_TOOBIGWIDTH:                  return "DDERR_TOOBIGWIDTH";	case DDERR_UNSUPPORTED:                  return "DDERR_UNSUPPORTED";	case DDERR_UNSUPPORTEDFORMAT:            return "DDERR_UNSUPPORTEDFORMAT";	case DDERR_UNSUPPORTEDMASK:              return "DDERR_UNSUPPORTEDMASK";	case DDERR_VERTICALBLANKINPROGRESS:      return "DDERR_VERTICALBLANKINPROGRESS";	case DDERR_WASSTILLDRAWING:              return "DDERR_WASSTILLDRAWING";	case DDERR_XALIGN:                       return "DDERR_XALIGN";	case DDERR_INVALIDDIRECTDRAWGUID:        return "DDERR_INVALIDDIRECTDRAWGUID";	case DDERR_DIRECTDRAWALREADYCREATED:     return "DDERR_DIRECTDRAWALREADYCREATED";	case DDERR_NODIRECTDRAWHW:               return "DDERR_NODIRECTDRAWHW";	case DDERR_PRIMARYSURFACEALREADYEXISTS:  return "DDERR_PRIMARYSURFACEALREADYEXISTS";	case DDERR_NOEMULATION:                  return "DDERR_NOEMULATION";	case DDERR_REGIONTOOSMALL:               return "DDERR_REGIONTOOSMALL";	case DDERR_CLIPPERISUSINGHWND:           return "DDERR_CLIPPERISUSINGHWND";	case DDERR_NOCLIPPERATTACHED:            return "DDERR_NOCLIPPERATTACHED";	case DDERR_NOHWND:                       return "DDERR_NOHWND";	case DDERR_HWNDSUBCLASSED:               return "DDERR_HWNDSUBCLASSED";	case DDERR_HWNDALREADYSET:               return "DDERR_HWNDALREADYSET";	case DDERR_NOPALETTEATTACHED:            return "DDERR_NOPALETTEATTACHED";	case DDERR_NOPALETTEHW:                  return "DDERR_NOPALETTEHW";	case DDERR_BLTFASTCANTCLIP:              return "DDERR_BLTFASTCANTCLIP";	case DDERR_NOBLTHW:                      return "DDERR_NOBLTHW";	case DDERR_NODDROPSHW:                   return "DDERR_NODDROPSHW";	case DDERR_OVERLAYNOTVISIBLE:            return "DDERR_OVERLAYNOTVISIBLE";	case DDERR_NOOVERLAYDEST:                return "DDERR_NOOVERLAYDEST";	case DDERR_INVALIDPOSITION:              return "DDERR_INVALIDPOSITION";	case DDERR_NOTAOVERLAYSURFACE:           return "DDERR_NOTAOVERLAYSURFACE";	case DDERR_EXCLUSIVEMODEALREADYSET:      return "DDERR_EXCLUSIVEMODEALREADYSET";	case DDERR_NOTFLIPPABLE:                 return "DDERR_NOTFLIPPABLE";	case DDERR_CANTDUPLICATE:                return "DDERR_CANTDUPLICATE";	case DDERR_NOTLOCKED:                    return "DDERR_NOTLOCKED";	case DDERR_CANTCREATEDC:                 return "DDERR_CANTCREATEDC";	case DDERR_NODC:                         return "DDERR_NODC";	case DDERR_WRONGMODE:                    return "DDERR_WRONGMODE";	case DDERR_IMPLICITLYCREATED:            return "DDERR_IMPLICITLYCREATED";	case DDERR_NOTPALETTIZED:                return "DDERR_NOTPALETTIZED";	case DDERR_UNSUPPORTEDMODE:              return "DDERR_UNSUPPORTEDMODE";	case DDERR_NOMIPMAPHW:                   return "DDERR_NOMIPMAPHW";	case DDERR_INVALIDSURFACETYPE:           return "DDERR_INVALIDSURFACETYPE";	case DDERR_DCALREADYCREATED:             return "DDERR_DCALREADYCREATED";	case DDERR_CANTPAGELOCK:                 return "DDERR_CANTPAGELOCK";	case DDERR_CANTPAGEUNLOCK:               return "DDERR_CANTPAGEUNLOCK";	case DDERR_NOTPAGELOCKED:                return "DDERR_NOTPAGELOCKED";	case DDERR_NOTINITIALIZED:               return "DDERR_NOTINITIALIZED";	case DD_OK:								 return "DD_OK";	default: return "Unknown error";	}}hRes = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);if(FAILED(hRes)){    DDrawErrDesc = GetErrorString(hRes);}    


Just copy and paste the function GetErrorString into your source and call it with the HRESULT returned from CreateSurface() as parameter. The return value is a string describing the error. You could post the return value and maybe i will be able to help you.
Thank you very much for your error message function!
Sorry, my answer is a bit late, but I was in the vacation.

This time with your code pasted in I received the error DDERR_NOTFOUND. But I don''t know what''s missing. It''s all there!

Thanks in advance
If I remember correctly, there are sevral errors in the first 4 chapters of learn dx in 24. goto the authors site to d/l the fixed code. (i forgot the url it should be somewhere in the book)
I found that with the DDraw7 surface in the loadbitmap function if i made the temp surface a global variable outside that function it worked otherwise it wouldnt...i still dont know why though.

The surface i am talking about is surf in the bitmap_surface function...i used that code to base what i currently have now

oddly a DirectDraw4 surface will work with surf declared in there and the code on the cd with the book uses DDraw4 surfaces on all the 2d stuff

This topic is closed to new replies.

Advertisement