-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddutil.cpp
More file actions
216 lines (175 loc) · 5.62 KB
/
ddutil.cpp
File metadata and controls
216 lines (175 loc) · 5.62 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
//-----------------------------------------------------------------------------
// File: ddutil.cpp
//
// Desc: DirectDraw framewark classes. Feel free to use this class as a
// starting point for adding extra functionality.
//
//
// Copyright (c) 1995-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include "stdafx.h"
#include <tchar.h>
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include "ddutil.h"
#include "dxutil.h"
#include <algorithm>
CDisplay::CDisplay():
m_pDD( NULL ),
m_pdds( NULL ),
m_bg_pdds( NULL )
{
}
CDisplay::~CDisplay()
{
DestroyObjects();
modes.clear();
}
HRESULT CDisplay::DestroyObjects()
{
SAFE_RELEASE( m_pdds );
SAFE_RELEASE( m_bg_pdds );
if ( m_pDD ) {
m_pDD->SetCooperativeLevel( parentWnd->m_hWnd, DDSCL_NORMAL );
}
SAFE_RELEASE( m_pDD );
return S_OK;
}
HRESULT CDisplay::CreateWindowedDisplay( CWnd* _parentWnd, int width, int height )
{
parentWnd = _parentWnd;
HRESULT hr;
DestroyObjects();
if( FAILED( hr = DirectDrawCreateEx( NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL ) ) ) {
return E_FAIL;
}
hr = m_pDD->SetCooperativeLevel( parentWnd->m_hWnd, DDSCL_NORMAL );
if( FAILED( hr ) ) {
return E_FAIL;
}
RECT rc;
::SetRect( &rc, 0, 0, width, height );
::AdjustWindowRectEx( &rc, parentWnd->GetStyle(), parentWnd->GetMenu() != NULL, parentWnd->GetExStyle() );
parentWnd->SetWindowPos( NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
parentWnd->SetWindowPos( &CWnd::wndNoTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
// Make sure our window does not hang outside of the work area
RECT rcWork;
::SystemParametersInfo( SPI_GETWORKAREA, 0, &rcWork, 0 );
parentWnd->GetWindowRect( &rc );
if ( rc.left < rcWork.left ) rc.left = rcWork.left;
if ( rc.top < rcWork.top ) rc.top = rcWork.top;
parentWnd->SetWindowPos( NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
// Create the primary surface
DDSURFACEDESC2 ddsd;
::ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pdds, NULL ) ) ) {
return E_FAIL;
}
m_ddsd.dwSize = sizeof( m_ddsd );
m_pdds->GetSurfaceDesc( &m_ddsd );
// Create the backbuffer surface
::ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
if( FAILED( m_pDD->CreateSurface( &ddsd, &m_bg_pdds, NULL ) ) ) {
return E_FAIL;
}
m_bg_ddsd.dwSize = sizeof( m_bg_ddsd );
m_bg_pdds->GetSurfaceDesc( &m_bg_ddsd );
LPDIRECTDRAWCLIPPER pcClipper;
if( FAILED( hr = m_pDD->CreateClipper( 0, &pcClipper, NULL ) ) ) {
return E_FAIL;
}
if( FAILED( hr = pcClipper->SetHWnd( 0, parentWnd->m_hWnd ) ) ) {
pcClipper->Release();
return E_FAIL;
}
if( FAILED( hr = m_pdds->SetClipper( pcClipper ) ) ) {
pcClipper->Release();
return E_FAIL;
}
// Done with clipper
pcClipper->Release();
return S_OK;
}
HRESULT WINAPI CDisplay::EnumModesCallback( LPDDSURFACEDESC2 lpDDSurfaceDesc, LPVOID lpDisplay )
{
CDisplay* display = static_cast<CDisplay*>(lpDisplay);
if ( display ) {
mode m;
m.width = lpDDSurfaceDesc->dwWidth;
m.height = lpDDSurfaceDesc->dwHeight;
m.bpp = lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount;
display->modes.push_back( m );
}
return DDENUMRET_OK;
}
HRESULT CDisplay::CreateFullScreenDisplay( CWnd* _parentWnd, int width, int height, int bpp )
{
parentWnd = _parentWnd;
HRESULT hr;
DestroyObjects();
if( FAILED( hr = DirectDrawCreateEx( NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL ) ) ) {
return E_FAIL;
}
hr = m_pDD->SetCooperativeLevel( parentWnd->m_hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( FAILED( hr ) ) {
return E_FAIL;
}
m_pDD->EnumDisplayModes( DDEDM_STANDARDVGAMODES, NULL, this, EnumModesCallback );
std::sort( modes.begin(), modes.end() );
std::vector< mode >::const_iterator it = modes.begin();
int w = 0;
int h = 0;
while ( it != modes.end() ) {
if ( it->bpp == bpp ) {
if ( it->width >= width && it->height >= height ) {
w = it->width;
h = it->height;
break;
}
}
it++;
}
if ( !w || !h ) {
return E_FAIL;
}
if( FAILED( m_pDD->SetDisplayMode( w, h, bpp, 0, 0 ) ) ) {
return E_FAIL;
}
// Create the primary surface
DDSURFACEDESC2 ddsd;
::ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if( FAILED( hr = m_pDD->CreateSurface( &ddsd, &m_pdds, NULL ) ) ) {
return E_FAIL;
}
m_ddsd.dwSize = sizeof( m_ddsd );
m_pdds->GetSurfaceDesc( &m_ddsd );
// Create the backbuffer surface
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
if( FAILED( m_pDD->CreateSurface( &ddsd, &m_bg_pdds, NULL ) ) ) {
return E_FAIL;
}
m_bg_ddsd.dwSize = sizeof( m_bg_ddsd );
m_bg_pdds->GetSurfaceDesc( &m_bg_ddsd );
RECT rc;
::SetRect( &rc, 0, 0, width, height );
::AdjustWindowRectEx( &rc, parentWnd->GetStyle(), parentWnd->GetMenu() != NULL, parentWnd->GetExStyle() );
parentWnd->SetWindowPos( NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
parentWnd->SetWindowPos( &CWnd::wndNoTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
return S_OK;
}