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() ...@@ -25,13 +25,8 @@ void CoreHandler::CloseCoreHandler()
ImGui_ImplDX11_Shutdown(); 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 // Process event
while (!Window::mouse.IsQueueEmpty()) while (!Window::mouse.IsQueueEmpty())
{ {
...@@ -39,72 +34,92 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& ...@@ -39,72 +34,92 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger&
switch (mouseEvent.GetType()) switch (mouseEvent.GetType())
{ {
case (Mouse::MouseEvent::Type::Move): case (Mouse::MouseEvent::Type::Move):
{
std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Moved";
// Check if drag by right button
if (Window::mouse.IsRightClicked())
{ {
std::ostringstream oss; cameraDeltaX = Window::mouse.GetRightDragDeltaX();
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Moved"; cameraDeltaY = Window::mouse.GetRightDragDeltaY();
// Check if drag by right button if (cameraY + cameraDeltaY >= 80)
if (Window::mouse.IsRightClicked())
{ {
cameraDeltaX = Window::mouse.GetRightDragDeltaX(); cameraDeltaY = 80 - cameraY;
cameraDeltaY = Window::mouse.GetRightDragDeltaY(); }
if (cameraY + cameraDeltaY <= -80)
{
cameraDeltaY = -80 - cameraY;
} }
oss << " | ViewAt(" << cameraX << "," << cameraY << ")";
oss << " | Dragging(" << cameraDeltaX << "," << cameraDeltaY << ")";
oss << " | vSinValue(" << sin((cameraY + cameraDeltaY) / 100.0f) << ")";
window.SetTitle(hWnd, oss.str());
break;
} }
oss << " | ViewAt(" << cameraX << "," << cameraY << ")";
oss << " | Dragging(" << cameraDeltaX << "," << cameraDeltaY << ")";
oss << " | vSinValue(" << sin((cameraY + cameraDeltaY) / 100.0f) << ")";
window.SetTitle(hWnd, oss.str());
break;
}
case (Mouse::MouseEvent::Type::LClick): case (Mouse::MouseEvent::Type::LClick):
{ {
std::ostringstream oss; std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Left Clicked"; oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Left Clicked";
window.SetTitle(hWnd, oss.str()); window.SetTitle(hWnd, oss.str());
break; break;
} }
case (Mouse::MouseEvent::Type::LRelease): case (Mouse::MouseEvent::Type::LRelease):
{ {
std::ostringstream oss; std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Left Released"; oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Left Released";
window.SetTitle(hWnd, oss.str()); window.SetTitle(hWnd, oss.str());
break; break;
} }
case (Mouse::MouseEvent::Type::RClick): case (Mouse::MouseEvent::Type::RClick):
{ {
std::ostringstream oss; std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Right Clicked"; oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Right Clicked";
window.SetTitle(hWnd, oss.str()); window.SetTitle(hWnd, oss.str());
break; break;
} }
case (Mouse::MouseEvent::Type::RRelease): case (Mouse::MouseEvent::Type::RRelease):
{ {
std::ostringstream oss; std::ostringstream oss;
cameraX += cameraDeltaX; cameraX += cameraDeltaX;
cameraY += cameraDeltaY; cameraY += cameraDeltaY;
cameraDeltaX = 0; cameraDeltaX = 0;
cameraDeltaY = 0; cameraDeltaY = 0;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Right Released"; oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Right Released";
window.SetTitle(hWnd, oss.str()); window.SetTitle(hWnd, oss.str());
break; break;
} }
case (Mouse::MouseEvent::Type::Wheel): case (Mouse::MouseEvent::Type::Wheel):
{ {
std::ostringstream oss; std::ostringstream oss;
cameraDeltaZ = Window::mouse.GetWheelDelta(); cameraDeltaZ = Window::mouse.GetWheelDelta();
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse WheelUp"; oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse WheelUp";
oss << " | cameraDeltaZ(" << cameraDeltaZ << ")"; oss << " | cameraDeltaZ(" << cameraDeltaZ << ")";
window.SetTitle(hWnd, oss.str()); window.SetTitle(hWnd, oss.str());
break; break;
} }
} }
} }
}
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 // Display background and set depthstencil
window.GetGfx().RendorFrame(); window.GetGfx().RendorFrame();
...@@ -116,7 +131,11 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& ...@@ -116,7 +131,11 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger&
window.DrawScenes(window.GetGfx(), logger); window.DrawScenes(window.GetGfx(), logger);
// render imgui // 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::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
......
...@@ -11,6 +11,7 @@ public: ...@@ -11,6 +11,7 @@ public:
CoreHandler(Window& window, Logger& logger); CoreHandler(Window& window, Logger& logger);
void InitCoreHandler(Window& window, Logger& logger); void InitCoreHandler(Window& window, Logger& logger);
void CloseCoreHandler(); void CloseCoreHandler();
void processEvents(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger);
void CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger); void CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger);
void SetCamera(Window& window, Logger& logger); void SetCamera(Window& window, Logger& logger);
private: private:
......
...@@ -5,16 +5,19 @@ ...@@ -5,16 +5,19 @@
/************************************ /************************************
* 构造函数Graphics * 构造函数Graphics
************************************/ ************************************/
Graphics::Graphics(HWND hWnd) Graphics::Graphics(HWND hWnd, int width, int height)
:
width(width),
height(height)
{ {
// 初始化D3D // 初始化D3D
InitD3D(hWnd); InitD3D(hWnd, width, height);
} }
/************************************ /************************************
* 1 初始化D3D * 1 初始化D3D
************************************/ ************************************/
void Graphics::InitD3D(HWND hWnd) void Graphics::InitD3D(HWND hWnd, int width, int height)
{ {
/************************************ /************************************
* 1.1 CreateDeviceAndSwapChain * 1.1 CreateDeviceAndSwapChain
...@@ -82,8 +85,8 @@ void Graphics::InitD3D(HWND hWnd) ...@@ -82,8 +85,8 @@ void Graphics::InitD3D(HWND hWnd)
// Create the depth stencil texture // Create the depth stencil texture
D3D11_TEXTURE2D_DESC depthStencilTextureDesc = {}; D3D11_TEXTURE2D_DESC depthStencilTextureDesc = {};
depthStencilTextureDesc.Width = 800; depthStencilTextureDesc.Width = width;
depthStencilTextureDesc.Height = 600; depthStencilTextureDesc.Height = height;
depthStencilTextureDesc.MipLevels = 1u; depthStencilTextureDesc.MipLevels = 1u;
depthStencilTextureDesc.ArraySize = 1u; depthStencilTextureDesc.ArraySize = 1u;
depthStencilTextureDesc.Format = DXGI_FORMAT_D32_FLOAT; depthStencilTextureDesc.Format = DXGI_FORMAT_D32_FLOAT;
...@@ -145,8 +148,8 @@ void Graphics::EndFrame(void) ...@@ -145,8 +148,8 @@ void Graphics::EndFrame(void)
// 控制View port的位置 // 控制View port的位置
viewport.TopLeftX = 0; viewport.TopLeftX = 0;
viewport.TopLeftY = 0; viewport.TopLeftY = 0;
viewport.Width = 800; viewport.Width = width;
viewport.Height = 600; viewport.Height = height;
viewport.MinDepth = 0; viewport.MinDepth = 0;
viewport.MaxDepth = 1; viewport.MaxDepth = 1;
......
...@@ -16,7 +16,7 @@ class Graphics ...@@ -16,7 +16,7 @@ class Graphics
friend class Bindable; friend class Bindable;
public: public:
// 构造函数 // 构造函数
Graphics(HWND hWnd); Graphics(HWND hWnd, int width, int height);
// 单例设计模式,禁止复制 // 单例设计模式,禁止复制
Graphics(const Graphics&) = delete; Graphics(const Graphics&) = delete;
...@@ -24,7 +24,7 @@ public: ...@@ -24,7 +24,7 @@ public:
~Graphics() = default; ~Graphics() = default;
// 初始化3大件,Device/SwapChain/deviceContext // 初始化3大件,Device/SwapChain/deviceContext
void InitD3D(HWND hWnd); void InitD3D(HWND hWnd, int width, int heigh);
void RendorFrame(void); void RendorFrame(void);
void EndFrame(void); void EndFrame(void);
...@@ -48,6 +48,8 @@ public: ...@@ -48,6 +48,8 @@ public:
wrl::ComPtr<ID3D11DeviceContext> getContext(); wrl::ComPtr<ID3D11DeviceContext> getContext();
private: private:
int width;
int height;
DirectX::XMMATRIX projection; DirectX::XMMATRIX projection;
// 指向Device的指针 // 指向Device的指针
wrl::ComPtr<ID3D11Device> pDevice; wrl::ComPtr<ID3D11Device> pDevice;
......
...@@ -144,6 +144,8 @@ ...@@ -144,6 +144,8 @@
<ClCompile Include="imgui\imgui_widgets.cpp" /> <ClCompile Include="imgui\imgui_widgets.cpp" />
<ClCompile Include="Mouse.cpp" /> <ClCompile Include="Mouse.cpp" />
<ClCompile Include="Sampler.cpp" /> <ClCompile Include="Sampler.cpp" />
<ClCompile Include="Scene.cpp" />
<ClCompile Include="Stage.cpp" />
<ClCompile Include="Surface.cpp" /> <ClCompile Include="Surface.cpp" />
<ClCompile Include="IndexBuffer.cpp" /> <ClCompile Include="IndexBuffer.cpp" />
<ClCompile Include="InputLayout.cpp" /> <ClCompile Include="InputLayout.cpp" />
...@@ -208,6 +210,8 @@ ...@@ -208,6 +210,8 @@
<ClInclude Include="imgui\imstb_truetype.h" /> <ClInclude Include="imgui\imstb_truetype.h" />
<ClInclude Include="Mouse.h" /> <ClInclude Include="Mouse.h" />
<ClInclude Include="Sampler.h" /> <ClInclude Include="Sampler.h" />
<ClInclude Include="Scene.h" />
<ClInclude Include="Stage.h" />
<ClInclude Include="Surface.h" /> <ClInclude Include="Surface.h" />
<ClInclude Include="IndexBuffer.h" /> <ClInclude Include="IndexBuffer.h" />
<ClInclude Include="InputLayout.h" /> <ClInclude Include="InputLayout.h" />
......
...@@ -147,6 +147,12 @@ ...@@ -147,6 +147,12 @@
<ClCompile Include="imgui\imgui_widgets.cpp"> <ClCompile Include="imgui\imgui_widgets.cpp">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Scene.cpp">
<Filter>Source Files\Scenes</Filter>
</ClCompile>
<ClCompile Include="Stage.cpp">
<Filter>Source Files\Scenes</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="PixelShader.hlsl"> <FxCompile Include="PixelShader.hlsl">
...@@ -259,5 +265,11 @@ ...@@ -259,5 +265,11 @@
<ClInclude Include="imgui\imstb_truetype.h"> <ClInclude Include="imgui\imstb_truetype.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Scene.h">
<Filter>Header Files\Scenes</Filter>
</ClInclude>
<ClInclude Include="Stage.h">
<Filter>Header Files\Scenes</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </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, ...@@ -21,7 +21,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
HWND hWnd = nullptr; HWND hWnd = nullptr;
// register window // register window
Window window(hWnd, hInstance, nCmdShow, 800, 600, logger); Window window(hWnd, hInstance, nCmdShow, 1280, 800, logger);
// init imgui // init imgui
ImGui::CreateContext(); ImGui::CreateContext();
......
#include "Window.h" #include "Window.h"
#include "Ground.h" #include "Ground.h"
#include "Stage.h"
#include "imgui/imgui.h" #include "imgui/imgui.h"
Mouse Window::mouse; Mouse Window::mouse;
...@@ -47,13 +48,13 @@ Window::Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Log ...@@ -47,13 +48,13 @@ Window::Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Log
ShowWindow(hWnd, nCmdShow); ShowWindow(hWnd, nCmdShow);
// Init graphics // Init graphics
pGfx = std::make_unique<Graphics>(hWnd); pGfx = std::make_unique<Graphics>(hWnd, x, y);
// Set projection // 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 // Init scenes
this->InitScene(logger); this->InitScenes(logger);
} }
LRESULT CALLBACK Window::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept LRESULT CALLBACK Window::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
...@@ -136,19 +137,20 @@ Graphics& Window::GetGfx() ...@@ -136,19 +137,20 @@ Graphics& Window::GetGfx()
return *pGfx; return *pGfx;
} }
void Window::InitScene(Logger &logger) void Window::InitScenes(Logger &logger)
{ {
//Ground ground(*pGfx, 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( void Window::DrawScenes(
Graphics& gfx, Graphics& gfx,
Logger& logger) noexcept Logger& logger) noexcept
{ {
for (auto& section : pSections) for (auto& pScene : pScenes)
{ {
section->DrawShapes(gfx, logger); pScene->DrawSections(gfx, logger);
} }
}; };
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include "Section.h" #include "Section.h"
#include "Mouse.h" #include "Mouse.h"
#include "Stage.h"
using namespace std; using namespace std;
...@@ -22,7 +23,7 @@ public: ...@@ -22,7 +23,7 @@ public:
Graphics& GetGfx(); Graphics& GetGfx();
// Put all scene initialization here // Put all scene initialization here
void InitScene(Logger& logger); void InitScenes(Logger& logger);
// Draw all scenes // Draw all scenes
void DrawScenes(Graphics& gfx, Logger& logger) noexcept; void DrawScenes(Graphics& gfx, Logger& logger) noexcept;
...@@ -40,5 +41,6 @@ public: ...@@ -40,5 +41,6 @@ public:
private: private:
unique_ptr<Graphics> pGfx; 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