Compress.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:8k
源码类别:
DirextX编程
开发平台:
Visual C++
- // Compress.cpp : Defines the class behaviors for the application.
- #include "stdafx.h"
- #include "Compress.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- const bool g_bFullScreen
- #ifdef _DEBUG
- = false; // true;
- #else
- = true; // false;
- #endif // _DEBUG
- const bool g_bUseZBuffer = false;
- const DWORD g_dwWidth = 640; // 800; // 1024;
- const DWORD g_dwHeight = 480; // 600; // 768;
- const DWORD g_dwBPP = 16; // 32;
- const bool g_bUseVoodoo12 = false; // true;
- const _GUID* const g_lpGuidD3DDevice
- = NULL;
- // = &IID_IDirect3DTnLHalDevice;
- // = &IID_IDirect3DRGBDevice;
- // = &IID_IDirect3DHALDevice;
- // = &IID_IDirect3DRefDevice;
- CCompressApp::CCompressApp(void)
- {
- m_dwAppInitFlags &= ~CDirectXApp::DXAPPIF_DD;
- m_dwAppInitFlags |= CDirectXApp::DXAPPIF_D3DIM;
- SetFullScreen(g_bFullScreen);
- SetPackFileName(_T("Data"), _T("gui"));
- // Called during initial app startup, this function
- // performs all the permanent initialization.
- m_pD3DDevice = NULL;
- m_pD3DMaterial = NULL;
- // add your permanent init code here !
- m_pLight = NULL;
- m_pTexture = NULL;
- // Generate the cube data
- m_pCube = new CD3DCube();
- }
- // Called before the app exits, this function gives the app
- // the chance to cleanup after itself.
- CCompressApp::~CCompressApp()
- {
- // add your cleaup code here !
- if (m_pCube != NULL)
- {
- delete m_pCube;
- m_pCube = NULL;
- }
- }
- bool CCompressApp::GetDXInitSettings(void)
- {
- if (m_pDirectSound != NULL)
- {
- if (m_pDirectSound->SelectDSDriver(0) == false)
- return false;
- }
- if (m_pDirectMusic != NULL)
- {
- if (m_pDirectMusic->SelectDefaultDMusPort() == false)
- return false;
- }
- if ((g_bUseVoodoo12 == false)
- || m_pDirectDraw->SelectDDDevice(0, 1) == false)
- {
- if (m_pDirectDraw->SelectDDDevice(0) == false)
- return false;
- }
- if (g_bUseZBuffer)
- m_dwDDInitFlags |= CDDDevice::D3DIF_ZBUFFER;
- if (GetFirstDDDevice()->IsCanRenderWindowed() == false)
- {
- m_dwDDInitFlags &= ~CDDDevice::DDIF_WINDOWED;
- m_dwDDInitFlags &= ~CDDDevice::DDIF_NO_FLIP;
- }
- else
- {
- m_dwDDInitFlags |= CDDDevice::DDIF_NO_FLIP;
- }
- if (g_lpGuidD3DDevice == NULL)
- {
- if (GetFirstDDDevice()->SelectDefaultD3DDevice() == false)
- return false;
- }
- else
- {
- if (GetFirstDDDevice()->SelectD3DDevice(g_lpGuidD3DDevice) == false)
- return false;
- }
- if (GetFirstDDDevice()->SelectDisplayMode(
- IsFullScreen(), g_dwWidth, g_dwHeight, g_dwBPP) == false)
- return false;
- return true;
- }
- // Called during device intialization, this code checks the device
- // for some minimum set of capabilities, Initialize scene objects.
- bool CCompressApp::InitDXObjects(void)
- {
- m_pD3DDevice = GetFirstD3DDevice();
- ASSERT(m_pD3DDevice != NULL);
- m_pD3DMaterial = m_pD3DDevice->GetD3DMaterial();
- // add your init code here !
- // Create and set up the object material
- m_pD3DMaterial->SetColor(1.0f, 1.0f, 1.0f, 0.0f);
- m_pD3DMaterial->Set();
- // Set the transform matrices
- D3DVECTOR vEyePt = D3DVECTOR( 0, 0, -6.5);
- D3DVECTOR vLookatPt = D3DVECTOR( 0, 0, 0);
- D3DVECTOR vUpVec = D3DVECTOR( 0, 1, 0);
- CD3DMatrix matWorld;
- matWorld.SetIdentity();
- CD3DMatrix matView;
- matView.SetView(vEyePt, vLookatPt, vUpVec);
- CD3DMatrix matProj;
- matProj.SetProjection(g_PI/4, 1.0f, 1.0f, 100.0f);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &matView);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProj);
- // Set up the light
- if (m_pD3DDevice->IsSupportDirectionalLights())
- {
- m_pLight = new CD3DDirectionalLight(D3DVECTOR(0.0f, -0.4f, 1.0f));
- if (m_pLight->Create(m_pD3DDevice) == false)
- return false;
- if (m_pLight->Enable(true) == false)
- return false;
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_LIGHTING, TRUE);
- }
- // Set texture states
- ChangeTexture();
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DITHERENABLE, TRUE);
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFN_LINEAR);
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
- return true;
- }
- // Called when the app is exitting, or the device is being changed,
- // this function deletes any device dependant objects.
- bool CCompressApp::DestroyDXObjects(void)
- {
- // add your destroy code here !
- if (m_pTexture != NULL)
- {
- delete m_pTexture;
- m_pTexture = NULL;
- }
- if (m_pLight != NULL)
- {
- delete m_pLight;
- m_pLight = NULL;
- }
- m_pD3DMaterial = NULL;
- m_pD3DDevice = NULL;
- return CDirectXApp::DestroyDXObjects();
- }
- // Called once per frame, the call is the entry point for
- // animating the scene. This function sets up render states,
- // clears the viewport, and renders the scene.
- bool CCompressApp::UpdateFrame(void)
- {
- // add your code here !
- static FLOAT fTimeKey = 0.0f;
- fTimeKey += 0.02f;
- // Compute the x and y rotation
- FLOAT fRotateY = fTimeKey / 0.5f;
- FLOAT fRotateX = fTimeKey / 0.3f;
- FLOAT fTransZ = (FLOAT)::fabs(::sin(fTimeKey) * 50);
- // Setup the world spin matrix
- CD3DMatrix matSpinY, matSpinX, matWorld;
- matSpinY.SetRotateY(fRotateY);
- matSpinX.SetRotateX(fRotateX);
- if (m_pGUIManager->GetKeyState()->GetKeyState() == CDIKeyState::KEY_DOWN)
- {
- ChangeTexture();
- }
- if (m_pTexture->IsMipTexture())
- matWorld.SetTranslate(0.0f, 0.0f, fTransZ);
- else
- matWorld.SetTranslate(0.0f, 0.0f, 0.0f);
- matWorld = matSpinY * matWorld;
- matWorld = matSpinX * matWorld;
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
- // Clear the viewport
- m_pD3DDevice->Clear(RGBA_MAKE(0, 0, 0xFF, 0));
- // Begin the scene
- if (SUCCEEDED(m_pD3DDevice->BeginScene()))
- {
- // Display the object
- m_pCube->Render(m_pD3DDevice);
- // End the scene.
- m_pD3DDevice->EndScene();
- }
- // Output the texture's pixel formats to the frame buffer
- return CDirectXApp::UpdateFrame();
- }
- // ---- add your functions ! ----
- // Frees the old texture and loads a new one.
- bool CCompressApp::ChangeTexture(void)
- {
- static DWORD dwDXT = 0;
- if (dwDXT > 3)
- dwDXT = 0;
- // Release the old texture
- if (m_pTexture != NULL)
- {
- delete m_pTexture;
- m_pTexture = NULL;
- }
- static const LPCTSTR lpszFmt = _T("TEXTURE%d.DDS");
- static CString strDDSName;
- strDDSName.Format(lpszFmt, dwDXT ++);
- m_pTexture = new CD3DTexture;
- if (m_pTexture->CreateDDSTexture(m_pD3DDevice,
- strDDSName, m_pPackFileManager) == false)
- return false;
- m_pTexture->SetAsCurrent();
- if (m_pTexture->IsPremultipliedAlpha())
- {
- // Use D3DBLEND_ONE if DDPF_ALPHAPREMULT is on
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_ONE);
- }
- else
- {
- // Use D3DBLEND_SRCALPHA if DDPF_ALPHAPREMULT is off
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
- }
- return true;
- }
- // ----
- #if _MSC_VER >= 1200 && _MSC_VER < 1400
- #ifdef _DEBUG
- #pragma comment(lib, "DXGuideD_VC6.lib")
- #else
- #pragma comment(lib, "DXGuide_VC6.lib")
- #endif // _DEBUG
- #endif // _MSC_VER
- #if _MSC_VER >= 1000 && _MSC_VER < 1200
- #ifdef _DEBUG
- #pragma comment(lib, "DXGuideD_VC5.lib")
- #else
- #pragma comment(lib, "DXGuide_VC5.lib")
- #endif // _DEBUG
- #endif // _MSC_VER
- BEGIN_MESSAGE_MAP(CCompressApp, CDirectXApp)
- //{{AFX_MSG_MAP(CCompressApp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- CCompressApp theApp;