CoolGamer48 65 Posting Pro in Training

Hey,

I'm trying to run a simple DirectX program, it loads a model and displays it. I run it once, and it works. I run it again, doesn't work. Didn't change any code. Just recompiled, and it doesn't work.

I'm not sure if the issue is with my computer (which has a pretty bad video card), or with my program.

Could someone look over my code or try to run it to see if there are any problems?

The entire thing is 7 files, but the relevant code should only be in 3 or 4 of them.

Graphics.h

//********************
//Graphics.h/cpp
//Encapsulated functionality for setting up Direct3D 9, setting up Direct3D to view
//a 3D world, rendering models and textures in a 3D world, drawing flat textures and
//fonts on the screen, and drawing fonts to the screen
//********************
#ifndef GRAPHICS_H
#define GRAPHICS_H

//Libraries
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"ReznebMath.lib")

//Includes
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include <ReznebMath.h>

//Constant colors
const D3DCOLOR BLACK = D3DCOLOR_XRGB(0,0,0);
const D3DCOLOR WHITE = D3DCOLOR_XRGB(255,255,255);
const D3DCOLOR RED = D3DCOLOR_XRGB(255,0,0);
const D3DCOLOR GREEN = D3DCOLOR_XRGB(0,255,0);
const D3DCOLOR BLUE = D3DCOLOR_XRGB(0,0,255);

//Prototypes
int Direct3DInit(HWND hwnd,int width,int height,bool windowed);
void Direct3DEnd();

void Clear(D3DCOLOR color = BLACK);
void Present();

int BeginScene();
int EndScene();
void BeginSprite();
void EndSprite();

void SetCamera(float x,float y,float z,float lookx,float looky,float lookz,float upx = 0.0f,float  upy = 1.0f,float upz = 0.0f);
void SetPerspective(int screen_width,int screen_height,float fieldOfView = 45.0f,float close = 0.1f,float far = 10000.0f);

//Model Class
//Used for storing and rendering 3D models
class Model
{
public:
	Model();
	~Model();
	int LoadFromX(std::string filename);
	void Render();
private:
	ID3DXMesh* m_mesh;
	D3DMATERIAL9* m_materials;
	IDirect3DTexture9** m_textures;
	int m_materialCount;
};

//Texture Class
//Used for drawing 2D textures to the screen
class Texture
{
public:
	Texture();
	~Texture();
	int InitTexture(std::string filename,D3DCOLOR transcolor,IDirect3DDevice9* d3ddev);
	int Draw(float x,float y,float rotation,RECT* source_rect,ID3DXSprite* sprite_handler);
	IDirect3DTexture9* GetTexture();
	D3DXIMAGE_INFO GetImageInfo();
	int GetWidth();
	int GetHeight();
private:
	IDirect3DTexture9* m_d3dTexture;
	D3DXIMAGE_INFO m_imgInfo;
};

//Sprite Class
//Used for managing and drawing a series of 2D textures as a single sprite on the screen
class Sprite
{
public:
	Sprite();
	~Sprite();
	int SetSourceImage(std::string filename,D3DCOLOR transcolor,IDirect3DDevice9* d3ddev);
	void SetInfo(int numFrames,int numCols,int frameChangeDelay,int frameWidth,int frameHeight);
	void Draw(float x,float y,float rotation,ID3DXSprite* sprite_handler);
	void Unpause();
	void Pause();
	int SetFrame(int frame);
	int GetFrame();
	int GetWidth();
	int GetHeight();
private:
	Texture* m_sourceTexture;
	int m_numFrames;
	int m_currentFrame;
	int m_numCols;
	int m_frameChangeDelay;
	int m_delayCount;
	int m_frameWidth;
	int m_frameHeight;
	bool m_paused;
};

//Font Class
//Used for writing 2D text to the screen based on system fonts
class Font
{
public:
	Font();
	~Font();
	int Load(std::string name,int height,std::string* error = NULL,int weight = FW_NORMAL,bool italic = false);
	void RenderText(std::string text,int x,int y,D3DCOLOR color,bool single_line = true,int rect_width = 1,int rect_height = 1);
	void RenderTextForce(std::string text,int x,int y,D3DCOLOR color,bool single_line = true,int rect_width = 1,int rect_height = 1);
private:
	ID3DXFont* m_font;
};

#endif

Graphics.cpp

#include "Graphics.h"
#include <windows.h>//for testing purposes only

//Globals
IDirect3D9* d3d = NULL;
IDirect3DDevice9* d3ddev = NULL;
IDirect3DSurface9* backbuffer = NULL;
ID3DXSprite* sprite_handler = NULL;
HRESULT result;

int Direct3DInit(HWND hwnd,int width,int height,bool windowed)
{
	if((d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
	{
		MessageBox(hwnd,"Error creating the Direct3D interface","Error",MB_OK | MB_ICONERROR);
		return 0;
	}
	
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.Windowed = windowed ? true : false;
	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = width;
	d3dpp.BackBufferHeight = height;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.hDeviceWindow = hwnd;
	d3dpp.EnableAutoDepthStencil = true;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

	if((result = d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3ddev)) != D3D_OK || d3ddev == NULL)
	{
		switch(result)
		{
		case D3DERR_DEVICELOST:
			MessageBox(hwnd,"Error creating the Direct3D device: The Direct3D device was lost. Error: D3DERR_DEVICELOST","Direct3D Error",MB_OK | MB_ICONERROR);
			break;
		case D3DERR_INVALIDCALL:
			MessageBox(hwnd,"Error creating the Direct3D device: The call to IDirect3D9::CreateDeivce was invalid. Error: D3DERR_INVALIDCALL","Direct3D Error",MB_OK | MB_ICONERROR);
			break;
		case D3DERR_NOTAVAILABLE:
			MessageBox(hwnd,"Error creating the Direct3D device: Error: D3DERR_NOTAVAILABLE","Direct3D Error",MB_OK | MB_ICONERROR);
			break;
		case D3DERR_OUTOFVIDEOMEMORY:
			MessageBox(hwnd,"Error creating the Direct3D device: Error: D3DERR_OUTOFVIDEOMEMORY","Direct3D Error",MB_OK | MB_ICONERROR);
			break;
		default:
			MessageBox(hwnd,"Error creating the Direct3D device: Unknown error.","Direct3D Error",MB_OK | MB_ICONERROR);
		}
		return 0;
	}

	d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,BLACK,1.0f,0);
	d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer);
	if(backbuffer == NULL)
	{
		MessageBox(hwnd,"Error retriving backbuffer","Error",MB_OK);
		return 0;
	}
	d3ddev->SetRenderState(D3DRS_ZENABLE,true);
	d3ddev->SetRenderState(D3DRS_AMBIENT,WHITE);

	if(D3DXCreateSprite(d3ddev,&sprite_handler) != S_OK)
	{
		MessageBox(hwnd,"Error creating D3DX Sprite interface","Error",MB_OK);
		return 0;
	}

	d3ddev->SetRenderState(D3DRS_LIGHTING,0);
	d3ddev->SetRenderState(D3DRS_ZENABLE,1);

	return 1;
}

void Direct3DEnd()
{
	if(backbuffer)
		backbuffer->Release();
	if(d3ddev)
		d3ddev->Release();
	if(d3d)
		d3d->Release();
}

void Clear(D3DCOLOR color)
{
	d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,color,1.0f,0);
}

void Present()
{
	d3ddev->Present(NULL,NULL,NULL,NULL);
}

int BeginScene()
{
	if(d3ddev->BeginScene() == D3D_OK)
		return 1;
	else
		return 0;
}

int EndScene()
{
	if(d3ddev->EndScene() == D3D_OK)
		return 1;
	else
		return 0;
}

void BeginSprite()
{
	sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
}

void EndSprite()
{
	sprite_handler->End();
}

void SetCamera(float x,float y,float z,float lookx,float looky,float lookz,float upx,float upy,float upz)
{
	D3DXVECTOR3 pos(x,y,x), look(lookx,looky,lookz), up(upx,upy,upz);
	D3DXMATRIX matView;

	D3DXMatrixLookAtLH(&matView,&pos,&look,&up);
	d3ddev->SetTransform(D3DTS_VIEW,&matView);
}

void SetPerspective(int screen_width,int screen_height,float fieldOfView,float close_clip,float far_clip)
{
	D3DXMATRIX matProj;
	float ratio = (float)screen_width / (float)screen_height;

	D3DXMatrixPerspectiveFovLH(&matProj,fieldOfView,ratio,close_clip,far_clip);//WTF??? WHERE THE SYNTAX ERROR AT?
	d3ddev->SetTransform(D3DTS_PROJECTION,&matProj);
}


Model::Model()
{
	m_materialCount = 0;
	m_mesh = NULL;
	m_materials = NULL;
	m_textures = NULL;
}

Model::~Model()
{
	if(m_materials != NULL)
	{
		delete[] m_materials;
	}

	if(m_textures != NULL)
	{
		for(int i = 0;i < m_materialCount;i++)
		{
			if(m_textures[i] != NULL)
				m_textures[i]->Release();
		}
		delete[] m_textures;
	}

	if(m_mesh != NULL)
		m_mesh->Release();
}

int Model::LoadFromX(std::string filename)
{
	ID3DXBuffer* matbuffer;
	HRESULT result;

	result = D3DXLoadMeshFromX(&filename[0],D3DXMESH_SYSTEMMEM,d3ddev,NULL,&matbuffer,NULL,(DWORD*)&m_materialCount,&m_mesh);
	if(result != D3D_OK)
	{
		switch(result)
		{
		case D3DERR_INVALIDCALL:
			MessageBox(NULL,"D3DERR_INVALIDCALL from D3DXLoadMeshFromX() in Model::LoadFromX()","Error",MB_OK | MB_ICONERROR);
			break;
		case E_OUTOFMEMORY:
			MessageBox(NULL,"E_OUTOFMEMORY from D3DXLoadMeshFromX() in Model::LoadFromX()","Error",MB_OK | MB_ICONERROR);
			break;
		case D3DXFERR_FILENOTFOUND:
			MessageBox(NULL,"D3DXFERR_FILENOTFOUND from D3DXLoadMeshFromX() in Model::LoadFromX()","Error",MB_OK | MB_ICONERROR);
			break;
		default:
		{
			char error[1000];
			sprintf(error,"Unknown error from D3DXLoadMeshFromX() in Model::LoadFromX(): %d",result);
			MessageBox(NULL,error,"Error",MB_OK | MB_ICONERROR);
		}
		}
		return 0;
	}

	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)matbuffer->GetBufferPointer();
	m_materials = new D3DMATERIAL9[m_materialCount];
	m_textures = new IDirect3DTexture9*[m_materialCount];

	for(int i = 0;i < m_materialCount;i++)
	{
		m_materials[i] = d3dxMaterials[i].MatD3D;
		m_materials[i].Ambient = m_materials[i].Diffuse;

		m_textures[i] = NULL;
		if(d3dxMaterials[i].pTextureFilename != NULL && d3dxMaterials[i].pTextureFilename != "")
		{
			result = D3DXCreateTextureFromFile(d3ddev,d3dxMaterials[i].pTextureFilename,&m_textures[i]);
			if(result != D3D_OK)
			{
				MessageBox(NULL,"Could not load proper texture file","Error loading model",MB_OK | MB_ICONERROR);
				return 0;
			}
		}
	}

	matbuffer->Release();
	return 1;
}

void Model::Render()
{
	for(int i = 0;i < m_materialCount;i++)
	{
		d3ddev->SetMaterial(&m_materials[i]);
		d3ddev->SetTexture(0,m_textures[i]);
		m_mesh->DrawSubset(i);
	}
}


Texture::Texture()
{
	m_d3dTexture = NULL;
}

Texture::~Texture()
{
	if(m_d3dTexture)
		m_d3dTexture->Release();
}

int Texture::InitTexture(std::string filename,D3DCOLOR transcolor,IDirect3DDevice9* d3ddev)
{
	if(m_d3dTexture)
		m_d3dTexture->Release();

	m_d3dTexture = NULL;

	if(D3DXGetImageInfoFromFile(&filename[0],&m_imgInfo) != D3D_OK)
		return 0;

	if(D3DXCreateTextureFromFileEx(d3ddev,&filename[0],m_imgInfo.Width,m_imgInfo.Height,1,D3DPOOL_DEFAULT,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,transcolor,&m_imgInfo,NULL,&m_d3dTexture) != D3D_OK)
		return 0;

	return 1;
}

int Texture::Draw(float x,float y,float rotation,RECT* source_rect,ID3DXSprite* sprite_handler)
{
	D3DXVECTOR3 pos(x,y,0);
	D3DXVECTOR2 center(x+m_imgInfo.Width/2,y+m_imgInfo.Height/2);
	D3DXMATRIX mat;

	D3DXMatrixTransformation2D(&mat,NULL,NULL,NULL,&center,ToRadians(Angle(-rotation)),NULL);
	sprite_handler->SetTransform(&mat);

	if(sprite_handler->Draw(m_d3dTexture,source_rect,NULL,&pos,D3DCOLOR_XRGB(255,255,255)) != S_OK)
		return 0;

	return 1;
}

IDirect3DTexture9* Texture::GetTexture()
{
	return m_d3dTexture;
}

int Texture::GetWidth()
{
	return m_imgInfo.Width;
}

int Texture::GetHeight()
{
	return m_imgInfo.Height;
}

D3DXIMAGE_INFO Texture::GetImageInfo()
{
	return m_imgInfo;
}


Sprite::Sprite()
{
	m_sourceTexture = new Texture;
	m_numFrames = m_numCols = m_currentFrame = m_frameChangeDelay = m_frameWidth = m_frameHeight = m_delayCount = 0;
	m_paused = false;
}

Sprite::~Sprite()
{
	if(m_sourceTexture)
		delete m_sourceTexture;
}

int Sprite::SetSourceImage(std::string filename,D3DCOLOR transcolor,IDirect3DDevice9* d3ddev)
{
	if(!m_sourceTexture->InitTexture(&filename[0],transcolor,d3ddev))
		return 0;

	return 1;
}

void Sprite::SetInfo(int numFrames,int numCols,int frameChangeDelay,int frameWidth,int frameHeight)
{
	m_numFrames = numFrames;
	m_numCols = numCols;
	m_frameWidth = frameWidth;
	m_frameHeight = frameHeight;
	m_frameChangeDelay = frameChangeDelay;
}

void Sprite::Draw(float x,float y,float rotation,ID3DXSprite* sprite_handler)
{
	RECT source_rect;
	source_rect.left = (m_currentFrame%m_numCols)*m_frameWidth;
	source_rect.top = floor((double)(m_currentFrame/m_numCols))*m_frameHeight;
	source_rect.right = source_rect.left + m_frameWidth;
	source_rect.bottom = source_rect.top + m_frameHeight;

	m_sourceTexture->Draw(x,y,rotation,&source_rect,sprite_handler);

	if(!m_paused)
	{
		if(m_delayCount >= m_frameChangeDelay)
		{
			m_delayCount = 0;
			if(++m_currentFrame >= m_numFrames)
			{
				m_currentFrame = 0;
			}
		}
		else
		{
			m_delayCount++;
		}
	}
}

void Sprite::Pause()
{
	m_paused = true;
}

void Sprite::Unpause()
{
	m_paused = false;
}

int Sprite::SetFrame(int frame)
{
	if(frame < 0 || frame >= m_numFrames)
		return 0;

	m_currentFrame = frame;

	return 1;
}

int Sprite::GetFrame()
{
	return m_currentFrame;
}

int Sprite::GetWidth()
{
	return m_frameWidth;
}

int Sprite::GetHeight()
{
	return m_frameHeight;
}


Font::Font()
{
	m_font = NULL;
}

Font::~Font()
{
	if(m_font)
		m_font->Release();
}

int Font::Load(std::string name,int height,std::string* error,int weight,bool italic)
{
	HRESULT result;
	*error = "";
	result = D3DXCreateFont(d3ddev,height,0,weight,1,italic,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH | FF_DONTCARE,&name[0],&m_font);
	switch(result)
	{
	case S_OK:
		sprintf(&(*error)[0],"No error:%d",result);
		return 1;
	case D3DERR_INVALIDCALL:
		sprintf(&(*error)[0],"Invalid call:%d",result);
		return 0;
	case D3DXERR_INVALIDDATA:
		sprintf(&(*error)[0],"Invalid data:%d",result);
		return 0;
	case E_OUTOFMEMORY:
		sprintf(&(*error)[0],"Out of memory:%d",result);
		return 0;
	default:
		sprintf(&(*error)[0],"Unknown error:%d",result);
		return 0;
	}
}

void Font::RenderText(std::string text,int x,int y,D3DCOLOR color,bool single_line,int rect_width,int rect_height)
{
	RECT rect;
	rect.left = x;
	rect.top = y;
	rect.right = rect.left + rect_width;
	rect.bottom = rect.top + rect_height;

	long format = single_line ? (DT_LEFT | DT_NOCLIP) : (DT_LEFT | DT_WORDBREAK);
	m_font->DrawTextA(NULL,&text[0],-1,&rect,format,color);

	return;
}

void Font::RenderTextForce(std::string text,int x,int y,D3DCOLOR color,bool single_line,int rect_width,int rect_height)
{
	RECT rect;
	rect.left = x;
	rect.top = y;
	rect.right = rect.left + rect_width;
	rect.bottom = rect.top + rect_height;
	if(BeginScene())
	{
		Clear();
		BeginSprite();

		long format = single_line ? (DT_LEFT | DT_NOCLIP) : (DT_LEFT | DT_WORDBREAK);
		m_font->DrawTextA(NULL,&text[0],-1,&rect,format,color);

		EndSprite();
		EndScene();
	}
	Present();

	return;
}

Game.h

#ifndef GAME_H
#define GAME_H

#include "Input.h"
#include "Graphics.h"

#define D3D_DEBUG_INFO

const bool FULLSCREEN = false;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

int GameInit(HWND hwnd);
void GameRun();
void GameEnd();

#endif

Game.cpp

#include "Game.h"

Font* debug_font = NULL;
Model* my_model = NULL;

int GameInit(HWND hwnd)
{
	std::string error;
	
	if(!Direct3DInit(hwnd,SCREEN_WIDTH,SCREEN_HEIGHT,!FULLSCREEN))
	{
		MessageBox(hwnd,"Error initializing Direct3D","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	debug_font = new Font;
	if(!debug_font->Load("Arial",30,&error))
	{
		sprintf(&error[0],"Error loading test_font:%s",error);
		MessageBox(hwnd,&error[0],"Error",MB_OK);
		return 0;
	}

	debug_font->RenderTextForce("Loading DirectInput...",0,0,WHITE);
	if(!DirectInputInit(hwnd))
	{
		MessageBox(hwnd,"Error initializing DirectInput","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	debug_font->RenderTextForce("Loading fachwerk33T.x...",0,0,WHITE);
	my_model = new Model;
	my_model->LoadFromX("fachwerk33T.x");

	debug_font->RenderTextForce("Setting up the 3D world...",0,0,WHITE);
	SetCamera(5.5f,0.0f,0.0f,0.0f,0.0f,0.0f);
	SetPerspective(SCREEN_WIDTH,SCREEN_HEIGHT);

	return 1;
}

void GameRun()
{
	DirectInputRun();

	if(BeginScene())
	{
		Clear();

		my_model->Render();

		BeginSprite();

		debug_font->RenderText("Running",0,0,WHITE);

		EndSprite();

		EndScene();
	}

	Present();

	if(KeyDown(DIK_ESCAPE))
	{		
		debug_font->RenderTextForce("Exiting...",0,0,WHITE);
		PostQuitMessage(0);
	}
}

void GameEnd()
{
	delete debug_font;
	delete my_model;
	Direct3DEnd();
	DirectInputEnd();
}

Input.h

//********************
//Input.h/cpp
//Encapsulated functionality for setting up DirectInput 8, reading key presses, mouse button
//presses, and mouse x, y, and z movement.
//********************
#ifndef INPUT_H
#define INPUT_H

//Libraries
#pragma comment(lib,"dinput8.lib")

//Includes
#include <dinput.h>

//Constants internally used for keeping track of key states
const int KS_UP = 0;
const int KS_DOWN = 1;

//Prototypes
int DirectInputInit(HWND hwnd);
void DirectInputEnd();
void DirectInputRun();
bool KeyDown(int vk_code,bool just_pressed = true);

#endif

Input.cpp

#include "Input.h"

//Globals
IDirectInput8* dinput = NULL;
IDirectInputDevice8* dikeyboard = NULL;
IDirectInputDevice8* dimouse = NULL;
char dikeys[256];
int dikeys_prev[256];

int DirectInputInit(HWND hwnd)
{
	if(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8A,(void**)&dinput,NULL) != DI_OK)
	{
		MessageBox(hwnd,"Error creating the DirectInput object","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dinput->CreateDevice(GUID_SysKeyboard,&dikeyboard,NULL) != DI_OK)
	{
		MessageBox(hwnd,"Error creating the DirectInput keyboard device","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dikeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK)
	{
		MessageBox(hwnd,"Error setting the DirectInput keyboard data format","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dikeyboard->SetCooperativeLevel(hwnd,DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
	{
		MessageBox(hwnd,"Error seeting the DirectInput keyboard cooperative level","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dikeyboard->Acquire() != DI_OK)
	{
		MessageBox(hwnd,"Error acquiring the DirectInput keyboard device","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dinput->CreateDevice(GUID_SysMouse,&dimouse,NULL) != DI_OK)
	{
		MessageBox(hwnd,"Error creating the DirectInput mouse device","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dimouse->SetDataFormat(&c_dfDIMouse) != DI_OK)
	{
		MessageBox(hwnd,"Error setting the DirectInput mouse dataformat","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dimouse->SetCooperativeLevel(hwnd,DISCL_EXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
	{
		MessageBox(hwnd,"Error setting the DirectInput mouse cooperative level","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if(dimouse->Acquire() != DI_OK)
	{
		MessageBox(hwnd,"Error acquiring the DirectInput mouse device","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	for(int x = 0;x < 256;x++)
		dikeys_prev[x] = KS_UP;

	dikeyboard->GetDeviceState(sizeof(dikeys),(void*)&dikeys);

	return 1;
}

void DirectInputEnd()
{
	if(dikeyboard)
	{
		dikeyboard->Unacquire();
		dikeyboard->Release();
	}

	if(dimouse)
	{
		dimouse->Unacquire();
		dimouse->Release();
	}
}

void DirectInputRun()
{
	for(int x = 0;x < 256;x++)
	{
		dikeys_prev[x] = dikeys[x] & 0x80;
	}
	dikeyboard->GetDeviceState(sizeof(dikeys),(void*)&dikeys);
}

bool KeyDown(int vk_code,bool just_pressed)
{
	if(just_pressed == false)
	{
		if(dikeys[vk_code] & 0x80)
			return true;
		else
			return false;
	}
	else
	{
		if((dikeys[vk_code] & 0x80) && (dikeys_prev[vk_code] == KS_UP))
			return true;
		else
			return false;
	}
}

Main.cpp

#include <windows.h>
#include "Game.h"

LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	MSG msg;
	WNDCLASSEX wcx;
	HWND hwnd;

	ZeroMemory(&wcx,sizeof(WNDCLASSEX));
	wcx.cbClsExtra = 0;
	wcx.cbWndExtra = 0;
	wcx.cbSize = sizeof(WNDCLASSEX);
	wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wcx.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
	wcx.hCursor = LoadCursor(NULL,IDC_ARROW);
	wcx.hInstance = hInstance;
	wcx.lpszClassName = "MyClass";
	wcx.lpszMenuName = NULL;
	wcx.style = 0;
	wcx.hbrBackground = (HBRUSH)COLOR_WINDOW+1;
	wcx.lpfnWndProc = WinProc;
	if(!RegisterClassEx(&wcx))
	{
		MessageBoxA(NULL,"Error registering the window class","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	if((hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"MyClass","Test",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,NULL,NULL,hInstance,NULL))  == NULL)
	{
		MessageBoxA(NULL,"Error creating the window","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

	ShowWindow(hwnd,nShowCmd);
	UpdateWindow(hwnd);

	if(!GameInit(hwnd))
		return 0;

	bool done = false;
	while(!done)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				done = true;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
			GameRun();
	}

	return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		GameEnd();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,msg,wParam,lParam);
	}
	return 0;
}

If you run it, it'll complain about not having ReznebMath.h. There's only one line that needs it, and the function that line is in is never called, so you can just comment that line out (or all the code in the function). The only thing you'll need to link to in the project options in dxguid.lib.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.