1
0

First steps

Rendering a blue background using DirectX9
This commit is contained in:
2023-05-10 17:28:36 +02:00
parent 9b923834e1
commit 1ecccf3c99
2 changed files with 55 additions and 5 deletions

View File

@@ -7,7 +7,7 @@ project "LearnDirectX"
targetdir "bin/%{cfg.buildcfg}" targetdir "bin/%{cfg.buildcfg}"
architecture "x64" architecture "x64"
links {"glfw3"} links {"glfw3", "d3d9"}
libdirs{ "vendor/GLFW/lib"} libdirs{ "vendor/GLFW/lib"}
includedirs {"vendor/GLFW/include"} includedirs {"vendor/GLFW/include"}

View File

@@ -1,5 +1,47 @@
#include <iostream> #include <iostream>
#include <Windows.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "GLFW/glfw3native.h"
#include <d3d9.h>
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) { void error_callback(int code , const char* description) {
std::cerr << description << std::endl; std::cerr << description << std::endl;
@@ -19,20 +61,28 @@ int main ( int argc , char** argv){
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
window = glfwCreateWindow(640, 480, "LearnDirectX", nullptr, nullptr); window = glfwCreateWindow(640, 480, "LearnDirectX", nullptr, nullptr);
if (!window) { if (!window) {
std::cout << "Failed to create window!" << std::endl; std::cout << "Failed to create window!" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
HWND NativeWindowHandle = glfwGetWin32Window(window);
initD3D(NativeWindowHandle);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
render_frame();
glfwPollEvents(); glfwPollEvents();
} }
cleanD3D();
glfwTerminate(); glfwTerminate();