Archived

This topic is now archived and is closed to further replies.

For people looking for a working example of a simple engine...

This topic is 4960 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 am currently working on a simple engine im calling the Pure Framework. It is currently very small, so nice and simple. I hope for those of you out there struggling with the concept of how an engine may work this will be of some use. It is basically 6 classes: 1 - Application Kernel - Inherits from the engine Kernel class (below). 2 - Engine Kernel - Handles general window and Direct 3D initiation and calls the Engine Task Manager class (below). 3 - Engine Task Manager - Controls switching between game/application states, like setup/play/pause etc. Within the states are function calls, calling the appropriate setup and play/render functions of the other various input and output type classes, like keyboard and mouse input. 4 - Engine Video - This class has no contents for now, but it is called by the taskmanager for the setup and rendering of your D3D scene. 5 - Engine Input - Used to setup and poll the input devices. 6 - Engine Camara - A primitve FPS capable camera system. Contains simple functions like enabling remote setting of the camera position. What this engine may help with: 1 - Inheritance 2 - Overriding base class functions, and usage of the "virtual" keyword. 3 - Simple initiation of Direct 3D 4 - Simple initiation of Direct Input 5 - Finite State architecture (task manager) Creating The Project 1. Create emtpy Win32 App. 2. make sure these libraries are included in the linker: dxguid.lib dinput8.lib d3dx9dt.lib d3d9.lib winmm.lib 3. Using the menu Insert->New Class, create 5 classes with the names: Ceng_Kernel Ceng_Camera Ceng_Video Ceng_TaskManager Ceng_Input 4. You should now have 10 files added to your project, the headers and sources of the classes. Below i have included the exact source code for each of the classes, in the scroll boxes. Simply completely replace the code in the appropriate files with the code below. eng_Camera.h
// eng_Camera.h: interface for the Ceng_Camera class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_ENG_CAMERA_H__3B554FD0_D77A_4DF8_8142_6F33A1880391__INCLUDED_)
#define AFX_ENG_CAMERA_H__3B554FD0_D77A_4DF8_8142_6F33A1880391__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <d3dx9.h>
#include <stdio.h>

class Ceng_Camera  
{
public:
	Ceng_Camera();
	virtual ~Ceng_Camera();


	// Function to create the camera

	HRESULT Create(LPDIRECT3DDEVICE9);
	void Destroy();
	HRESULT UpdateDevice();
	void SetPosition(D3DXVECTOR3 *inPos);
	void MoveForward(float Dist);
	void MoveRight(float Dist);
	void MoveUp(float Dist);
	void MoveInDirection(float Dist, D3DXVECTOR3 *Dir);
	void RotateDown(float Ang);
	void RotateRight(float Ang);
	void Roll(float Ang);

    D3DXVECTOR3			Position;
    D3DXVECTOR3			Facing;
	LPDIRECT3DDEVICE9	l_pD3DDevice;
	
private:
	bool				MovedSinceLastUpdate;
    D3DXVECTOR3			Up;
    D3DXVECTOR3			Right;

    float				RotAboutUp;
    float				RotAboutRight;


    D3DXMATRIX matView;
};

#endif // !defined(AFX_ENG_CAMERA_H__3B554FD0_D77A_4DF8_8142_6F33A1880391__INCLUDED_)

eng_Kernel.h
   // s_cRBW.h: interface for the Cs_cRBW class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_ENG_KERNEL_H__9E8163F7_69CE_410A_86D6_83EC9E4DE6F7__INCLUDED_)
#define AFX_ENG_KERNEL_H__9E8163F7_69CE_410A_86D6_83EC9E4DE6F7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <stdio.h>
#include <d3dx9.h>

#include "eng_TaskManager.h"

class Ceng_Kernel  
{
// Present Params

	public:
		struct PPSETTINGS
		{
			D3DFORMAT			l_AutoDepthStencilFormat;
			UINT				l_BackBufferCount;
			D3DFORMAT			l_BackBufferFormat;
			UINT				l_BackBufferHeight;
			UINT				l_BackBufferWidth;
			BOOL				l_EnableAutoDepthStencil;
			DWORD				l_Flags;
			UINT				l_FullScreen_RefreshRateInHz;
			D3DMULTISAMPLE_TYPE l_MultiSampleType;
			DWORD				l_MultiSampleQuality;
			UINT				l_PresentationInterval;
			D3DSWAPEFFECT		l_SwapEffect;
			BOOL				l_Windowed;
		};

		struct INITWINSETS
		{
			int	TopLeftX;
			int TopLeftY;
			int Width;
			int Height;
			D3DXCOLOR			l_BBColour;
		};

		virtual	HRESULT StandardSetup(HINSTANCE hInst, HINSTANCE , LPCTSTR, INT);
		virtual INT WINAPI WinMainBase( HINSTANCE l_hInstance, HINSTANCE , LPSTR, INT);

	protected:
		D3DPRESENT_PARAMETERS	d3dpp; 
		HRESULT					hr;
		HWND					l_hWnd;
		LPDIRECT3D9             l_pD3D; 
		LPDIRECT3DDEVICE9       l_pD3DDevice; 
		PPSETTINGS				l_PresentParams;
		INITWINSETS				l_InitWindowSets;
		HINSTANCE				l_hInstance;

		// objects... (after class so object can be created)



		Ceng_Kernel();
		virtual ~Ceng_Kernel();

		virtual HRESULT InitApp();
		virtual HRESULT InitD3D();
		virtual HRESULT Cleanup();
		virtual HRESULT D3DStates();
		virtual HRESULT Frame();
		virtual HRESULT MoveFrame();
		virtual HRESULT RenderFrame();
	public:
		// Public Helper Functions

		HRESULT				ConfirmDeviceNotNull();			
		HRESULT				ConfirmInterfacePtrNotNull();
		HRESULT				SetWindowDims(int inX, int inY, int inW, int inH);
		HRESULT				SetBackColour(float inR, float inG, float inB); 
		PPSETTINGS*			ReturnParamsPointer()		{return &l_PresentParams;}
		LPDIRECT3DDEVICE9*	ReturnDevicePointer()		{return &l_pD3DDevice;}
		HWND				ReturnWindowHandle()		{return l_hWnd;}
};

#endif // !defined(AFX_ENG_KERNEL_H__9E8163F7_69CE_410A_86D6_83EC9E4DE6F7__INCLUDED_)

eng_Input.h
   // eng_Input.h: interface for the eng_Input class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_ENG_INPUT_H__BC0FDB03_3CBB_4974_91B0_542FDFE41E3B__INCLUDED_)
#define AFX_ENG_INPUT_H__BC0FDB03_3CBB_4974_91B0_542FDFE41E3B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <d3dx9.h>
#include <dinput.h>
#include <stdio.h>


class eng_Input  
{
public:
	eng_Input();
	virtual ~eng_Input();

	#define KEYDOWN(name, key) (name[key] & 0x80) 
	#define MOUSEBUTTONDOWN(key) (key & 0x80)
	#define MOUSEBUTTON_LEFT 0
	#define MOUSEBUTTON_RIGHT 1
	#define MOUSEBUTTON_MIDDLE 2

	float	ChangeX,ChangeY,ChangeZ;

	HRESULT hr;

	LPDIRECTINPUT8			g_pDI;
	LPDIRECTINPUTDEVICE8	g_pDIKeyBoard;
	LPDIRECTINPUTDEVICE8	g_pDIMouse;
	HINSTANCE				l_hInstance;
	HWND					l_hWnd;

	LPDIDEVCAPS				l_KeyCaps;
	LPDIDEVCAPS				l_MouseCaps; // only really concerned with this one


	virtual HRESULT SetupMouse();
	virtual HRESULT SetupKeyboard();

	virtual HRESULT UseMouse();
	virtual HRESULT UseKeyboard();

	LPDIRECTINPUT8*			ReturnIInterfacePtr();
	LPDIRECTINPUTDEVICE8*	ReturnMouseDevicePtr();
	LPDIRECTINPUTDEVICE8*	ReturnKeyboardDevicePtr();
};

#endif // !defined(AFX_ENG_INPUT_H__BC0FDB03_3CBB_4974_91B0_542FDFE41E3B__INCLUDED_)

eng_Video.h
   // Ceng_Video.h: interface for the eng_Video class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_CENG_VIDEO_H__76AFEB96_2912_4C67_A01B_F68281B932A1__INCLUDED_)
#define AFX_CENG_VIDEO_H__76AFEB96_2912_4C67_A01B_F68281B932A1__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <stdio.h>
#include <d3dx9.h>

class Ceng_Video  
{
public:
	Ceng_Video();
	virtual ~Ceng_Video();

	LPDIRECT3DDEVICE9 l_pD3DDevice;
	virtual HRESULT VideoSetup();
	virtual HRESULT VideoUpdate();
};

#endif // !defined(AFX_CENG_VIDEO_H__76AFEB96_2912_4C67_A01B_F68281B932A1__INCLUDED_)

eng_TaskManager.h
   // eng_TaskManager.h: interface for the Ceng_TaskManager class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_ENG_TASKMANAGER_H__058675D9_40FE_47D4_83ED_9D4EAA4EB356__INCLUDED_)
#define AFX_ENG_TASKMANAGER_H__058675D9_40FE_47D4_83ED_9D4EAA4EB356__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <stdio.h>
#include <d3dx9.h>

#include "eng_Input.h"
#include "eng_Camera.h"

class Ceng_TaskManager  
{
public:
	Ceng_TaskManager();
	virtual ~Ceng_TaskManager();

	// 3 default states for any app

	#define RUNNING		0x01
	#define SETUP		0x02
	#define PAUSED		0x03
	// and a state tracker

	int CURRENTSTATE;

	// DI class object

	eng_Input thisInput;
	Ceng_Camera				thisCamera;


	HRESULT					hr;
	HWND					l_hWnd;
	HINSTANCE				l_hInstance;
	LPDIRECT3DDEVICE9		l_pD3DDevice;

	virtual HRESULT Manager();

	virtual HRESULT SetupVideo();
	virtual HRESULT SetupInput();
	virtual HRESULT SetupClock();
	virtual HRESULT SetupSound();
	
	virtual HRESULT UpdateVideo(int State);
	virtual HRESULT UpdateInput(int State);
	virtual HRESULT UpdateClock(int State);
	virtual HRESULT UpdateSound(int State);

	virtual HRESULT SpreadVariables();
};

#endif // !defined(AFX_ENG_TASKMANAGER_H__058675D9_40FE_47D4_83ED_9D4EAA4EB356__INCLUDED_)

eng_Kernel.cpp
   // s_cRBW.cpp: implementation of the Cs_cRBW class.

//

//////////////////////////////////////////////////////////////////////


#include "eng_Kernel.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


static Ceng_Kernel* g_pMyApp = NULL;



Ceng_TaskManager		thisTaskManager;


Ceng_Kernel::
Ceng_Kernel()
{
	l_PresentParams.l_AutoDepthStencilFormat		=	D3DFMT_D16;
	l_PresentParams.l_BackBufferCount				=	NULL;
    l_PresentParams.l_BackBufferFormat				=	D3DFMT_UNKNOWN;
	l_PresentParams.l_BackBufferHeight				=	NULL;
	l_PresentParams.l_BackBufferWidth				=	NULL;
	l_PresentParams.l_EnableAutoDepthStencil		=	TRUE;
	l_PresentParams.l_FullScreen_RefreshRateInHz	=	NULL;
	l_PresentParams.l_MultiSampleQuality			=	NULL;
	l_PresentParams.l_MultiSampleType				=	D3DMULTISAMPLE_NONE ;
	l_PresentParams.l_PresentationInterval			=	NULL;
    l_PresentParams.l_SwapEffect					=	D3DSWAPEFFECT_DISCARD;
    l_PresentParams.l_Windowed						=	true;

	l_InitWindowSets.TopLeftX	 =	100;
	l_InitWindowSets.TopLeftY	 =	100;
	l_InitWindowSets.Width		 =	300;
	l_InitWindowSets.Height		 =	300;

	l_InitWindowSets.l_BBColour.r = 189.0f;
	l_InitWindowSets.l_BBColour.g = 175.0f;
	l_InitWindowSets.l_BBColour.b = 2.0f;


	Ceng_TaskManager		thisTaskManager;


}

Ceng_Kernel::
~Ceng_Kernel()
{

}

HRESULT Ceng_Kernel::
InitApp()
{
	thisTaskManager.l_hInstance		=	l_hInstance;
	thisTaskManager.l_hWnd			=	l_hWnd;
	thisTaskManager.l_pD3DDevice	=	l_pD3DDevice;
	return S_OK;
}

HRESULT	Ceng_Kernel::SetBackColour(float inR, float inG, float inB)
{
	l_InitWindowSets.l_BBColour.r = inR;
	l_InitWindowSets.l_BBColour.g = inG;
	l_InitWindowSets.l_BBColour.b = inB;
	return S_OK;
}

HRESULT Ceng_Kernel::
SetWindowDims(int inX, int inY, int inW, int inH)
{
	l_InitWindowSets.TopLeftX	= inX;
	l_InitWindowSets.TopLeftY	= inY;
	l_InitWindowSets.Width		= inW;
	l_InitWindowSets.Height		= inH;
	return S_OK;
}

HRESULT Ceng_Kernel::
ConfirmDeviceNotNull()
{
	if (l_pD3DDevice != NULL)
	{
		return S_OK;
	}
	else
	{
		MessageBox(l_hWnd, "No Device Active!", "Device Error!", MB_OK);
		return E_FAIL;
	}
}
HRESULT Ceng_Kernel::
D3DStates()
{
	//DestroyWindow(l_hWnd);

	return S_OK;
}

HRESULT Ceng_Kernel::
InitD3D()
{
    // Create the D3D object, which is needed to create the D3DDevice.

    l_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
	if ( l_pD3D == NULL)
	{
		MessageBox(l_hWnd, "Direct3DCreate9 Failure!", "Direct3D Initiation Error", MB_OK);
		return E_FAIL;
	}

    // Set up the structure used to create the D3DDevice. Most parameters are

    // zeroed out. We set Windowed to TRUE, since we want to do D3D in a

    // window, and then set the SwapEffect to "discard", which is the most

    // efficient method of presenting the back buffer to the display.  And 

    // we request a back buffer format that matches the current desktop display 

    // format.

    ZeroMemory( &d3dpp, sizeof(d3dpp) );
	d3dpp.AutoDepthStencilFormat		=	l_PresentParams.l_AutoDepthStencilFormat;
	d3dpp.BackBufferCount				=	l_PresentParams.l_BackBufferCount;
	d3dpp.BackBufferFormat				=	l_PresentParams.l_BackBufferFormat;
	d3dpp.BackBufferHeight				=	l_PresentParams.l_BackBufferHeight;
	d3dpp.BackBufferWidth				=	l_PresentParams.l_BackBufferWidth;
	d3dpp.EnableAutoDepthStencil		=	l_PresentParams.l_EnableAutoDepthStencil;
	d3dpp.FullScreen_RefreshRateInHz	=	l_PresentParams.l_FullScreen_RefreshRateInHz;
	d3dpp.MultiSampleQuality			=	l_PresentParams.l_MultiSampleQuality;
	d3dpp.MultiSampleType				=	l_PresentParams.l_MultiSampleType;
	d3dpp.PresentationInterval			=	l_PresentParams.l_PresentationInterval;
    d3dpp.SwapEffect					=	l_PresentParams.l_SwapEffect;
    d3dpp.Windowed						=	l_PresentParams.l_Windowed;

	// Create the Direct3D device. Here we are using the default adapter (most

    // systems only have one, unless they have multiple graphics hardware cards

    // installed) and requesting the HAL (which is saying we want the hardware

    // device rather than a software one). Software vertex processing is 

    // specified since we know it will work on all cards. On cards that support 

    // hardware vertex processing, though, we would see a big performance gain 

    // by specifying hardware vertex processing.

    hr = l_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, l_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &l_pD3DDevice );
	if ( l_pD3DDevice == NULL)
	{
		switch ( hr)
		{
			case D3DERR_INVALIDCALL:
				{
					MessageBox(l_hWnd, "CreateDevice Failure, D3DERR_INVALIDCALL!", "Direct3D Initiation Error", MB_OK);
					break;
				}
			case D3DERR_NOTAVAILABLE:
				{
					MessageBox(l_hWnd, "CreateDevice Failure, D3DERR_NOTAVAILABLE!", "Direct3D Initiation Error", MB_OK);
					break;
				}
			case D3DERR_OUTOFVIDEOMEMORY:
				{
					MessageBox(l_hWnd, "CreateDevice Failure, D3DERR_OUTOFVIDEOMEMORY!", "Direct3D Initiation Error", MB_OK);
					break;
				}
		}
		return E_FAIL;
	}

	// Call states function

	hr = D3DStates();
	if (hr == E_FAIL)
		MessageBox(l_hWnd, "Function RenderStates() Failed","Function Failure!", MB_OK);

    return S_OK;
}

HRESULT Ceng_Kernel::
RenderFrame()
{
	return S_OK;
}


HRESULT Ceng_Kernel::
MoveFrame()
{
	return S_OK;
}

HRESULT Ceng_Kernel::
Frame()
{
    if( NULL == l_pD3DDevice )
        return E_FAIL;

    // Clear the backbuffer to a blue color

    l_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( (int)l_InitWindowSets.l_BBColour.r,(int)l_InitWindowSets.l_BBColour.g,(int)l_InitWindowSets.l_BBColour.b), 1.0f, 0 );
    
    // Begin the scene

    if( SUCCEEDED( l_pD3DDevice->BeginScene() ) )
    {
		thisTaskManager.Manager();
		
        // End the scene

        l_pD3DDevice->EndScene();
    }

    // Present the backbuffer contents to the display

    l_pD3DDevice->Present( NULL, NULL, NULL, NULL );

	return S_OK;
}

HRESULT Ceng_Kernel::
Cleanup ()
{
    if( l_pD3DDevice != NULL) 
        l_pD3DDevice->Release();

    if( l_pD3D != NULL)
        l_pD3D->Release();

	return S_OK;
}

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

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

INT WINAPI Ceng_Kernel::
WinMainBase( HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
    // Register the window class

    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
                      hInst, NULL, NULL, NULL, NULL,
                      "Pure FrameWork", NULL };
    RegisterClassEx( &wc );

    // Create the application's window

    l_hWnd = CreateWindow( "Pure FrameWork", "Pure FrameWork", 
                              WS_OVERLAPPEDWINDOW, 
							  l_InitWindowSets.TopLeftX, l_InitWindowSets.TopLeftY,
							  l_InitWindowSets.Width, l_InitWindowSets.Height,
							  GetDesktopWindow(), NULL, wc.hInstance, NULL );

	
	if (l_hWnd == NULL)
		MessageBox(l_hWnd, "Function CreateWindow() Failed!", "Function Failure!", MB_OK);


	// Initialize Direct3D

    if( SUCCEEDED( InitD3D(  ) ) )
    { 
		if ( SUCCEEDED( InitApp() ) )
		{
			// Show the window

			ShowWindow( l_hWnd, SW_SHOWDEFAULT );
			UpdateWindow( l_hWnd );

			// 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
					Frame();
			}
		}
    }
    UnregisterClass( "Pure FrameWork", wc.hInstance );
	return S_OK;
}

HRESULT Ceng_Kernel::
StandardSetup(HINSTANCE hInst, HINSTANCE hPrevInst, LPCTSTR, INT)
{
	l_hInstance = hInst;
	WinMainBase(hInst, NULL, NULL, NULL);
	return S_OK;
}
eng_Video.cpp
   // Ceng_Video.cpp: implementation of the eng_Video class.

//

//////////////////////////////////////////////////////////////////////


#include "eng_Video.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


Ceng_Video::Ceng_Video()
{

}

Ceng_Video::~Ceng_Video()
{

}

HRESULT Ceng_Video::VideoSetup()
{

	return S_OK;
}

HRESULT Ceng_Video::VideoUpdate()
{
	return S_OK;
}

eng_Input.cpp
   // eng_Input.cpp: implementation of the eng_Input class.

//

//////////////////////////////////////////////////////////////////////


#include "eng_Input.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


eng_Input::eng_Input()
{

}

eng_Input::~eng_Input()
{

}


LPDIRECTINPUT8* eng_Input::ReturnIInterfacePtr()
{
	if (g_pDI == NULL)
		return NULL;
	else
		return &g_pDI;
}

LPDIRECTINPUTDEVICE8* eng_Input::ReturnKeyboardDevicePtr()
{
	if (g_pDIKeyBoard == NULL)
		return NULL;
	else
		return &g_pDIKeyBoard;
}

LPDIRECTINPUTDEVICE8* eng_Input::ReturnMouseDevicePtr()
{
	if (g_pDIMouse == NULL)
		return NULL;
	else
		return &g_pDIMouse;
}

HRESULT eng_Input::SetupKeyboard()
{
	HRESULT hr;
	//Create DI Object

	hr =  DirectInput8Create(l_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL); 
	switch(hr)
	{
		case DIERR_BETADIRECTINPUTVERSION:
		{
			MessageBox(l_hWnd, "DIERR_BETADIRECTINPUTVERSION", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OLDDIRECTINPUTVERSION:
		{
			MessageBox(l_hWnd, "DIERR_OLDDIRECTINPUTVERSION", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OUTOFMEMORY:
		{
			MessageBox(l_hWnd, "DIERR_OUTOFMEMORY", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
	}

	//Create DI Device

	hr = g_pDI->CreateDevice(GUID_SysKeyboard, &g_pDIKeyBoard, NULL); 
	switch(hr)
	{
		case DIERR_DEVICENOTREG:
		{
			MessageBox(l_hWnd, "DIERR_DEVICENOTREG", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOINTERFACE:
		{
			MessageBox(l_hWnd, "DIERR_NOINTERFACE", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOTINITIALIZED:
		{
			MessageBox(l_hWnd, "DIERR_NOTINITIALIZED", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OUTOFMEMORY:
		{
			MessageBox(l_hWnd, "DIERR_OUTOFMEMORY", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}	
	}
	//Set the keyboard format, c_dfDIKeyboard is defined in dinput.h

	g_pDIKeyBoard->SetDataFormat(&c_dfDIKeyboard); 

	//Set the keyboard behaviour

	g_pDIKeyBoard->SetCooperativeLevel(l_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

	return S_OK;
}

HRESULT eng_Input::SetupMouse()
{
	//Create DI Object

	hr =  DirectInput8Create(l_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL); 
	switch(hr)
	{
		case DIERR_BETADIRECTINPUTVERSION:
		{
			MessageBox(l_hWnd, "DIERR_BETADIRECTINPUTVERSION", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OLDDIRECTINPUTVERSION:
		{
			MessageBox(l_hWnd, "DIERR_OLDDIRECTINPUTVERSION", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OUTOFMEMORY:
		{
			MessageBox(l_hWnd, "DIERR_OUTOFMEMORY", "DICreate Error!", MB_OK);
			//return E_FAIL;

			break;
		}
	}

	//Create DI Device

	hr = g_pDI->CreateDevice(GUID_SysMouse, &g_pDIMouse, NULL); 
	switch(hr)
	{
		case DIERR_DEVICENOTREG:
		{
			MessageBox(l_hWnd, "DIERR_DEVICENOTREG", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOINTERFACE:
		{
			MessageBox(l_hWnd, "DIERR_NOINTERFACE", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOTINITIALIZED:
		{
			MessageBox(l_hWnd, "DIERR_NOTINITIALIZED", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OUTOFMEMORY:
		{
			MessageBox(l_hWnd, "DIERR_OUTOFMEMORY", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}	
	}
	//Set the keyboard format, c_dfDIKeyboard is defined in dinput.h

	g_pDIMouse->SetDataFormat(&c_dfDIMouse); 

	//Set the keyboard behaviour

	g_pDIMouse->SetCooperativeLevel(l_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

	return S_OK;
}

HRESULT eng_Input::UseKeyboard()
{
 	char		buffer[256]; 
	hr = g_pDIKeyBoard->Acquire();
	switch(hr)
	{
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOTINITIALIZED:
		{
			MessageBox(l_hWnd, "DIERR_NOTINITIALIZED", "DICreateDevice Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OTHERAPPHASPRIO:
		{
			//MessageBox(l_hWnd, "DIERR_OTHERAPPHASPRIO", "DICreateDevice Error!", MB_OK);

			//return E_FAIL;

			break;
		}	
	}

	// retrieve the number of fogs from the fog class

	
	hr = g_pDIKeyBoard->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);
	switch(hr)
	{
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "GetDeviceState Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOTINITIALIZED:
		{
			MessageBox(l_hWnd, "DIERR_NOTINITIALIZED", "GetDeviceState Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOTACQUIRED:
		{
			//MessageBox(l_hWnd, "DIERR_NOTACQUIRED", "GetDeviceState Error!", MB_OK);

			//return E_FAIL;

			break;
		}
		case DIERR_INPUTLOST:
		{
			hr = g_pDIKeyBoard->Acquire();
			break;
		}	
	}



	if(KEYDOWN(buffer, DIK_UP))
	{
	}		
	if(KEYDOWN(buffer, DIK_DOWN))
	{
	}
	if(KEYDOWN(buffer, DIK_LEFT))
	{
	}
	if(KEYDOWN(buffer, DIK_RIGHT))
	{
	}
	if(KEYDOWN(buffer, DIK_ESCAPE))
	{
		DestroyWindow(l_hWnd);
	}	return S_OK;
}

HRESULT eng_Input::UseMouse()
{
	DIMOUSESTATE MouseState;
	hr = g_pDIMouse->Acquire();
	switch(hr)
	{
		case DIERR_INVALIDPARAM:
		{
			MessageBox(l_hWnd, "DIERR_INVALIDPARAM", "Acquire() Device Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_NOTINITIALIZED:
		{
			MessageBox(l_hWnd, "DIERR_NOTINITIALIZED", "Acquire() Device Error!", MB_OK);
			//return E_FAIL;

			break;
		}
		case DIERR_OTHERAPPHASPRIO:
		{
			//MessageBox(l_hWnd, "DIERR_OTHERAPPHASPRIO", "Acquire() Device Error!", MB_OK);

			//return E_FAIL;

			break;
		}	
	}
	if (FAILED(g_pDIMouse->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState)))
	{
	}
    //Is the left mouse button down?

    if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT]))
    {
	}
	else
	{
	}

    //Is the right mouse button down?

    if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT]))
    {

	}
	else
	{
	}
	
    //Is the right mouse button down?

    if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_MIDDLE]))
    {

    }
    else
    {
    }
	return S_OK;
}

eng_Camera.cpp
   // eng_Camera.cpp: implementation of the Ceng_Camera class.

//

//////////////////////////////////////////////////////////////////////

#include "eng_Camera.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


Ceng_Camera::Ceng_Camera()
{
	l_pD3DDevice				= NULL;
	MovedSinceLastUpdate		= false;
    Position					= D3DXVECTOR3(10.0f,7.0f,-18.0f);
    Facing						= D3DXVECTOR3(0.0f,0.0f,1.0f);
    Up							= D3DXVECTOR3(0.0f,1.0f,0.0f);
    Right						= D3DXVECTOR3(1.0f,0.0f,0.0f);
    RotAboutUp = RotAboutRight = 0.0f;
	
	D3DXMatrixIdentity(&matView); 
}

Ceng_Camera::~Ceng_Camera()
{
	Destroy();

}


HRESULT Ceng_Camera::Create(LPDIRECT3DDEVICE9 g_pD3DDevice)
{
	if (g_pD3DDevice != NULL)
	{
		l_pD3DDevice = g_pD3DDevice;
	}  
	D3DXMatrixIdentity(&matView); // ensure that the device's view matrix is the identity

    
	return l_pD3DDevice->SetTransform(D3DTS_VIEW,&matView); 
}

void Ceng_Camera::Destroy()
{
      if (l_pD3DDevice)
	  {
         l_pD3DDevice = NULL;
      }
}

void Ceng_Camera::MoveForward(float Dist)
{
   Position.x += Dist * Facing.x;
   Position.y += Dist * Facing.y;

   Position.z += Dist * Facing.z;

   MovedSinceLastUpdate = true;
}

void Ceng_Camera::MoveInDirection(float Dist, D3DXVECTOR3 *Dir)
{
    Position += Dist*(*Dir);
 	MovedSinceLastUpdate = true;
}

void Ceng_Camera::MoveRight(float Dist)
{
   Position.x += Dist * Right.x;
   Position.z += Dist * Right.z;
   MovedSinceLastUpdate = true;

}

void Ceng_Camera::MoveUp(float Dist)
{
   Position += Dist * Up;
   MovedSinceLastUpdate = true;
}

void Ceng_Camera::Roll(float Ang)
{
    //RotAboutFacing += Ang;

	MovedSinceLastUpdate = true;
}

void Ceng_Camera::RotateDown(float Ang)
{
    RotAboutRight += Ang;
	MovedSinceLastUpdate = true;
}

void Ceng_Camera::RotateRight(float Ang)
{
    RotAboutUp += Ang;

	MovedSinceLastUpdate = true;
}

void Ceng_Camera::SetPosition(D3DXVECTOR3 *inPos)
{
	Position = *inPos;
	MovedSinceLastUpdate = true;
}

HRESULT Ceng_Camera::UpdateDevice()
{
    HRESULT hr;
    D3DXMATRIX matTotal;

	// matrices to hold rotation info

    D3DXMATRIX matRotAboutUp, matRotAboutRight, matRotAboutFacing;

    // bail if we haven't moved since the last update

    if (!MovedSinceLastUpdate)
	{
       return S_OK;
    }

	// Get the rotation matrices for each rotation   

	D3DXMatrixRotationAxis(&matRotAboutRight,&Right,RotAboutRight);
	D3DXMatrixRotationAxis(&matRotAboutUp,&Up,RotAboutUp);
	//D3DXMatrixRotationAxis(&matRotAboutFacing,&Facing,RotAboutFacing);

	
    // concatenate them to form a total rotation matrix

    D3DXMatrixMultiply(&matTotal,&matRotAboutUp,&matRotAboutRight);
    //D3DXMatrixMultiply(&matTotal,&matRotAboutFacing,&matTotal);


    // transform 2 of the vectors by this matrix and get the third using the cross product

    D3DXVec3TransformCoord(&Right,&Right,&matTotal);   
    D3DXVec3TransformCoord(&Up,&Up,&matTotal);   
    D3DXVec3Cross(&Facing,&Right,&Up);

    // are these two still perpendicular?

    if (fabs(D3DXVec3Dot(&Up,&Right)) > 0.01)
	{      
		// these suckers aren't perpandicular anymore - get another using the cross product

        D3DXVec3Cross(&Up,&Facing,&Right);
    }

    // remember to normalise vectors before putting them into the view matrix

    D3DXVec3Normalize(&Right,&Right);
    D3DXVec3Normalize(&Up,&Up);
	D3DXVec3Normalize(&Facing,&Facing);

    // used to compute the bottom row of the view matrix

    float fView41,fView42,fView43;

    fView41 = -D3DXVec3Dot(&Right,&Position);
    fView42 = -D3DXVec3Dot(&Up,&Position);
    fView43 = -D3DXVec3Dot(&Facing,&Position);
   
	matView = D3DXMATRIX( Right.x, Up.x, Facing.x, 0.0f,
                          Right.y, Up.y, Facing.y, 0.0f,
                          Right.z, Up.z, Facing.z, 0.0f,
                          fView41, fView42,    fView43,     1.0f
                          );

    // update the device - if we fail then return the error code

    hr = l_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
    if (FAILED(hr))
	{
       return hr;
    }

    // reset the various variables to reflect the new update

    RotAboutUp = RotAboutRight = 0.0f;
    MovedSinceLastUpdate = false;

	return S_OK;
}
eng_TaskManager.cpp
   // eng_TaskManager.cpp: implementation of the Ceng_TaskManager class.

//

//////////////////////////////////////////////////////////////////////


#include "eng_TaskManager.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


Ceng_TaskManager::Ceng_TaskManager()
{
	CURRENTSTATE = 1;
}

Ceng_TaskManager::~Ceng_TaskManager()
{

}

HRESULT Ceng_TaskManager::SpreadVariables()
{
	// DI

	thisInput.l_hWnd		=	l_hWnd;
	thisInput.l_hInstance	=	l_hInstance;
	// Camera

	thisCamera.l_pD3DDevice	=	l_pD3DDevice;
	// Video

	return S_OK;
}

HRESULT Ceng_TaskManager::Manager()
{
	// switch onthe current state into, then call the correct functions

	switch (CURRENTSTATE)
	{
		case 1: // Setup

		{
			// pass the different classes pointers to the standard variables

			SpreadVariables();

			SetupVideo();
			SetupInput();
			SetupClock();
			SetupSound();
			// When the above has been done change the current state 

			// so that it automatically enters the run state

			CURRENTSTATE = 2;
			break;
		}
		case 2: // Run

		{
			// update everything

			UpdateVideo(2);
			UpdateInput(2);
			UpdateClock(2);
			UpdateSound(2);
			break;
		}
		case 3: // Currently Unused, possibly ending or paused

		{
			// update only current display, dont recalculate movement

			UpdateVideo(3);
			UpdateInput(3);
			UpdateClock(3);
			UpdateSound(3);
			break;
		}
	}
	return S_OK;
}

HRESULT Ceng_TaskManager::SetupClock()
{
	return S_OK;
}

HRESULT Ceng_TaskManager::SetupInput()
{
	// Pass the input class teh appropriate variables

	thisInput.SetupKeyboard();
	thisInput.SetupMouse();
	return S_OK;
}

HRESULT Ceng_TaskManager::SetupSound()
{
	return S_OK;
}

HRESULT Ceng_TaskManager::SetupVideo()
{
	return S_OK;
}

HRESULT Ceng_TaskManager::UpdateClock(int State)
{
	switch (State)
	{
		case 2:
		{
			break;
		}
		case 3:
		{
			break;
		}
	}
	return S_OK;
}

HRESULT Ceng_TaskManager::UpdateInput(int State)
{
	thisInput.UseKeyboard();
	thisInput.UseMouse();
	return S_OK;
}

HRESULT Ceng_TaskManager::UpdateSound(int State)
{

	return S_OK;
}

HRESULT Ceng_TaskManager::UpdateVideo(int State)
{
	return S_OK;
}
Right, i hope thats everything, if it aint, ill edit. Lastly, there's the app_Kernel.cpp file to create, so just create a normal C file and put this code in it:
   #include <stdio.h>
#include <windows.h>
#include <d3dx9.h>
#include "eng_Kernel.h"



class MyKernel : public Ceng_Kernel
{
};

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
	MyKernel	thisKernel;
	thisKernel.SetWindowDims(0,0,1024,768);
	thisKernel.SetBackColour(255.0f,255.0f,0.0f);
	thisKernel.StandardSetup(hInst,NULL,NULL,NULL);
	return 0;
}
Right that should now be a compilable app. All it does is create a blank screen, but it is created in such a way that its easy to build on top of it. Simply over write the base class functions to create ur own and the virtual keyword means your version will be used instead of the base one. It would be cool if you could give me feedback on how good or bad u think this is, or if you have any questions. . regards, ace [edited by - ace_lovegrove on May 18, 2004 8:55:29 AM] [edited by - ace_lovegrove on May 18, 2004 9:01:36 AM]

Share this post


Link to post
Share on other sites
do you have a simple example for
eng_Video.cpp
i mean how to import avi file and preview it..
thank you
ff8

Share this post


Link to post
Share on other sites