forked from mashakos/OmniScaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelclass.cpp
408 lines (327 loc) · 11.7 KB
/
modelclass.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
////////////////////////////////////////////////////////////////////////////////
// Filename: modelclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "modelclass.h"
#include <Uxtheme.h>
ModelClass::ModelClass()
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_Texture = 0;
}
ModelClass::ModelClass(const ModelClass& other)
{
}
ModelClass::~ModelClass()
{
}
bool ModelClass::Initialize(
ID3D11Device* device, // D3D11 device
ID3D11DeviceContext* deviceContext, // D3D11 device context
HWND& hwnd, // game HWND
HANDLE& appHandle, // Shared surface handle
int& gRealScreenWidth, // screen width
int& gRealScreenHeight, // screen height
int& gScreenWidth, // game window width
int& gScreenHeight, // game window height
float& topOffset, // top offset
float& bottomOffset, // bottom offset
float& leftOffset, // left offset
float& rightOffset, // right offset
bool integerScaling, // integer scaling
float integerScalingOverride, // integer scaling scale
bool IntegerScalingOverscan, // integer scaling overscan
int* mouseCursorBmpData, // mouse cursor bitmap data
float mousePosRemap_xscale, // mouse position remap scale X
float mousePosRemap_yscale, // mouse position remap scale Y
int mousePointer_size, // mouse pointer size
RECT& mouse_area // mouse area RECT
)
{
bool result;
// Initialize the vertex and index buffers.
result = InitializeBuffers(hwnd, device, gRealScreenWidth, gRealScreenHeight, gScreenWidth, gScreenHeight, topOffset, bottomOffset, leftOffset, rightOffset, integerScaling, integerScalingOverride, IntegerScalingOverscan, mouse_area);
if(!result)
{
return false;
}
// Load the texture for this model.
result = LoadTexture(device, deviceContext, hwnd, appHandle, mouseCursorBmpData, mousePosRemap_xscale, mousePosRemap_yscale, mousePointer_size);
if (!result)
{
return false;
}
return true;
}
void ModelClass::Shutdown()
{
// Release the model texture.
ReleaseTexture();
// Shutdown the vertex and index buffers.
ShutdownBuffers();
return;
}
void ModelClass::Render(ID3D11DeviceContext* deviceContext)
{
// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
RenderBuffers(deviceContext);
return;
}
int ModelClass::GetIndexCount()
{
return m_indexCount;
}
ID3D11ShaderResourceView* ModelClass::GetTexture()
{
return m_Texture->GetTexture();
}
ID3D11ShaderResourceView** ModelClass::GetTextureArray()
{
return m_Texture->GetTextureArray();
}
bool ModelClass::InitializeBuffers(
HWND& hwnd, // game HWND
ID3D11Device* device, // D3D11 device
int& gRealScreenWidth, // screen width
int& gRealScreenHeight, // screen height
int& gScreenWidth, // game window width
int& gScreenHeight, // game window height
float& topOffset, // top offset
float& bottomOffset, // bottom offset
float& leftOffset, // left offset
float& rightOffset, // right offset
bool integerScaling, // integer scaling
float integerScalingOverride, // integer scaling scale
bool IntegerScalingOverscan, // integer scaling overscan
RECT& mouse_area // mouse area RECT
)
{
VertexType* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
// Set the number of vertices in the vertex array.
m_vertexCount = 6;
// Set the number of indices in the index array.
m_indexCount = 6;
// Create the vertex array.
vertices = new VertexType[m_vertexCount];
if(!vertices)
{
return false;
}
// Create the index array.
indices = new unsigned long[m_indexCount];
if(!indices)
{
return false;
}
float left, right, top, bottom;
RECT gsr;
RECT gsr_win;
GetClientRect(hwnd, &gsr);
GetWindowRect(hwnd, &gsr_win);
POINT lefttop = { gsr.left, gsr.top }; // Practicaly both are 0
ClientToScreen(hwnd, &lefttop);
POINT rightbottom = { gsr.right, gsr.bottom };
ClientToScreen(hwnd, &rightbottom);
gScreenWidth = gsr.right;
gScreenHeight = gsr.bottom - (int)topOffset - (int)bottomOffset;
// border for gdi
//GetSystemMetrics(SM_CYEDGE)
// border for aero
// GetThemeSysSize(NULL, SM_CXBORDER);
//title bar for gdi
// GetSystemMetrics(SM_CYCAPTION)
// title bar for aero
// GetThemeSysSize(NULL, SM_CXPADDEDBORDER)
//frame for gdi
// GetSystemMetrics(SM_CYSIZEFRAME)
// frame for aero
// GetThemeSysSize(NULL, SM_CYSIZE)
//float gHwnd_border = (float)GetSystemMetrics(SM_CXSIZEFRAME);// -GetSystemMetrics(SM_CYEDGE);
//int gHwnd_titlebar = GetSystemMetrics(SM_CYCAPTION);
float gHwnd_winwidth = (float)gsr_win.right - (float)gsr_win.left;
float gHwnd_winheight = ((float)gsr_win.bottom - (float)gsr_win.top);
// pixel offsets for window that has extra menus or UI elements
float topOffsetTex, bottomOffsetTex, leftOffsetTex, rightOffsetTex;
topOffsetTex = (topOffset + 0.5f) / gHwnd_winheight;
bottomOffsetTex = (bottomOffset - 1.0f) / gHwnd_winheight;
leftOffsetTex = (leftOffset + 0.5f) / gHwnd_winwidth;
rightOffsetTex = (rightOffset - 1.0f) / gHwnd_winwidth;
//title bar and window frame border values
float h_offset = (1.0f / gHwnd_winheight);
//float margin_offset = (topOffset || bottomOffset) > 0 ? (6.0f / gHwnd_winheight) : 0;
float client_top = ((float)(lefttop.y - gsr_win.top) / gHwnd_winheight); // There is no transparent part
float client_bottom = (float)(gsr_win.bottom - rightbottom.y) / gHwnd_winheight; // As above
float client_left = (float)(lefttop.x - gsr_win.left) / gHwnd_winwidth; // Windows 10: includes transparent part
float client_right = (float)(gsr_win.right - rightbottom.x) / gHwnd_winwidth; // As above
float gHwnd_clientwidth = (((float)gScreenWidth) / gHwnd_winwidth) + client_right + rightOffsetTex;
float gHwnd_clientheight = ((((float)gScreenHeight) / gHwnd_winheight) + client_top) + topOffsetTex - h_offset;
float gHwnd_clienttop = client_top + topOffsetTex ;
float gHwnd_clientleft = client_left + leftOffsetTex;
float gHwnd_clientWidthAspect = (float)gScreenWidth / (float)gScreenHeight;
float d3dScaleFactor = 0.0f;
float initIntegerScale = 1.0f;
float orthoHeight = gScreenHeight / 2;
// Calculate the screen coordinates of the left side of the bitmap.
left = -orthoHeight * gHwnd_clientWidthAspect;
// Calculate the screen coordinates of the right side of the bitmap.
right = orthoHeight * gHwnd_clientWidthAspect;
// Calculate the screen coordinates of the top of the bitmap.
top = orthoHeight;
// Calculate the screen coordinates of the bottom of the bitmap.
bottom = -orthoHeight;
if (integerScaling)
{
initIntegerScale = floor((float)gRealScreenHeight / (float)gScreenHeight) * integerScalingOverride;
if(IntegerScalingOverscan)
initIntegerScale = ceil((float)gRealScreenHeight / (float)gScreenHeight) * integerScalingOverride;
left *= initIntegerScale;
right *= initIntegerScale;
top *= initIntegerScale;
bottom *= initIntegerScale;
}
else
{
orthoHeight = (gScreenHeight * (float)gRealScreenHeight / (float)gScreenHeight) / 2;
left = -orthoHeight * gHwnd_clientWidthAspect;
right = orthoHeight * gHwnd_clientWidthAspect;
top = orthoHeight;
bottom = -orthoHeight;
}
float gRealScreenAspect = (float)gRealScreenWidth / (float)gRealScreenHeight;
if (gRealScreenAspect < gHwnd_clientWidthAspect && !integerScaling)
{
float aspectReduction = gRealScreenAspect / gHwnd_clientWidthAspect;
left *= aspectReduction;
right *= aspectReduction;
top *= aspectReduction;
bottom *= aspectReduction;
}
// Load the vertex array with data.
vertices[0].position = XMFLOAT3(left, bottom, d3dScaleFactor); // Bottom left.
vertices[0].texture = XMFLOAT2(gHwnd_clientleft, gHwnd_clientheight);
vertices[1].position = XMFLOAT3(left, top, d3dScaleFactor); // Top left.
vertices[1].texture = XMFLOAT2(gHwnd_clientleft, gHwnd_clienttop);
vertices[2].position = XMFLOAT3(right, top, d3dScaleFactor); // top right.
vertices[2].texture = XMFLOAT2(gHwnd_clientwidth, gHwnd_clienttop);
vertices[3].position = XMFLOAT3(right, bottom, d3dScaleFactor); // Bottom right.
vertices[3].texture = XMFLOAT2(gHwnd_clientwidth, gHwnd_clientheight);
// Load the index array with data.
indices[0] = 0; // Bottom left.
indices[1] = 1; // Top left.
indices[2] = 2; // top right.
indices[3] = 0; // Bottom left.
indices[4] = 2; // Top right.
indices[5] = 3; // Bottom right.
// Set up the description of the static vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Now create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if(FAILED(result))
{
return false;
}
// Set up the description of the static index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the index data.
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// Create the index buffer.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
if(FAILED(result))
{
return false;
}
// Release the arrays now that the vertex and index buffers have been created and loaded.
delete [] vertices;
vertices = 0;
delete [] indices;
indices = 0;
return true;
}
void ModelClass::ShutdownBuffers()
{
// Release the index buffer.
if(m_indexBuffer)
{
m_indexBuffer->Release();
m_indexBuffer = 0;
}
// Release the vertex buffer.
if(m_vertexBuffer)
{
m_vertexBuffer->Release();
m_vertexBuffer = 0;
}
return;
}
void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
{
unsigned int stride;
unsigned int offset;
// Set vertex buffer stride and offset.
stride = sizeof(VertexType);
offset = 0;
// Set the vertex buffer to active in the input assembler so it can be rendered.
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
// Set the index buffer to active in the input assembler so it can be rendered.
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
return;
}
bool ModelClass::LoadTexture(
ID3D11Device* device, // D3D11 device
ID3D11DeviceContext* deviceContext, // D3D11 device context
HWND& hwnd, // game HWND
HANDLE& appHandle, // Shared surface handle
int* mouseCursorBmpData, // mouse cursor bitmap data
float mousePosRemap_xscale, // mouse position remap scale X
float mousePosRemap_yscale, // mouse position remap scale Y
int mousePointer_size // mouse pointer size
)
{
bool result;
// Create the texture object.
m_Texture = new TextureClass;
if (!m_Texture)
{
return false;
}
// Initialize the texture object.
result = m_Texture->Initialize(device, deviceContext, hwnd, appHandle, mouseCursorBmpData, mousePosRemap_xscale, mousePosRemap_yscale, mousePointer_size);
if (!result)
{
return false;
}
return true;
}
void ModelClass::ReleaseTexture()
{
// Release the texture object.
if (m_Texture)
{
m_Texture->Shutdown();
delete m_Texture;
m_Texture = 0;
}
return;
}