/*	This is called every frame, and can also be called explicitly to flush
	text to the screen.

	WARNING: be very careful calling this from elsewhere, because the refresh
	needs almost the entire 256k of stack space!
*/
void SCR_UpdateScreen (void)
{
	if(scr_disabled_for_loading)
	{
		if(realtime-scr_disabled_time > 60.0f)
		{
			scr_disabled_for_loading = false;

			Con_Printf ("load failed.\n");
		}
		else
			return;
	}

	if(!bScreenInitialized || !bConsoleInitialized)
		return;				// not initialized yet

	GL_BeginRendering(&glx,&gly,&glwidth,&glheight);

	Video.iFrameCount++;
	// Don't let us exceed a limited count.
	if (Video.iFrameCount > 100000)
		Video.iFrameCount = 0;

	// determine size of refresh window
	if(oldfov != scr_fov.value)
	{
		oldfov = scr_fov.value;
		vid.bRecalcRefDef = true;
	}

	if (oldscreensize != scr_viewsize.value)
	{
		oldscreensize = scr_viewsize.value;
		vid.bRecalcRefDef = true;
	}

	//johnfitz -- added oldsbarscale and oldsbaralpha
	if (oldsbarscale != scr_sbarscale.value)
	{
		oldsbarscale = scr_sbarscale.value;
		vid.bRecalcRefDef = true;
	}

	if (oldsbaralpha != scr_sbaralpha.value)
	{
		oldsbaralpha = scr_sbaralpha.value;
		vid.bRecalcRefDef = true;
	}
	//johnfitz

	if (vid.bRecalcRefDef)
		Screen_UpdateSize();

	// do 3D refresh drawing, and then update the screen
	Screen_SetUpToDrawConsole();

	if (cv_video_msaasamples.iValue > 0)
		VideoLayer_Enable(VIDEO_MULTISAMPLE);

	V_RenderView ();

	if (cv_video_msaasamples.iValue > 0)
		VideoLayer_Disable(VIDEO_MULTISAMPLE);

	Draw_ResetCanvas();

	//FIXME: only call this when needed
	SCR_TileClear();

	if(bDrawDialog) //new game confirm
	{
		g_menu->Draw();

		Draw_FadeScreen();
		SCR_DrawNotifyString();
	}
	else if (g_menu->GetState() & MENU_STATE_LOADING) //loading
		g_menu->Draw();
	else if(cl.intermission == 1 && key_dest == key_game) //end of level
	{}
	else if(cl.intermission == 2 && key_dest == key_game) //end of episode
		SCR_CheckDrawCenterString ();
	else
	{
		g_menu->Draw();

#ifdef VIDEO_SUPPORT_SHADERS
#ifdef VIDEO_SUPPORT_FRAMEBUFFERS
		//VideoPostProcess_Draw();
		DEBUG_FrameBufferDraw();
#endif
#endif

		SCR_DrawNet();
		SCR_DrawTurtle();
		SCR_DrawPause();
		SCR_CheckDrawCenterString ();
		SCR_DrawDevStats(); //johnfitz
		SCR_DrawClock(); //johnfitz
		Screen_DrawConsole();
		Screen_DrawFPS(); //johnfitz
	}

	V_UpdateBlend(); //johnfitz -- V_UpdatePalette cleaned up and renamed
}
/*	This is called every frame, and can also be called explicitly to flush
	text to the screen.

	WARNING: be very careful calling this from elsewhere, because the refresh
	needs almost the entire 256k of stack space!
*/
void SCR_UpdateScreen (void)
{
	if(scr_disabled_for_loading)
	{
		if(realtime-scr_disabled_time > 60.0f)
		{
			scr_disabled_for_loading = false;

			Con_Printf ("load failed.\n");
		}
		else
			return;
	}

	if(!bScreenInitialized || !bConsoleInitialized)
		return;				// not initialized yet

	GL_BeginRendering(&glx,&gly,&glwidth,&glheight);

	// determine size of refresh window
	if(oldfov != scr_fov.value)
	{
		oldfov = scr_fov.value;
		vid.bRecalcRefDef = true;
	}

	if (oldscreensize != scr_viewsize.value)
	{
		oldscreensize = scr_viewsize.value;
		vid.bRecalcRefDef = true;
	}

	//johnfitz -- added oldsbarscale and oldsbaralpha
	if (oldsbarscale != scr_sbarscale.value)
	{
		oldsbarscale = scr_sbarscale.value;
		vid.bRecalcRefDef = true;
	}

	if (oldsbaralpha != scr_sbaralpha.value)
	{
		oldsbaralpha = scr_sbaralpha.value;
		vid.bRecalcRefDef = true;
	}
	//johnfitz

	if (vid.bRecalcRefDef)
		SCR_CalcRefdef ();

	// do 3D refresh drawing, and then update the screen
	SCR_SetUpToDrawConsole ();

	V_RenderView ();

	Draw_ResetCanvas();

	//FIXME: only call this when needed
	SCR_TileClear();

	if(bDrawDialog) //new game confirm
	{
		Menu->Draw();

		Draw_FadeScreen();
		SCR_DrawNotifyString();
	}
	else if(Menu->GetState() & MENU_STATE_LOADING) //loading
		Menu->Draw();
	else if(cl.intermission == 1 && key_dest == key_game) //end of level
	{}
	else if(cl.intermission == 2 && key_dest == key_game) //end of episode
		SCR_CheckDrawCenterString ();
	else
	{
		Menu->Draw();

		SCR_DrawNet();
		SCR_DrawTurtle();
		SCR_DrawPause();
		SCR_CheckDrawCenterString ();
		SCR_DrawDevStats(); //johnfitz
		Screen_DrawFPS(); //johnfitz
		SCR_DrawClock(); //johnfitz
		SCR_DrawConsole();
	}

	V_UpdateBlend(); //johnfitz -- V_UpdatePalette cleaned up and renamed
}