home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Common / d3dApp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  4.0 KB  |  129 lines

  1. //***************************************************************************************
  2. // d3dApp.h by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. #pragma once
  6.  
  7. #if defined(DEBUG) || defined(_DEBUG)
  8. #define _CRTDBG_MAP_ALLOC
  9. #include <crtdbg.h>
  10. #endif
  11.  
  12. #include "d3dUtil.h"
  13. #include "GameTimer.h"
  14.  
  15. // Link necessary d3d12 libraries.
  16. #pragma comment(lib,"d3dcompiler.lib")
  17. #pragma comment(lib, "D3D12.lib")
  18. #pragma comment(lib, "dxgi.lib")
  19.  
  20. class D3DApp
  21. {
  22. protected:
  23.  
  24.     D3DApp(HINSTANCE hInstance);
  25.     D3DApp(const D3DApp& rhs) = delete;
  26.     D3DApp& operator=(const D3DApp& rhs) = delete;
  27.     virtual ~D3DApp();
  28.  
  29. public:
  30.  
  31.     static D3DApp* GetApp();
  32.     
  33.     HINSTANCE AppInst()const;
  34.     HWND      MainWnd()const;
  35.     float     AspectRatio()const;
  36.  
  37.     bool Get4xMsaaState()const;
  38.     void Set4xMsaaState(bool value);
  39.  
  40.     int Run();
  41.  
  42.     virtual bool Initialize();
  43.     virtual LRESULT MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  44.  
  45. protected:
  46.     virtual void CreateRtvAndDsvDescriptorHeaps();
  47.     virtual void OnResize(); 
  48.     virtual void Update(const GameTimer& gt)=0;
  49.     virtual void Draw(const GameTimer& gt)=0;
  50.  
  51.     // Convenience overrides for handling mouse input.
  52.     virtual void OnMouseDown(WPARAM btnState, int x, int y){ }
  53.     virtual void OnMouseUp(WPARAM btnState, int x, int y)  { }
  54.     virtual void OnMouseMove(WPARAM btnState, int x, int y){ }
  55.  
  56. protected:
  57.  
  58.     bool InitMainWindow();
  59.     bool InitDirect3D();
  60.     void CreateCommandObjects();
  61.     void CreateSwapChain();
  62.  
  63.     void FlushCommandQueue();
  64.  
  65.     ID3D12Resource* CurrentBackBuffer()const;
  66.     D3D12_CPU_DESCRIPTOR_HANDLE CurrentBackBufferView()const;
  67.     D3D12_CPU_DESCRIPTOR_HANDLE DepthStencilView()const;
  68.  
  69.     void CalculateFrameStats();
  70.  
  71.     void LogAdapters();
  72.     void LogAdapterOutputs(IDXGIAdapter* adapter);
  73.     void LogOutputDisplayModes(IDXGIOutput* output, DXGI_FORMAT format);
  74.  
  75. protected:
  76.  
  77.     static D3DApp* mApp;
  78.  
  79.     HINSTANCE mhAppInst = nullptr; // application instance handle
  80.     HWND      mhMainWnd = nullptr; // main window handle
  81.     bool      mAppPaused = false;  // is the application paused?
  82.     bool      mMinimized = false;  // is the application minimized?
  83.     bool      mMaximized = false;  // is the application maximized?
  84.     bool      mResizing = false;   // are the resize bars being dragged?
  85.     bool      mFullscreenState = false;// fullscreen enabled
  86.  
  87.     // Set true to use 4X MSAA (º4.1.8).  The default is false.
  88.     bool      m4xMsaaState = false;    // 4X MSAA enabled
  89.     UINT      m4xMsaaQuality = 0;      // quality level of 4X MSAA
  90.  
  91.     // Used to keep track of the ôdelta-timeö and game time (º4.4).
  92.     GameTimer mTimer;
  93.     
  94.     Microsoft::WRL::ComPtr<IDXGIFactory4> mdxgiFactory;
  95.     Microsoft::WRL::ComPtr<IDXGISwapChain> mSwapChain;
  96.     Microsoft::WRL::ComPtr<ID3D12Device> md3dDevice;
  97.  
  98.     Microsoft::WRL::ComPtr<ID3D12Fence> mFence;
  99.     UINT64 mCurrentFence = 0;
  100.     
  101.     Microsoft::WRL::ComPtr<ID3D12CommandQueue> mCommandQueue;
  102.     Microsoft::WRL::ComPtr<ID3D12CommandAllocator> mDirectCmdListAlloc;
  103.     Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> mCommandList;
  104.  
  105.     static const int SwapChainBufferCount = 2;
  106.     int mCurrBackBuffer = 0;
  107.     Microsoft::WRL::ComPtr<ID3D12Resource> mSwapChainBuffer[SwapChainBufferCount];
  108.     Microsoft::WRL::ComPtr<ID3D12Resource> mDepthStencilBuffer;
  109.  
  110.     Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> mRtvHeap;
  111.     Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> mDsvHeap;
  112.  
  113.     D3D12_VIEWPORT mScreenViewport; 
  114.     D3D12_RECT mScissorRect;
  115.  
  116.     UINT mRtvDescriptorSize = 0;
  117.     UINT mDsvDescriptorSize = 0;
  118.     UINT mCbvSrvUavDescriptorSize = 0;
  119.  
  120.     // Derived class should set these in derived constructor to customize starting values.
  121.     std::wstring mMainWndCaption = L"d3d App";
  122.     D3D_DRIVER_TYPE md3dDriverType = D3D_DRIVER_TYPE_HARDWARE;
  123.     DXGI_FORMAT mBackBufferFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
  124.     DXGI_FORMAT mDepthStencilFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
  125.     int mClientWidth = 800;
  126.     int mClientHeight = 600;
  127. };
  128.  
  129.