Commit 8b194540 authored by Administrator's avatar Administrator
Browse files

added Scene and Stage; locked camera Y angle range; added debug for framerate;...

added Scene and Stage; locked camera Y angle range; added debug for framerate; generally fit for all window size
parent b682ae99
......@@ -25,13 +25,8 @@ void CoreHandler::CloseCoreHandler()
ImGui_ImplDX11_Shutdown();
}
void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger)
void CoreHandler::processEvents(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger)
{
// render imgui
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// Process event
while (!Window::mouse.IsQueueEmpty())
{
......@@ -48,6 +43,15 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger&
{
cameraDeltaX = Window::mouse.GetRightDragDeltaX();
cameraDeltaY = Window::mouse.GetRightDragDeltaY();
if (cameraY + cameraDeltaY >= 80)
{
cameraDeltaY = 80 - cameraY;
}
if (cameraY + cameraDeltaY <= -80)
{
cameraDeltaY = -80 - cameraY;
}
}
oss << " | ViewAt(" << cameraX << "," << cameraY << ")";
oss << " | Dragging(" << cameraDeltaX << "," << cameraDeltaY << ")";
......@@ -105,6 +109,17 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger&
}
}
}
}
void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger)
{
// render imgui
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// Process IO events
processEvents(window, gfx, hWnd, logger);
// Display background and set depthstencil
window.GetGfx().RendorFrame();
......@@ -116,7 +131,11 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger&
window.DrawScenes(window.GetGfx(), logger);
// render imgui
ImGui::ShowDemoWindow();
auto imIO = ImGui::GetIO();
ImGui::Text("camera Y : [ %.2f ]", cameraY);
ImGui::Text("camera Delta Y : [ %.2f ]", cameraDeltaY);
ImGui::Text("Framerate : [ %.2f ]", imIO.Framerate);
ImGui::Text("MS per Frame : [ %.3f ]", 1000.0f / imIO.Framerate);
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
......
......@@ -11,6 +11,7 @@ public:
CoreHandler(Window& window, Logger& logger);
void InitCoreHandler(Window& window, Logger& logger);
void CloseCoreHandler();
void processEvents(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger);
void CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger);
void SetCamera(Window& window, Logger& logger);
private:
......
......@@ -5,16 +5,19 @@
/************************************
* 构造函数Graphics
************************************/
Graphics::Graphics(HWND hWnd)
Graphics::Graphics(HWND hWnd, int width, int height)
:
width(width),
height(height)
{
// 初始化D3D
InitD3D(hWnd);
InitD3D(hWnd, width, height);
}
/************************************
* 1 初始化D3D
************************************/
void Graphics::InitD3D(HWND hWnd)
void Graphics::InitD3D(HWND hWnd, int width, int height)
{
/************************************
* 1.1 CreateDeviceAndSwapChain
......@@ -82,8 +85,8 @@ void Graphics::InitD3D(HWND hWnd)
// Create the depth stencil texture
D3D11_TEXTURE2D_DESC depthStencilTextureDesc = {};
depthStencilTextureDesc.Width = 800;
depthStencilTextureDesc.Height = 600;
depthStencilTextureDesc.Width = width;
depthStencilTextureDesc.Height = height;
depthStencilTextureDesc.MipLevels = 1u;
depthStencilTextureDesc.ArraySize = 1u;
depthStencilTextureDesc.Format = DXGI_FORMAT_D32_FLOAT;
......@@ -145,8 +148,8 @@ void Graphics::EndFrame(void)
// 控制View port的位置
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 800;
viewport.Height = 600;
viewport.Width = width;
viewport.Height = height;
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
......
......@@ -16,7 +16,7 @@ class Graphics
friend class Bindable;
public:
// 构造函数
Graphics(HWND hWnd);
Graphics(HWND hWnd, int width, int height);
// 单例设计模式,禁止复制
Graphics(const Graphics&) = delete;
......@@ -24,7 +24,7 @@ public:
~Graphics() = default;
// 初始化3大件,Device/SwapChain/deviceContext
void InitD3D(HWND hWnd);
void InitD3D(HWND hWnd, int width, int heigh);
void RendorFrame(void);
void EndFrame(void);
......@@ -48,6 +48,8 @@ public:
wrl::ComPtr<ID3D11DeviceContext> getContext();
private:
int width;
int height;
DirectX::XMMATRIX projection;
// 指向Device的指针
wrl::ComPtr<ID3D11Device> pDevice;
......
......@@ -144,6 +144,8 @@
<ClCompile Include="imgui\imgui_widgets.cpp" />
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="Sampler.cpp" />
<ClCompile Include="Scene.cpp" />
<ClCompile Include="Stage.cpp" />
<ClCompile Include="Surface.cpp" />
<ClCompile Include="IndexBuffer.cpp" />
<ClCompile Include="InputLayout.cpp" />
......@@ -208,6 +210,8 @@
<ClInclude Include="imgui\imstb_truetype.h" />
<ClInclude Include="Mouse.h" />
<ClInclude Include="Sampler.h" />
<ClInclude Include="Scene.h" />
<ClInclude Include="Stage.h" />
<ClInclude Include="Surface.h" />
<ClInclude Include="IndexBuffer.h" />
<ClInclude Include="InputLayout.h" />
......
......@@ -147,6 +147,12 @@
<ClCompile Include="imgui\imgui_widgets.cpp">
<Filter>imgui</Filter>
</ClCompile>
<ClCompile Include="Scene.cpp">
<Filter>Source Files\Scenes</Filter>
</ClCompile>
<ClCompile Include="Stage.cpp">
<Filter>Source Files\Scenes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<FxCompile Include="PixelShader.hlsl">
......@@ -259,5 +265,11 @@
<ClInclude Include="imgui\imstb_truetype.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="Scene.h">
<Filter>Header Files\Scenes</Filter>
</ClInclude>
<ClInclude Include="Stage.h">
<Filter>Header Files\Scenes</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#pragma once
#include "Scene.h"
#include "Drawable.h"
void Scene::DrawSections(
Graphics& gfx,
Logger& logger) noexcept
{
for (auto& pSection : pSections)
{
// shape->Update(dt); TODO
pSection->DrawShapes(gfx, logger);
}
};
\ No newline at end of file
#pragma once
#include <vector>
#include "Graphics.h"
#include "Logger.h"
#include "Section.h"
class Scene
{
public:
Scene() = default;
Scene(const Scene&) = delete;
void DrawSections(
Graphics& gfx,
Logger& logger) noexcept;
~Scene() = default;
public:
std::vector<std::unique_ptr<Section>> pSections;
};
\ No newline at end of file
#pragma once
#include "Stage.h"
#include "Surface.h"
#include "Ground.h"
Stage::Stage(Graphics& gfx, Logger& logger) noexcept
{
pSections.push_back(std::make_unique<Ground>(gfx, logger));
};
#pragma once
#include "Scene.h"
class Stage : public Scene
{
public:
Stage(Graphics& gfx, Logger& logger) noexcept;
};
\ No newline at end of file
......@@ -21,7 +21,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
HWND hWnd = nullptr;
// register window
Window window(hWnd, hInstance, nCmdShow, 800, 600, logger);
Window window(hWnd, hInstance, nCmdShow, 1280, 800, logger);
// init imgui
ImGui::CreateContext();
......
#include "Window.h"
#include "Ground.h"
#include "Stage.h"
#include "imgui/imgui.h"
Mouse Window::mouse;
......@@ -47,13 +48,13 @@ Window::Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Log
ShowWindow(hWnd, nCmdShow);
// Init graphics
pGfx = std::make_unique<Graphics>(hWnd);
pGfx = std::make_unique<Graphics>(hWnd, x, y);
// Set projection
pGfx->SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, 3.0f / 4.0f, 0.5f, 40.0f));
pGfx->SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, float(y) / float(x), 0.5f, 40.0f));
// Init scenes
this->InitScene(logger);
this->InitScenes(logger);
}
LRESULT CALLBACK Window::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
......@@ -136,19 +137,20 @@ Graphics& Window::GetGfx()
return *pGfx;
}
void Window::InitScene(Logger &logger)
void Window::InitScenes(Logger &logger)
{
//Ground ground(*pGfx, logger);
pSections.push_back(make_unique<Ground>(*pGfx, logger));
pStage = make_shared<Stage>(*pGfx, logger);
pScenes.push_back(pStage);
}
void Window::DrawScenes(
Graphics& gfx,
Logger& logger) noexcept
{
for (auto& section : pSections)
for (auto& pScene : pScenes)
{
section->DrawShapes(gfx, logger);
pScene->DrawSections(gfx, logger);
}
};
......
......@@ -6,6 +6,7 @@
#include <vector>
#include "Section.h"
#include "Mouse.h"
#include "Stage.h"
using namespace std;
......@@ -22,7 +23,7 @@ public:
Graphics& GetGfx();
// Put all scene initialization here
void InitScene(Logger& logger);
void InitScenes(Logger& logger);
// Draw all scenes
void DrawScenes(Graphics& gfx, Logger& logger) noexcept;
......@@ -40,5 +41,6 @@ public:
private:
unique_ptr<Graphics> pGfx;
vector<unique_ptr<Section>> pSections;
vector<shared_ptr<Scene>> pScenes;
shared_ptr<Stage> pStage;
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment