Archived

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

DDraw app won't quit!!!

This topic is 6355 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

My ddraw app won''t quit after I hit esc to quit. It quits to desktop but when I hit ctrl+alt+del it still sitting there. Please help me, the code below is my whole cpp program not the header. THX! // INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN // just say no to MFC #define INITGUID #include // include important windows stuff #include #include #include // include important C/C++ stuff #include #include #include #include #include #include #include #include #include #include #include // include directdraw #include "VinEngine.h" // DEFINES //////////////////////////////////////////////// // defines for windows #define WINDOW_CLASS_NAME "WINCLASS1" // TYPES ////////////////////////////////////////////////////// // MACROS ///////////////////////////////////////////////// #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // GLOBALS //////////////////////////////////////////////// HWND main_window_handle = NULL; // globally track main window HINSTANCE hinstance_app = NULL; // globally track hinstance LPDIRECTDRAWSURFACE4 character; // FUNCTIONS ////////////////////////////////////////////// LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { // this is the main message handler of the system PAINTSTRUCT ps; // used in WM_PAINT HDC hdc; // handle to a device context // what is the message switch(msg) { case WM_CREATE: { // do initialization stuff here // return success return(0); } break; case WM_PAINT: { // simply validate the window hdc = BeginPaint(hwnd,&ps); // end painting EndPaint(hwnd,&ps); // return success return(0); } break; case WM_DESTROY: { // kill the application, this sends a WM_QUIT message PostQuitMessage(0); // return success return(0); } break; default:break; } // end switch // process any messages that we didn''t take care of return (DefWindowProc(hwnd, msg, wparam, lparam)); } // end WinProc //////////////////////////////////////////////////////////// int Game_Main(void *parms = NULL, int num_parms = 0) { // test if user is hitting ESC and send WM_CLOSE if (KEYDOWN(VK_ESCAPE)) SendMessage(main_window_handle,WM_CLOSE,0,0); FillSurface(backsurf,_RGB16BIT565(0,255,0)); FlipSurfaces(); return(1); } // end Game_Main //////////////////////////////////////////////////////////// int Game_Init(void *parms = NULL, int num_parms = 0) { InitDirectDraw(800,600,16); CreateOffscreenSurface(character,32,32,DDSCAPS_VIDEOMEMORY); return(1); } // end Game_Init ///////////////////////////////////////////////////////////// int Game_Shutdown(void *parms = NULL, int num_parms = 0) { // release the secondary surface if (backsurf) backsurf->Release(); // release the primary surface if (primsurf) primsurf->Release(); // finally, the main dd object if (ddraw4obj) ddraw4obj->Release(); // return success return(1); } // end Game_Shutdown // WINMAIN //////////////////////////////////////////////// int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASSEX winclass; // this will hold the class we create HWND hwnd; // generic window handle MSG msg; // generic message HDC hdc; // graphics device context // first fill in the window class stucture winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // save hinstance in global hinstance_app = hinstance; // register the window class if (!RegisterClassEx(&winclass)) return(0); // create the window if (!(hwnd = CreateWindowEx(NULL, // extended style WINDOW_CLASS_NAME, // class "VinEngine TEST!", // title WS_POPUP | WS_VISIBLE, 0,0, // initial x,y Screen_Width,Screen_Height, // initial width, height NULL, // handle to parent NULL, // handle to menu hinstance,// instance of this application NULL))) // extra creation parms return(0); // save main window handle main_window_handle = hwnd; // initialize game here Game_Init(); // enter main event loop while(TRUE) { // test if there is a message in queue, if so get it if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // test if this is a quit if (msg.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&msg); // send the message to the window proc DispatchMessage(&msg); } // end if // main game processing goes here Game_Main(); } // end while // closedown game here Game_Shutdown(); // return to Windows like this return(msg.wParam); } // end WinMain ///////////////////////////////////////////////////////////

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Just a friendly tip that has nothing to do with your actual question;

When posting code you can write as follows:

[ source ]
// Some useful code like:
void foo()
{
// does really do that much
};
[ / source ]

and it will look as follows:

    
// Some useful code like:

void foo()
{
// does really do that much <img src="smile.gif" width=15 height=15 align=middle>

};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Oh, dont write the spaces between the "[" and "source" etc...
I had to write the spaces there, or else it would have created a source-box,...

Any ways.... Makes the code a whole lot more readable.....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi....
I''m not too certain, but perhaps if you called ''DestroyWindow'' at the quit message, it may work....

[source]
if (msg.message == WM_QUIT)
DestroyWindow(hwnd);
break;
[source]



One other thing I noticed is that you send a WM_CLOSE message, but you don''t process that. was that done intentionally?

Hope that helps...

Mike

Share this post


Link to post
Share on other sites
I''ve had this same problem many times, even had it last night, but im not exactly sure how i fixed it (dont have my code here)

Try called PostQuitMessage(0) from WM_CLOSE instead of WM_DESTROY and let me know if that helps.

If not I''ll look it up when i get home today.



ByteMe95::~ByteMe95()

Share this post


Link to post
Share on other sites
One thing that could cause this is that you don''t realease all DDraw objects or release them in the wrong order.

So always make sure you realease all DIrectX interfaces and that you release them in the reverse order you create them.

Share this post


Link to post
Share on other sites
Hey

Well one thing you could do is this Using directinput:

    

if(FAILED(d_lpkb->GetDeviceState(sizeof(buffer),(LPVOID)&buffer))) {

}

if(KEYDOWN(buffer, DIK_ESCAPE)) {

PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;

}



Put this in your game loop and make sure you started up direct input.



James,
Later



Share this post


Link to post
Share on other sites
One reason might be the following:

If your MessageLoop receives the WM_QUIT message, it immediately exits the messageloop, so the window procedure will never receive the WM_QUIT message, and therefore never stop.

If this is the case, TranslateMessage() and DispatchMessage() should be called before exiting the MessageLoop. Like this:

    

MSG msg;
BOOL finished=false;

while(!finished)
{
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
finished=true;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DoSomething();
}



I''m not sure this is causing your neverending loop, but it sounds quite reasonable, I think. ;-)

Greetings, CondorWitte.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
CondorWitte:
No, that should not be necessary, the WndProc doesn''t run in it''s own loop, it runs in the message loop (through DispatchMessage), so when the message loops stops the WndProc "stops" too.

Very strange error, my message loops usually look exactly (well, no important differences at least) the same as yours.
Perhaps you should call DestroyWindow instead of sending a WM_CLOSE message when the escape key is pressed.

Previous Anon: The WM_QUIT message is posted in response to a WM_DESTROY message, so calling DestroyWindow in response to the WM_QUIT message doesn''t do much sense...

Share this post


Link to post
Share on other sites
All you need to change is in Game_Main where it says if (KEYDOWN(VK_ESCAPE). Instead of using SendMessage(), do something like this:
    
if (KEYDOWN(VK_ESCAPE))
{
PostQuitMessage(0);
return;
}


Then it''ll jump out of the while(TRUE) loop, call Game_Shutdown(), and kill the window. That''s the only thing that looks different from my code. Peace Out.

Share this post


Link to post
Share on other sites