⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsapp.cpp

📁 网络泡泡被.net管理
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GsApp.cpp: implementation of the CGsApp class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Gslib_internal.h"
#include "mmsystem.h"




//-----------------------------------------------------------------------------
// Internal function prototypes and variables 
//-----------------------------------------------------------------------------
enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOSOFTWARE };

//static INT     CALLBACK AboutProc( HWND, UINT, WPARAM, LPARAM );
//static LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

CGsApp* g_pGsApp	= NULL;




extern DWORD g_dwNumDevices;


static SDxDeviceInfo device_info;



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////





CGsApp::CGsApp()
{
    m_isActive		= false;
    m_isReady		= false;
	m_dwAppTime		= 0;
    m_dwBaseTime	= 0;
	m_dwStopTime	= 0;
	m_hWnd			= NULL;
	m_hAccel		= NULL;
	m_popupMenu		= NULL;
	m_hMainMenu		= NULL;
	g_pGsApp	= this;

	idr_main_accel			= 0;
	idr_main_menu			= 0;
	idr_popup_menu			= 0;
	idr_choose_mode			= 0;	
	idr_command_pause		= 0;
	idr_full_speed_mode		= 0;

	idr_app_exit			= 0;

	m_pDeviceInfo			= NULL;
    m_fnConfirmDevice		= NULL;

	m_dwSpeedRate	= 1;
	m_dwStepRate	= 10;
	m_isFrameMoving	= true;
	m_isFullspeedMode	= false;
	m_enableBackgroundActive	= false;
	strcpy(m_strWindowTitle, "");
	strcpy(m_strConfigFile, "");

	m_isMMXSupport			= ( CGsFunc::GetCPUCaps() & CPU_FEATURE_MMX ) != 0;;

	m_fLps	= 0.0f;

//	TIMER::init ();
//	TIMER::timer(0);
	timeBeginPeriod(1);
	// Initialize COM
	CoInitialize(NULL);

}

CGsApp::~CGsApp()
{
	timeEndPeriod(1);
	CleanupAllEngines();
	DxEnum_FreeResources();
	D3DXUninitialize();
    // Release COM
    CoUninitialize();
}

inline DWORD CGsApp::GetCurTime()
{
	return timeGetTime();//TIMER::time();
}

TCHAR*	CGsApp::GetAppPath(TCHAR* szBuf)
{
	if(szBuf)
		sprintf(szBuf, m_strAppPath);
	return m_strAppPath;
}


BOOL CGsApp::InitApp(HWND hWnd)
{
	if(NULL==hWnd)
		return false;
	m_hWnd	= hWnd;
	
	D3DXInitialize();
	::GetModuleFileName(NULL, m_strAppPath, 255);
	TCHAR* sz = strrchr(m_strAppPath,'\\');
	strcpy(sz, "");

	if(m_strConfigFile[0]=='\0')
	{
		if( FAILED( DxEnum_EnumerateDevices( m_fnConfirmDevice ) ) )
		{
			return false;
		}
		DxEnum_SelectDefaultDevice( &m_pDeviceInfo );
	}
	else
	{
		TCHAR strCfg[255];
		sprintf(strCfg, "%s\\%s", m_strAppPath, m_strConfigFile);
		ifstream ifs(strCfg);
		ifs.close();
		CConfig config(strCfg);
		int Flag = config.GetLong("DEVICES", "Flag", "-1");
		if(Flag==-1)
		{
			//find no info , try to create new info
			if( FAILED( DxEnum_EnumerateDevices( m_fnConfirmDevice ) ) )
			{
				return false;
			}
			DxEnum_SelectDefaultDevice( &m_pDeviceInfo );
			//save it
			config.WriteLong(0, "DEVICES", "Flag");
			config.WriteBinary("DEVICES", "DeviceInfo", (LPBYTE)(m_pDeviceInfo), sizeof(SDxDeviceInfo));
			config.WriteBinary("DEVICES", "ModeList", (LPBYTE)(m_pDeviceInfo->pddsdModes), sizeof(DDSURFACEDESC2)*m_pDeviceInfo->dwNumModes);
		}
		else
		{
			m_pDeviceInfo	= &device_info;
			ZeroMemory(m_pDeviceInfo, sizeof(SDxDeviceInfo));
			//load from config
			config.GetBinary("DEVICES", "DeviceInfo", (LPBYTE)(m_pDeviceInfo), sizeof(SDxDeviceInfo));
			m_pDeviceInfo->pDeviceGUID	= &m_pDeviceInfo->guidDevice;
			m_pDeviceInfo->pDriverGUID	= &m_pDeviceInfo->guidDriver;
			m_pDeviceInfo->pddsdModes	= new DDSURFACEDESC2[m_pDeviceInfo->dwNumModes];
			config.GetBinary("DEVICES", "ModeList", (LPBYTE)(m_pDeviceInfo->pddsdModes), sizeof(DDSURFACEDESC2)*m_pDeviceInfo->dwNumModes);

		}
	}


	OnInitApp();

//	TIMER::reset();
	m_dwBaseTime = timeGetTime();//TIMER::time();
	m_dwStopTime = m_dwBaseTime;

	//Createing popup menu
	if(m_popupMenu)
	{
		DestroyMenu(m_popupMenu);
	}
	m_popupMenu	= LoadMenu( 0, MAKEINTRESOURCE(idr_popup_menu) );

	m_hMainMenu	= GetMenu(m_hWnd);
	UpgrateAppMenu();

	m_wait_msg_list.clear();
	// The app is ready to go
	m_isReady = TRUE;
	return true;
}

//-----------------------------------------------------------------------------
// Name: Run()
// Desc: Message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT CGsApp::Run()
{

    // Now we're ready to recieve and process Windows messages.
    BOOL bGotMsg;
    MSG  msg;
    PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
    // Load keyboard accelerators
    m_hAccel = LoadAccelerators( NULL, MAKEINTRESOURCE(idr_main_accel) );

    while( WM_QUIT != msg.message  )
    {
        // Use PeekMessage() if the app is active, so we can use idle time to
        // render the scene. Else, use GetMessage() to avoid eating CPU time.
		if( m_enableBackgroundActive || m_isActive )
			bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
		else
			bGotMsg = GetMessage( &msg, NULL, 0U, 0U );

        if( bGotMsg )
        {
            // Translate and dispatch the message
            if( 0 == TranslateAccelerator( m_hWnd, m_hAccel, &msg ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
        }
        else
        {
			if(!DoIdle())
				return -1;
        }
    }

    return msg.wParam;
}

BOOL CGsApp::ProcessMSG(MSG &msg)
{
    BOOL bGotMsg;
	if( m_isActive )
		bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
	else
		bGotMsg = GetMessage( &msg, NULL, 0U, 0U );

	if( bGotMsg )
	{
		// Translate and dispatch the message
		if( 0 == TranslateAccelerator( m_hWnd, m_hAccel, &msg ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		return FALSE;
	}
	else
		return TRUE;
}

UINT CGsApp::WaitForMessage(UINT msg_wait, DWORD time_out)
{
	_WAIT_MSG	wmsg;
	wmsg.msg		= msg_wait;
	wmsg.time_out	= time_out;
	wmsg.time_pass	= 0;
	wmsg.time_start = GetAppTimeEx();
	m_wait_msg_list.push_front(wmsg);
//	std::set<UINT> msg_set;
//
//	msg_set.insert(msg_first);
//	va_list temp;
//	va_start( temp, msg_first );     /* Initialize variable arguments. */
//	UINT i=va_arg( temp, int);
//	while( i != -1 )
//	{
//		msg_set.insert(i);
//		i = va_arg( temp, int);
//	}
//	va_end( temp );              /* Reset variable arguments.      */
   
    BOOL bGotMsg;
    MSG  msg;
    PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
    // Load keyboard accelerators

    while( msg.message!=m_wait_msg_list.front().msg )
    {
		if(msg.message==WM_QUIT)
		{
			m_wait_msg_list.pop_front();
			PostQuitMessage(0);
			return 0;
		}
//		TASK_LIST::iterator	it	= m_wait_msg_list.begin();
//		it++;
//		while(it!=m_wait_msg_list.end())
//		{
//			if(msg.message==it->msg)
//			{
//				it->time_pass	= it->time_out;
//				return 0;
//			}
//			it++;
//		}
		
        // Use PeekMessage() if the app is active, so we can use idle time to
        // render the scene. Else, use GetMessage() to avoid eating CPU time.
		if( m_isActive )
			bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
		else
			bGotMsg = GetMessage( &msg, NULL, 0U, 0U );

        if( bGotMsg )
        {
            // Translate and dispatch the message
            if( 0 == TranslateAccelerator( m_hWnd, m_hAccel, &msg ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
        }
        else
        {
			if(!DoIdle())
			{
			//	return -1;
			}
        }
		m_wait_msg_list.front().time_pass	= GetAppTimeEx()-m_wait_msg_list.front().time_start;
		if(time_out>0 && m_wait_msg_list.front().time_pass>m_wait_msg_list.front().time_out)
		{
			m_wait_msg_list.pop_front();
			return 0;
		}
    }
	return msg.message;
}


HRESULT CGsApp::UpdateAllEngines( )
{
	HRESULT hr;
	CGsEngine*	pEngine;
	GSES::iterator p	= m_gses.begin();
	while(p!=m_gses.end())
	{
		pEngine	= p->second;
		//pEngine->UpdateBounds();
		if( m_isActive || m_enableBackgroundActive )
		{
			if( TRUE==pEngine->_UpdateEngine() && NULL==pEngine->GetPlayVideo() )
			{
				if(FAILED(hr=pEngine->_RenderEngine()))
				{
					if(hr==DDERR_SURFACELOST)
						return pEngine->RestoreEngine();
					return hr;
				}
				if(pEngine->IsChanged())
				{
					//frame_count	+= 10000;
					if(FAILED(hr = pEngine->Present()))
					{
						if(hr	== DDERR_SURFACELOST)
						{
							if(FAILED(pEngine->RestoreSurfaces()))
								return E_FAIL;
						}
						else
							return E_FAIL;
					}
				}
			}
		}
		p++;
	}

	return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Pause()
// Desc: Called in to toggle the pause state of the app. This function
//       brings the GDI surface to the front of the display, so drawing
//       output like message boxes and menus may be displayed.
//-----------------------------------------------------------------------------
VOID CGsApp::Pause( BOOL bPause )
{
    static DWORD dwAppPausedCount = 0L;

    dwAppPausedCount += ( bPause ? +1 : -1 );
    m_isReady          = ( dwAppPausedCount ? FALSE : TRUE );

    // Handle the first pause request (of many, nestable pause requests)
    if( bPause && ( 1 == dwAppPausedCount ) )
    {

       // Stop the scene from animating
		m_dwStopTime = timeGetTime();//TIMER::time();
    }
    if( 0 == dwAppPausedCount )
    {
        // Restart the scene
		m_dwBaseTime += timeGetTime() - m_dwStopTime;
		m_dwAppTime	= timeGetTime() - m_dwBaseTime;
    }
}



LRESULT	CGsApp::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	TASK_LIST::iterator	it = m_task_list.begin();
	while(it!=m_task_list.end())
	{
		if((*it)->IsActive())
		{
			(*it)->MsgProc(uMsg, wParam, lParam);
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -