Desktop2DX.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:6k
源码类别:
DirextX编程
开发平台:
Visual C++
- // Desktop2DX.cpp : Defines the class behaviors for the application.
- #include "stdafx.h"
- #include "Desktop2DX.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 DWORD g_dwWidth = 640; // 800; // 1024;
- const DWORD g_dwHeight = 480; // 600; // 768;
- const DWORD g_dwBPP = 8; // 16; // 24; // 32;
- CDesktop2DXApp::CDesktop2DXApp(void)
- {
- SetFullScreen(g_bFullScreen);
- SetPackFileName(_T("."), _T("gui"));
- // Called during initial app startup, this function
- // performs all the permanent initialization.
- m_pDDDevice = NULL;
- m_pRenderSurface = NULL;
- // add your permanent init code here !
- for (int i = 0; i < 4; ++ i)
- m_apSurface[i] = NULL;
- }
- // Called before the app exits, this function gives the app
- // the chance to cleanup after itself.
- CDesktop2DXApp::~CDesktop2DXApp()
- {
- // add your cleaup code here !
- }
- bool CDesktop2DXApp::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 (m_pDirectDraw->SelectDDDevice(0) == false)
- return false;
- if (GetFirstDDDevice()->SelectDisplayMode(
- IsFullScreen(), g_dwWidth, g_dwHeight, g_dwBPP) == false)
- return false;
- CDC dc;
- dc.Attach(::GetDC(NULL));
- if (m_pDirectDraw->GetFirstEnableDevice()->SelectDisplayMode(
- IsFullScreen(),
- dc.GetDeviceCaps(HORZRES),
- dc.GetDeviceCaps(VERTRES),
- dc.GetDeviceCaps(BITSPIXEL)/*8*/) == false)
- return false;
- m_dc.CreateCompatibleDC(&dc);
- m_bmp.CreateCompatibleBitmap(&dc,
- dc.GetDeviceCaps(HORZRES),
- dc.GetDeviceCaps(VERTRES));
- // Select the bitmaps into the compatible DC.
- m_pOldBMP = m_dc.SelectObject(&m_bmp);
- BOOL bRet = m_dc.BitBlt(0, 0,
- dc.GetDeviceCaps(HORZRES),
- dc.GetDeviceCaps(VERTRES),
- &dc,
- 0,
- 0,
- SRCCOPY);
- ::ReleaseDC(NULL, dc.Detach());
- dc.DeleteDC();
- if (bRet == FALSE)
- return false;
- return true;
- }
- // Called during device intialization, this code checks the device
- // for some minimum set of capabilities, Initialize scene objects.
- bool CDesktop2DXApp::InitDXObjects(void)
- {
- m_pDDDevice = GetFirstDDDevice();
- m_pRenderSurface = m_pDDDevice->GetRenderSurface();
- // add your init code here !
- for (int i = 0; i < 4; ++ i)
- {
- m_apSurface[i] = new CDDSurface;
- if (m_apSurface[i]->Create(m_pDDDevice,
- m_pDDDevice->GetWidth() / 2,
- m_pDDDevice->GetHeight() / 2) == false)
- return false;
- }
- CDC dc;
- for (i = 0; i < 4; ++ i)
- {
- m_arcDest[i].left = (i & 1) ? m_pDDDevice->GetWidth() / 2 : 0;
- m_arcDest[i].top = (i >> 1) ? m_pDDDevice->GetHeight() / 2 : 0;
- m_arcDest[i].right = m_arcDest[i].left + m_pDDDevice->GetWidth() / 2;
- m_arcDest[i].bottom = m_arcDest[i].top + m_pDDDevice->GetHeight() / 2;
- if (SUCCEEDED(m_apSurface[i]->GetDC(dc)))
- {
- dc.BitBlt(0, 0,
- m_apSurface[i]->GetWidth(),
- m_apSurface[i]->GetHeight(),
- &m_dc,
- m_arcDest[i].left,
- m_arcDest[i].top,
- SRCCOPY);
- m_apSurface[i]->ReleaseDC(dc);
- }
- }
- dc.DeleteDC();
- return true;
- }
- // Called when the app is exitting, or the device is being changed,
- // this function deletes any device dependant objects.
- bool CDesktop2DXApp::DestroyDXObjects(void)
- {
- // add your destroy code here !
- m_dc.SelectObject(m_pOldBMP);
- m_bmp.DeleteObject();
- m_dc.DeleteDC();
- for (int i = 0; i < 4; ++ i)
- {
- if (m_apSurface[i] != NULL)
- {
- delete m_apSurface[i];
- m_apSurface[i] = NULL;
- }
- }
- m_pRenderSurface = NULL;
- m_pDDDevice = 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 CDesktop2DXApp::UpdateFrame(void)
- {
- // add your code here !
- static int nStep=1;
- m_pRenderSurface->Fill(0);
- if (m_arcDest[3].left < m_arcDest[3].right - nStep)
- m_arcDest[3].left += nStep;
- if (m_arcDest[3].top < m_arcDest[3].bottom - nStep)
- m_arcDest[3].top += nStep;
- if (m_arcDest[2].right > m_arcDest[2].left + nStep)
- m_arcDest[2].right -= nStep;
- if (m_arcDest[2].top < m_arcDest[2].bottom - nStep)
- m_arcDest[2].top += nStep;
- if (m_arcDest[1].left < m_arcDest[1].right - nStep)
- m_arcDest[1].left += nStep;
- if (m_arcDest[1].bottom > m_arcDest[1].top + nStep)
- m_arcDest[1].bottom -= nStep;
- if (m_arcDest[0].right > m_arcDest[0].left + nStep)
- m_arcDest[0].right -= nStep;
- if (m_arcDest[0].bottom > m_arcDest[0].top + nStep)
- m_arcDest[0].bottom -= nStep;
- for (int i = 0; i < sizeof(m_apSurface) / sizeof(CDDSurface*); ++ i)
- {
- m_apSurface[i]->Draw(
- m_pDDDevice->GetRenderSurface(),
- &m_arcDest[i]);
- }
- nStep += 2;
- return CDirectXApp::UpdateFrame();
- }
- // ---- add your functions ! ----
- // ----
- #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(CDesktop2DXApp, CDirectXApp)
- //{{AFX_MSG_MAP(CDesktop2DXApp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- CDesktop2DXApp theApp;