// archd3d.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #include "arch3dobjdlg.h" #include "df3ddebugdialog.h" #include "dfd3dapp.h" /* Global Variables: */ CDFD3DApp DFD3DApp; CDF3DObjectD3D* pDF3DObject = NULL; CDFTextureD3D DFTexture; LPDIRECT3DRMMESHBUILDER2 pDFBuilder = NULL; int CurrentObjectIndex = -1; /* Foward declarations of functions included in this code module: */ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); #undef __FUNC__ #define __FUNC__ "LoadDF3DObject()" /*=========================================================================== * * Function - boolean LoadDF3DObject (NewIndex); * * Attempt to load a 3DObject into the scene. * *=========================================================================*/ boolean LoadDF3DObject (const int NewObjectIndex) { HRESULT Result; D3DVECTOR CameraVector; LPDIRECT3DRMMATERIAL pMaterial = NULL; LPDIRECT3DRMTEXTURE2 pTexture = NULL; LPDIRECT3DRMFRAME2 pFrame = DFD3DApp.GetOriginFrame(); LPDIRECT3DRMWRAP pWrap = NULL; D3DRMBOX box; D3DRMBOX DFBox; D3DVALUE miny, maxy; D3DVALUE height; /* Ensure a valid mode and objects */ if (DFD3DApp.pDirect3D == NULL || DFD3DApp.pCamera == NULL || pFrame == NULL) { SET_EXT_ERROR(ERR_NULL); return (FALSE); } /* Unselect the previous object if required */ if (pDFBuilder != NULL) { DFD3DApp.UnSelectObject(); Result = pFrame->DeleteVisual(pDFBuilder); if (FAILED(Result)) SystemLog.Printf ("LoadDF3DObject() - Couldn't delete the DF builder object, %s!", D3DRMErrorToString(Result)); } /* Ensure a valid index */ if (!DFArch3D.IsValidRecordIndex(NewObjectIndex) || NewObjectIndex == CurrentObjectIndex) { SET_EXT_ERROR(ERR_INDEX); return (FALSE); } /* Load the new object record */ pDF3DObject = DFArch3D.GetD3DObject(NewObjectIndex); if (pDF3DObject == NULL) return (FALSE); CurrentObjectIndex = NewObjectIndex; /* Make DF object */ pDFBuilder = pDF3DObject->GetD3DBuilder(); if (pDFBuilder == NULL) goto BUILDOBJECT_ERROR; /* Set the color of the mesh */ Result = pDFBuilder->SetColorRGB(D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0)); if (FAILED(Result)) goto BUILDOBJECT_ERROR; /* Create a material */ Result = DFD3DApp.pDirect3D->CreateMaterial(D3DVAL(30.0), &pMaterial); if (FAILED(Result)) goto BUILDOBJECT_ERROR; /* Set the mesh material */ //Result = pDFBuilder->SetMaterial(pMaterial); if (FAILED(Result)) goto BUILDOBJECT_ERROR; RELEASE(pMaterial); /* Retrieve a position to add the new object at */ Result = DFD3DApp.GetPositionInFrontOfCamera(CameraVector, pFrame); if (FAILED(Result)) goto BUILDOBJECT_ERROR; /* Move the new object */ pDFBuilder->GetBox(&DFBox); Result = pDFBuilder->Translate(CameraVector.x, CameraVector.y - DFBox.max.y, CameraVector.z); if (FAILED(Result)) goto BUILDOBJECT_ERROR; pTexture = DFGetD3DTexture(121, 3); pTexture = DFGetD3DTexture(121, 2); //DFD3DApp.pScene->SetSceneBackgroundImage(pTexture); /* Result = DFD3DApp.pD3DRM->LoadTexture("checker1.bmp", &pTexture); if (FAILED(Result)) { SET_D3D_ERROR2(Result, "CDFLandD3D::MakeD3DRMBuilder2() - Failed to load texture!"); } else { DFD3DApp.pScene->SetSceneBackgroundImage(pTexture); Result = pDFBuilder->SetTexture(pTexture); RELEASE(pTexture); if (FAILED(Result)) { SET_D3D_ERROR2(Result, "LoadDF3DObject() - Failed to set builder texture!"); } } //*/ Result = pDFBuilder->GetBox(&box); if (FAILED(Result)) goto BUILDOBJECT_ERROR; maxy = box.max.y; miny = box.min.y; height = maxy - miny; // Create a wrap Result = DFD3DApp.pDirect3D->CreateWrap(D3DRMWRAP_FLAT, NULL, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0), D3DVAL(1.0), D3DVAL(1/(box.max.x - box.min.x)), D3DVAL(1/height), &pWrap); if (FAILED(Result)) { SET_D3D_ERROR2(Result, "LoadDF3DObject() - Failed to create wrap!"); } else { Result = pWrap->Apply((LPDIRECT3DRMOBJECT)pDFBuilder); if (FAILED(Result)) { SET_D3D_ERROR2(Result, "LoadDF3DObject() - Failed to apply wrap!"); } } //*/ /* Attempt to load a texture file */ /* pTexture = DFGetD3DTexture(302, 2); if (pTexture) { Result = DFD3DApp.pScene->SetSceneBackgroundImage(pTexture); Result = pDFBuilder->SetTexture(pTexture); if (FAILED(Result)) { SET_D3D_ERROR2(Result, "CDFLandD3D::MakeD3DRMBuilder2() - Failed to set builder texture!"); } } //*/ /* Add the DF 3DObject to the origin frame */ Result = pFrame->AddVisual(pDFBuilder); if (FAILED(Result)) goto BUILDOBJECT_ERROR; return (TRUE); BUILDOBJECT_ERROR: SystemLog.Printf ("LoadDF3DObject(): A failure occurred while creating the object, %s!", D3DRMErrorToString(Result)); RELEASE(pDFBuilder); RELEASE(pMaterial); //RELEASE(pTexture); RELEASE(pWrap); return (FALSE); } /*=========================================================================== * End of Function LoadDF3DObject() *=========================================================================*/ #undef __FUNC__ #define __FUNC__ "DisplayPlaneInfo()" /*=========================================================================== * * Function - boolean DisplayPlaneInfo (ObjectAppData, FaceIndex); * * Displays information on the given face in the given object. * *=========================================================================*/ boolean DisplayPlaneInfo (const DWORD ObjectAppData, const unsigned long FaceIndex) { char Buffer[1024]; /* Ensure a valid 3DObject */ if (pDF3DObject == NULL) { MessageBox(NULL, "No 3DObject currently loaded!", "DF3D Error", MB_OK | MB_TASKMODAL); return (FALSE); } /* Create the plane info string */ pDF3DObject->GetPlaneInfo(FaceIndex, Buffer, 1023); D3DMessage (DFD3DApp.GetMainWindowHandle(), "Arch3D Plane Info", Buffer); return (TRUE); } /*=========================================================================== * End of Function DisplayPlaneInfo() *=========================================================================*/ #undef __FUNC__ #define __FUNC__ "WndProc()" /*=========================================================================== * * Function - LRESULT WndProc (hWnd, Message ,wParam, lParam); * * Standard windows message handler. * *=========================================================================*/ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId; int wmEvent; int Result; LRESULT lResult; Result = DFD3DApp.HandleMessages(lResult, hWnd, message, wParam, lParam); if (Result) return (lResult); switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); /* Parse the menu selections: */ switch (wmId) { case IDM_FILE_LOADOBJECT: Result = DisplayArch3DObjectDialog(hWnd, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), &DFArch3D); if (Result >= 0) LoadDF3DObject(Result); return (0); case IDM_EXIT: DFD3DApp.TerminateApp(); DestroyWindow(hWnd); break; case IDM_RENDER_POINTS: DFD3DApp.SetFillMode(D3DRMFILL_POINTS); break; case IDM_RENDER_WIREFRAME: DFD3DApp.SetFillMode(D3DRMFILL_WIREFRAME); break; case IDM_RENDER_SOLID: DFD3DApp.SetFillMode(D3DRMFILL_SOLID); break; case IDM_DISPLAY_FPS: DFD3DApp.SetDisplayFrameRate((boolean)!DFD3DApp.GetDisplayFrameRate()); break; case IDM_MOVE_FASTER: DFD3DApp.SetMovementSpeed(D3DVAL(DFD3DApp.GetMovementSpeed()*2.0)); break; case IDM_MOVE_SLOWER: DFD3DApp.SetMovementSpeed(D3DVAL(DFD3DApp.GetMovementSpeed()/2.0)); break; case IDM_MOVE_NORMAL: DFD3DApp.SetMovementSpeed(DF_DEFAULT_MOVEMENTSPEED); break; case IDM_DEBUG_DEBUGWINDOW: DisplayDF3DDebugDialog(hWnd, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE)); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } /*=========================================================================== * End of Function WndProc() *=========================================================================*/ #undef __FUNC__ #define __FUNC__ "WinMain()" /*=========================================================================== * * Function - int WinMain (hInstance, hPrevInstance, pCmdLine, nCmdShow); * * Standard Windows entry point. * *=========================================================================*/ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { int Result; byte Buf[10]; byte* pPtr = Buf; /* Open log file for debugging output */ SystemLog.Open("ArchD3D.log"); SystemLog.Printf ("CDFTextureD3D size = %d", sizeof(CDFTextureD3D)); SystemLog.Printf ("CDFTextureImageD3D size = %d", sizeof(CDFTextureImageD3D)); SystemLog.Printf ("CDFPaletteD3D size = %d", sizeof(CDFPaletteD3D)); SystemLog.Printf ("CDF3DObjectD3D size = %d", sizeof(CDF3DObjectD3D)); SystemLog.Printf ("dfarch_plane_t size = %d", sizeof(dfarch_plane_t)); /* Setup the D3D application */ DFD3DApp.pWindowProc = WndProc; DFD3DApp.pOnSelectObjectCallBack = DisplayPlaneInfo; DFD3DApp.SetAcceleratorName("ARCHD3D_ACCELERATOR"); DFD3DApp.SetClassName("ArchD3D Window"); DFD3DApp.SetIconName("ARCHD3D_ICON"); DFD3DApp.SetMenuName("ARCHD3D_MENU"); DFD3DApp.SetWindowCaption("ArchD3D"); /* Ensure we are in the DF directory */ chdir("f:\\dagger"); /* Attempt to read the DF config file */ Result = DFConfigFile.Load("z.cfg"); if (!Result) { ErrorHandler.Notify("\nEnsure that this program is run from the Daggerfall directory (eg: 'd:\\dagger\\') !"); return (0); } /* Initialize the arch3d objects */ DFArch3D.Open(); DFArch3D.Close(); DFWoodsFile.Open(); DFWoodsFile.Close(); DFBlocks.Open(); DFBlocks.Close(); //DFD3DApp.SetViewBackLength(5000.0); //ErrorHandler.SetD3DError(D3DRMERR_BADOBJECT); //SET_D3D_ERROR2(D3DRMERR_BADOBJECT, "Received a NULL object"); //ErrorHandler.Notify(); /* Spawn the D3D application WinMain */ Result = DFD3DApp.D3DWinMain(hInstance, hPrevInstance, pCmdLine, nCmdShow); return (Result); } /*=========================================================================== * End of Function WinMain() *=========================================================================*/