isle-portable/LEGO1/mxdirectx/mxdirect3d.cpp

317 lines
7.3 KiB
C++
Raw Normal View History

#include "mxdirect3d.h"
2023-10-24 19:38:27 -04:00
#include <SDL3/SDL.h> // for SDL_Log
DECOMP_SIZE_ASSERT(MxDirect3D, 0x894)
#if !defined(MXDIRECTX_FOR_CONFIG)
#define RELEASE(x) \
if (x != NULL) { \
x->Release(); \
x = NULL; \
}
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// FUNCTION: LEGO1 0x1009b0a0
MxDirect3D::MxDirect3D()
{
2023-10-24 19:38:27 -04:00
this->m_pDirect3d = NULL;
this->m_pDirect3dDevice = NULL;
this->m_bTexturesDisabled = FALSE;
this->m_assignedDevice = NULL;
}
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// FUNCTION: LEGO1 0x1009b140
MxDirect3D::~MxDirect3D()
{
2023-10-24 19:38:27 -04:00
Destroy();
}
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// FUNCTION: LEGO1 0x1009b1a0
BOOL MxDirect3D::Create(
2023-10-24 19:38:27 -04:00
HWND hWnd,
BOOL fullscreen_1,
BOOL surface_fullscreen,
BOOL onlySystemMemory,
int width,
int height,
int bpp,
const PALETTEENTRY* pPaletteEntries,
int paletteEntryCount
)
{
2023-10-24 19:38:27 -04:00
BOOL success = FALSE;
BOOL ret = MxDirectDraw::Create(
hWnd,
fullscreen_1,
surface_fullscreen,
onlySystemMemory,
width,
height,
bpp,
pPaletteEntries,
paletteEntryCount
);
if (ret && D3DCreate() && D3DSetMode()) {
2023-10-24 19:38:27 -04:00
success = TRUE;
}
2023-10-24 19:38:27 -04:00
if (!success) {
FUN_1009d920();
}
2023-10-24 19:38:27 -04:00
return success;
}
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// FUNCTION: LEGO1 0x1009b210
void MxDirect3D::Destroy()
{
RELEASE(m_pDirect3dDevice);
RELEASE(m_pDirect3d);
2023-10-24 19:38:27 -04:00
if (this->m_assignedDevice) {
delete m_assignedDevice;
this->m_assignedDevice = NULL;
2023-10-24 19:38:27 -04:00
}
if (m_pCurrentDeviceModesList) {
2023-10-24 19:38:27 -04:00
m_pCurrentDeviceModesList = NULL;
}
2023-10-24 19:38:27 -04:00
MxDirectDraw::Destroy();
}
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// FUNCTION: LEGO1 0x1009b290
void MxDirect3D::DestroyButNotDirectDraw()
{
RELEASE(m_pDirect3dDevice);
RELEASE(m_pDirect3d);
2023-10-24 19:38:27 -04:00
MxDirectDraw::DestroyButNotDirectDraw();
}
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// FUNCTION: LEGO1 0x1009b2d0
BOOL MxDirect3D::D3DCreate()
{
HRESULT result;
result = DirectDraw()->QueryInterface(IID_IDirect3D2, (LPVOID*) &m_pDirect3d);
if (result != DD_OK) {
Error("Creation of IDirect3D failed", result);
2023-10-24 19:38:27 -04:00
return FALSE;
}
return TRUE;
}
// FUNCTION: LEGO1 0x1009b310
BOOL MxDirect3D::D3DSetMode()
{
if (m_assignedDevice->m_flags & MxAssignedDevice::c_hardwareMode) {
if (m_bOnlySoftRender) {
Error("Failed to place vital surfaces in video memory for hardware driver", DDERR_GENERIC);
return FALSE;
}
if (m_assignedDevice->m_desc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
m_bTexturesDisabled = FALSE;
}
else {
m_bTexturesDisabled = TRUE;
}
if (!CreateZBuffer(DDSCAPS_VIDEOMEMORY, ZBufferDepth(m_assignedDevice))) {
return FALSE;
}
}
else {
if (m_assignedDevice->m_desc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
m_bTexturesDisabled = FALSE;
}
else {
m_bTexturesDisabled = TRUE;
}
if (!CreateZBuffer(DDSCAPS_SYSTEMMEMORY, ZBufferDepth(m_assignedDevice))) {
return FALSE;
}
}
HRESULT result = m_pDirect3d->CreateDevice(m_assignedDevice->m_guid, m_pBackBuffer, &m_pDirect3dDevice);
if (result != DD_OK) {
Error("Create D3D device failed", result);
return FALSE;
}
DeviceModesInfo::Mode mode = m_currentMode;
if (IsFullScreen()) {
if (!IsSupportedMode(mode.width, mode.height, mode.bitsPerPixel)) {
Error("This device cannot support the current display mode", DDERR_GENERIC);
return FALSE;
}
}
LPDIRECTDRAWSURFACE frontBuffer = m_pFrontBuffer;
LPDIRECTDRAWSURFACE backBuffer = m_pBackBuffer;
DDSURFACEDESC desc;
memset(&desc, 0, sizeof(desc));
desc.dwSize = sizeof(desc);
if (backBuffer->Lock(NULL, &desc, DDLOCK_WAIT, NULL) == DD_OK) {
unsigned char* surface = (unsigned char*) desc.lpSurface;
for (int i = mode.height; i > 0; i--) {
memset(surface, 0, mode.width * desc.ddpfPixelFormat.dwRGBBitCount / 8);
surface += desc.lPitch;
}
backBuffer->Unlock(desc.lpSurface);
}
else {
SDL_Log("MxDirect3D::D3DSetMode() back lock failed\n");
}
if (m_bFullScreen) {
memset(&desc, 0, sizeof(desc));
desc.dwSize = sizeof(desc);
if (frontBuffer->Lock(NULL, &desc, DDLOCK_WAIT, NULL) == DD_OK) {
unsigned char* surface = (unsigned char*) desc.lpSurface;
for (int i = mode.height; i > 0; i--) {
memset(surface, 0, mode.width * desc.ddpfPixelFormat.dwRGBBitCount / 8);
surface += desc.lPitch;
}
frontBuffer->Unlock(desc.lpSurface);
}
else {
SDL_Log("MxDirect3D::D3DSetMode() front lock failed\n");
}
}
2023-10-24 19:38:27 -04:00
return TRUE;
}
// FUNCTION: LEGO1 0x1009b5a0
int MxDirect3D::ZBufferDepth(MxAssignedDevice* p_assignedDevice)
{
int depth;
DWORD deviceDepth;
if (p_assignedDevice->m_desc.dwFlags & D3DDD_DEVICEZBUFFERBITDEPTH) {
deviceDepth = p_assignedDevice->m_desc.dwDeviceZBufferBitDepth;
}
else {
deviceDepth = 0;
}
if (deviceDepth & DDBD_32) {
depth = 32;
}
else if (deviceDepth & DDBD_24) {
depth = 24;
}
else if (deviceDepth & DDBD_16) {
depth = 16;
}
else if (deviceDepth & DDBD_8) {
depth = 8;
}
else {
depth = -1;
}
return depth;
}
// FUNCTION: LEGO1 0x1009b5f0
// FUNCTION: BETA10 0x1011bbca
BOOL MxDirect3D::SetDevice(MxDeviceEnumerate& p_deviceEnumerate, MxDriver* p_driver, Direct3DDeviceInfo* p_device)
{
if (m_assignedDevice) {
delete m_assignedDevice;
m_assignedDevice = NULL;
m_pCurrentDeviceModesList = NULL;
}
MxAssignedDevice* assignedDevice = new MxAssignedDevice;
int i = 0;
for (list<MxDriver>::iterator it = p_deviceEnumerate.m_list.begin(); it != p_deviceEnumerate.m_list.end(); it++) {
MxDriver& driver = *it;
if (&driver == p_driver) {
assignedDevice->m_deviceInfo = new DeviceModesInfo;
if (driver.m_guid) {
assignedDevice->m_deviceInfo->m_guid = new GUID;
memcpy(assignedDevice->m_deviceInfo->m_guid, driver.m_guid, sizeof(GUID));
}
assignedDevice->m_deviceInfo->m_count = driver.m_displayModes.size();
if (assignedDevice->m_deviceInfo->m_count > 0) {
assignedDevice->m_deviceInfo->m_modeArray =
new DeviceModesInfo::Mode[assignedDevice->m_deviceInfo->m_count];
int j = 0;
for (list<MxDisplayMode>::iterator it2 = driver.m_displayModes.begin();
it2 != driver.m_displayModes.end();
it2++) {
assignedDevice->m_deviceInfo->m_modeArray[j].width = (*it2).m_width;
assignedDevice->m_deviceInfo->m_modeArray[j].height = (*it2).m_height;
assignedDevice->m_deviceInfo->m_modeArray[j].bitsPerPixel = (*it2).m_bitsPerPixel;
j++;
}
}
memcpy(
&assignedDevice->m_deviceInfo->m_ddcaps,
&driver.m_ddCaps,
sizeof(assignedDevice->m_deviceInfo->m_ddcaps)
);
if (i == 0) {
assignedDevice->m_flags |= MxAssignedDevice::c_primaryDevice;
}
for (list<Direct3DDeviceInfo>::iterator it2 = driver.m_devices.begin(); it2 != driver.m_devices.end();
it2++) {
Direct3DDeviceInfo& device = *it2;
if (&device != p_device) {
continue;
}
memcpy(&assignedDevice->m_guid, device.m_guid, sizeof(assignedDevice->m_guid));
D3DDEVICEDESC* desc;
if (device.m_HWDesc.dcmColorModel) {
assignedDevice->m_flags |= MxAssignedDevice::c_hardwareMode;
desc = &device.m_HWDesc;
}
else {
desc = &device.m_HELDesc;
}
memcpy(&assignedDevice->m_desc, desc, sizeof(assignedDevice->m_desc));
m_assignedDevice = assignedDevice;
m_pCurrentDeviceModesList = assignedDevice->m_deviceInfo;
break;
}
}
i++;
}
if (!m_assignedDevice) {
delete assignedDevice;
return FALSE;
}
return TRUE;
}
#endif