From eb59407ba2baf5875fa96732832260a801f12f75 Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 7 Apr 2024 14:48:27 -0700 Subject: [PATCH] Try to render a cube and add a Win32 version --- XboxProject2.sln | 3 + XboxProject2.vcproj | 340 ++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 212 ++++++++++++++++++++++++--- stdafx.h | 7 + 4 files changed, 540 insertions(+), 22 deletions(-) diff --git a/XboxProject2.sln b/XboxProject2.sln index 96e5363..86afa4a 100644 --- a/XboxProject2.sln +++ b/XboxProject2.sln @@ -6,6 +6,7 @@ EndProject Global GlobalSection(SolutionConfiguration) = preSolution Debug = Debug + Debug Win32 = Debug Win32 Profile = Profile Profile_FastCap = Profile_FastCap Release = Release @@ -14,6 +15,8 @@ Global GlobalSection(ProjectConfiguration) = postSolution {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Debug.ActiveCfg = Debug|Xbox {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Debug.Build.0 = Debug|Xbox + {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Debug Win32.ActiveCfg = Debug|Win32 + {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Debug Win32.Build.0 = Debug|Win32 {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Profile.ActiveCfg = Profile|Xbox {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Profile.Build.0 = Profile|Xbox {65C32018-7565-47A4-BE2A-8217AC6C15CD}.Profile_FastCap.ActiveCfg = Profile_FastCap|Xbox diff --git a/XboxProject2.vcproj b/XboxProject2.vcproj index ec360a6..e42a2ec 100644 --- a/XboxProject2.vcproj +++ b/XboxProject2.vcproj @@ -4,10 +4,13 @@ Version="7.10" Name="XboxProject2" ProjectGUID="{65C32018-7565-47A4-BE2A-8217AC6C15CD}" + RootNamespace="XboxProject2" Keyword="XboxProj"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/main.cpp b/main.cpp index 0b34d63..df5af6c 100644 --- a/main.cpp +++ b/main.cpp @@ -15,17 +15,82 @@ struct Vertex #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, 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)} + {-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(3 * sizeof(Vertex), + HRESULT vertexBufferCreation = g_pd3dDevice->CreateVertexBuffer(36 * sizeof(Vertex), D3DUSAGE_WRITEONLY, D3DFVF_VERTEX, D3DPOOL_MANAGED, &g_pVB); if (FAILED(vertexBufferCreation)) return E_FAIL; @@ -36,14 +101,35 @@ HRESULT InitVertexBuffer() if (FAILED(g_pVB->Lock(0, 0, (BYTE**)&pVertices, 0))) return E_FAIL; // Copy vertices to the GPU - memcpy(pVertices, g_Vertices, 3 * sizeof(Vertex)); + memcpy(pVertices, g_Vertices, 36 * sizeof(Vertex)); // Unlock the Vertex Buffer g_pVB->Unlock(); return S_OK; } -HRESULT InitD3D8() +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); @@ -64,26 +150,56 @@ HRESULT InitD3D8() d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE_OR_IMMEDIATE; // Create the D3D8 device with the device settings (present parameters). - HRESULT deviceCreation = g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL, + HRESULT deviceCreation; + deviceCreation = g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice); if (FAILED(deviceCreation)) + { return E_FAIL; + } - // Set the projection matrix - D3DXMATRIX matProj; - D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 4.0f/3.0f, 1.0f, 200.0f); - g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj); - - // Set the view matrix - D3DXVECTOR3 vEyePt = D3DXVECTOR3(0.0f, 0.0f, -7.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); + 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() { @@ -96,15 +212,17 @@ VOID Render() 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, 1); + g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12); g_pd3dDevice->EndScene(); } -void __cdecl main() + +#ifdef _XBOX +void XboxMain() { // Initialize Direct3D 8.1 - if (FAILED(InitD3D8())) + if (FAILED(InitD3D8(NULL))) return; // Initialize the Vertex Buffer @@ -117,4 +235,54 @@ void __cdecl main() 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 } diff --git a/stdafx.h b/stdafx.h index 2913f50..4b273cf 100644 --- a/stdafx.h +++ b/stdafx.h @@ -1,4 +1,11 @@ #ifndef STDAFX_H #define STDAFX_H +#ifdef _XBOX #include +#else +#include +#include +#include +#include +#endif #endif \ No newline at end of file