Jump to content
  • Advertisement

Archived

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

SoGreen

Lets Play Help The Newbie

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

Ok, heres my problem. I''m trying to get a basic DirectDraw program running. I''ve got my code done(will follow), now I''m basing(?) my code on Andre LaMothe''s book Tricks of the windows game programming gurus. I ran the demo program (demo6_1 for you people who have the CD) and compared his code to mine, I''ve gone over this at least 10 times over the past two days and I can''t find the problem!!! My program compiles fine, no errors or warnings but when i exit it leaves "DirectDraw" in memory(after exiting the program press ctrl+alt+del and you''ll see what i mean. Any and all help would be greatly appreciated. Sorry for the bad grammar and things but im REALLY frustrated. Thanx. // My first attempt at writing a basic DirectDraw program // // Stamp out evil MFC ////////////////////////////////// // #define WIN32_LEAN_AND_MEAN // INCLUDES //////////////////////////////////////////// // #include // include all the windows headers #include // include useful macros #include #include #include // standard i/o functions #include // not sure obviously math // DEFINES ///////////////////////////////////////////// // #define WINDOW_CLASS_NAME "WINCLASS1" // GLOBAL VARIABLES //////////////////////////////////// // LPDIRECTDRAW lpdd = NULL; // Pointer to the direct draw 1 interface LPDIRECTDRAW lpdd4 = NULL; // Pointer to the direct draw 4 interface HWND global_hwnd = NULL; // Holds our hwnd for global use HINSTANCE global_hinstance = NULL; // Holds the hinstance for global use // FUNCTIONS /////////////////////////////////////////// // /////////////////// WindowProc //////////////////////// ///////////////////// GameInit ///////////////////////// //////// Initialize DirectDraw and other stuff ///////// // int GameInit(void *params = NULL, int num_params = 0) { if(FAILED(DirectDrawCreate(NULL, &lpdd, NULL))) // Create a direct draw interface { // Error stuff return(0); // Return success } // End if if(FAILED(lpdd->QueryInterface(IID_IDirectDraw4, (LPVOID *)&lpdd4))) // Query for a DirectDraw4 interface { // Error stuff return(0); // Return success } // End if lpdd4->SetCooperativeLevel(global_hwnd, DDSCL_NORMAL); // Set the cooperative level lpdd->Release(); lpdd = NULL; return(0); } // End GameInit ////////////////// GameShutdown //////////////////////// //////////// Release all Direct draw stuff ///////////// // int GameShutdown(void *params = NULL, int num_params = 0) { if(lpdd4) { lpdd4->Release(); lpdd4 = NULL; } return(0); } ///// Basic windows message handling function////////// // LRESULT CALLBACK WindowProc(HWND hwnd, // Handle to the window UINT msg, // Holds messages WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; // Used in WM_PAINT messages HDC hdc; // Handle to a device context // What is the message? switch(msg) { case WM_CREATE: // Message sent when a window is to be created { // Do any initialization stuff here return(0); // Return success }break; case WM_PAINT: // Message sent when a window is to be repainted { hdc = BeginPaint(hwnd, &ps); // Validate the window // Do paint stuff here EndPaint(hwnd, &ps); return(0); // Return success }break; case WM_DESTROY: // Message sent when a window is to be destroyed { PostQuitMessage(0); // Kill the app -- This sends a WM_QUIT message return(0); // Return success }break; default: break; } // End switch return(DefWindowProc(hwnd, msg, wparam, lparam)); // Take care of any other messages using // the default handler DefWindowProc } // End WindowProc ////////////////// WinMain ////////////////////// // Main function -- used to create your window // // int WINAPI WinMain(HINSTANCE hinstance, // Handle an instance HINSTANCE hprevinstance, // Handle to the previous instance LPSTR lpcmdline, // Pointer to the command lne int ncmdline) // Hold command line stuff { WNDCLASSEX winclass; // This will hold the window class we create HWND hwnd; // A handle to a window MSG msg; // Holds windows mesages // Fill in our windows class//////////////// winclass.cbSize = sizeof(WNDCLASSEX); // Holds the size of the class winclass.style = CS_DBLCLKS / CS_OWNDC / CS_HREDRAW / CS_VREDRAW; // Flags that will define our window winclass.lpfnWndProc = WindowProc; // Points to our message handler winclass.cbClsExtra = 0; // Windows extra stuff ??? winclass.cbWndExtra = 0; // Windows extra stuff ??? winclass.hInstance = hinstance; // Handle to an instance winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Application icon winclass.hCursor = LoadCursor(NULL, IDI_APPLICATION); // Application cursor winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // Brush to paint window with winclass.lpszMenuName = NULL; // Menu to attach to window winclass.lpszClassName = WINDOW_CLASS_NAME; // Name of your windows class winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // Icon for minimized application // Register the window class if(!RegisterClassEx(&winclass)) return(0); // Create the window using the registered class if(!(hwnd = CreateWindowEx(NULL, // Extended style flags WINDOW_CLASS_NAME, // Class name "Direct Draw Init Demo", // Title of your window WS_OVERLAPPEDWINDOW / WS_VISIBLE, // Style flags 0,0, // Top left corner starting coords 400,300, // Size of window NULL, // Handle to the parent NULL, // Handle to a menu hinstance, // Handle to an instance NULL))) // ???????? return(0); GameInit(); ////////////// Main Event Loop ////////////////// /////// Where all the action takes place /////// // while(TRUE) { if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) // Test if there are any messages { if(msg.message == WM_QUIT) // Test for WM_QUIT and kill if found break; TranslateMessage(&msg); // Translate any accelerator keys DispatchMessage(&msg); // Sends message to WindowProc for processing } // End if // Game_main will go here } // End while GameShutdown(); return(msg.wParam); // Return to windows with this ??? } // End WinMain

Share this post


Link to post
Share on other sites
Advertisement
Well, as I sat around and waited for a response i found my error...
In my main event loop I was passing hwnd to PeekMessage when i should have been passing NULL.
Ain''t I a fool.
If anyone can explain WHY this happend it''d be much appreciated (what does that variable do???)

Share this post


Link to post
Share on other sites
Hello!

It''s really late over here, so I''ve got no time to study your code (bed''s waiting).

I did notice a small thing, though:

In your main game loop you should use an if-else statement, like this:

if(PeekMessage(...)){
//ladidadida
}
else{
//Game processing goes here.
}

If you do not do this, your game will only be able to process one message for each game update. That will get you in serious trouble if your game is producing more than one message per update, which it may very well do (you never know when windows decides to throw a bunch of messages at you).

It''s no problem in this example, since you don''t have any game processing code, but soon enough you will...

*Sparkle*

Share this post


Link to post
Share on other sites
hwnd is the window handle... It''s the ID of an app to the OS, so it knows what to pass to that window and how...

--SR

----------------------------
"To err is human... To really foul things up requires a computer.."
----------------------------

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!