, Advanced 3D Game Programming with DirectX 9 

[ Pobierz całość w formacie PDF ]
.Exceptions get thrown sorarely that the added complexity that error codes add seems pretty unnecessary really.With exceptionhandling, the code is nice and clean.The error that almost all of the code in this book throws is calledcGameError, and is defined in Listing 1.2.Listing 1.2: The cGameError object and eResult enumerationclass cGameError{string m_errorText;public:cGameError( char* errorText ){DP1("***\n*** [ERROR] cGameError thrown! text: [%s]\n***\n",errorText );m_errorText = string( errorText );}const char* GetText(){return m_errorText.c_str();}};38 enum eResult{resAllGood = 0, // function passed with flying colorsresFalse = 1, // function worked and returns 'false'resFailed = -1, // function failed miserablyresNotImpl = -2, // function has not been implementedresForceDWord = 0x7FFFFFFF};The window abstraction, cWindow, is fairly straightforward.MyRegister- Class is replaced withcWindow::RegisterClass, MyInitInstance is now cWindow::InitInstance, and WndProc is now a staticfunction cWindow::WndProc.The function is static because non-static class functions have a hiddenfirst variable passed in (the this pointer) that is not compatible with the WndProc function declaration.Later on I'll define a child class for you that allows the creation of full-screen ready windows.In practice,this is the same as a normal window; the only change is that WS_POPUP is used as the window styleinstead of WS_OVERLAPPED-WINDOW.The message pump that you'll come to know and love (although probably hate at the start!) isencapsulated in two functions.HasMessages() checks the queue and sees if there are any messageswaiting to be processed, returning true if there are any.Pump() processes a single message, sending itoff to WndProc using TranslateMessage/DispatchMessage.When Pump receives the WM_QUITmessage, which again is a notification from Windows that the application should exit, it returns resFalse.Special care needs to be taken to handle thrown exceptions that happen during the window procedure.You see, between the execution of DispatchMessage and WndProc, the call stack meanders into somekernel DLL functions.If a thrown exception flies into them, bad stuff happens (anything from yourprogram crashing to your machine crashing).To handle this, any and all exceptions are caught in theWndProc and saved in a temporary variable.When Pump finishes pumping a message, it checks thetemporary variable to see if an error was thrown.If there is an error waiting, Pump rethrows the errorand it rises up to WinMain.class cWindow{protected:int m_width, m_height;39 HWND m_hWnd;std::string m_name;bool m_bActive;static cWindow* m_pGlobalWindow;public:cWindow(int width,int height,const char* name = "Default window name" );~cWindow();virtual LRESULT WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam );virtual void RegisterClass( WNDCLASSEX* pWc = NULL );virtual void InitInstance();HWND GetHWnd();bool IsActive();bool HasMessages();eResult Pump();static cWindow* GetMainWindow();};inline cWindow* MainWindow();m_width,Width and height of the client rectangle of the window.This is different fromm_heightthe width and height of the actual window.40 m_hWndHandle to the window.Use the public function GetHWnd to get access to itoutside the class.m_nameThe name of the window used to construct the window class and window.m_bActiveBoolean value; TRUE if the window is active (a window is active if it iscurrently in the foreground).m_pGlobalWindowStatic variable that points to the single instantiation of a cWindow class foran application.Initially set to NULL.cWindow(& ) Constructs a window object.You can only create one instance of thisobject; this is verified by setting the m_pGlobalWindow object.~cWindow()The destructor destroys the window and sets the global window variable toNULL so that it cannot be accessed any longer.WndProc()Window procedure for the class.Called by a hidden function insideWindow.cpp.RegisterClass(& ) Virtual function that registers the window class.This function can beoverloaded in child classes to add functionality, such as a menu or differentWndProc.InitInstance()Virtual function that creates the window.This function can be overloaded inchild classes to add functionality, such as changing the window style.GetHWnd()Returns the window handle for this window.IsActive()Returns true if the application is active and in the foreground.41 HasMessages()True if the window has any messages in its message queue waiting to beprocessed.Uses PeekMessage with PM_NOREMOVE.Pump()Pumps the first message off the queue and dispatches it to the WndProc.Returns resAllGood, unless the message gotten off the queue wasWM_QUIT, in which case it returns resFalse.GetMainWindow()Public function; used by the global function MainWindow to gain access tothe only window object.MainWindow()Global function that returns the single instance of the cWindow class for thisprogram.Any piece of code can use this to query information about thewindow.For example, any code can get the hWnd for the window by callingMainWindow()->GetHWnd().Finally, there is the Big Kahuna cApplication.Child classes will generally only reimplement SceneInitand DoFrame [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • anikol.xlx.pl