Jump to content
  • Advertisement

Archived

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

Fredric

Run time errors deluxe!

This topic is 6925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using the following code, I'm getting a run time error.. basically what happens is: when I run the program, an 'illegal operation' and closes the window. I clicked debug, and the problem pointed to GameMain(), where the primary surface locks... but, I dont' see anything wrong with it! If you could take a look at my code and point me to what I'm doing wrong, that'd be great! NOTE: All the windows crap has been left out so I could save some space, and not take up a lot more room than the DirectX code already is. int GameInit() { if(FAILED(DirectDrawCreate(NULL,&lpdd,NULL))) { return 0; } if(FAILED(lpdd->SetCooperativeLevel(hwnd, DDSCL_FULLSCREEN / DDSCL_ALLOWMODEX / DDSCL_EXCLUSIVE / DDSCL_ALLOWREBOOT))) { return 0; } if(FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP))) { return 0; } //clear the surface descriptor and state it's size memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; //create the primary surface if(FAILED(lpdd->CreateSurface(&ddsd, &lpddprimary, NULL))) { return 0; } //1) build up the palette //2) Set 0 to black //3)... and 255 to white for(int color = 0; color<255; color++) { palette[color].peRed = rand()%256; palette[color].peGreen = rand()%256; palette[color].peBlue = rand()%256; palette[color].peFlags = PC_NOCOLLAPSE; } palette[0].peRed = 0; palette[0].peGreen = 0; palette[0].peBlue = 0; palette[0].peFlags = PC_NOCOLLAPSE; palette[255].peRed = 255; palette[255].peGreen = 255; palette[255].peBlue = 255; palette[255].peFlags = PC_NOCOLLAPSE; //create the palette if(FAILED(lpdd->CreatePalette(DDPCAPS_8BIT / DDPCAPS_ALLOW256 / DDPCAPS_INITIALIZE, palette, &lpddpalette,NULL))) { return 0; } //attach the palette to the primary surface if(FAILED(lpddprimary->SetPalette(lpddpalette))) { return 0; } return 1; } // end GameInit() int GameShutdown() { if(lpdd!=NULL) lpdd->Release(); lpdd = NULL; if(lpddpalette!=NULL) lpddpalette->Release(); lpddpalette = NULL; if(lpddprimary!=NULL) lpddprimary->Release(); lpddprimary = NULL; return 1; } int GameMain() { if (KEYDOWN(VK_ESCAPE)) SendMessage(hwnd, WM_DESTROY, wParam, lParam); memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); if(FAILED(lpddprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR / DDLOCK_WAIT, NULL))) { return 0; } int mempitch = (int)ddsd.lPitch; UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface; for(int index=0; index<1000; index++) { UCHAR color = rand()%256; int x = rand()%SCREEN_WIDTH; int y = rand()%SCREEN_HEIGHT; video_buffer[x+y*mempitch] = color; } // unlock the surface if(FAILED(lpddprimary->Unlock(NULL))) { return 0; } Sleep(30); // 30 millisecond nap (hehe) return 1; } // end the game main PS- Remember that the OR's get converted into / because of the dumb boards... they should really find some sort of system that doesn't modify people's code! Edited by - Fredric on 5/7/00 7:09:57 PM

Share this post


Link to post
Share on other sites
Advertisement
Well, nothing''s obvious. Your first port of call should be to try commenting bits out, and running the program. That way, you get to see which part is crashing it. An alternative is to implement some sort of logging function: write to a file after every significant step, ensuring you flush the file each time, and when it crashes, you can read the log to see how far it got.

Share this post


Link to post
Share on other sites
quote:
Original post by Fredric
PS- Remember that the OR's get converted into / because of the dumb boards... they should really find some sort of system that doesn't modify people's code!


Let's see...

DWORD dwFlags = DXC_ONE/DXC_TWO/DXC_THREE;  


Nope, using the (code) tag doesn't work. Tsk.

TheTwistedOne
http://www.angrycake.com

Edited by - TheTwistedOne on May 8, 2000 7:07:20 AM

Share this post


Link to post
Share on other sites
I''m not THAT avanced! If I take out pieces of the code, then it will not work, correct? lol
Are there are any other solutions? Can any of you see what''s wrong?

GO LEAFS GO!

Share this post


Link to post
Share on other sites
quote:
Original post by Fredric
I'm not THAT avanced!


Fredric try when you write code to write some kind of error-function. Here is an example:


INT Error(INT nReturn, PTSTR pMsg)
{
// lpDD is the IDirectDraw7 interface.
// hWnd is you main-window handle

lpDD->FlipToGDISurface();
MessageBox(hWnd, pMsg, _T("Error in game!"), MB_OK);
return nReturn;
}


In your code, you can then check for errors like this:

if (lpddprimary == NULL)
return Error(1, _T("lpddprimary is NULL"));


This works cool for me, so try something like this in your code to test for errors.
(I have assumed you use directdraw in fullscreen, because in windowed-mode debugging is simpler).

Edited by - The big Question? on May 8, 2000 6:42:50 PM

Share this post


Link to post
Share on other sites
If you take a look at the game initialization, I have functions for error checking... but my problem is that there is an ILLEGAL OPERATION window that popups which is kind of weird, because it doesn''t necessarily mean any of my code didn''t work. Like... if I couldn''t, for example create the primary surface, the app would shut down! However, since the app sends an illegal operation windows, it''s very hard to debug..

GO LEAFS GO!

Share this post


Link to post
Share on other sites
First of all you need to remake your shutdown function, because you should release your interfaces in reverse order you created them:


int GameShutdown()
{
// Release palette first, because it was created last
if(lpddpalette != NULL)
lpddpalette->Release();
lpddpalette = NULL;


// Then the primary surface
if(lpddprimary != NULL)
lpddprimary->Release();
lpddprimary = NULL;


// At last the main ddraw interface:
if(lpdd != NULL)
lpdd->Release();
lpdd = NULL;
}


This is important, and could have caused the illigal operation.
Hope this helped you a bit.

Edited by - The big Question? on May 9, 2000 4:50:01 AM

Share this post


Link to post
Share on other sites
Some thoughts:

You never query for a DirectDraw interface before calling SetCooperativeLevel.

Write some debug text when a function call fails, like:

if(FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP)))
{
OutputDebugString("GameInit: Failed to set display mode\n");
return 0;
}


This way it is much easier to find out where the program crashes.

Check the value of lPitch by printing it to the debug window to make sure it is what you expect it to be.

Don''t know if this helps much...

*Sparkle*

Share this post


Link to post
Share on other sites
quote:
Original post by Fredric

I''m not THAT avanced! If I take out pieces of the code, then it will not work, correct? lol

This isn''t advanced. You just take a bit out which stands on its own, and if your code works without it, and not with it, then the error is in that bit. Simple.

For example, try commenting out the bit that sets the palette''s colours. Or try not actually constructing your palette at all. Or whatever. By a process of elimination, you can get to the exact line that causes the problem. You have to learn the art of debugging your programs

Share this post


Link to post
Share on other sites
Sparkle- you only need to query the interface when you''re using a different version LPDIRECTDRAW. You don''t have to do it all
Also, what does OutputDebugString("SetPalette: Failed to set display mode\n"); DO exactly? I added it to all the initialization code, but it didn''t do anything!

*ahh* the power of debugging! I''ll try doing what you suggested, thanks for the info!


GO LEAFS GO!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!