-
Notifications
You must be signed in to change notification settings - Fork 0
/
DdxDevice.cpp
85 lines (63 loc) · 2.27 KB
/
DdxDevice.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
// This file is part of the WinRayCast Application (a 3D Engine Demo).
// Copyright (C) 2005 - 2018
// Antonino Calderone ([email protected])
// All rights reserved.
// Licensed under the MIT License.
// See COPYING file in the project root for full license information.
#include "DdxDevice.h"
/* ------------------------------------------------------------------------- */
DdxDevice& DdxDevice::getInstance() noexcept
{
static DdxDevice _instance;
return _instance;
}
/* ------------------------------------------------------------------------- */
void DdxDevice::releaseObjects() noexcept
{
if (m_pDD) {
if (m_pDDSPrimary) {
m_pDDSPrimary->Release();
m_pDDSPrimary = nullptr;
}
m_pDD->Release();
m_pDD = nullptr;
}
}
/* ------------------------------------------------------------------------- */
DdxDevice::error_t DdxDevice::init(HWND hWnd, bool fullScreen , int xres, int yres)
{
if (m_pDD) {
return error_t::AlreadyInitialized;
}
// Create the main DirectDraw object
auto hRet = DirectDrawCreateEx(
nullptr, (VOID**)&m_pDD, IID_IDirectDraw7, nullptr);
if (hRet != DD_OK || m_pDD == nullptr) {
return error_t::DirectDrawCreateExFailed;
}
// Get normal mode
hRet = m_pDD->SetCooperativeLevel(hWnd,
fullScreen ? DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN : DDSCL_NORMAL);
if (hRet != DD_OK) {
return error_t::SetCooperativeLevelFailed;
}
DDSURFACEDESC2 ddsd = { 0 };
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if (fullScreen) {
hRet = m_pDD->SetDisplayMode(xres, yres, 32 /* bits per color */, 0, 0);
if (hRet != DD_OK) {
return error_t::SetDisplayModeFailed;
}
// Create the primary surface with 1 back buffer
ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps |= DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
}
hRet = m_pDD->CreateSurface(&ddsd, &m_pDDSPrimary, nullptr);
if (hRet != DD_OK) {
return error_t::CreateSurfaceFailed;
}
return error_t::Success;
}