Can't get window to show

This is a discussion on Can't get window to show within the Game Programming forums, part of the General Programming Boards category; So I've looked through everything and with a tutorial for DirectX9 I'm doing, and I for the life of me ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Can't get window to show

    So I've looked through everything and with a tutorial for DirectX9 I'm doing, and I for the life of me cannot find out what's wrong with the code here:
    Code:
    /************************************/
    /*            CRYSTAL BOND          */
    /*************            ***********/
    /*       DirectX game built by      */
    /**     Glitch Softwares Studios   **/
    /************************************/
    
    //Include important files
    #include <windows.h>
    #include <windowsx.h>
    #include <d3d9.h>
    
    #include "functions.h"
    
    //Include Direct3D Library
    #pragma comment (lib, "d3d9.lib")
    
    //Include files specifically for this game
    #include "functions.h"
    
    //Prototype of CALLBACK
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    // the entry point for any Windows program
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
    {
    	HWND hWnd;
    
    	WNDCLASSEX wc;
    		//Clear windows for use
    	ZeroMemory(&wc, sizeof(WNDCLASSEX));
    
    	//Build structure with information
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
        wc.lpszClassName = L"WindowClass";
    
    	//Make the Windows Program
    	hWnd = CreateWindowEx(NULL,
    						L"WindowClass",				//Class name
    						L"Crystal Bond",			//Window Caption
    						WS_OVERLAPPEDWINDOW,		//Style
    						5,							//X Start
    						5,							//Y Start
    						645,						//Width
    						485,						//Height
    						NULL,					//Parent Window
    						NULL,					//Menus
    						hInstance,				//Application handle
    						NULL);					//Use with multiple windows
    
    	//Make window visible
    	ShowWindow(hWnd, nCmdShow);
    
    	initD3D(hWnd);
    
    	MSG resp;	//Create the structure to communicate the user input
    
    	while(GetMessage(&resp,NULL,0,0))
    	{
    		TranslateMessage(&resp);	//Translate input to machine's language
    
    		DispatchMessage(&resp);		//Send message to be processed.
    	}
    	return resp.wParam;
    
    	while(true)
    	{
    		// Check to see if any messages are waiting in the queue
    		while(PeekMessage(&resp, NULL, 0, 0, PM_REMOVE))
    		{
    			// Translate the message and dispatch it to WindowProc()
    			TranslateMessage(&resp);
    			DispatchMessage(&resp);
    		}
    
    		// If the message is WM_QUIT, exit the while loop
    		if(resp.message == WM_QUIT)
    			break;
    		renderFrame();
    	}
    	cleanD3D();
    }
    
    LRESULT CALLBACK WindowProc(HWND hWnd,
    							UINT message,
    							WPARAM wParam,
    							LPARAM lParam)
    {
    	//Look for what to do at message recieved
    	switch(message)
    	{
    		//Message says: Close the program and window
    	case WM_DESTROY:
    		{PostQuitMessage(0);return 0;break;}
    	}
    	//Else, and nothing was supported in SWITCH
    	return DefWindowProc(hWnd, message, wParam, lParam);
    }
    It's not the functions and the lack of certain pointers and things, as their defined in the other headers in "functions.h", I just KNOW it's in main.cpp here, it's GOTTA be.
    So this code is compiled under Visual C++ 2008 Express Edition, and it compiles and works fine, but for some reason it doesn't show the window, but the program is running. However I've checked the line saying:
    Code:
    	//Make window visible
    	ShowWindow(hWnd, nCmdShow);
    And yet it still won't work, I'm pretty sure I did everything right, if anyone wants the other header files:

    "functions.h"
    Code:
    #ifndef _FUNCTIONS_H_
    #define _FUNCTIONS_H_
    
    //Global declarations
    LPDIRECT3D9 d3d;
    LPDIRECT3DDEVICE9 d3ddev;
    
    
    #include "prototypes.h"
    
    #include "dxfunctions.h"
    
    #endif
    "prototypes.h"
    Code:
    #ifndef _PROTOTYPES_H_
    #define _PROTOTYPES_H_
    
    void initD3D(HWND hWnd);
    // initilize DirectX
    
    void renderFrame(void);
    // renders one (1) frame
    
    void cleanD3D(void);
    // when the game ends, this will be called to clear everything
    
    #endif
    "dxfunctions.h"
    Code:
    #ifndef _DXFUNCTIONS_H_
    #define _DXFUNCTIONS_H_
    
    //                             Initilize Direct3D
    
    void initD3D(HWND hWnd)
    {
    	d3d=Direct3DCreate9(D3D_SDK_VERSION);		//Create the D3D interface
    
    	D3DPRESENT_PARAMETERS d3dpp;	//Structure for holding device information
    
    	ZeroMemory(&d3dpp,sizeof(d3dpp));	//Erase STRUCT for usability
    
    	d3dpp.Windowed = true;		// NOT Fullscreen
    	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	//Erase old frames
    	d3dpp.hDeviceWindow = hWnd;		//Makes the window compatible with D3D
    
    	//Make the Direct3D class, using structure info above
    	d3d->CreateDevice(D3DADAPTER_DEFAULT,
    						D3DDEVTYPE_HAL,
    						hWnd,
    						D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    						&d3dpp,
    						&d3ddev);
    }
    
    
    //                             Make new frame
    
    void renderFrame(void)
    {
    	//Clear screen to color BLUE
    	d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,40,100),1.0f,0);
    
    	d3ddev->BeginScene();	//Begin the 3D image render
    
    	//Render here
    
    	d3ddev->EndScene();		//End the 3D image render
    
    	//Display the rendered frame
    	d3ddev->Present(NULL,NULL,NULL,NULL);
    }
    
    
    //                             Clear the DirectX engine and COM
    
    void cleanD3D(void)
    {
    	d3ddev->Release();
    	d3d->Release();
    }
    
    
    #endif
    Thanks for the help, I suspect this was to go in either Windows Programming or Game Programming in DirectX, but I wasn't sure, so move if appropriate.

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Hey there, what errors do you get when you compile the program? The first one I spotted is that you haven't included prototype.h in dxfunctions.h. It's also frowned upon to define your functions in header files, as you have done in dxfunctions.h, unless things have changed while I've been away

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You haven't really said what's wrong except it doesn't work. Is it compiling, is the window showing and DX not drawing, is the window showing and not being repainted, is the application failing to start?

    First thing, you usually call UpdateWindow() after a ShowWindow to force a repaint. Also you are blocked in the GetMessage() loop until WM_QUIT is encountered, only then will your DX loop start, again until a WM_QUIT.

    First thing I'd work on is getting your window to show. To do that check the return paramaters of your calls and make sure they are succeeding, or step through it with a debugger. If you are stumped on something then post that problem. After that I'd make the change to the message loop that I mentioned above, and start working on getting the DX portion working. Your first matter on that would be ensuring the Device is created successfully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  2. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 08:23 AM
  3. Window won't show
    By Toraton in forum Windows Programming
    Replies: 4
    Last Post: 11-10-2002, 08:07 PM
  4. run a program
    By goodmonkie in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2001, 12:19 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 05:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21