1 #pragma once
2 
3 #include "stdafx.h"
4 #include "../Core/IRenderingDevice.h"
5 #include "../Core/IMessageManager.h"
6 #include "../Core/EmulationSettings.h"
7 #include "../Utilities/FolderUtilities.h"
8 #include "../Utilities/SimpleLock.h"
9 #include "../Utilities/Timer.h"
10 #include "../Core/BaseRenderer.h"
11 
12 using namespace DirectX;
13 
14 class Console;
15 
16 namespace DirectX {
17 	class SpriteBatch;
18 	class SpriteFont;
19 }
20 
21 class Renderer : public BaseRenderer, public IRenderingDevice
22 {
23 private:
24 	HWND                    _hWnd = nullptr;
25 
26 	ID3D11Device*           _pd3dDevice = nullptr;
27 	ID3D11DeviceContext*    _pDeviceContext = nullptr;
28 	IDXGISwapChain*         _pSwapChain = nullptr;
29 	ID3D11RenderTargetView* _pRenderTargetView = nullptr;
30 	ID3D11DepthStencilState* _pDepthDisabledStencilState = nullptr;
31 	ID3D11BlendState*			_pAlphaEnableBlendingState = nullptr;
32 
33 	ID3D11SamplerState*		_samplerState = nullptr;
34 
35 	atomic<bool>				_needFlip = false;
36 	uint8_t*						_textureBuffer[2] = { nullptr, nullptr };
37 	ID3D11Texture2D*			_pTexture = nullptr;
38 	ID3D11ShaderResourceView*	_pTextureSrv = nullptr;
39 	ID3D11Texture2D*			_overlayTexture = nullptr;
40 	ID3D11ShaderResourceView*	_pOverlaySrv = nullptr;
41 
42 	bool							_frameChanged = true;
43 	SimpleLock					_frameLock;
44 	SimpleLock					_textureLock;
45 
46 	VideoResizeFilter _resizeFilter = VideoResizeFilter::NearestNeighbor;
47 
48 	unique_ptr<SpriteFont>	_font;
49 	unique_ptr<SpriteFont>	_largeFont;
50 
51 	unique_ptr<SpriteBatch> _spriteBatch;
52 
53 	const uint32_t _bytesPerPixel = 4;
54 	uint32_t _screenBufferSize = 0;
55 
56 	bool _newFullscreen = false;
57 	bool _fullscreen = false;
58 
59 	uint32_t _realScreenHeight = 240;
60 	uint32_t _realScreenWidth = 256;
61 	uint32_t _leftMargin = 0;
62 	uint32_t _topMargin = 0;
63 	uint32_t _monitorWidth = 0;
64 	uint32_t _monitorHeight = 0;
65 
66 	uint32_t _nesFrameHeight = 0;
67 	uint32_t _nesFrameWidth = 0;
68 	uint32_t _newFrameBufferSize = 0;
69 
70 	uint32_t _noUpdateCount = 0;
71 
72 	HRESULT InitDevice();
73 	void CleanupDevice();
74 
75 	void SetScreenSize(uint32_t width, uint32_t height);
76 
77 	ID3D11Texture2D* CreateTexture(uint32_t width, uint32_t height);
78 	ID3D11ShaderResourceView* GetShaderResourceView(ID3D11Texture2D* texture);
79 	void DrawNESScreen();
80 	void DrawPauseScreen(bool disableOverlay);
81 
82 	void DrawString(string message, float x, float y, DirectX::FXMVECTOR color, float scale, SpriteFont* font = nullptr);
83 	void DrawString(std::wstring message, float x, float y, DirectX::FXMVECTOR color, float scale, SpriteFont* font = nullptr);
84 
85 	void DrawString(std::wstring message, int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t opacity);
86 	float MeasureString(std::wstring text);
87 	bool ContainsCharacter(wchar_t character);
88 
89 	HRESULT CreateRenderTargetView();
90 	void ReleaseRenderTargetView();
91 	HRESULT CreateNesBuffers();
92 	void ResetNesBuffers();
93 	HRESULT CreateSamplerState();
94 
95 public:
96 	Renderer(shared_ptr<Console> console, HWND hWnd, bool registerAsMessageManager);
97 	~Renderer();
98 
99 	void SetFullscreenMode(bool fullscreen, void* windowHandle, uint32_t monitorWidth, uint32_t monitorHeight);
100 
101 	void Reset();
102 	void Render();
103 
104 	void UpdateFrame(void *frameBuffer, uint32_t width, uint32_t height);
105 };