Skip to main content
gamedev.net
🔒 Locked

Problem trying to implement graphics engine

Started by LordFallout Oct 11, 2006 at 5:23 PM 3 replies 1k views
Original Post
LordFallout
LordFallout
I thought it might be a good idea to implement my own Graphics engine using classes since i know i can do it without classes, im talking about basic things like drawing shapes, colouring vertices and texturing, nothing to complicated, i cant even call it a engine, it will be one day however, anyhoo, i've been working on it for a couple of hours now and im pretty sure i've got everything down right, the end result should be that i can write about 10 ish lines of code and open a basic direct x window, however im having some problems with the wndproc function, i can seem to have this as a member of my class, however it must be since the hwnd is a member of my class, and i dont really want to expose it.i assume there must be a way to fix this problem, my code is as follows

#ifndef __QUESTIONMARKENGINEH__
#define __QUESTIONMARKENGINEH__

#define DIRECTINPUT_VERSION 0x0800

#include <d3dx9.h>

namespace QMEngine
{
	class QuestionMarkEngine
	{
	public:
		QuestionMarkEngine();
		void StartEngine( int _Type, int _Height, int _Width );
		void StartWindow();
		int EnterMessageLoop();
		void SetupScene();
		void DisplayScene();
		void EndScene();
		IDirect3DDevice9* GetDevice();
		bool IsActive();
		LRESULT CALLBACK WndProc(UINT msg, WPARAM wParam, LPARAM lParam);

	private:
		HINSTANCE				hInstance;
		WNDCLASS				wc;
		HWND					hWnd;
		IDirect3D9*				ID3D9;
		IDirect3DDevice9*		ID3D9Device;
		D3DCAPS9				d3dDeviceCaps;
		D3DPRESENT_PARAMETERS	d3dpp;
		D3DDEVTYPE				deviceType;
		WPARAM					wParam; 
		LPARAM					lParam;
		char					Type;
		int						Width;
		int						Height;
		float					timeDelta;
		bool					bActive;
	};
}


#endif QUESTIONMARKENGINE_H_




#include"QuestionMarkEngine.h"

using namespace QMEngine;

void QuestionMarkEngine::StartEngine(int _Type, int _Height, int _Width)
{
	ID3D9 = Direct3DCreate9( D3D_SDK_VERSION );

	deviceType = D3DDEVTYPE_HAL;

	ID3D9->GetDeviceCaps( D3DADAPTER_DEFAULT, deviceType, &d3dDeviceCaps);

	int vp = 0;
	if( d3dDeviceCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
	{
		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	}

	else
	{
		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	}

	d3dpp.BackBufferWidth				= Width;
	d3dpp.BackBufferHeight				= Height;
	d3dpp.BackBufferFormat				= D3DFMT_A8R8G8B8;
	d3dpp.BackBufferCount				= 1;
	d3dpp.MultiSampleType				= D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality			= 0;
	d3dpp.SwapEffect					= D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow					= hWnd;
	
	if( _Type == 1 )
	{
		d3dpp.Windowed					= true;
	}
	
	else
	{
		d3dpp.Windowed					= false;
	}

	d3dpp.EnableAutoDepthStencil		= true;
	d3dpp.AutoDepthStencilFormat		= D3DFMT_D24S8;
	d3dpp.Flags							= 0;
	d3dpp.FullScreen_RefreshRateInHz	= D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval			= D3DPRESENT_INTERVAL_IMMEDIATE;

	if( !( ID3D9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
									D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &ID3D9Device ) ) )
	{
		MessageBox(0, L"Creating Device Failed - ERROR", 0, 0);
	}
}

int QuestionMarkEngine::EnterMessageLoop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return (int)msg.wParam;
}

void QuestionMarkEngine::StartWindow()
{
	wc.cbClsExtra					= 0;
	wc.cbWndExtra					= NULL;
	wc.hbrBackground				= (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor						= LoadCursor(0, IDC_ARROW);
	wc.hIcon						= LoadIcon(0, IDI_APPLICATION);
	wc.hInstance					= hInstance;
	wc.lpfnWndProc					= QuestionMarkEngine::WndProc;
	wc.lpszClassName				= L"d3dwindow";
	wc.lpszMenuName					= 0;
	wc.style						= CS_HREDRAW | CS_VREDRAW;

	if(!RegisterClass(&wc)) 
	{
		::MessageBox(0, L"RegisterClass() - FAILED", 0, 0);
	}

	hWnd = CreateWindow(L"d3dwindow", L"Direct3D9App", WS_EX_TOPMOST, 0, 0, Width, Height, 0, 0, hInstance, 0); 

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
}

void QuestionMarkEngine::SetupScene()
{
	/*	Set the projection matrix	*/
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, 800 / 600, 1.0f, 1000.0f);
	
	ID3D9Device->SetTransform(D3DTS_PROJECTION, &proj);
}

void QuestionMarkEngine::DisplayScene()
{
	ID3D9Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0);
	ID3D9Device->BeginScene();
}

void QuestionMarkEngine::EndScene()
{

	ID3D9Device->EndScene();
	ID3D9Device->Present(0, 0, 0, 0);
}

IDirect3DDevice9* QuestionMarkEngine::GetDevice()
{
	return ID3D9Device;
}

LRESULT CALLBACK QuestionMarkEngine::WndProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
		
	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			DestroyWindow(hWnd);

	case WM_ACTIVATE:
		if (WA_INACTIVE == wParam)
		{
			bActive = false;
		}
		else
		{
			bActive = true;
		}
		break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}



i do intend to finish and clean my code up and tidy stuff up once i've got my problem solved and apparantly 1>d:\vc++ projects\revised engine\questionmarkengine\questionmarkengine\questionmarkengine.cpp(80) : error C3867: 'QMEngine::QuestionMarkEngine::WndProc': function call missing argument list; use '&QMEngine::QuestionMarkEngine::WndProc' to create a pointer to member 1>d:\vc++ projects\revised engine\questionmarkengine\questionmarkengine\questionmarkengine.cpp(80) : error C2440: '=' : cannot convert from 'LRESULT (__stdcall QMEngine::QuestionMarkEngine::* )(UINT,WPARAM,LPARAM)' to 'WNDPROC'
Simian Man
Simian Man
You can't do that because a free funcion has a different type than a member function, because member functions are passed a 'this' pointer.

Try making it static.
LordFallout
LordFallout
aye i tried that but it keeps giving me illegal reference to none static members then, and also the second compile error from before o.o
guess i'll keep at it, if theres a better / more convinient way to do this i'd like to know that aswell if anyone could share, i've read a couple of tutorials on the matter but still nothing seems to help me
deffer
deffer
Did you read this classic tutorial?

1. Make the function static.
2. In static function you don't have "this" pointer, nor are you allowed to access any member variables - the function behaves just as "normal function" - the only difference is that it has access to private/protected content of the class (if it has any instance of that class to operate on, that is).
3. In other words - you have to provide your QuestionMarkEngine* class instance all by yourself - that's what this article is about.
LordFallout
LordFallout
ah ok, thanks for the replies, i needed those things cleared up for me, maybe i should brush up on my c++ class methods etc and i'll have another stab at it tomorrow

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.