-
Notifications
You must be signed in to change notification settings - Fork 2
/
display.cpp
336 lines (277 loc) · 9.43 KB
/
display.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
#include <windows.h>
#include <d3d9.h>
#include "header.h"
#include "PaletteShader.h"
HWND hwnd_main = NULL;
BOOL IsWindowed = FALSE;
BOOL import_gdi_bits = FALSE;
WORD prtscn_toggle;
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DVERTEXBUFFER9 d3dvb = NULL;
IDirect3DTexture9* d3dtex = NULL;
IDirect3DTexture9* d3dpal = NULL;
IDirect3DPixelShader9* shader = NULL;
__declspec(align(128)) BYTE client_bits[640*480];
BYTE* dib_bits;
HDC hdc_offscreen;
HBITMAP hbmp_old;
COLORREF clear_color;
// double buffered (discard) with no vsync seems best,
// as there doesn't appear to be a way to drop frames while triple buffering w/vsync,
// ...which might be needed???,
// ...since we don't want to alter how many fps the game can draw
D3DPRESENT_PARAMETERS d3dpp = {
d3dpp.BackBufferWidth = 640,
d3dpp.BackBufferHeight = 480,
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8,
d3dpp.BackBufferCount = 1,
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE,
d3dpp.MultiSampleQuality = 0,
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD,
d3dpp.hDeviceWindow = 0,
d3dpp.Windowed = TRUE,
d3dpp.EnableAutoDepthStencil = FALSE,
d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN,
d3dpp.Flags = 0,
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT,
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE
};
BITMAPINFO256 bmi = {
bmi.h.biSize = sizeof( BITMAPINFOHEADER ),
bmi.h.biWidth = 640,
bmi.h.biHeight = 0 - 480, // origin top-left
bmi.h.biPlanes = 1,
bmi.h.biBitCount = 8,
bmi.h.biCompression = BI_RGB,
bmi.h.biSizeImage = 0,
bmi.h.biXPelsPerMeter = 0,
bmi.h.biYPelsPerMeter = 0,
bmi.h.biClrUsed = 0,
bmi.h.biClrImportant = 0
};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_TEX1)
struct CUSTOMVERTEX {float x, y, z, rhw, u, v;};
CUSTOMVERTEX vertices[] =
{
{ 0.0f - 0.5f, 480.0f - 0.5f, 0.0f, 1.0f, 0.0f, 480.0f / 1024.0f },
{ 0.0f - 0.5f, 0.0f - 0.5f, 0.0f, 1.0f, 0.0f, 0.0f },
{ 640.0f - 0.5f, 480.0f - 0.5f, 0.0f, 1.0f, 640.0f / 1024.0f, 480.0f / 1024.0f },
{ 640.0f - 0.5f, 0.0f - 0.5f, 0.0f, 1.0f, 640.0f / 1024.0f, 0.0f }
};
////////////////////////////////////////////////////////////////////////////////
HRESULT lock( LONG* pitch, void** surf_bits )
{
SDLGDIALOG_CACHE_ENTRY* ce;
HDC hdc;
DWORD i;
WORD key_state;
HRESULT hr;
D3DLOCKED_RECT lock_rc;
RECT rc = { 0,0,640,480 };
if( SDlgDialog_count == 0 )
{
hr = d3dtex->LockRect( 0, &lock_rc, &rc, 0 );
*pitch = lock_rc.Pitch;
*surf_bits = lock_rc.pBits;
return hr;
}
*pitch = 640;
*surf_bits = dib_bits;
if( import_gdi_bits == FALSE ){
key_state = GetKeyState( VK_SNAPSHOT );
// if_not (current_toggle_state ^ is_key_down ^ pev_toggle_state) ret;
if( !( ( key_state & 1 ) ^ ( key_state >> 15 ) ^ prtscn_toggle ) ) return 0;
// else update prev state then fall thru to import gdi bits for screenshot
prtscn_toggle ^= 1;
}
// assumes SDlgDialog_cache is sorted by z-order...(it is sorted by age not z-order)
// color converts from 32bpp to 8bpp... ( slow, is gdi trust worthy for this? )
for( i = 0; i < SDlgDialog_count; i++ ){
ce = &SDlgDialog_cache[i];
hdc = GetDC( ce->hwnd );
BitBlt( hdc_offscreen, ce->x, ce->y, ce->cx, ce->cy, hdc, 0, 0, SRCCOPY );
ReleaseDC( ce->hwnd, hdc );
}
GdiFlush();
return 0;
}
void unlock( void* surface ){
HDC hdc;
DWORD i;
SDLGDIALOG_CACHE_ENTRY* ce;
D3DLOCKED_RECT lock_rc;
RECT rc = { 0,0,640,480 };
if( surface != dib_bits ){
d3dtex->UnlockRect(0);
d3ddev->BeginScene();
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
d3ddev->EndScene();
if( FAILED( d3ddev->Present(NULL, NULL, NULL, NULL) ) ) d3d_reset();
return;
}
if( SUCCEEDED( d3dtex->LockRect( 0, &lock_rc, &rc, 0 ) ) ){
if( import_gdi_bits != FALSE ){
for( i = 0; i < SDlgDialog_count; i++ ){
ce = &SDlgDialog_cache[i];
hdc = GetDCEx( ce->hwnd, NULL, DCX_PARENTCLIP | DCX_CACHE );
BitBlt( hdc, 0, 0, ce->cx, ce->cy, hdc_offscreen, ce->x, ce->y, SRCCOPY );
ReleaseDC( ce->hwnd, hdc );
}
GdiFlush();
for( int i = 0; i < 480; i++ ){ // for each scanline
color_convert( &(((BYTE*)dib_bits)[i*640]), bmi.palette, (DWORD*)(((BYTE*)lock_rc.pBits) + (lock_rc.Pitch * i)), 640/4 );
}
} else {
for( i = 0; i < SDlgDialog_count; i++ ){
ce = &SDlgDialog_cache[i];
hdc = GetDCEx( ce->hwnd, NULL, DCX_PARENTCLIP | DCX_CACHE );
GdiTransparentBlt( hdc, 0, 0, ce->cx, ce->cy,
hdc_offscreen, ce->x, ce->y, ce->cx, ce->cy, clear_color );
ReleaseDC( ce->hwnd, hdc );
}
GdiFlush();
multiblt( lock_rc.Pitch, (DWORD*) lock_rc.pBits );
}
return unlock( lock_rc.pBits );
}
}
void cleanup( void )
{
; // todo
}
#pragma intrinsic( _byteswap_ulong, memcpy )
void set_palette( PALETTEENTRY* colors ){
DWORD i;
D3DLOCKED_RECT lock_rc;
RECT rc = { 0,0,256,1 };
// not to speed critical here...
// so don't diff between "in-game" and "bnet menu" modes
for( i = 0; i < 256; i++ )
{ // convert 0xFFBBGGRR to 0x00RRGGBB ( X8R8G8B8 )
// speed is unknown
*((DWORD*)&bmi.palette[i]) = _byteswap_ulong( *(DWORD*)&colors[i] ) >> 8;
}
if( SUCCEEDED( d3dpal->LockRect( 0, &lock_rc, &rc, 0 ) ) ){
memcpy( lock_rc.pBits, bmi.palette, 4 * 256 );
d3dpal->UnlockRect(0);
}
clear_color = *((DWORD*)&colors[254]) & 0x00FFFFFF;
SetDIBColorTable( hdc_offscreen, 0, 256, bmi.palette );
// animate palette
unlock(0);
}
#pragma intrinsic( memcpy )
HRESULT init( HWND hwnd )
{
HBITMAP hbmp;
HRESULT hr;
void* vb;
hwnd_main = hwnd;
d3dpp.hDeviceWindow = hwnd;
if( ! IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE ) ){
import_gdi_bits = TRUE; // sse2 not supported use only gdi instead
}
// create dib_section ( offscreen gdi drawing surface )
hbmp = CreateDIBSection( NULL, (BITMAPINFO*) &bmi, DIB_RGB_COLORS, (void**) &dib_bits, NULL, 0 );
hdc_offscreen = CreateCompatibleDC( NULL );
hbmp_old = (HBITMAP) SelectObject( hdc_offscreen, hbmp );
if( ( hbmp == NULL ) || ( hbmp_old == NULL ) ) goto fail;
//
HookWndProcs( hwnd );
// needed on bnet screens
// because the sibling windows are not scaled to new resolutions.
SetResolution_640x480();
// the window size is originally the size of the desktop...
SetWindowPos( hwnd, HWND_TOP, 0, 0, 640, 480, SWP_NOOWNERZORDER | SWP_NOZORDER );
// init d3d
// must not be exclusive, to keep gdi drawing visible
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(!d3d) goto fail;
hr = d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_NOWINDOWCHANGES | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
&d3dpp, &d3ddev );
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->SetFVF(CUSTOMFVF);
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->CreateVertexBuffer( sizeof(vertices), 0, CUSTOMFVF, D3DPOOL_MANAGED, &d3dvb, NULL );
if( FAILED ( hr ) ) goto fail;
hr = d3dvb->Lock(0, 0, (void**)&vb, 0);
if( FAILED ( hr ) ) goto fail;
memcpy(vb, vertices, sizeof(vertices));
hr = d3dvb->Unlock();
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->SetStreamSource(0, d3dvb, 0, sizeof(CUSTOMVERTEX));
if( FAILED ( hr ) ) goto fail;
// todo: NPOT if supported
hr = d3ddev->CreateTexture( 1024, 1024, 1, 0, D3DFMT_L8, D3DPOOL_MANAGED, &d3dtex, 0 );
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->SetTexture( 0, d3dtex );
if( FAILED ( hr ) ) goto fail;
// todo: NPOT if supported
hr = d3ddev->CreateTexture( 256, 256, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &d3dpal, 0 );
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->SetTexture( 1, d3dpal );
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->CreatePixelShader((DWORD*)g_ps20_main, &shader);
if( FAILED ( hr ) ) goto fail;
hr = d3ddev->SetPixelShader(shader);
if( FAILED ( hr ) ) goto fail;
// ...?
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
d3ddev->SetRenderState(D3DRS_COLORVERTEX, FALSE);
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);
// ...?
d3ddev->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_POINT);
d3ddev->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_POINT);
d3ddev->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_NONE);
// success
return S_OK;
fail:
MessageBox( hwnd, "Display Failure!", "aqrit's compat mod", MB_OK );
return E_FAIL;
}
BOOL SetResolution_640x480(void)
{
DEVMODE devMode;
RtlSecureZeroMemory(&devMode, sizeof(devMode));
devMode.dmSize = sizeof(devMode);
devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
devMode.dmPelsWidth = 640;
devMode.dmPelsHeight = 480;
if( DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettings(&devMode, CDS_FULLSCREEN) )
{
return TRUE;
}
return FALSE;
}
#pragma intrinsic( memcpy )
void d3d_reset(void)
{
void* vb;
if( SUCCEEDED( d3ddev->Reset( &d3dpp ) ) )
{
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, d3dvb, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetTexture( 0, d3dtex );
d3ddev->SetTexture( 1, d3dpal );
d3ddev->SetPixelShader(shader);
// ...?
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
d3ddev->SetRenderState(D3DRS_COLORVERTEX, FALSE);
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);
// ...?
d3ddev->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_POINT);
d3ddev->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_POINT);
d3ddev->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_NONE);
if( SUCCEEDED( d3dvb->Lock(0, 0, (void**)&vb, 0) ) ) {
memcpy(vb, vertices, sizeof(vertices));
d3dvb->Unlock();
}
}
else Sleep( 100 );
}