views:

51

answers:

2

I am confused. Sometimes when I initiate my window, the Handle returned is good. and sometimes its not. Here is my code

void GXRenderManager::InitWindows()
{
    WNDCLASSEX wndcex;

    wndcex.cbSize   = sizeof(WNDCLASSEX);
    wndcex.style    = CS_HREDRAW | CS_VREDRAW;
    wndcex.lpfnWndProc = GXRenderManager::WndProc;
    wndcex.cbClsExtra = 0;
    wndcex.cbWndExtra = 0;
    wndcex.hInstance = *GXRenderManager::hinstance;
    wndcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wndcex.hCursor  =   LoadCursor(NULL,IDC_ARROW);
    wndcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wndcex.lpszMenuName = NULL;
    wndcex.lpszClassName = L"GXRenderClass";
    wndcex.hIconSm  =   LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wndcex))
        throw GXWindowsException(L"Failed To Register Window");

    //EDIT AREA: This is a static size for window. Needs to be changed for dynamic size
    RECT rectangle = {0,0,GXRenderManager::width,GXRenderManager::height};
    AdjustWindowRect(&rectangle, WS_OVERLAPPEDWINDOW, FALSE);

    HWND tempWin;

    tempWin = CreateWindowEx(WS_EX_CLIENTEDGE,L"GXRenderClass",L"GXRender Engine",
        WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, 
        (rectangle.right - rectangle.left),
        (rectangle.bottom - rectangle.top), NULL, NULL,*GXRenderManager::hinstance, NULL);

    if(!tempWin)
        GXWindowsException(L"GX had an issue creating a window.");

    GXRenderManager::mainWindow = &tempWin;

    ShowWindow(*GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);

}

Sometimes GXRenderManager::mainWindow returns a number, but most of the time it returns a "expression can not be evaluated. Any takers ??

[edit]

the header

#ifndef GXRM
#define GXRM
#include <windows.h>
#include "DeviceEnum.h"
#include "GXRenderDevice.h"
#include "GXExceptions.h"

class GXRenderManager {

public:
    static int Ignite(HINSTANCE*, int*, GXDEVICE , int w = 800, int h = 600);
    static GXRenderer* Device();
    static void InitWindows();
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

    static int Run();

private:
    bool running;
    static GXRenderer *renderDevice;

protected:
    static HINSTANCE * hinstance;
    static int *nCmdShow;
    static HWND mainWindow;
    static int width;
    static int height;

};

#endif

.cpp

GXRenderManager::mainWindow is a static member . Prior to the responses below. I updated the code to the following...

void GXRenderManager::InitWindows()
{
    WNDCLASSEX wndcex;

    wndcex.cbSize   = sizeof(WNDCLASSEX);
    wndcex.style    = CS_HREDRAW | CS_VREDRAW;
    wndcex.lpfnWndProc = GXRenderManager::WndProc;
    wndcex.cbClsExtra = 0;
    wndcex.cbWndExtra = 0;
    wndcex.hInstance = *GXRenderManager::hinstance;
    wndcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wndcex.hCursor  =   LoadCursor(NULL,IDC_ARROW);
    wndcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wndcex.lpszMenuName = NULL;
    wndcex.lpszClassName = L"GXRenderClass";
    wndcex.hIconSm  =   LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wndcex))
        throw GXWindowsException(L"Failed To Register Window");

    //EDIT AREA: This is a static size for window. Needs to be changed for dynamic size
    RECT rectangle = {0,0,GXRenderManager::width,GXRenderManager::height};
    AdjustWindowRect(&rectangle, WS_OVERLAPPEDWINDOW, FALSE);

    HWND tempWin;

    tempWin = CreateWindowEx(WS_EX_CLIENTEDGE,L"GXRenderClass",L"GXRender Engine",
        WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, 
        (rectangle.right - rectangle.left),
        (rectangle.bottom - rectangle.top), NULL, NULL,*GXRenderManager::hinstance, NULL);

    if(!tempWin)
        GXWindowsException(L"GX had an issue creating a window.");

    GXRenderManager::mainWindow = tempWin;

    ShowWindow(GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);

}

Still I see the same issue.

+1  A: 

You are saving a pointer to a local variable (tempWin) and expecting that will be valid after you return from the InitWindows() function. Instead of making GXRenderManager::mainWindow a pointer, declare it as an actual data member. So:

class GXRenderManager {
    ...
    HWND mainWindow; // not a pointer
    ...
};

GXRenderManager::mainWindow = tempWin;

ShowWindow(GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);

I suspect you might have a similar problem with nCmdShow, but you haven't shown enough of your code to tell.

Greg Hewgill
You are absolutely right.
numerical25
I am not having that issue with nCmdShow. nCmdShow points directly to the argument that is passed in from the entry point
numerical25
Ok. I'm still not sure why it needs to be a pointer at all. The same applies to `hinstance`.
Greg Hewgill
A: 
thatsalok
your right but I am still having the same issue. I am updating the code
numerical25