Advertisement

Question about latest IPicture Code

Started by May 01, 2003 10:32 PM
-1 comments, last by Jumpman 21 years, 10 months ago
I've noticed in the latest Ipicture code, nehe has moved the update() and draw() functions from the message pump to the WM_PAINT() window message.. I'm just wondering if there is any performance benifits to that. I've been using the following message pump (which allows me to limit the MAX fps (based on the generic directX loop) or run it flat out..

HRESULT MyProgram::MainMsgLoop()
{
	DWORD fps = 75;				// the maximum frame rate, set this to 0 for unlimited

	BOOL fConsumed;				// do we have to dispatch the windows message
	MSG msg;				// memory for a windows message

	LONGLONG cur_time;			// actual time at the start of a check for the next frame
	DWORD time_count;			// time for a frame at the given fps rate
	LONGLONG perf_cnt;			// speed of the performance timer
	BOOL perf_flag = FALSE;			// are we using the mmsystem timer or the win32 one
	LONGLONG next_time = 0;			// start time of the next frame (will be calculated after a frame was rendered)
	LONGLONG elapsed_time = 0;		// time a frame really need to render

	// are we able to use the precion timer from mmsystem?
	if (QueryPerformanceFrequency((LARGE_INTEGER*) &perf_cnt))
	{
		// yes, fine
		perf_flag = TRUE;

		// length of a frame in ticks
		if ( fps )
			time_count = (DWORD) perf_cnt/fps;
		else
			time_count = 0;

		// init the time for the next frame (simply set it to now)
		QueryPerformanceCounter((LARGE_INTEGER*) &next_time);
	}
	else
	{
		// use wim32 timer ticks

		// length of a frame in ticks
		if ( fps )
			time_count = 1000/fps;
		else
			time_count = 0;

		// init the time for the next frame (simply set it to now)
		next_time = timeGetTime();
	}

	for (;
	{
		fConsumed = FALSE;

		// first hanlde win32 messages
		if (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
				break;

			ApcHost_OnTranslateMessage(&msg, &fConsumed);

			if (!fConsumed)
			{
				DispatchMessage(&msg);
			}
		}
		else 
		{
			// are we in game mode?
			if (g_pPlayer && !g_pPlayer->m_fPause)
			{
				// get the current timer ticks depending on measuring method (mmsystem or win32)
				if (perf_flag)
					QueryPerformanceCounter((LARGE_INTEGER*) &cur_time);
				else
					cur_time = timeGetTime();

				// is it time for rendering the next frame?
				if ( cur_time > next_time )
				{
					// Fine, so do it
					g_pPlayer->Render();
					fConsumed = TRUE;

					// calculate the start of the next frame
					// (start of the current frame) + (time slice of a frame) - (the time we used to render this frame)
					next_time = cur_time + time_count;
				}
			}
			else
			{
				// not in game mode, simply wait for the next message
				WaitMessage();
			}
		}
	}
	return S_OK;
}
  
and the render loop

void Player::Render()
{
	LARGE_INTEGER liNewTime;
	QueryPerformanceCounter(&liNewTime);

	float timepassed = (float)(liNewTime.LowPart - m_liOldTime.LowPart) / (float)m_liCounterFrequency.LowPart;

	// Get time in milliseconds since last render loop
	m_timeCur = (int)(((liNewTime.QuadPart - m_liStartTime.QuadPart)*1000)/m_liCounterFrequency.LowPart);

etc..
}
  
I know ev everybodys loops are different, just wondering why the move the PAINT message Hope somebody can help.. Cheers Chris [edited by - jumpman on May 1, 2003 11:34:25 PM]

This topic is closed to new replies.

Advertisement