Advertisement

windows handle...

Started by April 08, 2002 05:59 PM
8 comments, last by Magriep 22 years, 5 months ago
The window handle for directX is in a header file and I was wondering how I might return the values in the main cpp file. If any source is needed, I can post the source to the file.
What's a "window handle for directX"? Sure it needs a HWND, but there's no window handle for it.

[edited by - IndirectX on April 8, 2002 7:47:24 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
To initialize d3d you need a windows handle, which is what I was referring to.
Magriep, I got your code from yesterday to compile and run without error. If you''re still having troubles with it, I would suggest adding a method to your g_D3DEngine class that receives a HWND and assigns it to the hWnd member of your class.

From your question here, I would suggest looking into using the "extern" keyword. That should enable referencing variables across ''tranlational units'' (ie. the source files that go into an obj file).
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
ok, I have figured out my current problem, but I now get another error: CEngine.cpp(73) : error C2106: ''='' : left operand must be l-value (code: g_CenusEngine->GetHandle() = InitWindow(hInstance, nCmdShow). The object for me now is to convert an example into a class and in seperate files. Here''s the current source:

  //CEngine.h// include files ////////////////////////////////////////////////////////////#include <string.h>  // needed for strcpy()#include <windows.h> // needed for windows #include <d3d8.h>    // needed for direct 3dclass CEngine {private:	char *m_strAppName;	LPDIRECT3D8       m_pD3D;       // Used to create the D3DDevice	LPDIRECT3DDEVICE8 m_pd3dDevice; // Our rendering device	HWND              m_hWnd;       // our window handlepublic:	CEngine();	~CEngine();	HWND GetHandle();	char* GetAppName();	RenderBeg();	RenderEnd();	InitD3D();	};CEngine::CEngine() {	// member variables /////////////////////////////////////////////////////////	m_strAppName = "Cenus Engine build 1"; 	m_pD3D       = NULL; // Used to create the D3DDevice	m_pd3dDevice = NULL; // Our rendering device	m_hWnd = NULL;       // our window handle}CEngine::~CEngine() {	// first, release any devices we''ve created.  if(m_pd3dDevice != NULL) {    m_pd3dDevice->Release();    m_pd3dDevice = NULL; // just to be safe  }  // next, release the direct3d interface.  if(m_pD3D != NULL) {    m_pD3D->Release();    m_pD3D = NULL; // just to be safe  }}HWND CEngine::GetHandle() {	return m_hWnd;}char *CEngine::GetAppName() {  		return m_strAppName;}CEngine::RenderBeg() {	m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );    // Begin the scene    m_pd3dDevice->BeginScene();    // Rendering of scene objects can happen after here}CEngine::RenderEnd() {	// End the scene    m_pd3dDevice->EndScene();    // Present the backbuffer contents to the display    m_pd3dDevice->Present(NULL, NULL, NULL, NULL );}CEngine::InitD3D() {	// create a Direct3D device  m_pD3D = Direct3DCreate8( D3D_SDK_VERSION );  if(m_pD3D == NULL) {    throw("Direct3DCreate8 failed.");  }  // Get the current desktop display mode  D3DDISPLAYMODE d3ddm;  if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))) {    throw("GetAdapterDisplayMode failed.");  }    D3DPRESENT_PARAMETERS d3dpp;   ZeroMemory( &d3dpp, sizeof(d3dpp) ); // this will set all members to zero    // setting zero for width and height will tell windows to use window  // dimensions for backbuffer dimensions.  d3dpp.BackBufferWidth = 0;  d3dpp.BackBufferHeight = 0;  // request a back buffer format that matches the current desktop display   // format.  d3dpp.BackBufferFormat = d3ddm.Format;  // we want 1 back buffer  d3dpp.BackBufferCount = 1;  // no multisampling  d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;  // "discard" swap effect.  This means that we should never care about  // the contents of the front buffer - we should always re-render the  // entire screen on the back buffer each frame.  This is the most  // efficient method of presenting the back buffer.  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;  // use our window as the device window  d3dpp.hDeviceWindow = m_hWnd;  // we want to do D3D in a window  d3dpp.Windowed = TRUE;   // we don''t want Direct3D to manage depth buffers for us  // (that''s because we don''t use any right now)  d3dpp.EnableAutoDepthStencil = false;  d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;  // no special flags  d3dpp.Flags = 0;  // default refresh rate  d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;  // default presentation interval (we must use this because we''re in  // a window)  d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;    // Create the Direct3D device.   if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,                                  D3DDEVTYPE_HAL, m_hWnd,                                 D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                 &d3dpp, &m_pd3dDevice)))  {    throw("CreateDevice failed.");  }}// CEngine.h#include "CEngine.h"CEngine* g_CenusEngine;LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter){  switch(uMsg) {    case WM_PAINT:    {      g_CenusEngine->RenderBeg();    }    // we processed the message, so return 0.    return(0);  case WM_DESTROY: // our window is being destroyed!  quit!    g_CenusEngine->RenderEnd();	g_CenusEngine->~CEngine();	PostQuitMessage(0);    return(0);  }  // all the other messages, we don''t care about, so we let DefWindowProc  // handle them.  return(DefWindowProc(hwnd, uMsg, wParam, lParam));}HWND InitWindow(HINSTANCE g_hInstance, int nCmdShow){  HWND                hwnd;  WNDCLASSEX          wc;    // set up and register window class  memset(&wc, 0, sizeof(wc));  wc.cbSize = sizeof(wc);  wc.style = CS_HREDRAW | CS_VREDRAW;  wc.lpfnWndProc = WindowProc; // change this to NULL and crash!  wc.cbClsExtra = 0;  wc.cbWndExtra = 0;  wc.hInstance = g_hInstance;  wc.hIcon = NULL;  wc.hIconSm = NULL;  wc.hCursor = LoadCursor(NULL, IDC_ARROW);  wc.hbrBackground = NULL;   wc.lpszMenuName = NULL;  wc.lpszClassName = "CenusEngineWindow";  RegisterClassEx(&wc);    // create a window that''s 640 pixels wide, 480 tall.  hwnd = CreateWindowEx(0, "CenusEngineWindow", g_CenusEngine->GetAppName(),	    WS_OVERLAPPEDWINDOW,       10, 10, 640, 480, NULL, NULL, g_hInstance, NULL);    if (!hwnd) {    ::MessageBox(NULL, "CreateWindow failed!", g_CenusEngine->GetAppName(), MB_ICONSTOP);  }    ShowWindow(hwnd, nCmdShow);  return(hwnd);} int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow){  // create a window 	g_CenusEngine->GetHandle() = InitWindow(hInstance, nCmdShow);  // this is a C++ try-catch block.  It works very simply.  Inside the try,  // there are throw() functions.  These "throw" errors, which are caught  // by the "catch" statement at the end of the try block.    // (for more information, see the appendix.)  try {    g_CenusEngine->InitD3D();  } catch(const char *errmsg) {    // if we threw any const char *''s, catch them here, display the error,    // then exit.    MessageBox(g_CenusEngine->GetHandle(), errmsg, g_CenusEngine->GetAppName(), 0);  }  catch(...) {    // catch unhandled exceptions.    MessageBox(g_CenusEngine->GetHandle(), "An unknown error occurred.", g_CenusEngine->GetAppName(), 0);  }  // note that we clean all this stuff up when our window is destroyed,  // aka when we get a WM_DESTROY message.  // message pump  while (1) {    MSG msg;    if (PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))    {      int result = GetMessage(&msg, 0, 0, 0);      if (!result) return(msg.wParam);      TranslateMessage(&msg);       DispatchMessage(&msg);    }  }  // we''ll never get here, but the compiler will complain if we don''t  // have this line.  return 0;}  
Greets Magriep!

That error message indicates that the operand on the left side of the assignment operator isn''t appropriate for receiving a value.

// create a window
g_CenusEngine->GetHandle() = InitWindow(hInstance, nCmdShow);

This code calls a function on both sides of the assignment. The code evaluates from right to left, so that InitWindow returns before ->GetHandle() does and so the compiler doesn''t know where to store the value returned by InitWindow - there''s no "place" for it - so to speak.

Try assigning the HWND using a method like this

void CEngine::SetHandle(HWND hwnd)
{
m_hWnd = hwnd;
}

And then call it like this:

g_CenusEngine->SetHandle(InitWindow(hInstance, nCmdShow));


Take a look at some of the examples here: Windows API Tutorials to get a better feel for using C++ with the basic win32 api. That site is one of the best non MFC windows tutorial sites on the www. There are about a dozen tutorials there, the tutorials are fairly straight forward and not to difficult to get your head around. And what they lay down there, even though they don''t get to Direct X until the last tut, will jive with what you''ve got so far.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
It compiles now, but now I get that same illegal operation as before. Could it be my compiler or my code? I''m using VC++ 6, with DX 8.1
Hmm... nothing else really jumps out at me. Have you tried stepping through the code with the debugger to see where the problem is?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
How would I do that? I only fix the errors that occur while compiling.
Since I don''t use VC++ I can''t say for certain. The system I use comes with a debugger built into the IDE, I''m surprised VC++ doesn''t come with one. WinDbg and more can be found here: Microsoft Debugging Tools. And you might want to post another thread asking for pointers on how to use it.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement