Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ DirectInput Mouse Issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 19 February 2012 - 04:05 PM

I'm trying to use GetDeviceState to obtain mouse information such as mouse movement and clicks but using GetDeviceState causes the program to give me an Unhandled Exception at... error over in the first line at the sub Mouse_Controls(). The program I have when compliled is error free and warning free. Also using GetDeviceState for keyboard works ok. It's only the mouse part that pops up an error. I'm pretty much stumped but this is what I have so far. Could anyone tell me whats wrong with the mouse part of the code? Any help is appreciated.

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <math.h>

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

struct CUSTOM_VERTEX
{
	float X, Y, Z;
	DWORD Color;
};

#define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE)

#define PI		   3.141592654f
#define FOV		  (float)PI / 4.0f
#define ASPECT_RATIO 4.0f / 3.0f
#define NEAR_Z	   1.0f
#define FAR_Z		10000.0f

LPDIRECT3D9 D3D;
LPDIRECT3DDEVICE9 Device;
D3DDISPLAYMODE Display_Mode;
D3DPRESENT_PARAMETERS Screen;

LPDIRECTINPUT8 DI;

HWND hWnd;
MSG msg;
HINSTANCE hInstance;
int Width;
int Height;

bool Fullscreen_Enabled;
bool Running;

D3DXMATRIX View_Matrix;
D3DXMATRIX Projection_Matrix;

D3DVECTOR Camera_Position;
D3DVECTOR Camera_Angle;

LPDIRECTINPUTDEVICE8 Mouse_Device;
DIMOUSESTATE Mouse_State;
D3DXVECTOR2 Mouse;

LPDIRECTINPUTDEVICE8 Keyboard_Device;
BYTE Keyboard_State[256];

CUSTOM_VERTEX Create_Custom_Vertex();
void DirectX_Initialize();
void DirectInput_Initialize();
void DirectInput_Initialize_Mouse();
void Mouse_Controls();
void DirectInput_Initialize_Keyboard();
void DirectInput_Key_State();
void Keyboard_Controls();
void Setup_Matrices();
void Settings();
void Create_Polygon();
void Draw_Polygon();
void Render();
void Game_Loop();
void Main();
void Shutdown();

CUSTOM_VERTEX Vertex_List[3];

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

void DirectX_Initialize()
{
	D3D = Direct3DCreate9(D3D_SDK_VERSION);
	memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS));

	if (Fullscreen_Enabled == true)
	{
	   Display_Mode.Width = 800;
	   Display_Mode.Height = 600;
	   Display_Mode.Format = D3DFMT_R5G6B5;
	   Screen.Windowed = FALSE;
	   Screen.BackBufferCount = 1;
	   Screen.BackBufferWidth = Display_Mode.Width;
	   Screen.BackBufferHeight = Display_Mode.Height;
	   Screen.hDeviceWindow = hWnd;
	}
	else
	{
	   D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);
	   Screen.Windowed = TRUE;
	}

	Screen.SwapEffect = D3DSWAPEFFECT_DISCARD;
	Screen.BackBufferFormat = D3DFMT_R5G6B5;
	Screen.EnableAutoDepthStencil = TRUE;
	Screen.AutoDepthStencilFormat = D3DFMT_D16;

	D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device);
}

void DirectInput_Initialize()
{
	DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL);
}

void DirectInput_Initialize_Mouse()
{
	DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL);
	Mouse_Device->SetDataFormat(&c_dfDIMouse);
	Mouse_Device->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	Mouse_Device->Acquire();
}

void Mouse_Controls()
{
	//THIS IS WHERE I GET THE ERROR!!
	//--------------------------------------------------------------------------------------------------------
	Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);
	//--------------------------------------------------------------------------------------------------------
	Mouse.x += Mouse_State.lX;
	Mouse.y += Mouse_State.lY;
	
	if (Mouse.x > Width) Mouse.x = (float)Width;
	if (Mouse.y < 0) Mouse.x = 0;
	
	if (Mouse.x > Height) Mouse.y = (float)Height;
	if (Mouse.y < 0) Mouse.y = 0;
	
	Camera_Angle.x += Mouse_State.lY;
	Camera_Angle.y += Mouse_State.lX;
}

void DirectInput_Initialize_Keyboard()
{
	DI->CreateDevice(GUID_SysKeyboard, &Keyboard_Device, NULL);
	Keyboard_Device->SetDataFormat(&c_dfDIKeyboard);
	Keyboard_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
	Keyboard_Device->Acquire();
}

bool DirectInput_Key_State(int Key_Code)
{
	Keyboard_Device->GetDeviceState(256, (LPVOID)Keyboard_State);
	return Keyboard_State[Key_Code] && 0x80;
}

void Keyboard_Controls()
{
	#define Camera_Speed 5;

	if (DirectInput_Key_State(DIK_ESCAPE)) DestroyWindow(hWnd);

	if (DirectInput_Key_State(DIK_W))
	{
		Camera_Position.x = Camera_Position.x - (float)sin(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
		Camera_Position.z = Camera_Position.z - (float)cos(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
	}

	if (DirectInput_Key_State(DIK_S))
	{
		Camera_Position.x = Camera_Position.x + (float)sin(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
		Camera_Position.z = Camera_Position.z + (float)cos(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
	}
}

void Settings()
{
	Device->SetRenderState(D3DRS_ZENABLE, TRUE);
	Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
	Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	Device->SetRenderState(D3DRS_LIGHTING, FALSE);
}

void Setup_Matrices()
{
	D3DXMatrixLookAtRH(&View_Matrix, &D3DXVECTOR3 (Camera_Position.x, Camera_Position.y, Camera_Position.z), &D3DXVECTOR3 (Camera_Angle.x, Camera_Angle.y, Camera_Angle.z), &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
	Device->SetTransform(D3DTS_VIEW, &View_Matrix);

	D3DXMatrixPerspectiveFovRH(&Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
	Device->SetTransform(D3DTS_PROJECTION, &Projection_Matrix);
}

CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, DWORD Color)
{
	CUSTOM_VERTEX Vertex;

	Vertex.X = X;
	Vertex.Y = Y;
	Vertex.Z = Z;
	Vertex.Color = Color;

	return Vertex;
}

void Create_Polygon()
{
	Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
	Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
	Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
	Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
}

void Draw_Polygon()
{
	Device->SetFVF(CUSTOM_VERTEX_FORMAT);
	Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));
}

void Render()
{
	Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	Device->BeginScene();
	//Rendering code goes here
	Setup_Matrices();
	Create_Polygon();
	Draw_Polygon();
	Device->EndScene();
	Device->Present(NULL, NULL, NULL, NULL);
}

void Game_Loop()
{
	while (Running == true)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
		{
			if (WM_QUIT == msg.message) break;
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
		else
			Mouse_Controls();
			Keyboard_Controls();
			Render();
	}
}

void Main()
{
	Camera_Position.x = 0.0f;
	Camera_Position.y = 0.0f;
	Camera_Position.z = 250.0f;

	DirectX_Initialize();
	DirectInput_Initialize();
	DirectInput_Initialize_Mouse();
	DirectInput_Initialize_Keyboard();
	Settings();
	Setup_Matrices();
	Running = true;
}

void Shutdown()
{
	Running = false;
	Mouse_Device->Release();
	Keyboard_Device->Release();
	Device->Release();
	D3D->Release();
	PostQuitMessage (0);
	HANDLE Process;
	Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
	TerminateProcess(Process , 0);
}

int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
	
	WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hinstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL};
	RegisterClassEx(&wc);
	
	if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES)
	   Fullscreen_Enabled = true;

	if (Fullscreen_Enabled == true)
		hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
	else
		hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
	hInstance = hinstance;
	ShowWindow (hWnd, nCmdShow);

	if (Fullscreen_Enabled == true)
	{
		Width = 800;
		Height = 600;
	}
	else
	{
		Width = 330;
		Height = 250;
	}

	Main();
	Game_Loop();
	return msg.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_DESTROY:
			Shutdown();
			break;
		default:
			return DefWindowProc (hWnd, msg, wParam, lParam);
	}
	return 0;
}


Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 5041

Like
0Likes
Like

Posted 19 February 2012 - 04:08 PM

DirectInput as a means of getting keyboard and mouse states is generally not recommended anymore (by microsoft or anyone really).

It simply creates a thread and pumps windows messages to generate the state. Something you can do in your main message handler on your own.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#3 Evil Steve   Members   -  Reputation: 1959

Like
0Likes
Like

Posted 19 February 2012 - 04:57 PM

What Washu said.

As for the actual problem, what is the *exact* error you get, what line does it occur on, and are you sure your interfaces are all created? (An issue you wouldn't have with window messages...)

Steve Macpherson
Senior programmer, Firebrand Games


#4 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 19 February 2012 - 05:51 PM

This is where I get the error:
Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);

Located over in the routine Mouse_Controls() in the first line like I said. And the error I receive is this:

Unhandled exception at 0x003a187c in DX Tut.exe: 0xC0000005: Access violation reading location 0x42480000.

Everythings fine with the windows messages and interfaces.

#5 programci_84   Members   -  Reputation: 335

Like
0Likes
Like

Posted 20 February 2012 - 02:37 AM

Are you sure that mouse device is created successfully? Why don't you check return values in DirectInput_Initialize(), DirectInput_Initialize_Mouse() functions? Like this:
HRESULT DirectInput_Initialize()
{
	HRESULT hrD = DirectInput8Create (hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&amp;DI, 0);
	return hrD;
}

HRESULT DirectInput_Initialize_Mouse()
{
	HRESULT hrM;
	hrM = DI->CreateDevice (...);
	if (SUCCEEDED(hrM))
	{
		hrM = Mouse_Device->SetDataFormat (...);
		hrM = Mouse_Device->SetCooperativeLevel ( ... );
		hrM = Mouse_Device->Acquire ();
	}

	return hrM;
}

While debugging step-by-step, you can check the values hrD and hrM.

btw, like the others said, you must not use DirectInput for neither mouse nor keyboard. Standard window message handling is better. Also, you can try raw input. Take a look at here and here.

hth.
-R
There's no "hard", and "the impossible" takes just a little time.

#6 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 20 February 2012 - 11:28 PM

Yes I tried that and sure enough it passed:

HRESULT DirectInput_Initialize()
{
	HRESULT hr;

	hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL);
	return hr;
}

HRESULT DirectInput_Initialize_Mouse()
{
	HRESULT hr;

	hr = DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL);

	if (SUCCEEDED(hr))
	{
		hr = Mouse_Device->SetDataFormat(&c_dfDIMouse);
		hr = Mouse_Device->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
		hr = Mouse_Device->Acquire();
	}
	else if (FAILED(hr))
	{
		return 0;
	}

	return hr;
}

Yet for some reason it still doesn't wanna GetDeviceState. I know I can use windows messaging to handle controls, but the only reason I'm sticking with directinput is cause I have the same programs written in C#, VB6, and VB.Net, which all work and all have a similar code structure and wanna keep it all consistant:

C# 2008 / 2010 version
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;

namespace DX_Tut
{
	public partial class frmMain : Form
	{
		private const float FOV = (float)Math.PI / 4.0f;
		private const float ASPECT_RATIO = 4.0f / 3.0f;
		private const float NEAR_Z = 1.0f;
		private const float FAR_Z = 10000.0f;

		Microsoft.DirectX.Direct3D.Device Device;
		DisplayMode Display_Mode;
		PresentParameters Screen;

		bool Fullscreen_Enabled;
		bool Running;

		CustomVertex.PositionColored[] Vertex_List = new CustomVertex.PositionColored[4];

		Matrix View_Matrix;
		Matrix Projection_Matrix;

		Vector3 Camera_Position;
		Vector3 Camera_Angle;

		Microsoft.DirectX.DirectInput.Device Mouse_Device;
		MouseState Mouse_State;
		Vector2 Mouse;

		Microsoft.DirectX.DirectInput.Device Keyboard_Device;
		KeyboardState Keyboard_State;

		public void DirectX_Initialize()
		{
			Screen = new PresentParameters();
			if (Fullscreen_Enabled == true)
			{
				Display_Mode.Width = 800;
				Display_Mode.Height = 600;
				Display_Mode.Format = Format.R5G6B5;
				Screen.Windowed = false;
				Screen.BackBufferCount = 1;
				Screen.BackBufferWidth = Display_Mode.Width;
				Screen.BackBufferHeight = Display_Mode.Height;
			}
			else
			{
				Screen.Windowed = true;
			}
			Screen.SwapEffect = SwapEffect.Copy;
			Screen.BackBufferFormat = Display_Mode.Format;
			Screen.AutoDepthStencilFormat = DepthFormat.D16;
			Screen.EnableAutoDepthStencil = true;

			Device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen);
		}

		void DirectInput_Initialize_Mouse()
		{
			Mouse_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);
			Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse);
			Mouse_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
			Mouse_Device.Acquire();
		}

		void Mouse_Controls()
		{
			Mouse_State = Mouse_Device.CurrentMouseState;

			Mouse.X += Mouse_State.X;
			Mouse.Y += Mouse_State.Y;

			if (Mouse.X > this.Width) Mouse.X = this.Width;
			if (Mouse.X < 0) Mouse.X = 0;

			if (Mouse.Y > this.Height) Mouse.Y = this.Height;
			if (Mouse.Y < 0) Mouse.Y = 0;

			Camera_Angle.X += Mouse_State.Y;
			Camera_Angle.Y += Mouse_State.X;
		}

		void DirectInput_Initialize_Keyboard()
		{
			Keyboard_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
			Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard);
			Keyboard_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
			Keyboard_Device.Acquire();
		}

		bool DirectInput_Key_State(Microsoft.DirectX.DirectInput.Key Key_Code)
		{
			Keyboard_State = Keyboard_Device.GetCurrentKeyboardState();
			return Keyboard_State[Key_Code];
		}

		void Keyboard_Controls()
		{
			const int Camera_Speed = 3;

			if (DirectInput_Key_State(Key.W)) //Move Forward
			{
				Camera_Position.X = Convert.ToSingle(Camera_Position.X - Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
				Camera_Position.Z = Convert.ToSingle(Camera_Position.Z - Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
			}

			if (DirectInput_Key_State(Key.S)) //Move Backward
			{
				Camera_Position.X = Convert.ToSingle(Camera_Position.X + Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
				Camera_Position.Z = Convert.ToSingle(Camera_Position.Z + Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
			}

			if (DirectInput_Key_State(Key.A)) //Move Left
			{
				Camera_Position.X = Convert.ToSingle(Camera_Position.X + Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
				Camera_Position.Z = Convert.ToSingle(Camera_Position.Z - Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
			}

			if (DirectInput_Key_State(Key.D)) //Move Right
			{
				Camera_Position.X = Convert.ToSingle(Camera_Position.X - Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
				Camera_Position.Z = Convert.ToSingle(Camera_Position.Z + Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed);
			}

			if (DirectInput_Key_State(Key.Q)) //Look Left
				Camera_Angle.Y -= 1;

			if (DirectInput_Key_State(Key.E)) //Look Right
				Camera_Angle.Y += 1;

			if (DirectInput_Key_State(Key.R)) //Look Up
				Camera_Angle.X -= 1;

			if (DirectInput_Key_State(Key.F)) //Look Down
				Camera_Angle.X += 1;

			if (DirectInput_Key_State(Key.C)) //Move Up
				Camera_Position.Y -= Camera_Speed;

			if (DirectInput_Key_State(Key.Z)) //Move Down
				Camera_Position.Y += Camera_Speed;
		}

		void Settings()
		{
			Device.SetRenderState(RenderStates.ZEnable, true);
			Device.SetRenderState(RenderStates.ZBufferWriteEnable, true);
			Device.SetRenderState(RenderStates.CullMode, (int)Cull.None);
			Device.SetRenderState(RenderStates.Lighting, false);
		}

		void Setup_Matrices()
		{
			View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0));
			Device.Transform.View = View_Matrix;

			Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
			Device.Transform.Projection = Projection_Matrix;
		}

		Vector3 Create_Vertex(float X, float Y, float Z)
		{
			Vector3 Vertex;

			Vertex.X = X;
			Vertex.Y = Y;
			Vertex.Z = Z;

			return Vertex;
		}

		CustomVertex.PositionColored Create_Custom_Vertex(float X, float Y, float Z, int Color)
		{
			CustomVertex.PositionColored Vertex = new CustomVertex.PositionColored();
			Vertex.Position = new Vector3(X, Y, Z);
			Vertex.Color = Color;

			return Vertex;
		}

		void Reset_Device()
		{
			if (this.WindowState != FormWindowState.Minimized && Fullscreen_Enabled == false)
			{
				Running = false;
				Device.Reset(Screen);
				Settings();
				Setup_Matrices();
				Application.DoEvents();
				Running = true;
			}
		}

		void Camera_Control()
		{
			Matrix Camera_Translation_Matrix;
			Matrix Camera_Angle_Matrix_X;
			Matrix Camera_Angle_Matrix_Y;

			View_Matrix = Matrix.Identity;
			Camera_Translation_Matrix = Matrix.Identity;

			Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z);
			View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix);

			Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180);
			View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y);

			Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180);
			View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X);

			Device.Transform.View = View_Matrix;
		}

		void Create_Polygon()
		{
			Vertex_List[0] = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb());
			Vertex_List[1] = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb());
			Vertex_List[2] = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb());
			Vertex_List[3] = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb());
		}

		void Draw_Polygon()
		{
			Device.VertexFormat = CustomVertex.PositionColored.Format;
			Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List);
		}

		void Create_Platform()
		{
			Vertex_List[0] = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb());
			Vertex_List[1] = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb());
			Vertex_List[2] = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb());
			Vertex_List[3] = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb());
		}

		public void Render()
		{
			Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1.0f, 0);
			Device.BeginScene();
			Create_Platform();
			Draw_Polygon();
			Create_Polygon();
			Draw_Polygon();
			Device.EndScene();
			Device.Present();
		}

		public void Game_Loop()
		{
			do
			{
				Mouse_Controls();
				Keyboard_Controls();
				Camera_Control();
				Render();
				if (DirectInput_Key_State(Key.Escape)) Shutdown();
				Application.DoEvents();
			} while (Running == true);
		}

		public void Main()
		{
			if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				Fullscreen_Enabled = true;

			this.Show();
			this.KeyPreview = true;
			this.Width = 330;
			this.Height = 250;
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
			this.Text = "DirectX Tutorial";
			if (Fullscreen_Enabled == true)
				this.FormBorderStyle = FormBorderStyle.None;

			Camera_Position.X = 0;
			Camera_Position.Y = -50;
			Camera_Position.Z = 250;

			DirectX_Initialize();
			Settings();
			DirectInput_Initialize_Mouse();
			DirectInput_Initialize_Keyboard();
			Setup_Matrices();
			Running = true;
		}

		public void Shutdown()
		{
			Running = false;
			Mouse_Device.Dispose();
			Keyboard_Device.Dispose();
			Device.Dispose();
			Application.Exit();
		}

		public frmMain()
		{
			InitializeComponent();
		}

		private void frmMain_Load(object sender, EventArgs e)
		{
			Main();
		}

		private void frmMain_Paint(object sender, PaintEventArgs e)
		{
			Game_Loop();
		}

		private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
		{
			Shutdown();
		}

		private void frmMain_Resize(object sender, EventArgs e)
		{
			Reset_Device();
		}
	}
}

VB.Net 2008 / 2010 version
Option Explicit On
Option Strict On

Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.DirectInput

Public Class frmMain

	Private Const FOV As Single = Math.PI / 4
	Private Const ASPECT_RATIO As Single = 4 / 3
	Private Const NEAR_Z As Single = 1
	Private Const FAR_Z As Single = 10000

	Private Device As Direct3D.Device
	Private Display_Mode As DisplayMode
	Private Screen As New PresentParameters

	Private Fullscreen_Enabled As Boolean
	Private Running As Boolean

	Private Vertex_List(5) As CustomVertex.PositionColored

	Private View_Matrix As Matrix
	Private Projection_Matrix As Matrix

	Private Camera_Position As Vector3
	Private Camera_Angle As Vector3

	Private Mouse_Device As DirectInput.Device
	Private Mouse_State As MouseState
	Private Mouse As Vector2

	Private Keyboard_Device As DirectInput.Device
	Private Keyboard_State As KeyboardState

	Private Sub DirectX_Initialize()

		If Fullscreen_Enabled = True Then
			Display_Mode.Width = 800
			Display_Mode.Height = 600
			Display_Mode.Format = Direct3D.Format.R5G6B5
			Screen.Windowed = False
			Screen.BackBufferCount = 1
			Screen.BackBufferWidth = Display_Mode.Width
			Screen.BackBufferHeight = Display_Mode.Height
		Else
			Screen.Windowed = True
		End If

		Screen.SwapEffect = SwapEffect.Copy
		Screen.BackBufferFormat = Display_Mode.Format
		Screen.AutoDepthStencilFormat = DepthFormat.D16
		Screen.EnableAutoDepthStencil = True

		Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)

	End Sub

	Private Sub DirectInput_Initialize_Mouse()

		Mouse_Device = New DirectInput.Device(SystemGuid.Mouse)
		Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse)
		Mouse_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
		Mouse_Device.Acquire()

	End Sub

	Private Sub Mouse_Controls()

		Mouse_State = Mouse_Device.CurrentMouseState

		Mouse.X = Mouse.X + Mouse_State.X
		Mouse.Y = Mouse.Y + Mouse_State.Y

		If Mouse.X > Me.Width Then Mouse.X = Me.Width
		If Mouse.X < 0 Then Mouse.X = 0

		If Mouse.Y > Me.Height Then Mouse.Y = Me.Height
		If Mouse.Y < 0 Then Mouse.Y = 0

		Camera_Angle.X = Camera_Angle.X + Mouse_State.Y
		Camera_Angle.Y = Camera_Angle.Y + Mouse_State.X

	End Sub

	Private Sub DirectInput_Initialize_Keyboard()

		Keyboard_Device = New DirectInput.Device(SystemGuid.Keyboard)
		Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard)
		Keyboard_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
		Keyboard_Device.Acquire()

	End Sub

	Private Function DirectInput_Key_State(ByVal Key_Code As DirectInput.Key) As Boolean

		Keyboard_State = Keyboard_Device.GetCurrentKeyboardState
		Return Keyboard_State.Item(Key_Code)

	End Function

	Private Sub Keyboard_Controls()

		Const Camera_Speed As Long = 3

		If DirectInput_Key_State(Key.W) Then 'Move Forward
			Camera_Position.X = Convert.ToSingle(Camera_Position.X - Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
			Camera_Position.Z = Convert.ToSingle(Camera_Position.Z - Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
		End If

		If DirectInput_Key_State(Key.S) Then 'Move Backward
			Camera_Position.X = Convert.ToSingle(Camera_Position.X + Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
			Camera_Position.Z = Convert.ToSingle(Camera_Position.Z + Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
		End If

		If DirectInput_Key_State(Key.A) Then 'Move Left
			Camera_Position.X = Convert.ToSingle(Camera_Position.X + Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
			Camera_Position.Z = Convert.ToSingle(Camera_Position.Z - Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
		End If

		If DirectInput_Key_State(Key.D) Then 'Move Right
			Camera_Position.X = Convert.ToSingle(Camera_Position.X - Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
			Camera_Position.Z = Convert.ToSingle(Camera_Position.Z + Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed)
		End If

		If DirectInput_Key_State(Key.Q) Then 'Look Left
			Camera_Angle.Y = Camera_Angle.Y - 1
		End If

		If DirectInput_Key_State(Key.E) Then 'Look Right
			Camera_Angle.Y = Camera_Angle.Y + 1
		End If

		If DirectInput_Key_State(Key.R) Then 'Look Up
			Camera_Angle.X = Camera_Angle.X - 1
		End If

		If DirectInput_Key_State(Key.F) Then 'Look Down
			Camera_Angle.X = Camera_Angle.X + 1
		End If

		If DirectInput_Key_State(Key.C) Then 'Move Up
			Camera_Position.Y = Camera_Position.Y - Camera_Speed
		End If

		If DirectInput_Key_State(Key.Z) Then 'Move Down
			Camera_Position.Y = Camera_Position.Y + Camera_Speed
		End If

	End Sub

	Private Sub Settings()

		Device.SetRenderState(RenderStates.ZEnable, True)
		Device.SetRenderState(RenderStates.ZBufferWriteEnable, True)
		Device.SetRenderState(RenderStates.CullMode, Cull.None)
		Device.SetRenderState(RenderStates.Lighting, False)

	End Sub

	Private Sub Reset_Device()

		If Me.WindowState <> FormWindowState.Minimized And Fullscreen_Enabled = False Then
			Running = False
			Device.Reset(Screen)
			Settings()
			Setup_Matrices()
			Application.DoEvents()
			Running = True
		End If

	End Sub

	Private Function Create_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Vector3

		Dim Vertex As Vector3

		Vertex.X = X
		Vertex.Y = Y
		Vertex.Z = Z

		Return Vertex

	End Function

	Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer) As CustomVertex.PositionColored

		Dim Vertex As CustomVertex.PositionColored = New CustomVertex.PositionColored

		Vertex.Position = New Vector3(X, Y, Z)
		Vertex.Color = Color

		Return Vertex

	End Function

	Private Sub Setup_Matrices()

		View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0))
		Device.Transform.View = View_Matrix

		Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z)
		Device.Transform.Projection = Projection_Matrix

	End Sub

	Private Sub Camera_Control()

		Dim Camera_Translation_Matrix As Matrix
		Dim Camera_Angle_Matrix_X As Matrix
		Dim Camera_Angle_Matrix_Y As Matrix

		View_Matrix = Matrix.Identity
		Camera_Translation_Matrix = Matrix.Identity

		Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z)
		View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix)

		Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180)
		View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y)

		Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180)
		View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X)

		Device.Transform.View = View_Matrix

	End Sub

	Private Sub Create_Polygon()

		Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
		Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
		Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
		Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)

	End Sub

	Private Sub Draw_Polygon()

		Device.VertexFormat = CustomVertex.PositionColored.Format
		Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)

	End Sub

	Private Sub Create_Platform()

		Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb)
		Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb)
		Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb)
		Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb)

	End Sub

	Private Sub Render()

		Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.FromArgb(255, 0, 0, 0), 1, 0)
		Device.BeginScene()
		Create_Polygon()
		Draw_Polygon()
		Create_Platform()
		Draw_Polygon()
		Device.EndScene()
		Device.Present()

	End Sub

	Private Sub Game_Loop()

		Do While Running = True
			Mouse_Controls()
			Keyboard_Controls()
			Camera_Control()
			Render()
			If DirectInput_Key_State(Key.Escape) Then Shutdown()
			Application.DoEvents()
		Loop

	End Sub

	Private Sub Main()

		If MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
			Fullscreen_Enabled = True
		End If

		With Me
			.Show()
			.Width = 330
			.Height = 250
			.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
			.Text = "DirectX Tutorial"
			If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
		End With

		Camera_Position.X = 0
		Camera_Position.Y = -50
		Camera_Position.Z = 250

		DirectX_Initialize()
		DirectInput_Initialize_Mouse()
		DirectInput_Initialize_Keyboard()
		Settings()
		Setup_Matrices()
		Running = True

	End Sub

	Private Sub Shutdown()

		Running = False
		Mouse_Device.Dispose()
		Keyboard_Device.Dispose()
		Device.Dispose()
		Application.Exit()

	End Sub

	Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		Main()

	End Sub

	Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

		Shutdown()

	End Sub

	Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

		Game_Loop()

	End Sub

	Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

		Reset_Device()

	End Sub

End Class

VB6 Version
Option Explicit

Private Type CUSTOM_VERTEX

	X As Single
	Y As Single
	Z  As Single
	Color As Long
	
End Type

Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZ Or D3DFVF_DIFFUSE

Private Const PI As Single = 3.141592654
Private Const FOV As Single = PI / 4
Private Const ASPECT_RATIO As Single = 3 / 4
Private Const NEAR_Z As Single = 1
Private Const FAR_Z As Single = 10000

Private DX As DirectX8
Private D3D As Direct3D8
Private Device As Direct3DDevice8
Private Display_Mode As D3DDISPLAYMODE
Private Screen As D3DPRESENT_PARAMETERS

Private DI As DirectInput8

Private Keyboard_Device As DirectInputDevice8
Private Keyboard_State As DIKEYBOARDSTATE

Private Mouse_Device As DirectInputDevice8
Private Mouse_State As DIMOUSESTATE
Private Mouse As D3DVECTOR2

Private Fullscreen_Enabled As Boolean
Private Running As Boolean

Private Vertex_List(3) As CUSTOM_VERTEX

Private View_Matrix As D3DMATRIX
Private Projection_Matrix As D3DMATRIX

Private Camera_Position As D3DVECTOR
Private Camera_Angle As D3DVECTOR

Private Sub DirectX_Initialize()

	Set DX = New DirectX8
	Set D3D = DX.Direct3DCreate()
	
	If Fullscreen_Enabled = True Then
		Display_Mode.Width = 800
		Display_Mode.Height = 600
		Display_Mode.Format = D3DFMT_R5G6B5
		Screen.Windowed = False
		Screen.BackBufferCount = 1
		Screen.BackBufferWidth = Display_Mode.Width
		Screen.BackBufferHeight = Display_Mode.Height
		Screen.hDeviceWindow = frmMain.hWnd
	Else
		D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode
		Screen.Windowed = True
	End If
	
	Screen.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
	Screen.BackBufferFormat = Display_Mode.Format
	Screen.AutoDepthStencilFormat = D3DFMT_D16
	Screen.EnableAutoDepthStencil = 1

	Set Device = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Screen)
	
End Sub

Private Sub DirectInput_Initialize_Mouse(Window As Form)
	
	Set DI = DX.DirectInputCreate
	Set Mouse_Device = DI.CreateDevice("GUID_SYSMOUSE")
	Mouse_Device.SetCommonDataFormat DIFORMAT_MOUSE
	Mouse_Device.SetCooperativeLevel Window.hWnd, DISCL_FOREGROUND Or DISCL_EXCLUSIVE
	Mouse_Device.Acquire
	
End Sub

Private Sub Mouse_Controls()

	Mouse_Device.GetDeviceStateMouse Mouse_State
	
	Mouse.X = Mouse.X + Mouse_State.lX
	Mouse.Y = Mouse.Y + Mouse_State.lY
	
	If Mouse.X > frmMain.ScaleWidth Then Mouse.X = frmMain.ScaleWidth
	If Mouse.X < 0 Then Mouse.X = 0
	
	If Mouse.Y > frmMain.ScaleHeight Then Mouse.Y = frmMain.ScaleHeight
	If Mouse.Y < 0 Then Mouse.Y = 0
	
	Camera_Angle.X = Camera_Angle.X + Mouse_State.lY
	Camera_Angle.Y = Camera_Angle.Y + Mouse_State.lX

End Sub

Private Sub DirectInput_Initialize_Keyboard(Window As Form)
	
	Set DI = DX.DirectInputCreate
	Set Keyboard_Device = DI.CreateDevice("GUID_SysKeyboard")
	Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD
	Keyboard_Device.SetCooperativeLevel Window.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
	Keyboard_Device.Acquire

End Sub

Private Function DirectInput_Key_State(Key_Code As Long) As Long

	Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
	DirectInput_Key_State = Keyboard_State.Key(Key_Code)

End Function

Private Sub Keyboard_Controls()
	
	Const Camera_Speed As Long = 3
	
	If DirectInput_Key_State(DIK_W) Then 'Move Forward
		Camera_Position.X = Camera_Position.X - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
		Camera_Position.Z = Camera_Position.Z - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
	End If
	
	If DirectInput_Key_State(DIK_S) Then 'Move Backward
		Camera_Position.X = Camera_Position.X + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
		Camera_Position.Z = Camera_Position.Z + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
	End If
	
	If DirectInput_Key_State(DIK_A) Then 'Move Left
		Camera_Position.X = Camera_Position.X + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
		Camera_Position.Z = Camera_Position.Z - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
	End If
	
	If DirectInput_Key_State(DIK_D) Then 'Move Right
		Camera_Position.X = Camera_Position.X - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
		Camera_Position.Z = Camera_Position.Z + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
	End If
	
	If DirectInput_Key_State(DIK_Q) Then 'Look Left
		Camera_Angle.Y = Camera_Angle.Y - 1
	End If
	
	If DirectInput_Key_State(DIK_E) Then 'Look Right
		Camera_Angle.Y = Camera_Angle.Y + 1
	End If
	
	If DirectInput_Key_State(DIK_R) Then 'Look Up
		Camera_Angle.X = Camera_Angle.X - 1
	End If
	
	If DirectInput_Key_State(DIK_F) Then 'Look Down
		Camera_Angle.X = Camera_Angle.X + 1
	End If
	
	If DirectInput_Key_State(DIK_C) Then 'Move Up
		Camera_Position.Y = Camera_Position.Y - Camera_Speed
	End If
	
	If DirectInput_Key_State(DIK_Z) Then 'Move Down
		Camera_Position.Y = Camera_Position.Y + Camera_Speed
	End If

End Sub

Private Sub Settings()

	Device.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE
	Device.SetRenderState D3DRS_ZWRITEENABLE, 1
	Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
	Device.SetRenderState D3DRS_LIGHTING, 0

End Sub

Private Function Create_Vertex(X As Single, Y As Single, Z As Single) As D3DVECTOR

	Create_Vertex.X = X
	Create_Vertex.Y = Y
	Create_Vertex.Z = Z
	
End Function

Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Long) As CUSTOM_VERTEX

	Create_Custom_Vertex.X = X
	Create_Custom_Vertex.Y = Y
	Create_Custom_Vertex.Z = Z
	Create_Custom_Vertex.Color = Color
	
End Function

Private Sub Setup_Matrices()
	
	D3DXMatrixLookAtRH View_Matrix, Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0)
	Device.SetTransform D3DTS_VIEW, View_Matrix
	
	D3DXMatrixPerspectiveFovRH Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z
	Device.SetTransform D3DTS_PROJECTION, Projection_Matrix

End Sub

Public Sub Camera_Control()

	Dim Camera_Translation_Matrix As D3DMATRIX
	Dim Camera_Angle_Matrix_X As D3DMATRIX
	Dim Camera_Angle_Matrix_Y As D3DMATRIX
	
	D3DXMatrixIdentity View_Matrix
	D3DXMatrixIdentity Camera_Translation_Matrix
	
	D3DXMatrixTranslation Camera_Translation_Matrix, Camera_Position.X, Camera_Position.Y, -Camera_Position.Z
	D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Translation_Matrix

	D3DXMatrixRotationY Camera_Angle_Matrix_Y, (PI * Camera_Angle.Y) / 180
	D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_Y

	D3DXMatrixRotationX Camera_Angle_Matrix_X, (PI * Camera_Angle.X) / 180
	D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_X
	
	Device.SetTransform D3DTS_VIEW, View_Matrix
	
End Sub

Private Sub Create_Polygon()

	Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, D3DColorXRGB(255, 255, 255))
	Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, D3DColorXRGB(255, 255, 255))
	Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, D3DColorXRGB(255, 255, 255))
	Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, D3DColorXRGB(255, 255, 255))

End Sub

Private Sub Create_Platform()

	Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, D3DColorXRGB(255, 0, 0))
	Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, D3DColorXRGB(0, 255, 0))
	Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, D3DColorXRGB(0, 0, 255))
	Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, D3DColorXRGB(255, 0, 255))

End Sub

Private Sub Draw_Polygon()

	Device.SetVertexShader CUSTOM_VERTEX_FORMAT
	Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))

End Sub

Private Sub Render()

	Device.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, D3DColorXRGB(0, 0, 0), 1#, 0
	Device.BeginScene
	'Rendering code goes here
	Create_Platform
	Draw_Polygon
	Create_Polygon
	Draw_Polygon
	Device.EndScene
	Device.Present ByVal 0, ByVal 0, 0, ByVal 0

End Sub

Private Sub Game_Loop()

	Do While Running = True
		Mouse_Controls
		Keyboard_Controls
		Camera_Control
		Render
		If DirectInput_Key_State(DIK_ESCAPE) <> 0 Then Shutdown
		DoEvents
	Loop

End Sub

Private Sub Main()

	If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True

	With Me
		If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone
		.Show
		.Caption = "DirectX Tutorial"
	End With
	
	Camera_Position.X = 0
	Camera_Position.Y = -50
	Camera_Position.Z = 250
	
	DirectX_Initialize
	Settings
	DirectInput_Initialize_Mouse frmMain
	DirectInput_Initialize_Keyboard frmMain
	Setup_Matrices
	Running = True
	Game_Loop
	
End Sub

Private Sub Shutdown()

	Running = False
	Set Mouse_Device = Nothing
	Set Keyboard_Device = Nothing
	Set DI = Nothing
	Set Device = Nothing
	Set D3D = Nothing
	Set DX = Nothing
	Unload Me

End Sub

Private Sub Form_Load()

	Main

End Sub

Private Sub Form_Unload(Cancel As Integer)

	Shutdown

End Sub


And as you can see it has that similar structure between all of em.

#7 Overdrive   Members   -  Reputation: 187

Like
0Likes
Like

Posted 21 February 2012 - 06:04 AM

This is where I get the error:
Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);

Located over in the routine Mouse_Controls() in the first line like I said. And the error I receive is this:

Unhandled exception at 0x003a187c in DX Tut.exe: 0xC0000005: Access violation reading location 0x42480000.

Everythings fine with the windows messages and interfaces.


I would assume that
Mouse_Device is null

#8 Evil Steve   Members   -  Reputation: 1959

Like
0Likes
Like

Posted 21 February 2012 - 07:01 AM


This is where I get the error:
Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);

Located over in the routine Mouse_Controls() in the first line like I said. And the error I receive is this:

Unhandled exception at 0x003a187c in DX Tut.exe: 0xC0000005: Access violation reading location 0x42480000.

Everythings fine with the windows messages and interfaces.


I would assume that
Mouse_Device is null

If it was null, then it'd be an access violation reading 0x00000000. 0x4xxxxxxx looks a bit suspicious for a pointer value, particularly since the low 16 bits are all 0 (Which is perfectly valid, but it's suspicious).

@OP: What does the debugger tell you on that line? What are the values of Mouse_Device and &Mouse_State? Are you sure that Mouse_Device hasn't been Release()d already?

I know I can use windows messaging to handle controls, but the only reason I'm sticking with directinput is cause I have the same programs written in C#, VB6, and VB.Net, which all work and all have a similar code structure and wanna keep it all consistant

So long as you're not even thinking of using the mouse input for ever doing GUI-style input then it's not too bad. But if you're going to ever display a cursor on-screen, then you *really, really, REALLY* need to ditch DirectInput or else you'll annoy the hell out of your users (Google pointer ballistics and you'll see what I mean - DirectInput has none, "Normal" mouse input does).

Steve Macpherson
Senior programmer, Firebrand Games


#9 Adam_42   Crossbones+   -  Reputation: 2465

Like
2Likes
Like

Posted 21 February 2012 - 08:31 AM

The reason 0x42480000 looks suspicious is that it's the integer representation of 50.0f.

If you search the code you'll see that 50.0f is used initializing the Vertex_List array in the Create_Polygon() function.

A quick inspection of the array declaration says you're going off the end of the array:

CUSTOM_VERTEX Vertex_List[3];

void Create_Polygon()
{
		Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
}


#10 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 21 February 2012 - 08:58 AM

The reason 0x42480000 looks suspicious is that it's the integer representation of 50.0f.

If you search the code you'll see that 50.0f is used initializing the Vertex_List array in the Create_Polygon() function.

A quick inspection of the array declaration says you're going off the end of the array:

CUSTOM_VERTEX Vertex_List[3];

void Create_Polygon()
{
		Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
}


Wow you were right! I changed it to CUSTOM_VERTEX Vertex_List[4]; and the Mouse part worked! Thank you so much. Problem solved :D

#11 Evil Steve   Members   -  Reputation: 1959

Like
0Likes
Like

Posted 21 February 2012 - 03:19 PM

The reason 0x42480000 looks suspicious is that it's the integer representation of 50.0f.

If you search the code you'll see that 50.0f is used initializing the Vertex_List array in the Create_Polygon() function.

A quick inspection of the array declaration says you're going off the end of the array:

CUSTOM_VERTEX Vertex_List[3];

void Create_Polygon()
{
		Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
		Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
}

Wow, that's magnificent debug-fu :D

Steve Macpherson
Senior programmer, Firebrand Games


#12 Adam_42   Crossbones+   -  Reputation: 2465

Like
0Likes
Like

Posted 21 February 2012 - 06:22 PM

Thanks Posted Image

I did cheat a bit use a debugger for one thing there - typing 0x42480000,f into a Visual Studio watch window does the hex to float conversion and tells you that it's 50.0f. I didn't compile or run the code myself though.

The more usual way of debugging it would be to spot that the pointer had changed since you initialized it, and then rerun it and put a data breakpoint on it just after initialization.

However that's not always possible, especially if the memory that gets trashed isn't in a consistent location, so working backwards from the data that it's been trashed with can come in handy sometimes. For example when what you have is a one-off crash dump instead of a reproducible bug.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS