Advertisement

Errors setting up Direct Draw

Started by February 12, 2000 05:59 PM
5 comments, last by Ratman 25 years ago
Im writing a direct draw wrapper, and well, already running into problems What I wanted to do was make a DDraw_Manager class, and use that to handle basically everything except drawing text and bitmaps. I want to be able to initlize everything in the constructor, and release everything with the deconstructor. Basically, I wanted it to be as simple as possible. I'll put my code at the bottom, but heres whats going wrong; First I can initilize everything, set the screen resolution and everything. But then when I go to release it (when I call ->Release on ANYTHING) I get a "this program has performed an illegal operation". Next, I tried to simply lock and unlock the primary surface, but again, no luck. The program just quits whenever I call either function. I fooled around with the debugger, and it said "access violation" and gave me a hexnumber. I dont know what it could be, my only guess is Im creating everything wrong, or what Im trying to do cant be done. Any help would be greatly appreciated, as Ive been trying to fix this for a few days and am getting no where. Thanks, Dave heres some snippets of the code: //how I create my DDrawing manager CDDraw_Manager* DDraw_Manager; DDraw_Manager = new CDDraw_Manager(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP); //in the main loop DDraw_Manager->PrepFrame(); // this is where it exits DDraw_Manager->ReleaseFrame(); //and delete it like this: delete DDraw_Manager; //this crashed the program if I make it this far by not calling ->PrepFrame and ->ReleaseFrame // dir_draw_manager.cpp CDDraw_Manager :: CDDraw_Manager(int width, int height, int bpp) { //set all these things to null, just to be safe LPDIRECTDRAW lpdd = NULL; // dd object LPDIRECTDRAW4 lpdd4 = NULL; // dd4 object LPDIRECTDRAWSURFACE4 lpddsprimary = NULL; // dd primary surface LPDIRECTDRAWSURFACE4 lpddsback = NULL; // dd back surface LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette LPDIRECTDRAWCLIPPER lpddclipper = NULL; // dd clipper // first create base IDirectDraw interface if (FAILED(DirectDrawCreate(NULL, &lpdd, NULL))) { // error } // end if // now query for IDirectDraw4 if (FAILED(lpdd->QueryInterface(IID_IDirectDraw4, (LPVOID *)&lpdd4))) { // error } // end if //We should be able to release lpdd now // set cooperation to full screen if (FAILED(lpdd4->SetCooperativeLevel(main_window_handle, DDSCL_FULLSCREEN / DDSCL_ALLOWMODEX / DDSCL_EXCLUSIVE / DDSCL_ALLOWREBOOT))) { // error } // end if // set display mode to parm if (FAILED(lpdd4->SetDisplayMode(width, height, bpp,0,0))) { // error } // end if // clear ddsd and set size memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); // enable valid fields ddsd.dwFlags = DDSD_CAPS; // request primary surface ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; // create the primary surface if (FAILED(lpdd4->CreateSurface(&ddsd, &lpddsprimary, NULL))) { // error } // end if ///////////////////////////////////////////////////// } CDDraw_Manager :: ~CDDraw_Manager() { // this is used after the game to clean up everything. // now the primary surface if (lpddsprimary) { lpddsprimary->Release(); lpddsprimary = NULL; } // end if // now release the IDirectDraw4 interface if (lpdd4) { lpdd4->Release(); lpdd4 = NULL; } // end if } ////////////////////////////////////// CDDraw_Manager:repFrame() { ////////////////////////////////////////////////////////////////////////////////////// // Purpose: This locks the frame. This should be called before drawing anything. // // // // // ////////////////////////////////////////////////////////////////////////////////////// // this function locks the priamary surface // and updates the global variables primary_buffer, and primary_lpitch DDRAW_INIT_STRUCT(ddsd); // lock the primary surface if(lpddsprimary) { lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR / DDLOCK_WAIT, NULL); } } Edited by - ratman on 2/12/00 6:02:59 PM
Are you checking and responding to the error codes Direct Draw returns?


if (FAILED(DirectDrawCreate(NULL, &lpdd, NULL)))
{
// error <- What is here?

} // end if


If DirectDrawCreate fails lpdd will still == NULL and you''ll get an access violation when you try to use it.
- Ryan -
Advertisement
Your code *seems* fine, but what you''re describing sounds like you''re trying to call DirectDraw with Null pointers. Particularly the Release crashes, that''s nowhere near normal. I presume there''s actually error-quitting code on those if(FAILED()) calls?
I like how your class''s PrepFrame function turned out on the post
In your constructor, you''ve got the following
declarations:

LPDIRECTDRAW lpdd = NULL; // dd object
LPDIRECTDRAW4 lpdd4 = NULL; // dd4 object
LPDIRECTDRAWSURFACE4 lpddsprimary = NULL; // dd primary surface
LPDIRECTDRAWSURFACE4 lpddsback = NULL; // dd back surface
LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette
LPDIRECTDRAWCLIPPER lpddclipper = NULL; // dd clipper

You are redeclaring these variables as local and so
any values assigned to them are thrown away when this
function ends. The values in your class aren''t adjusted.
And so when you call any of your other functions, it''s
using random garbage which is why you''re getting weird
errors.

I have error checking, and the same thing is happening

I agree, it seems like Im trying to do things to NULLs, but I dont see why the error checking isnt getting it...

dave
Advertisement
thanks LordFoul, that did it. Cant believe I missed that.. I feel foolish I guess thats what you get with so much lack of sleep

This topic is closed to new replies.

Advertisement