288 lines
6.7 KiB
C++
288 lines
6.7 KiB
C++
#include "stdafx.h"
|
|
|
|
// D3D object
|
|
LPDIRECT3D8 g_pD3D = NULL;
|
|
// D3D device
|
|
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL;
|
|
// D3D Vertex Buffer
|
|
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL;
|
|
|
|
struct Vertex
|
|
{
|
|
FLOAT x, y, z;
|
|
D3DCOLOR color;
|
|
};
|
|
|
|
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
|
|
|
|
#ifndef _XBOX
|
|
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
{
|
|
switch (msg)
|
|
{
|
|
case WM_DESTROY:
|
|
PostQuitMessage(0);
|
|
return 0;
|
|
}
|
|
|
|
return DefWindowProc(hWnd, msg, wParam, lParam);
|
|
}
|
|
|
|
HWND CreateGameWindow() {
|
|
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL),
|
|
NULL, NULL, NULL, NULL, "Cube", NULL};
|
|
RegisterClassEx(&wc);
|
|
return CreateWindow("Cube", "Cube", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL);
|
|
}
|
|
#endif
|
|
|
|
HRESULT InitVertexBuffer()
|
|
{
|
|
// Vertex g_Vertices[] =
|
|
// {
|
|
// {-1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0)},
|
|
// {0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0)},
|
|
// {1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255)}
|
|
// };
|
|
|
|
Vertex g_Vertices[] =
|
|
{
|
|
{-1.0f,-1.0f,-1.0f},
|
|
{-1.0f,-1.0f, 1.0f},
|
|
{-1.0f, 1.0f, 1.0f},
|
|
{1.0f, 1.0f,-1.0f},
|
|
{-1.0f,-1.0f,-1.0f},
|
|
{-1.0f, 1.0f,-1.0f},
|
|
{1.0f,-1.0f, 1.0f},
|
|
{-1.0f,-1.0f,-1.0f},
|
|
{1.0f,-1.0f,-1.0f},
|
|
{1.0f, 1.0f,-1.0f},
|
|
{1.0f,-1.0f,-1.0f},
|
|
{-1.0f,-1.0f,-1.0f},
|
|
{-1.0f,-1.0f,-1.0f},
|
|
{-1.0f, 1.0f, 1.0f},
|
|
{-1.0f, 1.0f,-1.0f},
|
|
{1.0f,-1.0f, 1.0f},
|
|
{-1.0f,-1.0f, 1.0f},
|
|
{-1.0f,-1.0f,-1.0f},
|
|
{-1.0f, 1.0f, 1.0f},
|
|
{-1.0f,-1.0f, 1.0f},
|
|
{1.0f,-1.0f, 1.0f},
|
|
{1.0f, 1.0f, 1.0f},
|
|
{1.0f,-1.0f,-1.0f},
|
|
{1.0f, 1.0f,-1.0f},
|
|
{1.0f,-1.0f,-1.0f},
|
|
{1.0f, 1.0f, 1.0f},
|
|
{1.0f,-1.0f, 1.0f},
|
|
{1.0f, 1.0f, 1.0f},
|
|
{1.0f, 1.0f,-1.0f},
|
|
{-1.0f, 1.0f,-1.0f},
|
|
{1.0f, 1.0f, 1.0f},
|
|
{-1.0f, 1.0f,-1.0f},
|
|
{-1.0f, 1.0f, 1.0f},
|
|
{1.0f, 1.0f, 1.0f},
|
|
{-1.0f, 1.0f, 1.0f},
|
|
{1.0f,-1.0f, 1.0f}
|
|
};
|
|
|
|
for (int i = 0; i < (sizeof(g_Vertices) / sizeof(*g_Vertices)); i++) {
|
|
g_Vertices[i].color = D3DCOLOR_XRGB(255,255,0);
|
|
}
|
|
|
|
// Create a Vertex Buffer
|
|
HRESULT vertexBufferCreation = g_pd3dDevice->CreateVertexBuffer(36 * sizeof(Vertex),
|
|
D3DUSAGE_WRITEONLY, D3DFVF_VERTEX, D3DPOOL_MANAGED, &g_pVB);
|
|
if (FAILED(vertexBufferCreation))
|
|
return E_FAIL;
|
|
|
|
|
|
Vertex* pVertices;
|
|
// Lock the Vertex Buffer and retreive a pointer to it
|
|
if (FAILED(g_pVB->Lock(0, 0, (BYTE**)&pVertices, 0)))
|
|
return E_FAIL;
|
|
// Copy vertices to the GPU
|
|
memcpy(pVertices, g_Vertices, 36 * sizeof(Vertex));
|
|
// Unlock the Vertex Buffer
|
|
g_pVB->Unlock();
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
void SetupMatrices()
|
|
{
|
|
D3DXMATRIX matWorld;
|
|
D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 10.0f);
|
|
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
|
|
|
|
// Set the projection matrix
|
|
D3DXMATRIX matProj;
|
|
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f);
|
|
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
|
|
|
|
// Set the view matrix
|
|
D3DXVECTOR3 vEyePt = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
|
|
D3DXVECTOR3 vLookatPt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
|
|
D3DXVECTOR3 vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
|
|
D3DXMATRIX matView;
|
|
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUp);
|
|
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
|
|
}
|
|
|
|
#ifdef _XBOX
|
|
HRESULT InitD3D8(void* hWnd)
|
|
{
|
|
// Create D3D8 object
|
|
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
|
|
if (g_pD3D == (NULL))
|
|
return E_FAIL;
|
|
|
|
// Create D3D device settings
|
|
D3DPRESENT_PARAMETERS d3dpp;
|
|
ZeroMemory(&d3dpp, sizeof(d3dpp));
|
|
// Set the resolution to 480p (640x480).
|
|
d3dpp.BackBufferWidth = 640;
|
|
d3dpp.BackBufferHeight = 480;
|
|
d3dpp.BackBufferFormat = D3DFMT_LIN_X8R8G8B8;
|
|
d3dpp.BackBufferCount = 1;
|
|
d3dpp.EnableAutoDepthStencil = TRUE;
|
|
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
|
|
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
|
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE_OR_IMMEDIATE;
|
|
|
|
// Create the D3D8 device with the device settings (present parameters).
|
|
HRESULT deviceCreation;
|
|
deviceCreation = g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,
|
|
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);
|
|
if (FAILED(deviceCreation))
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
|
|
SetupMatrices();
|
|
|
|
return S_OK;
|
|
}
|
|
#else
|
|
HRESULT InitD3D8(HWND hWnd)
|
|
{
|
|
// Create D3D8 object
|
|
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
|
|
if (g_pD3D == (NULL))
|
|
return E_FAIL;
|
|
|
|
D3DDISPLAYMODE d3ddm;
|
|
if (FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
|
|
{
|
|
MessageBox(NULL, "Error getting adapter display mode", "", 0);
|
|
return E_FAIL;
|
|
}
|
|
|
|
// Create D3D device settings
|
|
D3DPRESENT_PARAMETERS d3dpp;
|
|
ZeroMemory(&d3dpp, sizeof(d3dpp));
|
|
d3dpp.Windowed = TRUE;
|
|
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
|
d3dpp.BackBufferFormat = d3ddm.Format;
|
|
|
|
// Create the D3D8 device with the device settings (present parameters).
|
|
HRESULT deviceCreation;
|
|
deviceCreation = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
|
|
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);
|
|
|
|
if (FAILED(deviceCreation))
|
|
{
|
|
MessageBox(NULL, "Error creating device", "", 0);
|
|
return E_FAIL;
|
|
}
|
|
|
|
SetupMatrices();
|
|
|
|
return S_OK;
|
|
}
|
|
#endif
|
|
|
|
VOID Render()
|
|
{
|
|
DWORD clearFlags = D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL;
|
|
D3DCOLOR color = D3DCOLOR_XRGB(0, 0, 0);
|
|
g_pd3dDevice->Clear(0L, NULL, clearFlags, color, 1.0f, 0L);
|
|
|
|
g_pd3dDevice->BeginScene();
|
|
|
|
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
|
|
g_pd3dDevice->SetStreamSource(0, g_pVB, sizeof(Vertex));
|
|
g_pd3dDevice->SetVertexShader(D3DFVF_VERTEX);
|
|
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);
|
|
|
|
g_pd3dDevice->EndScene();
|
|
}
|
|
|
|
|
|
#ifdef _XBOX
|
|
void XboxMain()
|
|
{
|
|
// Initialize Direct3D 8.1
|
|
if (FAILED(InitD3D8(NULL)))
|
|
return;
|
|
|
|
// Initialize the Vertex Buffer
|
|
InitVertexBuffer();
|
|
|
|
while (TRUE)
|
|
{
|
|
// Render the current scene
|
|
Render();
|
|
|
|
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
|
|
}
|
|
|
|
}
|
|
#else
|
|
void WindowsMain()
|
|
{
|
|
HWND window = CreateGameWindow();
|
|
|
|
// Initialize Direct3D 8.1
|
|
if (FAILED(InitD3D8(window)))
|
|
return;
|
|
|
|
// Initialize the Vertex Buffer
|
|
InitVertexBuffer();
|
|
|
|
ShowWindow(window, SW_SHOWDEFAULT);
|
|
UpdateWindow(window);
|
|
|
|
MSG msg;
|
|
ZeroMemory(&msg, sizeof(msg));
|
|
while (msg.message != WM_QUIT)
|
|
{
|
|
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
|
|
{
|
|
TranslateMessage(&msg);
|
|
DispatchMessage(&msg);
|
|
}
|
|
else
|
|
{
|
|
// Render the current scene
|
|
Render();
|
|
|
|
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
#ifdef _XBOX
|
|
void __cdecl main()
|
|
#else
|
|
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
|
|
#endif
|
|
{
|
|
#ifdef _XBOX
|
|
XboxMain();
|
|
#else
|
|
WindowsMain();
|
|
return 0;
|
|
#endif
|
|
}
|