Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Forward Windows messages to a derived class's virtual CallWindowProc() method

$
0
0
Basically my question is about what is going on here -> https://github.com/jjuiddong/Introduction-to-3D-Game-Programming-With-DirectX11/blob/master/Common/d3dApp.cpp He create a D3DApp framework from which other classes have to inherit in order to get a Window, a Renderer and a GameTimer. Now as you know Windows CallWindowProc() function require that CALLBACK macro in order for it to work, so since the MsgProc() in the code above is a virtual method inside the class, the author of the code use a trick to forward windows messages into it from outside the class #include "d3dApp.h" #include <WindowsX.h> #include <sstream> namespace { // This is just used to forward Windows messages from a global window // procedure to our member function window procedure because we cannot // assign a member function to WNDCLASS::lpfnWndProc. D3DApp* gd3dApp = 0; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { // Forward hwnd on because we can get messages (e.g., WM_CREATE) // before CreateWindow returns, and thus before mhMainWnd is valid. return gd3dApp->MsgProc(hwnd, msg, wParam, lParam); } D3DApp::D3DApp(HINSTANCE hInstance) { // Get a pointer to the application object so we can forward // Windows messages to the object's window procedure through // the global window procedure. gd3dApp = this; } Ok, I kind of get what's going on, my question is, if I inherit from D3DApp my derived class is going to call D3DApp constructor, in which case, what will be the value of "this"? The derived class adress? (that would allow for re-defining the virtual member function from the derived class and that's how I wish it is) Also, I don't quite get the need of wrapping the D3DApp* into an empty namespace, is this another trick? What it is achieving? Furthermore, what would happen if I where to do something strange and derive two different classes from D3DApp? What I mean is, is the gd3dApp pointer a global variable or each derived class get it's own? Doesn't seems the case since is not even part of the class... Much confusion

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>