Cxbx-Reloaded/Source/Win32/CxbxKrnl/EmuXD3D.cpp

349 lines
12 KiB
C++
Raw Normal View History

// ******************************************************************
// *
// * .,-::::: .,:: .::::::::. .,:: .:
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
// * $$$ Y$$$P $$""""Y$$ Y$$$P
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
// *
// * Cxbx->Win32->CxbxKrnl->EmuXD3D.cpp
// *
// * This file is part of the Cxbx project.
// *
// * Cxbx and Cxbe are free software; you can redistribute them
// * and/or modify them under the terms of the GNU General Public
// * License as published by the Free Software Foundation; either
// * version 2 of the license, or (at your option) any later version.
// *
// * This program 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 recieved a copy of the GNU General Public License
// * along with this program; see the file COPYING.
// * If not, write to the Free Software Foundation, Inc.,
// * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA.
// *
// * (c) 2002-2003 Aaron Robinson <caustik@caustik.com>
// *
// * All rights reserved
// *
// ******************************************************************
2003-02-21 00:07:28 +00:00
#define CXBXKRNL_INTERNAL
#include "Cxbx.h"
#include "EmuX.h"
2003-02-21 00:07:28 +00:00
namespace win32
{
#include <process.h>
}
2003-03-04 07:05:25 +00:00
#include "ResCxbxDll.h"
using namespace win32;
// ******************************************************************
// * globals
// ******************************************************************
2003-02-20 08:07:52 +00:00
LPDIRECT3D8 g_pD3D8 = NULL; // Direct3D8
LPDIRECT3DDEVICE8 g_pD3D8Device = NULL; // Direct3D8 Device
HWND g_EmuXWindow = NULL; // Rendering Window
// ******************************************************************
// * static
// ******************************************************************
static LRESULT WINAPI EmuXMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
2003-02-21 04:51:03 +00:00
static void EmuXRenderWindow(PVOID);
// ******************************************************************
// * func: EmuXInitD3D
// ******************************************************************
VOID xboxkrnl::EmuXInitD3D()
2003-02-21 00:07:28 +00:00
{
// ******************************************************************
// * spark up a new thread to handle window message processing
// ******************************************************************
{
2003-02-21 04:51:03 +00:00
_beginthread(EmuXRenderWindow, 0, NULL);
2003-02-21 00:07:28 +00:00
while(g_EmuXWindow == NULL)
2003-03-04 07:05:25 +00:00
Sleep(100);
2003-02-21 00:07:28 +00:00
}
// ******************************************************************
// * create Direct3D8
// ******************************************************************
{
// xbox Direct3DCreate8 returns "1" always, so we need our own ptr
g_pD3D8 = Direct3DCreate8(D3D_SDK_VERSION);
}
}
// ******************************************************************
2003-02-21 04:51:03 +00:00
// * func: EmuXRenderWindow
2003-02-21 00:07:28 +00:00
// ******************************************************************
2003-02-21 04:51:03 +00:00
void EmuXRenderWindow(PVOID)
{
// ******************************************************************
// * register window class
// ******************************************************************
{
2003-03-04 07:05:25 +00:00
HMODULE hCxbxDll = GetModuleHandle("Cxbx.dll");
WNDCLASSEX wc =
{
sizeof(WNDCLASSEX),
CS_CLASSDC,
EmuXMsgProc,
0, 0, GetModuleHandle(NULL),
2003-03-04 07:05:25 +00:00
LoadIcon(hCxbxDll, MAKEINTRESOURCE(IDI_CXBX)), LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_APPWORKSPACE + 1), NULL,
"CxbxRender",
NULL
};
RegisterClassEx(&wc);
}
// ******************************************************************
// * create the window
// ******************************************************************
{
2003-02-19 20:53:33 +00:00
g_EmuXWindow = CreateWindow
(
2003-02-21 20:33:21 +00:00
"CxbxRender", "Cxbx : Xbox Emulator",
WS_OVERLAPPEDWINDOW, 100, 100, 640, 480,
GetDesktopWindow(), NULL, GetModuleHandle(NULL), NULL
);
}
// ******************************************************************
// * display the window
// ******************************************************************
{
2003-02-19 20:53:33 +00:00
ShowWindow(g_EmuXWindow, SW_SHOWDEFAULT);
UpdateWindow(g_EmuXWindow);
}
2003-02-22 07:49:02 +00:00
// ******************************************************************
// * message processing loop
// ******************************************************************
{
MSG msg;
2003-02-22 07:49:02 +00:00
ZeroMemory(&msg, sizeof(msg));
2003-02-21 00:07:28 +00:00
2003-02-22 07:49:02 +00:00
while(msg.message != WM_QUIT)
2003-02-21 00:07:28 +00:00
{
2003-02-22 07:49:02 +00:00
if(PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
Sleep(10);
2003-02-21 00:07:28 +00:00
}
2003-02-22 07:49:02 +00:00
ExitProcess(0);
}
}
2003-02-21 04:51:03 +00:00
// ******************************************************************
// * func: EmuXMsgProc
// ******************************************************************
LRESULT WINAPI EmuXMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// ******************************************************************
// * func: EmuXIDirect3D8_CreateDevice
// ******************************************************************
2003-02-19 20:53:33 +00:00
HRESULT WINAPI xboxkrnl::EmuXIDirect3D8_CreateDevice
(
UINT Adapter,
D3DDEVTYPE DeviceType,
HWND hFocusWindow,
DWORD BehaviorFlags,
D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice8 **ppReturnedDeviceInterface
)
{
2003-02-19 20:53:33 +00:00
EmuXSwapFS(); // Win2k/XP FS
// ******************************************************************
// * debug trace
// ******************************************************************
#ifdef _DEBUG_TRACE
{
printf("EmuXD3D8 (0x%.08X): EmuXIDirect3D8_CreateDevice\n"
"(\n"
" Adapter : 0x%.08X\n"
" DeviceType : 0x%.08X\n"
" hFocusWindow : 0x%.08X\n"
" BehaviorFlags : 0x%.08X\n"
" pPresentationParameters : 0x%.08X\n"
" ppReturnedDeviceInterface : 0x%.08X\n"
");\n",
GetCurrentThreadId(), Adapter, DeviceType, hFocusWindow,
BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
}
#endif
// ******************************************************************
// * make adjustments to parameters to make sense with windows d3d
// ******************************************************************
{
Adapter = D3DADAPTER_DEFAULT;
pPresentationParameters->Windowed = TRUE;
2003-02-23 08:01:01 +00:00
// TODO: More intelligently set this only when the game wants it
pPresentationParameters->SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
2003-02-19 20:53:33 +00:00
hFocusWindow = g_EmuXWindow;
2003-02-22 07:49:02 +00:00
// TODO: Use lookup table that is dependant on library version
{
// Tricky MS randomizing .h #defines :[
if(pPresentationParameters->BackBufferFormat == 0x07)
pPresentationParameters->BackBufferFormat = D3DFMT_X8R8G8B8;
2003-02-19 20:53:33 +00:00
2003-02-22 07:49:02 +00:00
// Tricky MS randomizing .h #defines :[
if(pPresentationParameters->AutoDepthStencilFormat == 0x2A)
pPresentationParameters->AutoDepthStencilFormat = D3DFMT_D24S8;
}
2003-02-19 20:53:33 +00:00
}
2003-02-20 08:07:52 +00:00
// ******************************************************************
// * TODO: Query for Software Vertex Processing abilities!!
// ******************************************************************
2003-02-22 07:49:02 +00:00
BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
2003-02-20 08:07:52 +00:00
2003-02-19 20:53:33 +00:00
// ******************************************************************
// * redirect to windows d3d
// ******************************************************************
2003-02-20 08:07:52 +00:00
HRESULT ret = g_pD3D8->CreateDevice
2003-02-19 20:53:33 +00:00
(
Adapter,
DeviceType,
hFocusWindow,
2003-02-22 07:49:02 +00:00
BehaviorFlags,
2003-02-19 20:53:33 +00:00
pPresentationParameters,
ppReturnedDeviceInterface
);
2003-02-20 08:07:52 +00:00
// ******************************************************************
// * it is necessary to store this pointer globally for emulation
// ******************************************************************
2003-02-22 07:49:02 +00:00
g_pD3D8Device = *ppReturnedDeviceInterface;
2003-02-20 08:07:52 +00:00
2003-02-19 20:53:33 +00:00
EmuXSwapFS(); // XBox FS
return ret;
}
// ******************************************************************
2003-02-21 00:07:28 +00:00
// * func: EmuXIDirect3DDevice8_Clear
2003-02-19 20:53:33 +00:00
// ******************************************************************
HRESULT WINAPI xboxkrnl::EmuXIDirect3DDevice8_Clear
(
DWORD Count,
CONST D3DRECT *pRects,
DWORD Flags,
D3DCOLOR Color,
float Z,
DWORD Stencil
)
{
EmuXSwapFS(); // Win2k/XP FS
// ******************************************************************
// * debug trace
// ******************************************************************
#ifdef _DEBUG_TRACE
{
printf("EmuXD3D8 (0x%.08X): EmuXIDirect3DDevice8_Clear\n"
"(\n"
" Count : 0x%.08X\n"
" pRects : 0x%.08X\n"
" Flags : 0x%.08X\n"
" Color : 0x%.08X\n"
" Z : 0x%.08X\n"
" Stencil : 0x%.08X\n"
");\n",
GetCurrentThreadId(), Count, pRects, Flags,
Color, Z, Stencil);
}
#endif
// ******************************************************************
// * make adjustments to parameters to make sense with windows d3d
// ******************************************************************
{
2003-02-21 20:33:21 +00:00
// TODO: D3DCLEAR_TARGET_A, *R, *G, *B don't exist on windows
2003-02-22 07:49:02 +00:00
// TODO: Use lookup table that is dependant on library version
2003-02-20 21:42:39 +00:00
// Tricky MS randomizing .h #defines :[
2003-02-22 07:49:02 +00:00
DWORD newFlags = 0;
2003-02-20 08:07:52 +00:00
2003-02-22 07:49:02 +00:00
if(Flags & 0x000000f0l)
newFlags |= D3DCLEAR_TARGET;
2003-02-20 08:07:52 +00:00
2003-02-22 07:49:02 +00:00
if(Flags & 0x00000001l)
newFlags |= D3DCLEAR_ZBUFFER;
2003-02-20 08:07:52 +00:00
2003-02-22 07:49:02 +00:00
if(Flags & 0x00000002l)
newFlags |= D3DCLEAR_STENCIL;
2003-02-20 08:07:52 +00:00
2003-02-22 07:49:02 +00:00
Flags = newFlags;
2003-02-19 20:53:33 +00:00
}
2003-02-20 08:07:52 +00:00
HRESULT ret = g_pD3D8Device->Clear(Count, pRects, Flags, Color, Z, Stencil);
2003-02-19 20:53:33 +00:00
EmuXSwapFS(); // XBox FS
return ret;
}
2003-02-21 00:07:28 +00:00
// ******************************************************************
// * func: EmuXIDirect3DDevice8_Swap
// ******************************************************************
HRESULT WINAPI xboxkrnl::EmuXIDirect3DDevice8_Swap
(
DWORD Flags
)
{
EmuXSwapFS(); // Win2k/XP FS
// ******************************************************************
// * debug trace
// ******************************************************************
#ifdef _DEBUG_TRACE
{
printf("EmuXD3D8 (0x%.08X): EmuXIDirect3DDevice8_Swap\n"
"(\n"
" Flags : 0x%.08X\n"
");\n",
GetCurrentThreadId(), Flags);
}
#endif
// TODO: Ensure this flag is always the same across library versions
if(Flags != 0)
EmuXPanic();
// Swap(0) is equivalent to present(0,0,0,0)
HRESULT ret = g_pD3D8Device->Present(0, 0, 0, 0);
EmuXSwapFS(); // XBox FS
return ret;
}