Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libmupen64plus/D3D8Interceptor/d3d8Wrapper.cpp
2 views
1
#include "d3d8Wrapper.h"
2
3
D3D8Base::LPDIRECT3D8 g_D3D=NULL;
4
5
HMODULE realDLL;
6
7
extern "C"
8
{
9
namespace D3D8Wrapper
10
{
11
IDirect3DDevice8 *last_device = NULL;
12
IDirect3DSurface8 *render_surface = NULL;
13
void (*rendering_callback)( int );
14
15
D3D8Wrapper::IDirect3D8* WINAPI Direct3DCreate8(UINT Version)
16
{
17
//sometimes, Intel drivers will clear the dll path. So let's save and restore it (do their job for them)
18
char oldDllDirectory[MAX_PATH];
19
GetDllDirectory(MAX_PATH, oldDllDirectory);
20
21
// Get the real DLL path from the system directory, needs to be specific to avoid binding to the d3d8.dll we're in now!
22
// Might be unsafe
23
CHAR dll_path[1024];
24
GetSystemDirectory(dll_path,1024);
25
strcat(dll_path,"\\d3d8.dll");
26
27
realDLL = LoadLibrary(dll_path);
28
29
D3D8Wrapper::D3DCREATE realDirect3DCreate8 = (D3D8Wrapper::D3DCREATE)GetProcAddress(realDLL, "Direct3DCreate8");
30
31
// Use the real Direct3DCreate8 to make the base object
32
D3D8Base::IDirect3D8* realD3D = realDirect3DCreate8(D3D_SDK_VERSION);
33
34
// Wrap the object
35
D3D8Wrapper::IDirect3D8* wrappedD3D = D3D8Wrapper::IDirect3D8::GetDirect3D(realD3D);
36
37
//restore old DLL directory
38
SetDllDirectory(oldDllDirectory);
39
40
return wrappedD3D;
41
}
42
}
43
44
45
__declspec(dllexport) void __cdecl CloseDLL()
46
{
47
FreeLibrary(realDLL);
48
}
49
50
__declspec(dllexport) void __cdecl SetRenderingCallback(void (*callback)(int))
51
{
52
D3D8Wrapper::rendering_callback = callback;
53
}
54
55
__declspec(dllexport) void __cdecl ReadScreen(void *dest, int *width, int *height)
56
{
57
if (D3D8Wrapper::last_device == NULL)
58
{
59
*width = 0;
60
*height = 0;
61
return;
62
}
63
64
// surface...
65
// make a D3DSURFACE_DESC, pass to GetDesc
66
D3D8Base::D3DSURFACE_DESC desc;
67
D3D8Wrapper::render_surface->GetDesc(&desc);
68
69
// get out height/width
70
*width = desc.Width;
71
*height = desc.Height;
72
73
// if dest isn't null
74
if (dest != NULL)
75
{
76
// make a RECT with size of buffer
77
RECT entire_buffer;
78
entire_buffer.left = 0;
79
entire_buffer.top = 0;
80
entire_buffer.right = desc.Width;
81
entire_buffer.bottom = desc.Height;
82
83
//resolve rendertarget to a system memory texture, for locking
84
//TODO! UNACCEPTABLE CODE! THIS IS HORRIBLE!
85
//X8R8G8B8 or fail -- A8R8G8B8 will malfunction (is it because the source surface format isnt matching? I think so)
86
static D3D8Wrapper::IDirect3DTexture8* tex = NULL;
87
static RECT texRect;
88
if(!tex || (texRect.right != entire_buffer.right) || (texRect.bottom != entire_buffer.bottom))
89
{
90
if(tex)
91
tex->Release();
92
texRect = entire_buffer;
93
D3D8Wrapper::last_device->CreateTexture(desc.Width, desc.Height, 1, 0, D3D8Base::D3DFMT_X8R8G8B8, D3D8Base::D3DPOOL_SYSTEMMEM, &tex);
94
}
95
96
D3D8Wrapper::IDirect3DSurface8* surf;
97
tex->GetSurfaceLevel(0,&surf);
98
HRESULT hr = D3D8Wrapper::last_device->CopyRects(D3D8Wrapper::render_surface,NULL,0,surf,NULL);
99
100
// make a D3DLOCKED_RECT, pass to LockRect
101
D3D8Base::D3DLOCKED_RECT locked;
102
hr = surf->LockRect(&locked,&entire_buffer,D3DLOCK_READONLY);
103
104
//this loop was reversed from the original.
105
//it should be faster anyway if anything since the reading can be prefetched forwardly.
106
//TODO - allow bizhawk to handle flipped images.... nonetheless, it might not handle images with unusual pitches, although maybe we should consider that.
107
//so this code will probably remain for quite some time
108
int dest_row = desc.Height - 1;
109
for (UINT from_row = 0; from_row < desc.Height; from_row++)
110
{
111
memcpy((char*)dest + (dest_row * desc.Width*4),(char*)locked.pBits + from_row * locked.Pitch, desc.Width*4);
112
dest_row--;
113
}
114
115
// unlock rect
116
surf->UnlockRect();
117
surf->Release();
118
}
119
120
// release the surface
121
//backbuffer->Release();
122
123
// we're done, maybe?
124
}
125
}
126