[Solved] Win32 / DirectX wrapper issues

Started by
3 comments, last by Sol462 17 years, 9 months ago
Everything compiles fine, yet nothing shows when I try to run it. I read in an article about a Win32 wrapper that crutial messages can arrive in the wrong order, which is what I believe is happening. What I am trying to do is keep the code that I currently have as intact as possible. Everything worked before when I kept the window and message handling code seperate from the Spazuh class. What I think is wrong is my render() loop which calls the PeekMessage function; it may be out of place. Has anyone else faced this type of problem before? I have looked at how a window is set up in the Irrlicht engine and the process looks similar. spazuh.h(class declaration):

#include <d3d9.h>
#include <d3dx9.h>

#ifndef SPAZUH_H
#define SPAZUH_H

class Spazuh
{
public:
	int init(int width, int height, bool fullscreen);
	void render(void);
	void exit(void);

private:
   HWND hWnd;
   WNDCLASSEX			   wc;
   HINSTANCE			   hInstance;
   LPDIRECT3D9             Direct3D_object;    
   LPDIRECT3DDEVICE9       Direct3D_device;   
   D3DPRESENT_PARAMETERS   present_parameters; 
   D3DXMATRIX              projection_matrix;  
};

#endif



spazuh.cpp(functions, Win32/DX init, etc):

#include "spazuh.h"

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

//
// Initialization
//
int Spazuh::init(int width, int height, bool fullscreen)
{
    wc.cbSize         = sizeof(WNDCLASSEX) ;
    wc.style        = CS_HREDRAW | CS_VREDRAW ;
    wc.lpfnWndProc        = MsgProc ;
    wc.cbClsExtra        = 0 ;
    wc.cbWndExtra        = 0 ;
    wc.hInstance        = hInstance ;
    wc.hIcon        = 0 ;
    wc.hCursor        = 0 ;
    wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1) ;
    wc.lpszMenuName    = 0 ;
    wc.lpszClassName    = "Spazuh";
    wc.hIconSm        = 0 ;
	RegisterClassEx( &wc );

    // Create the application's window
    hWnd = CreateWindow( "Spazuh", "Spazuh", 
                         WS_OVERLAPPEDWINDOW, 0, 0, 800, 600,
                         GetDesktopWindow(), NULL, wc.hInstance, NULL );
    // Show the window
    ShowWindow( hWnd, SW_SHOWDEFAULT );
    UpdateWindow( hWnd );
	
	Direct3D_object = Direct3DCreate9(D3D_SDK_VERSION);
	if( Direct3D_object == NULL )
	{
		MessageBox(hWnd,"Could not create Direct3D Object","D3D_OBJ ERR",MB_OK);
		return 0;
	}
	ZeroMemory(&present_parameters, sizeof(present_parameters));
	present_parameters.Windowed = !fullscreen;
	present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
	present_parameters.EnableAutoDepthStencil = true;
	present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
	present_parameters.hDeviceWindow = hWnd;
	present_parameters.BackBufferWidth = width;
	present_parameters.BackBufferHeight = height;
	present_parameters.BackBufferFormat = D3DFMT_R5G6B5;
	present_parameters.MultiSampleType = D3DMULTISAMPLE_NONE;

	if( FAILED(Direct3D_object->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
           hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
           &present_parameters,&Direct3D_device)))
	{
		MessageBox(hWnd,"Could not create Direct3D Device","D3D_DEV ERR",MB_OK);
		return 0;
	}
}

//
// Render loop
//
void Spazuh::render()
{
    // Enter the message loop
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
        if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		else {
			Direct3D_device->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                             D3DCOLOR_XRGB(0,0,255),1.0f,0);
			Direct3D_device->BeginScene();

			Direct3D_device->EndScene();

			Direct3D_device->Present(NULL,NULL,NULL,NULL);
		}
	}
}

//
// Clean-up
//
void Spazuh::exit()
{
    UnregisterClass( "Spazuh", wc.hInstance );
    //TODO: finish
}

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}



Note that MsgProc is not of the Spazuh class. All the necessary structures (WNDCLASSEX, PRESENT_PARAMETERS, etc) are filled here. main.cpp:

#include "spazuh.h"

int WINAPI WinMain( HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                    int       nCmdShow )
{
	Spazuh *spazuh;
	spazuh->init(800, 600, false);

	while(1)
	{
		spazuh->render();
	}
    return 0;

}



Sorry for the long post and thanks in advance. [Edited by - Sol462 on July 20, 2006 1:05:16 PM]
____________________________________Spazuh- Because I've had too much coffee
Advertisement
Is there anything in the debug output?
http://www.gamedev.net/community/forums/showfaq.asp?forum_id=10#q61

Are you just getting a black screen? You're not drawing anything so all you should see is a blue background (from your clear).
Stay Casual,KenDrunken Hyena
You need to create a Spazuh object ...

Spazuh* spazuh = new Spazuh;
lol you should also get rid of the while part in WinMain. your code literaly brough my PC to a screeching halt ha ha ha.

while(1) // not needed{ spazuh->render();}


Ah, I initialized the Spazuh object incorrectly. And for kicks I kept the while loop in to see how it ran. Very slow indeed! (spazuh.exe even lingers after closing).
Works correctly now, thanks guys.
____________________________________Spazuh- Because I've had too much coffee

This topic is closed to new replies.

Advertisement