c++ Code:
#include <windows.h>
#include <d3d8.h>
struct CUSTOM_VERTEX
{
float X, Y, Z, RHW;
DWORD Color;
};
#define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
const D3DFORMAT COLOR_DEPTH_16_BIT = D3DFMT_R5G6B5;
const D3DFORMAT COLOR_DEPTH_24_BIT = D3DFMT_A8R8G8B8;
const D3DFORMAT COLOR_DEPTH_32_BIT = D3DFMT_X8R8G8B8;
LPDIRECT3D8 Direct3D = NULL;
LPDIRECT3DDEVICE8 Direct3D_Device = NULL;
D3DDISPLAYMODE Display_Mode;
D3DPRESENT_PARAMETERS Direct3D_Window;
HWND hWnd;
MSG msg;
bool Fullscreen_Enabled;
bool Running;
CUSTOM_VERTEX Vertex_List[3];
CUSTOM_VERTEX Create_Custom_Vertex();
void Create_Polygon();
void Draw_Polygon();
void Render();
void Game_Loop();
void Main();
void Shutdown();
void DirectX8_Initialize();
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, float RHW, DWORD Color)
{
CUSTOM_VERTEX Vertex;
Vertex.X = X;
Vertex.Y = Y;
Vertex.Z = Z;
Vertex.RHW = RHW;
Vertex.Color = Color;
return Vertex;
}
void DirectX8_Initialize()
{
Direct3D = Direct3DCreate8(D3D_SDK_VERSION);
memset(&Direct3D_Window, 0, sizeof(D3DPRESENT_PARAMETERS));
if (Fullscreen_Enabled == true)
{
Display_Mode.Width = 800;
Display_Mode.Height = 600;
Display_Mode.Format = COLOR_DEPTH_16_BIT;
Direct3D_Window.Windowed = FALSE;
Direct3D_Window.BackBufferCount = 1;
Direct3D_Window.BackBufferWidth = Display_Mode.Width;
Direct3D_Window.BackBufferHeight = Display_Mode.Height;
Direct3D_Window.hDeviceWindow = hWnd;
}
else
{
Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);
Direct3D_Window.Windowed = TRUE;
}
Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY;
Direct3D_Window.BackBufferFormat = Display_Mode.Format;
Direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Direct3D_Window, &Direct3D_Device);
}
void Create_Polygon()
{
Vertex_List[0] = Create_Custom_Vertex(0, 0, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));
Vertex_List[1] = Create_Custom_Vertex(100, 0, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));
Vertex_List[2] = Create_Custom_Vertex(0, 100, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));
Vertex_List[3] = Create_Custom_Vertex(100, 100, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));
}
void Draw_Polygon()
{
Direct3D_Device->SetVertexShader(CUSTOM_VERTEX_FORMAT);
Direct3D_Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));
}
void Render()
{
Direct3D_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0); // clear frame
Direct3D_Device->BeginScene();
Create_Polygon();
Draw_Polygon();
Direct3D_Device->EndScene();
Direct3D_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
Render();
}
}
void Main()
{
DirectX8_Initialize();
Running = true;
}
void Shutdown()
{
Running = false;
Direct3D_Device->Release();
Direct3D->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);
ShowWindow (hWnd, nCmdShow);
Main();
Game_Loop();
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Shutdown();
break;
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
{
DestroyWindow(hWnd);
return(0);
}
default:
return DefWindowProc (hWnd, msg, wParam, lParam);
}
return 0;
}
vb Code:
Option Explicit
Private Type CUSTOM_VERTEX
X As Single
Y As Single
Z As Single
RHW As Single
Color As Long
End Type
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZRHW Or D3DFVF_DIFFUSE
Private DX As DirectX8
Private Direct3D As Direct3D8
Private Direct3D_Device As Direct3DDevice8
Private Display_Mode As D3DDISPLAYMODE
Private Direct3D_Window As D3DPRESENT_PARAMETERS
Private Fullscreen_Enabled As Boolean
Private Running As Boolean
Private Vertex_List(3) As CUSTOM_VERTEX
Private Function Create_Custom_Vertex(X As Single, Y As Single, Z As Single, RHW As Single, Color As Long) As CUSTOM_VERTEX
Create_Custom_Vertex.X = X
Create_Custom_Vertex.Y = Y
Create_Custom_Vertex.Z = Z
Create_Custom_Vertex.RHW = RHW
Create_Custom_Vertex.Color = Color
End Function
Private Sub DirectX_Initialize()
Set DX = New DirectX8
Set Direct3D = DX.Direct3DCreate()
If Fullscreen_Enabled = True Then
Display_Mode.Width = 800
Display_Mode.Height = 600
Display_Mode.Format = COLOR_DEPTH_16_BIT
Direct3D_Window.Windowed = False
Direct3D_Window.BackBufferCount = 1
Direct3D_Window.BackBufferWidth = Display_Mode.Width
Direct3D_Window.BackBufferHeight = Display_Mode.Height
Direct3D_Window.hDeviceWindow = frmMain.hWnd
Else
Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode
Direct3D_Window.Windowed = True
End If
Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
Direct3D_Window.BackBufferFormat = Display_Mode.Format
Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
End Sub
Private Sub Create_Polygon()
Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, 1, D3DColorRGBA(255, 255, 255, 255))
Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, 1, D3DColorRGBA(255, 255, 255, 255))
Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, 1, D3DColorRGBA(255, 255, 255, 255))
Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, 1, D3DColorRGBA(255, 255, 255, 255))
End Sub
Private Sub Draw_Polygon()
Direct3D_Device.SetVertexShader CUSTOM_VERTEX_FORMAT
Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
End Sub
Private Sub Render()
Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 255), 1#, 0
Direct3D_Device.BeginScene
Create_Polygon
Draw_Polygon
Direct3D_Device.EndScene
Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub
Private Sub Game_Loop()
Do While Running = True
Render
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
.Show
.Caption = "DirectX Tutorial"
If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone
End With
DirectX_Initialize
Running = True
Game_Loop
End Sub
Private Sub Shut_Down()
Running = False
Set Direct3D_Device = Nothing
Set Direct3D = Nothing
Set DX = Nothing
Unload Me
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then Shut_Down
End Sub
Private Sub Form_Load()
Main
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shut_Down
End Sub