//
// Asura_Wii_Screenlog.cpp
//
// Created by KOR - 26 July 2007
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

// This file's header.
#include "Debugging/Asura_Wii_Screenlog.h"

#ifdef ASURA_ENABLE_WII_SCREENLOG

// Asura
#include "Rendering/Asura_Render.h"

// Wii
#include "Rendering/Asura_Wii_ROMFont.h"

DEBUGVAR short dbg_sROMFontSize = 16;

Asura_Mutex Asura_Wii_Screenlog::s_xMutex;
u_int Asura_Wii_Screenlog::s_uCurrentLine = 0;
const char* Asura_Wii_Screenlog::s_aszMessageLog[uASURA_WII_SCREENLOG_MAX_LINES] = { NULL };
bool Asura_Wii_Screenlog::s_bInitialised = false;


void Asura_Wii_Screenlog::Initialise()
{
	Asura_Assert(!s_bInitialised, "Already initialised");
	for(u_int uMessage = 0; uMessage < uASURA_WII_SCREENLOG_MAX_LINES; ++uMessage)
	{
		s_aszMessageLog[uMessage] = NULL;
	}
}

void Asura_Wii_Screenlog::Log(const char* szMessage)
{
	if(!s_bInitialised)
	{
	}
	s_xMutex.Lock();
	if(Asura_Render::IsRendering())
	{
		Asura_Assert(0, "Already rendering");
		s_xMutex.Unlock();
		return;
	}
	s_aszMessageLog[s_uCurrentLine] = szMessage;

	Asura_Wii_ROMFont::SetSize(dbg_sROMFontSize, 0);
	Asura_Render::BeginFrame();
	for(u_int uMessage = 0; uMessage < uASURA_WII_SCREENLOG_MAX_LINES; ++uMessage)
	{
		const char* szMessage = s_aszMessageLog[uMessage];
		const short sX = 50;
		const short sY = 80 + dbg_sROMFontSize * uMessage;
		if(szMessage)
		{
			Asura_Wii_ROMFont::RenderString(sX, sY, 0, szMessage);
		}
		if(s_uCurrentLine == uMessage)
		{
			Asura_Wii_ROMFont::RenderString(25, sY, 0, ">");
		}

	}
	s_uCurrentLine = (s_uCurrentLine + 1) % uASURA_WII_SCREENLOG_MAX_LINES;
	Asura_Render::EndFrame();
	s_xMutex.Unlock();
}

#endif