From 1ecccf3c9929aa4c64b0054a5ba0798ba5e0e48f Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Wed, 10 May 2023 17:28:36 +0200 Subject: [PATCH] First steps Rendering a blue background using DirectX9 --- premake5.lua | 2 +- src/main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/premake5.lua b/premake5.lua index cab7973..ecc92a8 100644 --- a/premake5.lua +++ b/premake5.lua @@ -7,7 +7,7 @@ project "LearnDirectX" targetdir "bin/%{cfg.buildcfg}" architecture "x64" - links {"glfw3"} + links {"glfw3", "d3d9"} libdirs{ "vendor/GLFW/lib"} includedirs {"vendor/GLFW/include"} diff --git a/src/main.cpp b/src/main.cpp index 0602213..176edab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,47 @@ #include +#include +#define GLFW_EXPOSE_NATIVE_WIN32 #include "GLFW/glfw3.h" +#include "GLFW/glfw3native.h" +#include +LPDIRECT3D9 d3d; // pointer to our Direct3D interface +LPDIRECT3DDEVICE9 d3ddev; // pointer to the device class + +void initD3D(HWND hWnd) { + d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface + + D3DPRESENT_PARAMETERS d3dpp; // create the strcut to hold device info + + ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear the struct + d3dpp.Windowed = TRUE; + d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; + d3dpp.hDeviceWindow = hWnd; + + // create a device class using the information gathered + d3d->CreateDevice(D3DADAPTER_DEFAULT, + D3DDEVTYPE_HAL, + hWnd, + D3DCREATE_SOFTWARE_VERTEXPROCESSING, + &d3dpp, + &d3ddev); +} + + +void render_frame() { + d3ddev->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0); + d3ddev->BeginScene(); + // do 3D rendering + d3ddev->EndScene(); + + d3ddev->Present(NULL, NULL, NULL, NULL); +} + +void cleanD3D() { + d3ddev->Release(); + d3d->Release(); +} + + void error_callback(int code , const char* description) { std::cerr << description << std::endl; @@ -19,20 +61,28 @@ int main ( int argc , char** argv){ glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); window = glfwCreateWindow(640, 480, "LearnDirectX", nullptr, nullptr); - - - if (!window) { std::cout << "Failed to create window!" << std::endl; glfwTerminate(); return -1; } + + HWND NativeWindowHandle = glfwGetWin32Window(window); + + + initD3D(NativeWindowHandle); + + + + while (!glfwWindowShouldClose(window)) { - + render_frame(); + glfwPollEvents(); } + cleanD3D(); glfwTerminate();