Error creating Direct Draw Surface

Started by
3 comments, last by drdarkon 21 years, 10 months ago
Hello everyone, Oops, Im sorry I think I may have put the other one in the wrong forum... I will put it here since this is the forum on directX. Sorry about that I have a problem. I am writing a little pong game in Direct X and I am currently just working on getting Direct X initialized etc. I had everything working okay and the image loading and displaying fine when I just had the primary surface. But now I add the back buffer and all hell broke loose. VC++ isn''t giving me any errors but I have little message boxes that I made are telling me the error is right after I try and make the primary surface. Bah! Now I can''t make my primary surface. I''m sure the error is something simple that I just passed over, but I have gone over this a bunch of times and I am not sure what I have done. I''m still learning and could really use some help. Thanks a bunch for taking the time to read this. Okay now my problem is around the bottom where I try to make the primary surface. I think the problem may have something to do with ddsd because that was changed around a bit. I''m not really sure. Thanks I hope this is how I put code up: /code /*--------------------------------------------------------------------------*/ // SUPER SMASH PONG // Programmed by Jesse Barksdale // Art by Matthew Herms // June 2002 /*--------------------------------------------------------------------------*/ #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif //------ Include Files ------// #include "stdafx.h" #define INITGUID #include <ddraw.h> //------ Image Loading and Initialization Flags ------// BOOL bInit=FALSE; //------ Global Interface Pointers ------// LPDIRECTDRAW4 lpDD=NULL; // DirectDraw object LPDIRECTDRAWSURFACE4 lpDDSPrimary=NULL; // DirectDraw primary surface LPDIRECTDRAWSURFACE4 lpDDSBack=NULL; // DirectDraw back buffer LPDIRECTDRAWCLIPPER lpClip=NULL; // Directray Clipper Object LPDIRECTDRAWSURFACE4 lpTitleScreen=NULL; // Bitmap surface //------ Window Class Information ------// static char szClass[] = "XmplHr2Class"; static char szCaption[] = "SUPER SMASH PONG"; //------ Menu Variables ------------// #define MENU_MAIN_START 1 //main menu selections #define MENU_MAIN_OPTIONS 2 #define MENU_MAIN_DIRECTIONS 3 #define MENU_MAIN_ABOUT 4 #define MENU_MAIN_EXIT 5 //coordinates to draw the menu cursor #define MENU_START_X 32 #define MENU_START_Y 230 #define MENU_STYLE_ONEONONE 1 //game style menu selections #define MENU_STYLE_ONEONCOMP 2 #define MENU_PADDLE_NORMAL 0 #define MENU_PADDLE_FAST 1 #define MENU_PADDLE_STRONG 2 int menu_main_cursor = MENU_MAIN_START; //keeps track of the location of the main menu cursor int menu_game_style = MENU_STYLE_ONEONONE; //keeps track of the cursor location for selecting game style (1on1, 1onCOMP) int game_style; //holds info telling whether the game is 1on1 or 1onCOMP (1 = 1on1, 2 = 1onCOMP) int menu_paddle_select = MENU_PADDLE_NORMAL; int player_one_paddle;//holds the player paddle types int player_two_paddle; int menu_options_cursor = 1; //keeps track of the options cursor location //------ Game variables ------------// #define GAME_STATE_MAIN_MENU 0 //the main menu with all the selections #define GAME_STATE_OPTIONS 1 //options screen #define GAME_STATE_ABOUT 2 //about us screen #define GAME_STATE_DIRECTIONS 3 //directions screen #define GAME_STATE_EXIT 4 //shows an exit screen with info about us, etc #define GAME_STATE_PLAY_STYLE 5 //1on1 or 1onCOMP? #define GAME_STATE_PADDLE_SELECT 6 //choose your paddle #define GAME_STATE_PLAY 7 //the actual game #define GAME_STATE_SCORE 8 //the end game score screen int game_state = GAME_STATE_MAIN_MENU; int current_level = 1;//The current level being played int player_one_score = 0;//player scores for the current game. Scores are reset every level int player_two_score = 0; //------ Error Return String ------// const char *ErrStr; //------ Error Messages ------// 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[] = "QueryInterface FAILED"; const char Err_Coop[] = "SetCooperativeLevel FAILED"; const char Err_CreateClip[] = "CreateClip FAILED"; const char Err_CreatePrimSurf[] = "CreateSurface Primary FAILED"; const char Err_CreateBackSurf[] = "CreateSurface Back FAILED"; const char Err_LoadBMP[] = "Error Loading Image"; //------ Cleanup Function to Release Objects ------// #define SafeRelease(x) if (x) { x->Release(); x=NULL; } void Cleanup(void) { // release the interfaces SafeRelease(lpTitleScreen); SafeRelease(lpDDSPrimary); SafeRelease(lpClip); SafeRelease(lpDD); // display error if one thrown if (ErrStr) MessageBox(NULL, ErrStr, szCaption, MB_OK); } //------ Function to Load a Bitmap into a DirectDraw Surface ------// LPDIRECTDRAWSURFACE4 bitmap_surface(LPCTSTR file_name) { HDC hdc; HBITMAP bit; LPDIRECTDRAWSURFACE4 surf; // load the interface bitmap bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0, LR_DEFAULTSIZE|LR_LOADFROMFILE); if (!bit) // failed to load, return failure to caller return NULL; // get bitmap dimensions BITMAP bitmap; GetObject( bit, sizeof(BITMAP), &bitmap ); int surf_width=bitmap.bmWidth; int surf_height=bitmap.bmHeight; // create surface 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|DDSCAPS_SYSTEMMEMORY; ddsd.dwWidth = surf_width; ddsd.dwHeight = surf_height; // attempt to create surface ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL); // created ok? if (ddrval!=DD_OK) { // no, release the bitmap and return failure to caller DeleteObject(bit); return NULL; } else { // yes, get a DC for the surface surf->GetDC(&hdc); // generate a compatible DC HDC bit_dc=CreateCompatibleDC(hdc); // blit the interface to the surface SelectObject(bit_dc,bit); BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY); // release the DCs surf->ReleaseDC(hdc); DeleteDC(bit_dc); } // clear bitmap DeleteObject(bit); // return pointer to caller return surf; } //------ Draw the Bitmap to the Primary Surface ------// void DrawImage() { // return if not ready to draw at this time if (!lpTitleScreen||!bInit) return; // draw the image full screen lpDDSPrimary->Blt(NULL,lpTitleScreen,NULL,DDBLT_WAIT,NULL); } //------ Windows Message Handler ------// LRESULT CALLBACK WindowProc(HWND hWnd, unsigned uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_PAINT: PAINTSTRUCT ps; BeginPaint(hWnd, &ps); DrawImage(); EndPaint(hWnd, &ps); break; case WM_DESTROY: Cleanup(); PostQuitMessage(0); break; case WM_KEYDOWN: switch (wParam) { case VK_LEFT: // Process the LEFT ARROW key. case VK_RIGHT: // Process the RIGHT ARROW key. case VK_ESCAPE: // exit the program on escape DestroyWindow(hWnd); break; // Process other non-character keystrokes. default: break; } default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0L; } //------ Function to Initialize DirectDraw and the Application ------// static BOOL Init(HINSTANCE hInstance, int nCmdShow) { WNDCLASS wc; HRESULT hRet; DDSURFACEDESC2 ddsd; DDSCAPS2 ddscaps; LPDIRECTDRAW pDD; // Set up and register window class 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; } // Get dimensions of display int ScreenWidth = GetSystemMetrics(SM_CXSCREEN); int ScreenHeight = GetSystemMetrics(SM_CYSCREEN); // Create a window and display HWND hWnd; hWnd = CreateWindow(szClass, // class szCaption, // caption WS_VISIBLE|WS_POPUP, // style 0, // left 0, // top ScreenWidth, // width ScreenHeight, // height NULL, // parent window NULL, // menu hInstance, // instance NULL); // parms if (!hWnd) { ErrStr=Err_Create_Win; return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // Create the main DirectDraw object hRet = DirectDrawCreate(NULL, &pDD, NULL); if (hRet != DD_OK) { ErrStr=Err_DirectDrawCreate; return FALSE; } // Fetch DirectDraw4 interface hRet = pDD->QueryInterface(IID_IDirectDraw4, (LPVOID *) & lpDD); if (hRet != DD_OK) { ErrStr=Err_Query; return FALSE; } // Set our cooperative level hRet = lpDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL); if (hRet != DD_OK) { ErrStr=Err_Coop; return FALSE; } // create the clipper hRet=lpDD->CreateClipper(NULL,&lpClip,NULL); if (hRet != DD_OK) { ErrStr=Err_CreateClip; return FALSE; } // set the window for the clipper lpClip->SetHWnd(0,hWnd); // Create the primary surface 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_CreatePrimSurf; return FALSE; } // Grab the back buffer ddscaps.dwCaps=DDSCAPS_BACKBUFFER; hRet = lpDDSPrimary->GetAttachedSurface(&ddscaps,&lpDDSBack); if (hRet!=DD_OK) { ErrStr=Err_CreateBackSurf; return FALSE; } // Set the Clipper for the Primary Surface lpDDSPrimary->SetClipper(lpClip); // Set the display mode lpDD->SetDisplayMode(640,480,24,0,0); // flag initialization as completed bInit=TRUE; // load the default bitmap lpTitleScreen=bitmap_surface("TitleScreen.bmp"); if (!lpTitleScreen) { ErrStr=Err_LoadBMP; return FALSE; } // display the image DrawImage(); return TRUE; } //------ Application Loop ------// int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; // windows message structure // initialize the application, exit on failure if (!Init(hInstance, nCmdShow)) { Cleanup(); return FALSE; } // handle the message loop till we exit while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } // exit returning final message return (msg.wParam); } /code
Thanks
Advertisement
Hi... i think it will help if you post what exactly is the message error, so we can understand better since the code posted is a little bigger

To code is to make things come to life
To code is to make things come to life ;)
All that is happening is that there is an error creating my direct draw primary surface. There is no VC++ error. It is just not being made.
Thanks
Well check the error return value then !!!! That might give you a clue !

[edited by - granat on June 25, 2002 11:23:41 PM]
-------------Ban KalvinB !
I already answered your question in the game programming forum,
check it out

This topic is closed to new replies.

Advertisement