File: BaseManager.cpp

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (181 lines) | stat: -rw-r--r-- 4,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Precompiled.h"
#include "BaseManager.h"

#pragma warning(push, 0)
#include <d3dx11.h>
#pragma warning(pop)
#include <MyGUI_DirectX11Platform.h>

#include <SDL_syswm.h>

namespace base
{
	bool BaseManager::createRender(int _width, int _height, bool _windowed)
	{
		D3D_FEATURE_LEVEL featureLevels[] = {
			D3D_FEATURE_LEVEL_11_0,
			D3D_FEATURE_LEVEL_10_1,
			D3D_FEATURE_LEVEL_10_0,
			D3D_FEATURE_LEVEL_9_3,
			D3D_FEATURE_LEVEL_9_2,
			D3D_FEATURE_LEVEL_9_1};

		static const int numFeatureLevels = 6;

		D3D_FEATURE_LEVEL selectedFeatureLevel = D3D_FEATURE_LEVEL_11_0;

		DXGI_SWAP_CHAIN_DESC swapChainDesc;
		ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
		swapChainDesc.BufferCount = 1;
		swapChainDesc.BufferDesc.Width = _width;
		swapChainDesc.BufferDesc.Height = _height;
		swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
		swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
		swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
		swapChainDesc.SampleDesc.Count = 1;
		swapChainDesc.SampleDesc.Quality = 0;
		swapChainDesc.Windowed = _windowed;

		SDL_SysWMinfo sysWMInfo;
		SDL_VERSION(&sysWMInfo.version);
		SDL_GetWindowWMInfo(mSdlWindow, &sysWMInfo);
		swapChainDesc.OutputWindow = sysWMInfo.info.win.window;

		HRESULT hr = S_OK;

		if (FAILED(
				hr = D3D11CreateDeviceAndSwapChain(
					nullptr,
					D3D_DRIVER_TYPE_HARDWARE,
					nullptr,
					0,
					featureLevels,
					numFeatureLevels,
					D3D11_SDK_VERSION,
					&swapChainDesc,
					&mSwapChain,
					&mDevice,
					&selectedFeatureLevel,
					&mDeviceContext)))
		{
			return false;
		}

		hr = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBuffer);

		mDevice->CreateRenderTargetView(mBackBuffer, nullptr, &mRenderTarget);

		mDeviceContext->OMSetRenderTargets(1, &mRenderTarget, nullptr);

		D3D11_VIEWPORT vp;
		vp.Width = (float)_width;
		vp.Height = (float)_height;
		vp.MinDepth = 0.0f;
		vp.MaxDepth = 1.0f;
		vp.TopLeftX = 0.0f;
		vp.TopLeftY = 0.0f;
		mDeviceContext->RSSetViewports(1, &vp);
		return true;
	}

	void BaseManager::destroyRender()
	{
		if (mRenderTarget)
		{
			mRenderTarget->Release();
			mRenderTarget = nullptr;
		}
		if (mBackBuffer)
		{
			mBackBuffer->Release();
			mBackBuffer = nullptr;
		}
		if (mSwapChain)
		{
			mSwapChain->Release();
			mSwapChain = nullptr;
		}
		if (mDeviceContext)
		{
			mDeviceContext->Release();
			mDeviceContext = nullptr;
		}
		if (mDevice)
		{
			mDevice->Release();
			mDevice = nullptr;
		}
	}

	void BaseManager::createGuiPlatform()
	{
		mPlatform = new MyGUI::DirectX11Platform();
		setupResources();
		mPlatform->initialise(mDevice);
	}

	void BaseManager::destroyGuiPlatform()
	{
		if (mPlatform)
		{
			mPlatform->shutdown();
			delete mPlatform;
			mPlatform = nullptr;
		}
	}

	void BaseManager::drawOneFrame()
	{
		const float clearColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
		mDeviceContext->ClearRenderTargetView(mRenderTarget, clearColor);
		mPlatform->getRenderManagerPtr()->drawOneFrame();
		mSwapChain->Present(0, 0);
	}

	void BaseManager::resizeRender(int _width, int _height)
	{
		if (mDevice != nullptr)
		{
			// Release the current render target view and its back buffer
			mDeviceContext->OMSetRenderTargets(0, nullptr, nullptr);
			if (mRenderTarget)
			{
				mRenderTarget->Release();
				mRenderTarget = nullptr;
			}
			if (mBackBuffer)
			{
				mBackBuffer->Release();
				mBackBuffer = nullptr;
			}

			// Resize
			mSwapChain->ResizeBuffers(1, _width, _height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);

			// Acquire the new back buffer
			mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBuffer);

			// Create a new render target from the resized buffer
			mDevice->CreateRenderTargetView(mBackBuffer, nullptr, &mRenderTarget);
			// Set the new render target
			mDeviceContext->OMSetRenderTargets(1, &mRenderTarget, nullptr);

			// Устанавливаем новый вьюпорт
			D3D11_VIEWPORT vp;
			vp.Width = (float)_width;
			vp.Height = (float)_height;
			vp.MinDepth = 0.0f;
			vp.MaxDepth = 1.0f;
			vp.TopLeftX = 0.0f;
			vp.TopLeftY = 0.0f;
			mDeviceContext->RSSetViewports(1, &vp);
		}
	}

	void BaseManager::addResourceLocation(const std::string& _name, bool _recursive)
	{
		mPlatform->getDataManagerPtr()->addResourceLocation(_name, _recursive);
	}

}