-
Notifications
You must be signed in to change notification settings - Fork 0
/
D2DRenderer.cpp
155 lines (130 loc) · 3 KB
/
D2DRenderer.cpp
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
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include <math.h>
#include "D2DRenderer.h"
#include "windows.h"
#include "Utility.h"
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
D2DRenderer::D2DRenderer()
{
m_d2dContext = nullptr;
m_game = nullptr;
ThrowIfFailed(
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
(IUnknown **)(&m_dwriteFactory)
)
);
#ifdef DEBUG
ThrowIfFailed(
m_dwriteFactory->CreateTextFormat(
L"Segoe UI",
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
32.0f,
L"en-US",
&m_textFormat));
#endif
}
D2DRenderer::~D2DRenderer()
{
assert(m_game != NULL);
delete m_game;
}
void D2DRenderer::CreateDeviceIndependentResources()
{
DirectXBase::CreateDeviceIndependentResources();
}
void D2DRenderer::CreateDeviceResources()
{
DirectXBase::CreateDeviceResources();
}
void D2DRenderer::RecreateTarget()
{
m_d2dContext->SetTarget(nullptr);
m_d2dTargetBitmap = nullptr;
CreateDeviceResources();
CreateWindowSizeDependentResources();
}
#ifdef DEBUG
void D2DRenderer::RenderFPS(D2D1_SIZE_F)
{
SYSTEMTIME time;
GetSystemTime(&time);
if (time.wSecond != m_lastFrameSecond)
{
m_lastFrameSecond = time.wSecond;
m_fps = m_frameCounter;
m_frameCounter = 0;
char buf[20];
memset(buf, 0, 20);
sprintf_s(buf, 20, "FPS: %d", m_fps);
mbstowcs_s(&m_fpsTextLength, m_fpsText, strlen(buf)+1, buf, _TRUNCATE);
}
else
{
m_frameCounter++;
}
ThrowIfFailed(
m_d2dContext->CreateSolidColorBrush(D2D1::ColorF(1.0, 1.0, 1.0, 0.5), &m_whiteBrush)
);
D2D1_RECT_F layoutRect;
layoutRect.top = 150;
layoutRect.left = 60;
layoutRect.bottom = 400;
layoutRect.right = 400;
m_d2dContext->DrawText(
m_fpsText,
m_fpsTextLength,
m_textFormat,
&layoutRect,
m_whiteBrush);
}
#endif
void D2DRenderer::OnMouseMove(PointerEventArgs ^args)
{
Point p = args->CurrentPoint->Position;
m_game->OnMouseMove(b2Vec2(p.X, p.Y));
}
void D2DRenderer::OnMouseDown(PointerEventArgs ^args)
{
m_game->OnMouseDown(args);
}
void D2DRenderer::Render()
{
HRESULT hr = S_OK;
// Retrieve the size of the render target.
D2D1_SIZE_F renderTargetSize = m_d2dContext->GetSize();
if (m_game == nullptr)
{
// Initialize Game
m_game = new Game(b2Vec2(renderTargetSize.width, renderTargetSize.height), m_d2dContext, m_dwriteFactory);
}
m_d2dContext->BeginDraw();
m_d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Black));
m_d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
m_game->Draw();
#ifdef DEBUG
RenderFPS(renderTargetSize);
#endif
hr = m_d2dContext->EndDraw();
if (hr == D2DERR_RECREATE_TARGET)
{
RecreateTarget();
}
else
{
// TODO why is it failing here?
//ThrowIfFailed(hr);
}
Present();
}