• Advertisement
Sign in to follow this  

(solved) 3d engine classes and the basics?

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

Hi, I'm working for a 3d engine (D3D) for quite a while and now rethinking some basics, like windows message handling (because of alt+tab causing troubles). What I'd like to know, is if my choices on the basic layout of my classes, is logical looking at windows programming. I've added some parts of my source to illustrate. I'm not 100% sure of the usage of my HWND variable is correct, same for 'hinstance' etc. Debugging shows that the HWND variable sometimes has a logical value, i.e.: (unused = 327680000) and sometimes (unused=????; CXX0030: error: expression cannot be evaluated). Might this have to do with my choices on the hwnd variable (in d3d class)? These basics might also affect basic message handling like WM_ACTIVEAPP etc. (Which is acting weird at the moment, I think). I also don't pass 'hwnd' yet as a pointer the DirectInput (initialization). Any help and hints are appreciated. [source lang="cpp"]/** SOURCE FILE D3D.H **/ class CD3d { public: HWND mHwnd; LPDIRECT3DDEVICE9 mD3ddev; // other vars deleted for forum/ prevent too much source bool InitWindowClass(WNDPROC pWindowProc, int *pNcmdShow, HINSTANCE *pHinstance, int pSw, int pSh, bool pWindowed); bool InitD3dDevice(int pAa); CD3d(); ~CD3d(); private: WNDCLASSEX mWc; LPDIRECT3D9 mD3d; D3DPRESENT_PARAMETERS mD3dpp; D3DDEVTYPE mDevType; D3DFORMAT mAdapterFormat; DWORD mVertexProcessing; D3DFORMAT mDepthStencilFormat; // other vars deleted for forum/ prevent too much source bool FindMaxDeviceSettings(); //D3DDEVTYPE *pDevType, D3DFORMAT *pAdapterFormat, D3DFORMAT *pDepthStencilFormat, DWORD *pVertexProcessing, bool pWindowed); bool ChooseMSAASettings(int pAa); }; /** SOURCE FILE D3D.CPP **/ CD3d::CD3d() { mHwnd = NULL; mD3ddev = NULL; mD3d = NULL; // other code removed for forum/ prevent too much source } bool CD3d::InitWindowClass(WNDPROC pWindowProc, int *pNcmdShow, HINSTANCE *pHinstance, int pSw, int pSh, bool pWindowed) { DWORD style; mScreenWidth = pSw; mScreenHeight = pSh; if(pWindowed) mWindowed = true; else mWindowed = false; ZeroMemory(&mWc, sizeof(WNDCLASSEX)); mWc.cbSize = sizeof(WNDCLASSEX); mWc.style = CS_HREDRAW | CS_VREDRAW; mWc.lpfnWndProc = (WNDPROC)pWindowProc; mWc.hInstance = *pHinstance; ShowCursor(FALSE); mWc.lpszClassName = L"WindowClass"; if(!mWindowed) { mWc.hbrBackground = (HBRUSH)COLOR_WINDOW; style = WS_EX_TOPMOST | WS_POPUP | WS_OVERLAPPEDWINDOW; } style = WS_OVERLAPPEDWINDOW; if(0 == (RegisterClassEx(&mWc))) return false; if(NULL == (mHwnd = CreateWindow(L"WindowClass", L"Crealysm", style, 0, 0, mScreenWidth, mScreenHeight, NULL, NULL, *pHinstance, NULL))) return false; ShowWindow(mHwnd, *pNcmdShow); return true; } bool CD3d::InitD3dDevice(int pAa) { // check capabilities and select adapter format settings if(!FindMaxDeviceSettings()) return false; // d3dpp present parameters setup ZeroMemory(&mD3dpp, sizeof(mD3dpp)); if(!ChooseMSAASettings(pAa)) return false; mD3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; mD3dpp.hDeviceWindow = mHwnd; mD3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; if(!mWindowed) { mD3dpp.Windowed = false; mD3dpp.BackBufferWidth = mScreenWidth; mD3dpp.BackBufferHeight = mScreenHeight; } else mD3dpp.Windowed = true; mD3dpp.BackBufferFormat = mAdapterFormat; mD3dpp.BackBufferCount = 1; mD3dpp.EnableAutoDepthStencil = TRUE; mD3dpp.AutoDepthStencilFormat = mDepthStencilFormat; HRESULT hr = mD3d->CreateDevice(D3DADAPTER_DEFAULT, mDevType, mHwnd, mVertexProcessing, &mD3dpp, &mD3ddev); if(hr != D3D_OK) return false; return true; } /** SOURCE FILE MAIN.H **/ LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); MSG msg; CD3d _d3d; // other code removed for forum/ prevent too much source bool Start(HINSTANCE *pInstance, int *pCmdShow, int pScreenWidth, int pScreenHeight, bool pWindowed, int pFsaa, char *pSceneFile); /** SOURCE FILE MAIN.CPP **/ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int width = 1600; int height = 900; int fsaa = NO_MSAA; bool windowed = true; char *scene1 = "data/scn4/test.scn"; if(!Start(&hInstance, &nCmdShow, width, height, windowed, fsaa, scene1)) return (INT)msg.wParam; _timer.Init(50.0f); // main loop PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE); while(msg.message != WM_QUIT) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { _timer.CalcFps(); SetWindowTextA(_d3d.mHwnd, _timer.mStrFramerate); // other code removed for forum/ prevent too much source // render 3d scene if(!_d3d.RenderFrame(&_d3dscene, &_d3dcam)) { MessageBox(_d3d.mHwnd, err_render, err_windowtext, MB_ICONERROR); return (INT)msg.wParam; } } } return (INT)msg.wParam; } bool Start(HINSTANCE *pInstance, int *pCmdShow, int pScreenWidth, int pScreenHeight, bool pWindowed, int pFsaa, char *pSceneFile) { if(!_d3d.InitWindowClass(WindowProc, pCmdShow, pInstance, pScreenWidth, pScreenHeight, pWindowed)) { MessageBox(NULL, err_wc, err_windowtext, MB_ICONERROR); return false; } if(!_d3d.InitD3dDevice(pFsaa)) { MessageBox(NULL, err_initd3d, err_windowtext, MB_ICONERROR); return false; } if(!_dinput.Init(*pInstance, _d3d.mHwnd, pScreenWidth, pScreenHeight)) { MessageBox(NULL, err_initdi, err_windowtext, MB_ICONERROR); return false; } return true; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { // case WM_ACTIVATE: // break; case WM_DESTROY: PostQuitMessage(WM_QUIT); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } [/source]

Edited by cozzie

Share this post


Link to post
Share on other sites
Advertisement
Addition;
going back in time/ documentation reminds me that handles like HWND and hInstance can be passed without threating them as a spointer with * / &, because they are already pointers and never changed during runtime. Is this correct? (and if so, is this the same for intcmdshow)

Share this post


Link to post
Share on other sites
I just pass: HWND, HINSTANCE and other pointer Win32 defines as they are defined.
Example:

void Foo( HWND hWnd, HINSTANCE hInst, int nCMDShow )
{
// do something here
}


Although i do try to use the "const" keyword wherever applicable.
so i would make "nCMDShow" "const int nCMDShow"

Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement