-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectX.cpp
More file actions
490 lines (420 loc) · 17.5 KB
/
DirectX.cpp
File metadata and controls
490 lines (420 loc) · 17.5 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2015 by Joseph Forgion
// This file is part of VCC (Virtual Color Computer).
//
// VCC (Virtual Color Computer) is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VCC (Virtual Color Computer) is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with VCC. If not, see <http://www.gnu.org/licenses/>.
//
// 2025/04/10 - Craig Allsop - Add OpenGL rendering.
//
#include "DirectX.h"
#if USE_DIRECTX
#include "DirectDrawInterface.h"
#include "ddraw.h"
namespace VCC
{
//
// Private wrapper on result code for logging errors
//
static int Result(int code)
{
if (code != IDisplay::OK)
{
char message[256];
snprintf(message, 64, "DirectX error %d\nCheck DirectX support", code);
MessageBox(nullptr, message, "Error", 0);
// FIXME: This is needed and should not be commented out. Wrap it conditional
// either here or in the debug log functions.
//PrintLogC("OpenGL Error: %d\n", code);
}
return code;
}
namespace Detail
{
//Global Variables for Direct Draw funcions
LPDIRECTDRAW g_pDD; // The DirectDraw object
LPDIRECTDRAWCLIPPER g_pClipper; // Clipper for primary surface
LPDIRECTDRAWSURFACE g_pDDS; // Primary surface
LPDIRECTDRAWSURFACE g_pDDSBack; // Back surface
IDirectDrawPalette* ddpal; // Needed for 8bit Palette mode
bool isInitialized;
bool isFullscreen;
bool forceAspect;
bool resizeable;
int statusBarHeight;
RECT WindowDefaultSize;
POINT ForcedAspectBorderPadding;
// surface while locked
void* surface;
int bitDepth;
long surfacePitch;
}
DirectX::DirectX(ISystemState* state) : state(state)
{
using namespace Detail;
isInitialized = false;
isFullscreen = false;
forceAspect = false;
statusBarHeight = 0;
resizeable = true;
WindowDefaultSize = {};
ForcedAspectBorderPadding = {};
g_pDDSBack = nullptr;
g_pDDS = nullptr;
g_pClipper = nullptr;
g_pDD = nullptr;
ddpal = nullptr;
surface = nullptr;
bitDepth = 0;
surfacePitch = 0;
}
int DirectX::Setup(void* hwnd, int width, int height, int statusHeight, bool fullscreen)
{
using namespace Detail;
isFullscreen = fullscreen;
statusBarHeight = statusHeight;
GetWindowRect((HWND)hwnd, &WindowDefaultSize);
HRESULT hr;
PALETTEENTRY pal[256];
DDSURFACEDESC ddsd; // A structure to describe the surfaces we want
unsigned char ColorValues[4] = { 0,85,170,255 };
memset(&ddsd, 0, sizeof(ddsd)); // Clear all members of the structure to 0
ddsd.dwSize = sizeof(ddsd); // The first parameter of the structure must contain the size of the structure
// Create an instance of a DirectDraw object
hr = ::DirectDrawCreate(nullptr, &g_pDD, nullptr);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
VCC::Rect windowRect = { 0, 0, 640, 480 };
if (fullscreen)
{
ddsd.lPitch = 0;
ddsd.ddpfPixelFormat.dwRGBBitCount = 0;
hr = DirectDrawCreate(nullptr, &g_pDD, nullptr); // Initialize DirectDraw
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pDD->SetCooperativeLevel((HWND)hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_NOWINDOWCHANGES);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pDD->SetDisplayMode(windowRect.w, windowRect.h, 32); // Set 640x480x32 Bit full-screen mode
if (FAILED(hr)) return Result(ERR_UNKNOWN);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
ddsd.dwBackBufferCount = 1;
hr = g_pDD->CreateSurface(&ddsd, &g_pDDS, nullptr);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
g_pDDS->GetAttachedSurface(&ddsd.ddsCaps, &g_pDDSBack);
hr = g_pDD->GetDisplayMode(&ddsd);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
//***********************************TEST*****************************************
for (unsigned short i = 0;i <= 63;i++)
{
pal[i + 128].peBlue = ColorValues[(i & 8) >> 2 | (i & 1)];
pal[i + 128].peGreen = ColorValues[(i & 16) >> 3 | (i & 2) >> 1];
pal[i + 128].peRed = ColorValues[(i & 32) >> 4 | (i & 4) >> 2];
pal[i + 128].peFlags = PC_RESERVED | PC_NOCOLLAPSE;
}
g_pDD->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256, pal, &ddpal, nullptr);
g_pDDS->SetPalette(ddpal); // Set pallete for Primary surface
//**********************************END TEST***************************************
}
else
{
// Initialize the DirectDraw object
hr = g_pDD->SetCooperativeLevel((HWND)hwnd, DDSCL_NORMAL); // Set DDSCL_NORMAL to use windowed mode
if (FAILED(hr)) return Result(ERR_UNKNOWN);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
// Create our Primary Surface
hr = g_pDD->CreateSurface(&ddsd, &g_pDDS, nullptr);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
ddsd.dwWidth = windowRect.w; // Make our off-screen surface
ddsd.dwHeight = windowRect.h;
ddsd.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY; // Try to create back buffer in video RAM
hr = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, nullptr);
if (FAILED(hr)) // If not enough Video Ram
{
ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY; // Try to create back buffer in System RAM
hr = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, nullptr);
if (FAILED(hr)) return Result(ERR_UNKNOWN); //Giving Up
MessageBox(nullptr, "Creating Back Buffer in System Ram\n", "Performance Warning", 0);
}
hr = g_pDD->GetDisplayMode(&ddsd);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pDD->CreateClipper(0, &g_pClipper, nullptr); // Create the clipper using the DirectDraw object
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pClipper->SetHWnd(0, (HWND)hwnd); // Assign your window's HWND to the clipper
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pDDS->SetClipper(g_pClipper); // Attach the clipper to the primary surface
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pDDSBack->Lock(nullptr, &ddsd, DDLOCK_WAIT, nullptr);
if (FAILED(hr)) return Result(ERR_UNKNOWN);
hr = g_pDDSBack->Unlock(nullptr); // Unlock surface
if (FAILED(hr)) return Result(ERR_UNKNOWN);
}
return Result(OK);
}
int DirectX::Render()
{
using namespace std;
using namespace Detail;
static HRESULT hr;
static RECT rcSrc; // source blit rectangle
static RECT rcDest; // destination blit rectangle
static RECT Temp;
static POINT p;
HWND hwnd;
if (state->GetWindowHandle((void**)&hwnd) != OK)
return Result(ERR_UNKNOWN);
VCC::Rect windowRect;
if (state->GetRect(1, &windowRect) != OK)
return Result(ERR_UNKNOWN);
ForcedAspectBorderPadding = { 0,0 };
if (isFullscreen) // if we're windowed do the blit, else just Flip
hr = g_pDDS->Flip(nullptr, DDFLIP_NOVSYNC | DDFLIP_DONOTWAIT); //DDFLIP_WAIT
else
{
p.x = 0; p.y = 0;
// The ClientToScreen function converts the client-area coordinates of a specified point to screen coordinates.
// in other word the client rectangle of the main windows 0, 0 (upper-left corner)
// in a screen x,y coords which is put back into p
::ClientToScreen(hwnd, &p); // find out where on the primary surface our window lives
// get the actual client rectangle, which is always 0,0 - w,h
::GetClientRect(hwnd, &rcDest);
// The OffsetRect function moves the specified rectangle by the specified offsets
// add the delta screen point we got above, which gives us the client rect in screen coordinates.
::OffsetRect(&rcDest, p.x, p.y);
// our destination rectangle is going to be
::SetRect(&rcSrc, 0, 0, windowRect.w, windowRect.h);
if (resizeable)
{
rcDest.bottom -= statusBarHeight;
if (forceAspect) // Adjust the Aspect Ratio if window is resized
{
float srcWidth = (float)windowRect.w;
float srcHeight = (float)windowRect.h;
float srcRatio = srcWidth / srcHeight;
// change this to use the existing rcDest and the calc, w = right-left & h = bottom-top,
// because rcDest has already been converted to screen cords, right?
static RECT rcClient;
::GetClientRect(hwnd, &rcClient); // x,y is always 0,0 so right, bottom is w,h
rcClient.bottom -= statusBarHeight;
float clientWidth = (float)rcClient.right;
float clientHeight = (float)rcClient.bottom;
float clientRatio = clientWidth / clientHeight;
float dstWidth = 0, dstHeight = 0;
if (clientRatio > srcRatio)
{
dstWidth = srcWidth * clientHeight / srcHeight;
dstHeight = clientHeight;
}
else
{
dstWidth = clientWidth;
dstHeight = srcHeight * clientWidth / srcWidth;
}
float dstX = (clientWidth - dstWidth) / 2;
float dstY = (clientHeight - dstHeight) / 2;
static POINT pDstLeftTop;
pDstLeftTop.x = (long)dstX; pDstLeftTop.y = (long)dstY;
ForcedAspectBorderPadding = pDstLeftTop;
::ClientToScreen(hwnd, &pDstLeftTop);
static POINT pDstRightBottom;
pDstRightBottom.x = (long)(dstX + dstWidth); pDstRightBottom.y = (long)(dstY + dstHeight);
::ClientToScreen(hwnd, &pDstRightBottom);
::SetRect(&rcDest, pDstLeftTop.x, pDstLeftTop.y, pDstRightBottom.x, pDstRightBottom.y);
}
}
else
{
// this does not seem ideal, it lets you begin to resize and immediately resizes it back ... causing a lot of flicker.
rcDest.right = rcDest.left + windowRect.w;
rcDest.bottom = rcDest.top + windowRect.h;
::GetWindowRect(hwnd, &Temp);
::MoveWindow(hwnd, Temp.left, Temp.top, WindowDefaultSize.right - WindowDefaultSize.left, WindowDefaultSize.bottom - WindowDefaultSize.top, 1);
}
if (g_pDDSBack == nullptr)
return Result(ERR_UNKNOWN);
hr = g_pDDS->Blt(&rcDest, g_pDDSBack, &rcSrc, DDBLT_WAIT, nullptr); // DDBLT_WAIT
if (FAILED(hr)) return Result(ERR_UNKNOWN);
}
return Result(OK);
}
int DirectX::Present()
{
return Result(OK);
}
int DirectX::Cleanup()
{
using namespace Detail;
if (g_pDD != nullptr)
g_pDD->Release(); //Destroy the current Window
return Result(OK);
}
int DirectX::SetOption(int flagOption, bool enabled)
{
using namespace Detail;
//if (!isInitialized) return Result(ERR_NOTINITIALIZED);
switch (flagOption)
{
case OPT_FLAG_ASPECT: forceAspect = enabled; break;
case OPT_FLAG_RESIZEABLE: resizeable = enabled; break;
default: return Result(ERR_BADOPTION);
}
return Result(OK);
}
int DirectX::GetSurface(Pixel** pixels)
{
using namespace Detail;
if (!surface) return Result(ERR_UNKNOWN);
if (bitDepth == 3)
{
*pixels = (Pixel*)surface;
return Result(OK);
}
return Result(ERR_UNSUPPORTED);
}
int DirectX::GetRect(int rectOption, Rect* rect)
{
switch (rectOption)
{
case OPT_RECT_DISPLAY: GetDisplayArea(rect); break;
case OPT_RECT_SURFACE: GetSurfaceArea(rect); break;
default: return Result(ERR_BADOPTION);
}
return Result(OK);
}
int DirectX::LockSurface()
{
using namespace Detail;
CheckSurfaces();
HRESULT hr;
DDSURFACEDESC ddsd; // A structure to describe the surfaces we want
memset(&ddsd, 0, sizeof(ddsd)); // Clear all members of the structure to 0
ddsd.dwSize = sizeof(ddsd); // The first parameter of the structure must contain the size of the structure
// Lock entire surface, wait if it is busy, return surface memory pointer
hr = g_pDDSBack->Lock(nullptr, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, nullptr);
if (FAILED(hr))
{
return Result(ERR_UNKNOWN);
}
switch (ddsd.ddpfPixelFormat.dwRGBBitCount)
{
case 8:
surfacePitch = ddsd.lPitch;
bitDepth = 0;
break;
case 15:
case 16:
surfacePitch = ddsd.lPitch / 2;
bitDepth = 1;
break;
case 24:
MessageBox(nullptr, "24 Bit color is currently unsupported", "Ok", 0);
exit(0);
surfacePitch = ddsd.lPitch;
bitDepth = 2;
break;
case 32:
surfacePitch = ddsd.lPitch / 4;
bitDepth = 3;
break;
default:
MessageBox(nullptr, "Unsupported Color Depth!", "Error", 0);
return 1;
break;
}
if (ddsd.lpSurface == nullptr)
MessageBox(nullptr, "Returning NULL!!", "ok", 0);
surface = ddsd.lpSurface;
state->SetSurface(surface, bitDepth, surfacePitch);
return Result(OK);
}
int DirectX::UnlockSurface()
{
using namespace Detail;
HRESULT hr;
hr = g_pDDSBack->Unlock(nullptr);
surface = nullptr;
if (FAILED(hr)) return Result(ERR_UNKNOWN);
return Result(OK);
}
void DirectX::GetSurfaceArea(Rect* rect) const
{
rect->x = 0;
rect->y = 0;
rect->w = 640;
rect->h = 480;
}
void DirectX::GetDisplayArea(Rect* rect) const
{
using namespace Detail;
rect->x = (float)ForcedAspectBorderPadding.x;
rect->y = (float)ForcedAspectBorderPadding.y;
rect->w = 640;
rect->h = 480;
}
void DirectX::CheckSurfaces() const
{
using namespace Detail;
if (g_pDDS) // Check the primary surface
if (g_pDDS->IsLost() == DDERR_SURFACELOST)
g_pDDS->Restore();
if (g_pDDSBack) // Check the back buffer
if (g_pDDSBack->IsLost() == DDERR_SURFACELOST)
g_pDDSBack->Restore();
}
int DirectX::RenderSignalLostMessage()
{
using namespace Detail;
static unsigned short TextX = rand() % 580, TextY = rand() % 470;
static unsigned char Counter, Counter1 = 32;
static char Phase = 1;
static char Message[] = " Signal Missing! Press F9";
HDC hdc;
g_pDDSBack->GetDC(&hdc);
SetBkColor(hdc, 0);
SetTextColor(hdc, RGB(Counter1 << 2, Counter1 << 2, Counter1 << 2));
TextOut(hdc, TextX, TextY, Message, strlen(Message));
Counter++;
Counter1 += Phase;
if (Counter1 == 60 || Counter1 == 20)
Phase = -Phase;
Counter %= 60; //about 1 seconds
if (!Counter)
{
TextX = rand() % 580;
TextY = rand() % 470;
}
g_pDDSBack->ReleaseDC(hdc);
return Result(OK);
}
int DirectX::RenderStatusLine(char * statusText)
{
using namespace Detail;
size_t Index = 0;
HDC hdc;
//Put StatusText for full screen here
if (isFullscreen)
{
g_pDDSBack->GetDC(&hdc);
SetBkColor(hdc, RGB(0, 0, 0));
SetTextColor(hdc, RGB(255, 255, 255));
Index = strlen(statusText);
for (;Index < 132;Index++)
statusText[Index] = 32;
statusText[Index] = 0;
TextOut(hdc, 0, 0, statusText, 132);
g_pDDSBack->ReleaseDC(hdc);
}
return Result(OK);
}
}
#endif // USE_DIRECTX