Archived

This topic is now archived and is closed to further replies.

DS

DirectX 7 DirectDraw newbie with Visual C++ 6 Standard.

Recommended Posts

I would like to thank all you considerate people out there who take time out of their day to read newbie posts. Thanks! Now to my problem. My program compiles fine, but when I run it, it gives me an access violation. The code to this program is below, and there''s a comment (//) where the little yellow debug arrow points. #include #include "ddraw.h" int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { HWND hWnd; hWnd = GetActiveWindow(); LPDIRECTDRAW lpDD; LPDIRECTDRAWSURFACE lpDDSPrimary; LPDIRECTDRAWSURFACE lpDDSBack; DDSURFACEDESC ddsd; DDSCAPS ddscaps; // Make DirectDraw Object //////////////////////////////////////// DirectDrawCreate( NULL, &lpDD, NULL ); // Set Cooperative Level ///////////////////////////////////////// lpDD->SetCooperativeLevel( hWnd, DDSCL_EXCLUSIVE / DDSCL_FULLSCREEN ); // Set Screen Mode /////////////////////////////////////////////// lpDD->SetDisplayMode( 640, 480, 8); // Make Primary Surface ////////////////////////////////////////// memset( &ddsd, 0, sizeof(ddsd) ); ddsd.dwSize = sizeof( ddsd ); ddsd.dwFlags = DDSD_CAPS/ DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE / DDSCAPS_FLIP / DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL ); // Make Back Buffer ////////////////////////////////////////////// ddscaps.dwCaps = DDSCAPS_BACKBUFFER; lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack ); // This is where it gives me an access violation error. (Little yellow debug arrow) => // Write to the back buffer ////////////////////////////////////// HDC hdc; if (lpDDSBack->GetDC(&hdc) == DD_OK) { SetBkColor(hdc, RGB(0, 0, 255)); SetTextColor(hdc, RGB(255, 255, 0)); TextOut(hdc, 0, 0, "DABSOFT", lstrlen("DABSOFT")); lpDDSBack->ReleaseDC(hdc); } lpDDSPrimary->Flip(NULL, 0); return 0; }

Share this post


Link to post
Share on other sites
Hi

The Problem is not in the Line where the Debug ointer points, the Problem is the CreateSurface Function, because when it does not succeed in creating an Surface it sets your Surface Pointer to NULL, and you can''t execute any Member Functions on Nothing
So you should get the Return Value of ALL your function calls, and Check what is wrong.

Lars

Share this post


Link to post
Share on other sites
Tormh... the vertical lines/pipes are OR, not AND

DS (catchy name)... you never actually clear the ddscaps structure before you use it for the back buffer do you? That tends to make things fall over.

But Lars has good advice (whether it is relevant in this case or not) - most DX crashes and errors are because a previous function returned a failure and yet the program carried on regardless. So checking all errors and reporting them to a logfile, messagebox, or whatever, is a good step. it will save you more time than it loses you, I''m sure. I''ve been there

Share this post


Link to post
Share on other sites