bool Lighting::LoadAttenTexture(TSTR &Name, LPDIRECT3DDEVICE9 Device) { HRESULT Hr; void *Data; unsigned long Size; TCHAR error[256]; _stprintf(error,_T("%s"),GetString(IDS_ERROR)); Hr = S_OK; SAFE_RELEASE(m_Atten); if(GetFileResource(MAKEINTRESOURCE(IDR_ATTENUATE),_T("RT_RCDATA"), &Data,Size)) { Hr = D3DXCreateTextureFromFileInMemoryEx(Device, Data, Size, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, (IDirect3DTexture9 **)&m_Atten); } else { Hr = E_FAIL; } if(Hr != S_OK) { TSTR Str; Str = TSTR(GetString(IDS_LOAD_FILES)) + Name + TSTR(GetString(IDS_CHECK_PATHS)) + TSTR(GetString(IDS_ERROR_CODE)) + TSTR(DXGetErrorString(Hr)); MessageBox(NULL,Str,error,MB_OK | MB_ICONERROR | MB_SETFOREGROUND | MB_APPLMODAL); return(false); } return(true); }
HRESULT ResetDevice(D3DPRESENT_PARAMETERS d3dpp) { // Check device state HRESULT hr = g_pd3dDevice->TestCooperativeLevel() ; // Device can be reset now if (SUCCEEDED(hr) || hr == D3DERR_DEVICENOTRESET) { // Release resource allocated as D3DPOOL_DEFAULT g_pVB->Release(); // Reset device HRESULT hr = g_pd3dDevice->Reset(&d3dpp) ; if (FAILED(hr)) { const CHAR* errorString = DXGetErrorString(hr) ; DXTRACE_ERR_MSGBOX(errorString, hr) ; } // Recreate the vertex buffer, since it is create by using // D3DPOOL_DEFAULT, the function MUST placed here!!! why? // you cann't place it before the return clause of this function // what's the inner cause? InitVB(); } // Device is still in lost state, wait else if (hr == D3DERR_DEVICELOST) { Sleep(25) ; } else // Other error, Show error box { const CHAR* errorString = DXGetErrorString(hr) ; DXTRACE_ERR_MSGBOX(errorString, hr) ; } return hr ; }
bool DXInput::InitMouse() { DIDEVCAPS MouseCapabilities; //device capabilities // Create mouse device HRESULT hr = m_pDIObject->CreateDevice(GUID_SysMouse, &m_pDIMouseDevice, NULL) ; if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } // Set mouse data format hr = m_pDIMouseDevice->SetDataFormat(&c_dfDIMouse2) ; if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } // Set mouse cooperate level hr = m_pDIMouseDevice->SetCooperativeLevel(GetForegroundWindow(),DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) ; if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } // Acquire for mouse input hr = m_pDIMouseDevice->Acquire() ; if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } return true ; }
// Initialize keyboard device bool DXInput::InitKeyboard() { // Create keyboard device HRESULT hr = m_pDIObject->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboardDevice, NULL) ; if (FAILED(hr)) { //ERRORBOX(L"Create keyboard device failed!") ; DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } // Set keyboard data format hr = m_pDIKeyboardDevice->SetDataFormat(&c_dfDIKeyboard) ; if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } // Set keyboard cooperate level hr = m_pDIKeyboardDevice->SetCooperativeLevel(GetForegroundWindow(), DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) ; if (FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } // Acquire for keyboard input hr = m_pDIKeyboardDevice->Acquire() ; if (FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; return false ; } return true ; }
bool D3DEffect::EndPass() { if (!IsCreated()) return false; const HRESULT hr = m_effect->EndPass(); if (FAILED(hr)) { Log::Error(L"ID3DXEffect::EndPass failed, hr=%s, desc=%s", DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } return true; }
bool D3DEffect::SetTechnique(const std::string& techName) { if (!IsCreated()) return false; const HRESULT hr = m_effect->SetTechnique(techName.c_str()); if (FAILED(hr)) { Log::Error(L"SetTechnique with %s failed, hr=%s, desc=%s", techName.c_str(), DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } return true; }
bool CDXGraphicsContext::IsResultGood(HRESULT hr, bool displayError) { if(FAILED(hr)) { if( displayError ) { const char *errmsg = DXGetErrorString(hr); TRACE1("D3D Error: %s", errmsg); //ErrorMsg(errmsg); } return false; } else return true; }
bool D3DEffect::Begin(unsigned int &numPass) { if (!IsCreated() || m_techBegun) return false; const HRESULT hr = m_effect->Begin(&numPass, 0); if (FAILED(hr)) { Log::Error(L"ID3DXEffect::Begin failed, hr=%s, desc=%s", DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } m_techBegun = true; return true; }
bool D3DEffect::BeginPass(unsigned int pass) { if (!IsCreated()) return false; const HRESULT hr = m_effect->BeginPass(pass); if (FAILED(hr)) { Log::Error(L"ID3DXEffect::BeginPass with pass=%u failed, hr=%s, desc=%s", pass, DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } return true; }
bool Graphics2D::CheckHR(HRESULT hr) { // just checks a HRESULT object from directX contains an error message if (SUCCEEDED(hr)) return true; // An error occured so find out what it was and output it to the output pane in Vis OutputDebugString("\nDirectX Error Reported:\n"); OutputDebugString(DXGetErrorString(hr)); OutputDebugString(" : "); OutputDebugString(DXGetErrorDescription(hr)); OutputDebugString("\n\n"); return false; }
bool D3DEffect::Parameter::SetValue(const Texture& texture) { SK_REQUIRE(m_d3dEffect && m_handle); if (!m_d3dEffect->IsCreated()) return false; const HRESULT hr = m_d3dEffect->m_effect->SetTexture(m_handle, texture.GetD3D9Texture()); if (FAILED(hr)) { Log::Error(L"ID3DXEffect::SetTexture failed, hr=%s, desc=%s", DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } return true; }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE ; d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE) ; // Create texture HRESULT hr = D3DXCreateTextureFromFile(g_pd3dDevice, "../Common/Media/chessboard.jpg", &g_pTexture) ; if (FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; } // Texture state g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); D3DXMatrixIdentity(&matTexture) ; return S_OK; }
bool D3DEffect::Parameter::SetValue(const Matrix4& m) { SK_REQUIRE(m_d3dEffect && m_handle); if (!m_d3dEffect->IsCreated()) return false; D3DXMATRIXA16 d3dm; D3DMapping::ToD3DXMatrixA16(m, d3dm); const HRESULT hr = m_d3dEffect->m_effect->SetMatrix(m_handle, &d3dm); if (FAILED(hr)) { Log::Error(L"ID3DXEffect::SetMatrix failed, hr=%s, desc=%s", DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } return true; }
void CheckForDxError(const char *file, int line, HRESULT hr) { if (!FAILED(hr)) return; // Get the direct X error and description char desc[1024]; sprintf(desc,"(DX) %s - %s",DXGetErrorString(hr), DXGetErrorDescription(hr)); // Output the file and line number in the correct format + the above DX error //char buf[2048]; //sprintf(buf,"%s(%d) : Error: %s\n", file, line, desc); //OutputDebugString(buf); // Cause the debugger to break here so we can fix the problem //DebugBreak(); }
bool D3DEffect::Parameter::SetValue(const Vector4& v) { SK_REQUIRE(m_d3dEffect && m_handle); if (!m_d3dEffect->IsCreated()) return false; D3DXVECTOR4 d3dv; D3DMapping::ToD3DXVector4(v, d3dv); const HRESULT hr = m_d3dEffect->m_effect->SetVector(m_handle, &d3dv); if (FAILED(hr)) { Log::Error(L"ID3DXEffect::SetVector failed, hr=%s, desc=%s", DXGetErrorString(hr), DXGetErrorDescription(hr)); return false; } return true; }
void SRRasterization::CopyToScreen() { g_pd3dDevice->BeginScene(); LPDIRECT3DSURFACE9 backBuffer; HRESULT ret = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer); D3DSURFACE_DESC surfacedesc; HRESULT ret2 = backBuffer->GetDesc(&surfacedesc); const TCHAR * message1 = DXGetErrorString(ret2); const TCHAR * message2 = DXGetErrorDescription(ret2); m_colorBuffer->CopyBufferToSurface(backBuffer); g_pd3dDevice->EndScene(); // Present the backbuffer contents to the display g_pd3dDevice->Present(NULL, NULL, NULL, NULL); }
const char* DiD3D9ErrorCheck::GetResultString(HRESULT pr) { const int cnMaxLen = 512; static char szResult[cnMaxLen]; szResult[ 0 ] = NULL; if ( IsD3DResult( pr ) ) { const char* pszString = DXGetErrorString( static_cast<HRESULT>(pr) ); const char* pszDesc = DXGetErrorDescription( static_cast<HRESULT>(pr) ); _snprintf( szResult, cnMaxLen, "%s, description : %s", pszString, pszDesc ); return szResult; } return NULL; }
bool DSound::CreateBuffer() { PCMWAVEFORMAT pcmwf; DSBUFFERDESC dsbdesc; memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT)); memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM; pcmwf.wf.nChannels = 2; pcmwf.wf.nSamplesPerSec = m_mixer->GetSampleRate(); pcmwf.wf.nBlockAlign = 4; pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign; pcmwf.wBitsPerSample = 16; // Fill out DSound buffer description. dsbdesc.dwSize = sizeof(DSBUFFERDESC); dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS | DSBCAPS_CTRLVOLUME; dsbdesc.dwBufferBytes = bufferSize = BUFSIZE; dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf; dsbdesc.guid3DAlgorithm = DS3DALG_DEFAULT; HRESULT res = ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL); if (SUCCEEDED(res)) { dsBuffer->SetCurrentPosition(0); dsBuffer->SetVolume(m_volume); return true; } else { // Failed. PanicAlertT("Sound buffer creation failed: %s", DXGetErrorString(res)); dsBuffer = NULL; return false; } }
CString GetErrorString( HRESULT hr ) { return DXGetErrorString(hr); }
// OLD D3D9 hook codes - disabled static int create_d3d9_object() { HRESULT hr; LPDIRECT3D9 pD3D = NULL; LPDIRECT3DDEVICE9 pd3dDevice = NULL; LPDIRECT3DSWAPCHAIN9 pSwapChain = NULL; HMODULE hMod; HWND hWnd; // D3DDISPLAYMODE d3ddm; D3DCAPS9 d3dCaps; DWORD dwBehaviorFlags = 0; D3DPRESENT_PARAMETERS d3dpp; if((hMod = GetModuleHandle("d3d9.dll")) == NULL) { if((hMod = LoadLibrary("d3d9.dll")) == NULL) { ga_error("Load d3d9.dll failed.\n"); return -1; } } hWnd = CreateWindowA("BUTTON", "Temporary Window", WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, NULL, NULL); if (hWnd == NULL) { ga_error("CreateWindow failed.\n"); return -1; } if((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) { ga_error("Direct3DCreate9 failed.\n"); return -1; } // provide information for the current display mode for a // specified device d3d format if(FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))) { ga_error("GetAdapterDisplayMode failed.\n"); return -1; } ga_error("Display mode: w: %d, h: %d, rate: %d, format: %d\n", d3ddm.Width, d3ddm.Height, d3ddm.RefreshRate, d3ddm.Format); // retrieve device-specific information about a device // D3DDEVTYPE_HAL: hardware rasterization, shading is done with software, // hardware, or mixed transform and lighting if(FAILED(pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps))) { ga_error("GetDeviceCaps failed.\n"); return -1; } ga_error("Device information: type: %d, intervals: %d", d3dCaps.DeviceType, d3dCaps.PresentationIntervals); // if(d3dCaps.VertexProcessingCaps != 0 ) { dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING; } else { dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; } ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.BackBufferFormat = d3ddm.Format; //d3dpp.BackBufferHeight = d3ddm.Height; //d3dpp.BackBufferWidth = d3ddm.Width; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.Windowed = TRUE; //FALSE; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, dwBehaviorFlags, &d3dpp, &pd3dDevice); if(FAILED(hr)){ ga_error("CreateDevice failed.: %s\n", DXGetErrorString(hr)); return -1; } // start to hook D3D9DevicePresent() // 17: IDirect3DDevice9::Present uintptr_t* pInterfaceVTable = (uintptr_t*)*(uintptr_t*)pd3dDevice; pD3D9DevicePresent = (TD3D9DevicePresent)pInterfaceVTable[17]; //OutputDebugString("Start to hook IDirect3DDevice9::Present"); // start to hook D3D9SwapChainPresent() // 3: IDirect3DSwapChain9::Present pd3dDevice->GetSwapChain(0, &pSwapChain); uintptr_t* pInterfaceVTable2 = (uintptr_t*)*(uintptr_t*)pSwapChain; pSwapChainPresent = (TSwapChainPresent)pInterfaceVTable2[3]; //OutputDebugString("Start to hook IDirect3DSwapChain9::Present"); DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&(PVOID&)pD3D9DevicePresent, hook_D3D9DevicePresent); DetourAttach(&(PVOID&)pSwapChainPresent, hook_D3D9SwapChainPresent); DetourTransactionCommit(); pd3dDevice->Release(); pD3D->Release(); DestroyWindow(hWnd); return 0; }
std::wstring GraphicsException::GetErrorName() const { return DXGetErrorString( hr ); }
static void showError(HRESULT result, const char* message) { WCHAR buffer[1024]; wsprintf(buffer, L"ImageFilterSpline36: %s\n%s", DXGetErrorString(result), message); MessageBox(NULL, buffer, L"RemoteJoyLite", MB_OK); }
BOOL CClientDlg::Client_EnumHosts() { if (!g_pClient) return false; /////////////////////////////////////////////////////////////////// DPN_APPLICATION_DESC AppDesc; ZeroMemory(&AppDesc, sizeof(DPN_APPLICATION_DESC)); AppDesc.dwSize = sizeof(DPN_APPLICATION_DESC); AppDesc.guidApplication = STALKER_NET_GUID; HRESULT hr; IDirectPlay8Address *net_Address_server = NULL; u32 psNET_Port = 5445; char Name[1024]; sprintf(Name, "%s", ""); hr = (CoCreateInstance (CLSID_DirectPlay8Address,NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay8Address,(LPVOID*) &net_Address_server )); hr = (net_Address_server->SetSP (&CLSID_DP8SP_TCPIP )); hr = (net_Address_server->AddComponent (DPNA_KEY_PORT, &psNET_Port, sizeof(psNET_Port), DPNA_DATATYPE_DWORD )); // hr = (net_Address_server->AddComponent (DPNA_KEY_HOSTNAME, Name, (strlen(Name) + 1), DPNA_DATATYPE_STRING )); if (g_dpnhEnum) g_pClient->CancelAsyncOperation(g_dpnhEnum, DPNCANCEL_ENUM); g_dpnhEnum = NULL; GUID tmpGuid = STALKER_NET_GUID; hr = g_pClient->EnumHosts( &AppDesc, // pApplicationDesc net_Address_server, // Host Address g_pClientDeviceAddress,// Device Address &tmpGuid, sizeof(STALKER_NET_GUID), //NULL, 0, 1, // dwEnumCount 0, // dwRetryInterval 1500, // dwTimeOut NULL, // pvUserContext NULL,//&m_dpnhEnum, // pAsyncHandle DPNENUMHOSTS_SYNC// | DPNENUMHOSTS_OKTOQUERYFORADDRESSING ); // dwFlags ///////////////////////////////////////////////////////////////// // logfilePrint("end enum call"); // char Text[256]; // sprintf(Text, "Result code %X", HRESULT_CODE(hr)); // logfilePrint(Text); switch (hr) { case S_OK: { // tntConsole_Print("\nEnum Ok."); // sprintf(m_pEnumEndCommand, "%s", pEndEnumCommand); // logfilePrint("S_OK"); int x=0; x=x; }break; case DPNERR_INVALIDDEVICEADDRESS: { // logfilePrint("DPNERR_INVALIDDEVICEADDRESS"); // tntConsole_Print("\nEnum DPNERR_INVALIDDEVICEADDRESS."); int x=0; x=x; }break; case DPNERR_INVALIDFLAGS: { // logfilePrint("DPNERR_INVALIDFLAGS"); // tntConsole_Print("\nEnum DPNERR_INVALIDFLAGS."); int x=0; x=x; }break; case DPNERR_INVALIDHOSTADDRESS: { // logfilePrint("DPNERR_INVALIDHOSTADDRESS"); // tntConsole_Print("\nEnum DPNERR_INVALIDHOSTADDRESS."); int x=0; x=x; }break; case DPNERR_INVALIDPARAM: { // logfilePrint("DPNERR_INVALIDPARAM"); // tntConsole_Print("\nEnum DPNERR_INVALIDPARAM."); int x=0; x=x; }break; case DPNERR_ENUMQUERYTOOLARGE: { // logfilePrint("DPNERR_ENUMQUERYTOOLARGE"); // tntConsole_Print("\nEnum DPNERR_ENUMQUERYTOOLARGE."); int x=0; x=x; }break; case DPNSUCCESS_PENDING : { // logfilePrint("DPNSUCCESS_PENDING"); // tntConsole_Print("\nEnum DPNSUCCESS_PENDING ."); // sprintf(m_pEnumEndCommand, "%s", pEndEnumCommand); int x=0; x=x; }break; case DPNERR_USERCANCEL: { int x=0; x=x; }break; }; if (hr != S_OK) { const char* text = DXGetErrorString(hr); int x=0; x=x; } net_Address_server->Release(); return true; };
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE ; d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE) ; // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // Initialize terrain g_pTerrain->BuildGridsBuffer(g_pd3dDevice) ; D3DXMatrixIdentity(&g_matBillboardWorld) ; // Set view matrix D3DXVECTOR3 eyePt(0, 0.0f, -20.0f) ; D3DXVECTOR3 lookAt(0, 1.0f, 0) ; D3DXVECTOR3 upVec(0, 1.0f, 0) ; g_pCamera->SetViewParams(eyePt, lookAt, upVec) ; // Set projection matrix g_pCamera->SetProjParams(D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; // Create texture HRESULT hr = D3DXCreateTextureFromFile(g_pd3dDevice, "../Common/Media/autumn.jpg", &g_pTexture) ; if (FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; } // Set texture sampler state g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); // Set material g_Material.Diffuse = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ; // white g_Material.Ambient = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ; // white g_Material.Specular = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ; // white // Set light g_Light.Type = D3DLIGHT_DIRECTIONAL ; g_Light.Direction = D3DXVECTOR3(1.0f, 0, 0) ; g_Light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) ; // white g_Light.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) ; // white g_Light.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) ; // white g_Light.Range = 1000.0f ; return S_OK; }
void LogD3dError(const char* method, HRESULT result) { logger->warn("Direct3D Error @ {}: {}", method, DXGetErrorString(result)); }
bool Lighting::LoadSpotTexture(TSTR &Name, LPDIRECT3DDEVICE9 Device) { HRESULT Hr; void *Data; unsigned long Size; D3DSURFACE_DESC Desc; float Bias,X,Y; unsigned int Range; TCHAR error[256]; _stprintf(error,_T("%s"),GetString(IDS_ERROR)); Hr = S_OK; SAFE_RELEASE(m_Spot); if(GetFileResource(MAKEINTRESOURCE(IDR_SPOTLIGHT),_T("RT_RCDATA"), &Data,Size)) { Hr = D3DXCreateTextureFromFileInMemoryEx(Device, Data, Size, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, (IDirect3DTexture9 **)&m_Spot); } else { Hr = E_FAIL; } if(Hr != S_OK) { TSTR Str; Str = TSTR(GetString(IDS_LOAD_FILES)) + Name + TSTR(GetString(IDS_CHECK_PATHS)) + TSTR(GetString(IDS_ERROR_CODE)) + TSTR(DXGetErrorString(Hr)); MessageBox(NULL,Str,error,MB_OK | MB_ICONERROR | MB_SETFOREGROUND | MB_APPLMODAL); return(false); } // // Set projection matrix // ((LPDIRECT3DTEXTURE9)m_Spot)->GetLevelDesc(0,&Desc); X = 0.5f + (0.5f / (float)Desc.Width); Y = 0.5f + (0.5f / (float)Desc.Height); Range = 0xFFFFFFFF >> (32 - 24); Bias = -0.001f * (float)Range; m_MatScaleBias = D3DXMATRIX(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, (float)Range, 0.0f, X, Y, Bias, 1.0f); return(true); }
void SetupHooks() { logInfo("Setting up hooks."); if(hooksSetup) return; HMODULE d3dMod = GetModuleHandle("d3d9.dll"); if(d3dMod == NULL) { ErrorMsg("GetModuleHandle(d3d9.dll)"); return; } HMODULE d3dxMod = LoadLibrary("d3dx9_43.dll"); if(d3dxMod == NULL) { ErrorMsg("LoadLibrary(d3dx9_43.dll)"); return; } HMODULE winmmMod = LoadLibrary("winmm.dll"); if(winmmMod == NULL) { ErrorMsg("LoadLibrary(winmm.dll)"); return; } D3DCreate = (pDirect3DCreate9)GetProcAddress(d3dMod, "Direct3DCreate9"); if(D3DCreate == NULL) { ErrorMsg("GetProcAddress(d3dMod, \"Direct3DCreate9\")"); return; } oPlaySound = (pPlaySoundA)GetProcAddress(winmmMod, "PlaySoundA"); if(oPlaySound == NULL) { ErrorMsg("GetProcAddress(winmmMod, \"PlaySoundA\")"); return; } oD3DXCreateFont = (pD3DXCreateFont)GetProcAddress(d3dxMod, "D3DXCreateFontA"); if(oD3DXCreateFont == NULL) { ErrorMsg("GetProcAddress(d3dxMod, \"D3DXCreateFontA\")"); return; } oD3DXCreateLine = (pD3DXCreateLine)GetProcAddress(d3dxMod, "D3DXCreateLine"); if(oD3DXCreateLine == NULL) { ErrorMsg("GetProcAddress(d3dxMod, \"D3DXCreateLine\")"); return; } // Create a dummy window to call CreateDevice on HWND hwnd; hwnd = CreateWindow("BUTTON", "APMAlertDummyWindow", 0, 0, 0, 27, 27, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { ErrorMsg("CreateWindow"); return; } //UpdateWindow(hwnd); IDirect3D9 *pD3D = D3DCreate(D3D_SDK_VERSION); if(pD3D == NULL) { ErrorMsg("Direct3DCreate9"); return; } D3DDISPLAYMODE d3ddm; HRESULT hRes = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); if(FAILED(hRes)) { char errorMsg[512]; const char * dxErrorStr = DXGetErrorString(hRes); sprintf_s(errorMsg, 512, "GetAdapterDisplayMode returned 0x%08x: %s", hRes, dxErrorStr); logError(errorMsg); goto cleanup; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; IDirect3DDevice9 * ppD3DDevice; hRes = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_DISABLE_DRIVER_MANAGEMENT, &d3dpp, &ppD3DDevice); if(FAILED(hRes)) { char errorMsg[512]; const char * dxErrorStr = DXGetErrorString(hRes); sprintf_s(errorMsg, 512, "CreateDevice returned 0x%08x: %s", hRes, dxErrorStr); logError(errorMsg); goto cleanup; } // Get our function pointers from the virtual table // This pointer dereferencing works because the virtual table is the first item in memory // of the every object void ** vTable = *((void***)ppD3DDevice); // Access the function pointers we need addrEndScene = vTable[42]; // EndScene is the 43rd function (you can just count in the interface decl in the header) /* char path_d3d9_dll[MAX_PATH]; GetSystemDirectory(path_d3d9_dll, MAX_PATH); strncat_s(path_d3d9_dll, MAX_PATH, "\\d3d9.dll", 10); SIZE_T offset = (unsigned int)addrEndScene - (unsigned int)GetModuleHandle(path_d3d9_dll); printf("EndScene() Addr: 0x%08x -- SC2.exe!d3d9.dll+0x%x\n", addrEndScene, offset); */ DWORD oldProtect; // backup the top 6 bytes of each function if(VirtualProtect(addrEndScene, 6, PAGE_EXECUTE_READWRITE, &oldProtect) == FALSE) { ErrorMsg("VirtualProtect"); return; // make the address read/writable } memcpy(backup_EndScene, addrEndScene, 6); VirtualProtect(addrEndScene, 6, oldProtect, &oldProtect); // restore old protection // We are going to write over the top 6 bytes of every function we want to hook. // This way, whenever they are called, we can jump to our custom hook function and run our own stuff. // To maintain proper game functionality we will restore the backup code, run the function as it should be, // then restore our patch code at the top when it returns to our hook function. // create our 6 byte patch consisting of: push <addr_hook>; retn (essentially a call that doesn't disturb the stack) patch_EndScene[0] = 0x68; // PUSH *((DWORD *)(patch_EndScene+1)) = (DWORD)&hkEndScene; // value to push patch_EndScene[5] = 0xC3; // RETN hooksSetup = true; logInfo("Hooks setup and ready for use."); ppD3DDevice->Release(); ppD3DDevice = NULL; pD3D->Release(); pD3D = NULL; cleanup: if(pD3D != NULL) pD3D->Release(); // Destroy the dummy window DestroyWindow(hwnd); }