Skip to content

Commit 7cd5a4d

Browse files
committed
Add project 1-1-2-HelloD3D11-Raw
1 parent d6337b9 commit 7cd5a4d

File tree

8 files changed

+351
-0
lines changed

8 files changed

+351
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<VCProjectVersion>16.0</VCProjectVersion>
15+
<Keyword>Win32Proj</Keyword>
16+
<ProjectGuid>{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2}</ProjectGuid>
17+
<RootNamespace>My12HelloD3D11</RootNamespace>
18+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
22+
<ConfigurationType>Application</ConfigurationType>
23+
<UseDebugLibraries>true</UseDebugLibraries>
24+
<PlatformToolset>v143</PlatformToolset>
25+
<CharacterSet>Unicode</CharacterSet>
26+
</PropertyGroup>
27+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
28+
<ConfigurationType>Application</ConfigurationType>
29+
<UseDebugLibraries>false</UseDebugLibraries>
30+
<PlatformToolset>v143</PlatformToolset>
31+
<WholeProgramOptimization>true</WholeProgramOptimization>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
35+
<ImportGroup Label="ExtensionSettings">
36+
</ImportGroup>
37+
<ImportGroup Label="Shared">
38+
</ImportGroup>
39+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
40+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
41+
</ImportGroup>
42+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
43+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
44+
</ImportGroup>
45+
<PropertyGroup Label="UserMacros" />
46+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
47+
<LinkIncremental>true</LinkIncremental>
48+
<IncludePath>$(SolutionDir)..\lib\glfw-3.3.6\include\;$(IncludePath)</IncludePath>
49+
<LibraryPath>$(SolutionDir)..\lib\glfw-3.3.6\lib-vc2022\;$(LibraryPath)</LibraryPath>
50+
<OutDir>bin\$(Configuration)\</OutDir>
51+
<IntDir>obj\$(Configuration)\</IntDir>
52+
</PropertyGroup>
53+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
54+
<LinkIncremental>false</LinkIncremental>
55+
<IncludePath>$(SolutionDir)..\lib\glfw-3.3.6\include\;$(IncludePath)</IncludePath>
56+
<LibraryPath>$(SolutionDir)..\lib\glfw-3.3.6\lib-vc2022\;$(LibraryPath)</LibraryPath>
57+
<OutDir>bin\$(Configuration)\</OutDir>
58+
<IntDir>obj\$(Configuration)\</IntDir>
59+
</PropertyGroup>
60+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
61+
<ClCompile>
62+
<WarningLevel>Level3</WarningLevel>
63+
<SDLCheck>true</SDLCheck>
64+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
65+
<ConformanceMode>true</ConformanceMode>
66+
<LanguageStandard>stdcpp17</LanguageStandard>
67+
</ClCompile>
68+
<Link>
69+
<SubSystem>Console</SubSystem>
70+
<GenerateDebugInformation>true</GenerateDebugInformation>
71+
<AdditionalDependencies>glfw3.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
72+
</Link>
73+
<PostBuildEvent>
74+
<Command>mkdir $(OutDir)Assets\Models\
75+
mkdir $(OutDir)Assets\Shaders\
76+
mkdir $(OutDir)Assets\Textures\
77+
xcopy /Y $(ProjectDir)Assets\Models\*.* $(OutDir)\Assets\Models\
78+
xcopy /Y $(ProjectDir)Assets\Shaders\*.* $(OutDir)Assets\Shaders\
79+
xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
80+
</PostBuildEvent>
81+
</ItemDefinitionGroup>
82+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<ClCompile>
84+
<WarningLevel>Level3</WarningLevel>
85+
<FunctionLevelLinking>true</FunctionLevelLinking>
86+
<IntrinsicFunctions>true</IntrinsicFunctions>
87+
<SDLCheck>true</SDLCheck>
88+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
89+
<ConformanceMode>true</ConformanceMode>
90+
<LanguageStandard>stdcpp17</LanguageStandard>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
95+
<OptimizeReferences>true</OptimizeReferences>
96+
<GenerateDebugInformation>true</GenerateDebugInformation>
97+
<AdditionalDependencies>glfw3.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
98+
</Link>
99+
<PostBuildEvent>
100+
<Command>mkdir $(OutDir)Assets\Models\
101+
mkdir $(OutDir)Assets\Shaders\
102+
mkdir $(OutDir)Assets\Textures\
103+
xcopy /Y $(ProjectDir)Assets\Models\*.* $(OutDir)\Assets\Models\
104+
xcopy /Y $(ProjectDir)Assets\Shaders\*.* $(OutDir)Assets\Shaders\
105+
xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
106+
</PostBuildEvent>
107+
</ItemDefinitionGroup>
108+
<ItemGroup>
109+
<ClCompile Include="Main.cpp" />
110+
</ItemGroup>
111+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
112+
<ImportGroup Label="ExtensionTargets">
113+
</ImportGroup>
114+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="Main.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ShowAllFiles>true</ShowAllFiles>
5+
</PropertyGroup>
6+
</Project>

src/Cpp/1-getting-started/1-1-2-HelloD3D11-Raw/Assets/Models/.gitkeep

Whitespace-only changes.

src/Cpp/1-getting-started/1-1-2-HelloD3D11-Raw/Assets/Shaders/.gitkeep

Whitespace-only changes.

src/Cpp/1-getting-started/1-1-2-HelloD3D11-Raw/Assets/Textures/.gitkeep

Whitespace-only changes.
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#include <d3d11_2.h>
2+
#include <wrl.h>
3+
4+
#include <GLFW/glfw3.h>
5+
#define GLFW_EXPOSE_NATIVE_WIN32
6+
#include <GLFW/glfw3native.h>
7+
8+
#include <string_view>
9+
#include <iostream>
10+
11+
#pragma comment(lib, "d3d11.lib")
12+
#pragma comment(lib, "dxgi.lib")
13+
#pragma comment(lib, "d3dcompiler.lib")
14+
#pragma comment(lib, "winmm.lib")
15+
#pragma comment(lib, "dxguid.lib")
16+
17+
template <typename T>
18+
using ComPtr = Microsoft::WRL::ComPtr<T>;
19+
20+
int32_t g_Width = 0;
21+
int32_t g_Height = 0;
22+
23+
ComPtr<ID3D11Device> g_Device = nullptr;
24+
ComPtr<ID3D11DeviceContext> g_DeviceContext = nullptr;
25+
ComPtr<IDXGIFactory2> g_DxgiFactory = nullptr;
26+
ComPtr<IDXGISwapChain1> g_SwapChain = nullptr;
27+
ComPtr<ID3D11RenderTargetView> g_RenderTarget = nullptr;
28+
29+
bool CreateSwapchainResources()
30+
{
31+
ComPtr<ID3D11Texture2D> backBuffer = nullptr;
32+
if (FAILED(g_SwapChain->GetBuffer(
33+
0,
34+
IID_PPV_ARGS(&backBuffer))))
35+
{
36+
std::cout << "D3D11: Failed to get Back Buffer from the SwapChain\n";
37+
return false;
38+
}
39+
40+
if (FAILED(g_Device->CreateRenderTargetView(
41+
backBuffer.Get(),
42+
nullptr,
43+
&g_RenderTarget)))
44+
{
45+
std::cout << "D3D11: Failed to create RTV from Back Buffer\n";
46+
return false;
47+
}
48+
49+
return true;
50+
}
51+
52+
void DestroySwapchainResources()
53+
{
54+
g_RenderTarget.Reset();
55+
}
56+
57+
void HandleResize(
58+
GLFWwindow* window,
59+
const int32_t width,
60+
const int32_t height)
61+
{
62+
g_Width = width;
63+
g_Height = height;
64+
65+
g_DeviceContext->Flush();
66+
67+
DestroySwapchainResources();
68+
if (FAILED(g_SwapChain->ResizeBuffers(
69+
0,
70+
width,
71+
height,
72+
DXGI_FORMAT::DXGI_FORMAT_B8G8R8A8_UNORM,
73+
0)))
74+
{
75+
std::cout << "D3D11: Failed to recreate SwapChain buffers\n";
76+
return;
77+
}
78+
79+
CreateSwapchainResources();
80+
}
81+
82+
int main()
83+
{
84+
if (!glfwInit())
85+
{
86+
std::cout << "Unable to initialize GLFW\n";
87+
return false;
88+
}
89+
90+
GLFWmonitor* primaryMonitor = glfwGetPrimaryMonitor();
91+
const GLFWvidmode* videoMode = glfwGetVideoMode(primaryMonitor);
92+
g_Width = static_cast<int32_t>(videoMode->width * 0.9f);
93+
g_Height = static_cast<int32_t>(videoMode->height * 0.9f);
94+
95+
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE);
96+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
97+
GLFWwindow* window = glfwCreateWindow(
98+
g_Width,
99+
g_Height,
100+
"LearnD3D11 - HelloD3D11",
101+
nullptr,
102+
nullptr);
103+
104+
if (window == nullptr)
105+
{
106+
std::cout << "GLFW: Unable to create window\n";
107+
glfwTerminate();
108+
return false;
109+
}
110+
111+
const int32_t windowLeft = videoMode->width / 2 - g_Width / 2;
112+
const int32_t windowTop = videoMode->height / 2 - g_Height / 2;
113+
glfwSetWindowPos(window, windowLeft, windowTop);
114+
115+
glfwSetFramebufferSizeCallback(window, HandleResize);
116+
117+
// This section initializes DirectX's devices and SwapChain
118+
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&g_DxgiFactory))))
119+
{
120+
std::cout << "DXGI: Unable to create DXGIFactory\n";
121+
return false;
122+
}
123+
124+
constexpr D3D_FEATURE_LEVEL deviceFeatureLevel = D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0;
125+
126+
if (FAILED(D3D11CreateDevice(
127+
nullptr,
128+
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE,
129+
nullptr,
130+
0,
131+
&deviceFeatureLevel,
132+
1,
133+
D3D11_SDK_VERSION,
134+
&g_Device,
135+
nullptr,
136+
&g_DeviceContext)))
137+
{
138+
std::cout << "D3D11: Failed to create Device and Device Context\n";
139+
return false;
140+
}
141+
142+
DXGI_SWAP_CHAIN_DESC1 swapChainDescriptor = {};
143+
swapChainDescriptor.Width = g_Width;
144+
swapChainDescriptor.Height = g_Height;
145+
swapChainDescriptor.Format = DXGI_FORMAT::DXGI_FORMAT_B8G8R8A8_UNORM;
146+
swapChainDescriptor.SampleDesc.Count = 1;
147+
swapChainDescriptor.SampleDesc.Quality = 0;
148+
swapChainDescriptor.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
149+
swapChainDescriptor.BufferCount = 2;
150+
swapChainDescriptor.SwapEffect = DXGI_SWAP_EFFECT::DXGI_SWAP_EFFECT_FLIP_DISCARD;
151+
swapChainDescriptor.Scaling = DXGI_SCALING::DXGI_SCALING_STRETCH;
152+
swapChainDescriptor.Flags = {};
153+
154+
DXGI_SWAP_CHAIN_FULLSCREEN_DESC swapChainFullscreenDescriptor = {};
155+
swapChainFullscreenDescriptor.Windowed = true;
156+
157+
if (FAILED(g_DxgiFactory->CreateSwapChainForHwnd(
158+
g_Device.Get(),
159+
glfwGetWin32Window(window),
160+
&swapChainDescriptor,
161+
&swapChainFullscreenDescriptor,
162+
nullptr,
163+
&g_SwapChain)))
164+
{
165+
std::cout << "DXGI: Failed to create SwapChain\n";
166+
return false;
167+
}
168+
169+
if (!CreateSwapchainResources())
170+
{
171+
return false;
172+
}
173+
174+
while (!glfwWindowShouldClose(window))
175+
{
176+
glfwPollEvents();
177+
178+
D3D11_VIEWPORT viewport = {};
179+
viewport.TopLeftX = 0;
180+
viewport.TopLeftY = 0;
181+
viewport.Width = g_Width;
182+
viewport.Height = g_Height;
183+
viewport.MinDepth = 0.0f;
184+
viewport.MaxDepth = 1.0f;
185+
186+
constexpr float clearColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };
187+
188+
g_DeviceContext->ClearRenderTargetView(
189+
g_RenderTarget.Get(),
190+
clearColor);
191+
g_DeviceContext->RSSetViewports(
192+
1,
193+
&viewport);
194+
g_DeviceContext->OMSetRenderTargets(
195+
1,
196+
g_RenderTarget.GetAddressOf(),
197+
nullptr);
198+
g_SwapChain->Present(1, 0);
199+
}
200+
201+
return 0;
202+
}

src/LearnD3D11.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1-3-5-RasterizerState", "Cp
5757
EndProject
5858
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1-3-6-Camera", "Cpp\1-getting-started\1-3-6-Camera\1-3-6-Camera.vcxproj", "{E38C17E3-29B9-40BF-9372-0EB24DDE4B06}"
5959
EndProject
60+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1-1-2-HelloD3D11-Raw", "Cpp\1-getting-started\1-1-2-HelloD3D11-Raw\1-1-2-HelloD3D11-Raw.vcxproj", "{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2}"
61+
EndProject
6062
Global
6163
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6264
Debug|x64 = Debug|x64
@@ -131,6 +133,10 @@ Global
131133
{E38C17E3-29B9-40BF-9372-0EB24DDE4B06}.Debug|x64.Build.0 = Debug|x64
132134
{E38C17E3-29B9-40BF-9372-0EB24DDE4B06}.Release|x64.ActiveCfg = Release|x64
133135
{E38C17E3-29B9-40BF-9372-0EB24DDE4B06}.Release|x64.Build.0 = Release|x64
136+
{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2}.Debug|x64.ActiveCfg = Debug|x64
137+
{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2}.Debug|x64.Build.0 = Debug|x64
138+
{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2}.Release|x64.ActiveCfg = Release|x64
139+
{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2}.Release|x64.Build.0 = Release|x64
134140
EndGlobalSection
135141
GlobalSection(SolutionProperties) = preSolution
136142
HideSolutionNode = FALSE
@@ -156,6 +162,7 @@ Global
156162
{B2D92817-E897-4797-9864-C1470F217F1E} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
157163
{1B35D552-7FE9-43AD-B0BC-7375D96034A5} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
158164
{E38C17E3-29B9-40BF-9372-0EB24DDE4B06} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
165+
{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2} = {69E36221-7B54-4891-AB39-B9F2C5025815}
159166
EndGlobalSection
160167
GlobalSection(ExtensibilityGlobals) = postSolution
161168
SolutionGuid = {839E559F-F97F-45D6-986E-121DDB0297EA}

0 commit comments

Comments
 (0)