cApplication.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
- // CMAIN LIB - APPLICATION AND DIRECT WRAPPER
- //
- // Written by Mauricio Teichmann Ritter
- //
- // Copyright (C) 2002, Brazil. All rights reserved.
- //
- //
- #include "cApplication.h"
- cApplication* CreateApplication();
- cApplication* pApp;
- cApplication* GetMainApp()
- {
- return pApp;
- }
- long FAR PASCAL MainWndproc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
- {
- switch( message )
- {
- case WM_SIZE:
- // Check to see if we are losing our window...
- if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
- GetMainApp()->m_bActive = FALSE;
- else
- GetMainApp()->m_bActive = TRUE;
- break;
- case WM_CREATE:
- break;
- case WM_CLOSE:
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- break;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- } /* MainWndproc */
- int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int iCmdShow)
- {
- cApplication::m_hInst = hInst;
- pApp = CreateApplication();
- CoInitialize(NULL);
- if(!pApp->InitApplication())
- {
- MessageBox(NULL, "Error Starting up application.n Close all open programs and try again.", "Error", MB_ICONERROR);
- return 0;
- }
- if(!pApp->InitDirectX())
- {
- MessageBox(NULL, "Error Starting up DirectX.n Check if you have Microsoft DirectX correctly installed on your machine.n You can download the latest version from http://www.microsoft.com/directx.", "Error", MB_ICONERROR);
- return 0;
- }
- // Call virtual function to tell the derived class that with
- // have setup everything
- pApp->AppInitialized();
- // Start Message Loop
- pApp->RunApplication();
- delete pApp;
- return 0;
- }
- cApplication::cApplication()
- {
- m_pDD = NULL;
- m_pFrontBuffer = NULL;
- m_pBackBuffer = NULL;
-
- m_ColorDepth = 16;
- m_ScreenWidth = 640;
- m_ScreenHeight = 480;
- m_bDontFlip = false;
- m_bActive = TRUE;
- };
- cApplication::~cApplication()
- {
- // Time for cleanup
- //
- if( m_pBackBuffer != NULL )
- m_pBackBuffer->Release();
- if( m_pFrontBuffer != NULL )
- m_pFrontBuffer->Release();
- if( m_pDD != NULL )
- m_pDD->Release();
- CoUninitialize();
- };
- HINSTANCE cApplication::m_hInst = NULL;
- void cApplication::DoIdle()
- {
- return;
- }
- BOOL cApplication::InitApplication()
- {
- m_bActive = TRUE;
- // First Try to register the window
- if(FAILED(m_pWindow.RegisterWindow(m_lpszwndClassName)))
- return FALSE;
- // Then try to create it
- if(!m_pWindow.Create(m_lpszAppName))
- return FALSE;
- return TRUE;
- }
- BOOL cApplication::RunApplication()
- {
- // MessageLoop implementation
- // The DoIdle function is a virtual function that can be implemented
- // In the derived class
- MSG msg;
- BOOL bContinue = TRUE;
- HRESULT hRet;
- while (bContinue)
- {
- // Check the message pool
- if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
- {
- if(msg.message == WM_QUIT)
- {
- ExitApp();
- }
- if( !GetMessage( &msg, NULL, 0, 0 ) )
- {
- return msg.wParam;
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- // Nothing to do... idle;
- DoIdle();
- if(pApp->m_bActive)
- {
- if(m_bDontFlip == false)
- {
- hRet = m_pFrontBuffer->Flip(NULL, 0 );
- if(hRet != DD_OK)
- {
- DXTRACE_MSG("Error Flipping Front Buffer !n");
- }
- }
- else
- m_bDontFlip = false;
- }
- else
- {
- WaitMessage();
- }
- }
- }
- return TRUE;
- }
- BOOL cApplication::InitDirectX()
- {
- HRESULT hRet;
- LPDIRECTDRAW pDD;
- DDSURFACEDESC2 ddsd;
- DDSCAPS2 ddscaps;
- // First of all, create the DirectX object
- hRet = DirectDrawCreate( NULL, &pDD, NULL );
- if(hRet != DD_OK)
- {
- // if failed, quit the app
- return FALSE;
- }
- // Fetch DirectDraw7 interface
- hRet = pDD->QueryInterface(IID_IDirectDraw7, (LPVOID*)&m_pDD);
- if(hRet != DD_OK)
- {
- // if failed, quit the app
- return FALSE;
- }
- // Since we don磘 need this DirectDraw interface anymore, release it
- pDD->Release();
-
- // Set cooperative level to fullscreenn
- hRet = m_pDD->SetCooperativeLevel(m_pWindow.GetHWnd(), DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
- if(hRet != DD_OK)
- {
- // if failed, quit the app
- return FALSE;
- }
-
-
- // Set Display mode
- hRet = m_pDD->SetDisplayMode(m_ScreenWidth, m_ScreenHeight, m_ColorDepth, 0, 0 );
- if(hRet != DD_OK)
- {
- // if failed, quit the app
- return FALSE;
- }
-
- // Create Main Surfaces
- // We磍l not use cSurface here because we only need reference of this
- // two guys
- ZeroMemory( &ddsd, sizeof( ddsd ) );
- ddsd.dwSize = sizeof( ddsd );
- ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
-
- ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
- DDSCAPS_FLIP |
- DDSCAPS_COMPLEX;
-
- ddsd.dwBackBufferCount = 1;
-
- hRet = m_pDD->CreateSurface(&ddsd, &m_pFrontBuffer, NULL );
- if(hRet != DD_OK)
- {
- // if failed, quit the app
- return FALSE;
- }
- // get a pointer to the back buffer
-
- ZeroMemory(&ddscaps, sizeof(ddscaps));
- ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
- hRet = m_pFrontBuffer->GetAttachedSurface(
- &ddscaps,
- &m_pBackBuffer);
- if(hRet != DD_OK)
- {
- // if failed, quit the app
- return FALSE;
- }
- // All OK ! :)
- return TRUE;
- }
- LPDIRECTDRAW7 cApplication::GetDirectDraw()
- {
- return m_pDD;
- }
- void cApplication::AppInitialized()
- {
- }
- void cApplication::ExitApp()
- {
- }
- HWND cApplication::GetMainWnd()
- {
- return m_pWindow.GetHWnd();
- }
- void cApplication::PreventFlip()
- {
- m_bDontFlip = true;
- }
- #ifdef _DEBUG
- void Log(char* sFormat, ...)
- {
- FILE* pFile;
- char buffer[1024];
- va_list marker;
- va_start( marker, sFormat );
- pFile = fopen("log.txt", "a+");
- vsprintf(buffer, sFormat, marker);
- fputs(buffer, pFile);
- OutputDebugString(buffer);
- va_end( marker );
- fclose(pFile);
- }
- #endif