/********************************************************************
	液晶検査(開発技術の依頼ver)
********************************************************************/

#include <wchar.h>
#include <stdlib.h>

#include <nn.h>
#include "sys.h"
#include "TestLcd_RED.h"
#include "../backlight/BackLightTest.h"
#include "../sec/FileViewer.h"
#include <nn/gx/CTR/gx_LcdBacklight.h>

using namespace uji;
using namespace uji::sys;
using namespace uji::seq;
using namespace uji::eva;



bool TestLcd_RED::IsBmp( const wchar_t* openPath )
{
	const wchar_t* cmp_bmp = L".bmp";
    int pathLength = std::wcslen( openPath ) - 1;  // パス名配列の末尾の添字
    if( openPath[ pathLength ] == L'p' )
	{
		pathLength--;
	    if( openPath[ pathLength ] == L'm' )
		{
			pathLength--;
			if( openPath[ pathLength ] == L'b' )
			{
				pathLength--;
				if( openPath[ pathLength ] == L'.' )
				{
					return true;
				}
				else	return false;
			}
			else	return false;
		}
		else	return false;
	}
	else	return false;
}

bool TestLcd_RED::IsLeft( const wchar_t* openPath )
{
	const wchar_t* cmp_left = L".left";
    int pathLength = std::wcslen( openPath ) - 5;  // パス名配列の末尾の添字
    if( openPath[ pathLength ] == L't' )
	{
		pathLength--;
		    if( openPath[ pathLength ] == L'f' )
			{
				pathLength--;
				if( openPath[ pathLength ] == L'e' )
				{
					pathLength--;
					if( openPath[ pathLength ] == L'l' )
					{
						pathLength--;
						if( openPath[ pathLength ] == L'_' )
						{
							return true;
						}
						else	return false;
					}
					else	return false;
				}
				else	return false;
			}
			else	return false;
	}
	else	return false;
}

/*
	3D表示専用プログラム
*/
void TestLcd_RED::OpenBmp3D( const wchar_t* openPath )
{
	// bmpじゃなかったら終了
	if( !TestLcd_RED::IsBmp(openPath) )		return;
	if( !TestLcd_RED::IsLeft(openPath) )	return;

	bool loop_once = true; 
	bool flag_BmpTop = false;
	bool abl_flag = false;	// アクティブバックライトのON/OFF判定用
	int light_level = 5;	// 輝度設定用

	BmpTexture *s_BmpTop      = 0;
	GLuint s_TextureIdTop     = 0;
	BmpTexture *s_BmpTop2     = 0;
	GLuint s_TextureIdTop2    = 0;

	uji::sys::GraphicsDrawing *gfx = uji::sys::GraphicsDrawing::GetInstance();
    uji::sys::Pad pad;

	// 上画面に画像表示
	s_BmpTop  = new BmpTexture();
	// openPathのファイルを開く
	s_BmpTop -> Open( openPath );

	if( s_TextureIdTop != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpTop->INTERNAL_FORMAT,
											s_BmpTop->GetTextureWidth(),
											s_BmpTop->GetTextureHeight(),
											s_BmpTop->FORMAT,
											s_BmpTop->TYPE,
											s_BmpTop->GetTextureDataPointer(),
											s_TextureIdTop );
    wchar_t* dirPath  = new wchar_t[ 256 ];
    std::wcscpy( dirPath, openPath );
	int pathLength = std::wcslen( dirPath ) - 1;  // パス名配列の末尾の添字
    do{
        dirPath[ pathLength ] = L'\0';
        pathLength--;
    }while( dirPath[ pathLength ] != L'_' );  // アンダーバーの手前までを\0で埋める
	std::wcscat( dirPath, L"right.bmp" );
	// 3D用、第2の画像を読み込み・表示
	s_BmpTop2  = new BmpTexture();
	// 右目用を開く
	if( !s_BmpTop2 -> Open( dirPath ) )
	{
        NN_LOG("Failed to open File.\n");
		gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop );
		s_BmpTop -> Close();
		delete s_BmpTop;
		return;
	}
	if( s_TextureIdTop2 != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop2 );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpTop2->INTERNAL_FORMAT,
											s_BmpTop2->GetTextureWidth(),
											s_BmpTop2->GetTextureHeight(),
											s_BmpTop2->FORMAT,
											s_BmpTop2->TYPE,
											s_BmpTop2->GetTextureDataPointer(),
											s_TextureIdTop2 );
	flag_BmpTop = true;

	do{
		// スリープモードの有効
		if (sys::SleepHandler::IsSleepRequested())
		{
			// スリープモードに入る
			sys::SleepHandler::SleepSystem();
			// 再開後の処理
			nn::gx::StartLcdDisplay();
		}
        //--------------------------------------------------------------------------
        // キー操作関連
        //--------------------------------------------------------------------------
        Pad().UpdatePad();
		// 上下キーで輝度を1〜5に変更
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_UP))
		{
			light_level = uji::eva::backlight::UpLightLevel( light_level );
		}
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_DOWN))
		{
			light_level = uji::eva::backlight::DownLightLevel( light_level );
		}
		// LボタンでアクティブバックライトのON
		if( sys::Pad().IsButtonRepeat( Pad::BUTTON_L ) )
		{
			nn::gx::CTR::Backlight::EnableActiveBl(nn::gx::CTR::LCD_BOTH);
			abl_flag = true;
		}
		// RボタンでアクティブバックライトのOFF
		if( sys::Pad().IsButtonRepeat( Pad::BUTTON_R ) )
		{
			nn::gx::CTR::Backlight::DisableActiveBl(nn::gx::CTR::LCD_BOTH);
			abl_flag = false;
		}

        //--------------------------------------------------------------------------
        // 表示部分
        //--------------------------------------------------------------------------

		// 3Dモード
		gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_STEREO);
		gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
        gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
		gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
													0.0f, 0.0f, 400.0f, 240.0f,
													400.0f, 240.0f,
													s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
		gfx->m_DrawFramework->Transfer();

		gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0_EXT );
        gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
		gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop2,
													0.0f, 0.0f, 400.0f, 240.0f,
													400.0f, 240.0f,
													s_BmpTop2->GetTextureWidth(), s_BmpTop2->GetTextureHeight() );
		gfx->m_DrawFramework->Transfer();
		gfx->m_DrawFramework->SwapBuffers_ParallaxBarrier();


		// 下画面に表示窓作成
		gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
		gfx->m_DrawFramework->Clear();
		if( loop_once )
		{
			m_WindowManager.CreateWindow( &m_LowerWindow, NN_GX_DISPLAY1, 0, 0 );
			loop_once = false;
		}
		m_LowerWindow.SetTitle( "HOW TO USE" );
		m_LowerWindow.Gotoxy(0,0);
		m_LowerWindow.Printf("    B:Back\n");
		m_LowerWindow.Printf("    L:Active Backlight ON\n");
		m_LowerWindow.Printf("    R:Active Backlight OFF\n");
		m_LowerWindow.Printf("   UP:Increase Brightness Level\n");
		m_LowerWindow.Printf(" DOWN:Decrease Brightness Level\n\n");
		m_LowerWindow.Printf("Brightness Level : %d\n", light_level );
		if( abl_flag )	m_LowerWindow.Printf("Active Backlight : ON\n");
		else			m_LowerWindow.Printf("Active Backlight : OFF\n");
		m_LowerWindow.Printf("    Top LCD Mode : 3D\n");
		m_WindowManager.Update();
		m_WindowManager.DrawDisplay1();	
		gfx->m_DrawFramework->SwapBuffers();

		gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);


	// Bボタンでファイルリストに戻る
    }while( !Pad().IsButtonRepeat( Pad::BUTTON_B ) );
	// 画像消去
	if( s_TextureIdTop != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop );
	if( s_TextureIdTop2 != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop2 );
	if( flag_BmpTop )
	{
		s_BmpTop -> Close();
		delete s_BmpTop;
		s_BmpTop2 -> Close();
		delete s_BmpTop2;
		NN_LOG("BmpTop*2_deleted\n");
		flag_BmpTop = false;
	}
	gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
	// バックライト輝度最大
	nn::gx::CTR::Backlight::SetLuminanceLevel(nn::gx::CTR::LCD_BOTH, 5);
	// アクティブバックライトOFF
	nn::gx::CTR::Backlight::DisableActiveBl(nn::gx::CTR::LCD_BOTH);

}

/*---------------------------------------------------------------------------
  name: OpenBmpViewer
  Desc: 選択した画像(.bmp)を表示する

  Args: openPath = 開くファイルのパス
        
  Rtns: なし
---------------------------------------------------------------------------*/  
void TestLcd_RED::OpenBmpViewer( const wchar_t* openPath )
{
	// bmpじゃなかったら終了
	if( !TestLcd_RED::IsBmp(openPath) )		return;

	bool loop_once = true; 
	bool flag_BmpTop = false;
	bool flag_BmpBottom = false; 
	bool abl_flag = false;	// アクティブバックライトのON/OFF判定用
	int lcd_mode  = 0;		// 上下画面の表示切り替え
	int upper_mode = 0;		// 上画面の表示切り替え
	int light_level = 5;	// 輝度設定用

	BmpTexture *s_BmpTop      = 0;
	GLuint s_TextureIdTop     = 0;
	BmpTexture *s_BmpBottom   = 0;
	GLuint s_TextureIdBottom  = 0;
	BmpTexture *s_BmpTop2     = 0;
	GLuint s_TextureIdTop2    = 0;

	uji::sys::GraphicsDrawing *gfx = uji::sys::GraphicsDrawing::GetInstance();
    uji::sys::Pad pad;

	// 上画面に画像表示
	s_BmpTop  = new BmpTexture();
	// openPathのファイルを開く
	s_BmpTop -> Open( openPath );
	// 3D用、第2の画像を読み込み・表示
	s_BmpTop2  = new BmpTexture();
	// 決まったファイルを開く
	s_BmpTop2 -> Open( L"rom:/bmp/ColorBar_400x240.bmp" );
	flag_BmpTop = true;
	// 下画面にも画像ロード
	s_BmpBottom  = new BmpTexture();
	s_BmpBottom -> Open( openPath );
	flag_BmpBottom = true;

	if( s_TextureIdTop != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpTop->INTERNAL_FORMAT,
											s_BmpTop->GetTextureWidth(),
											s_BmpTop->GetTextureHeight(),
											s_BmpTop->FORMAT,
											s_BmpTop->TYPE,
											s_BmpTop->GetTextureDataPointer(),
											s_TextureIdTop );
	if( s_TextureIdTop2 != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop2 );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpTop2->INTERNAL_FORMAT,
											s_BmpTop2->GetTextureWidth(),
											s_BmpTop2->GetTextureHeight(),
											s_BmpTop2->FORMAT,
											s_BmpTop2->TYPE,
											s_BmpTop2->GetTextureDataPointer(),
											s_TextureIdTop2 );
	if( s_TextureIdBottom != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpBottom->INTERNAL_FORMAT,
											s_BmpBottom->GetTextureWidth(),
											s_BmpBottom->GetTextureHeight(),
											s_BmpBottom->FORMAT,
											s_BmpBottom->TYPE,
											s_BmpBottom->GetTextureDataPointer(),
											s_TextureIdBottom );

    do{
		// スリープモードの有効
		if (sys::SleepHandler::IsSleepRequested())
		{
			// スリープモードに入る
			sys::SleepHandler::SleepSystem();
			// 再開後の処理
			nn::gx::StartLcdDisplay();
		}
        //--------------------------------------------------------------------------
        // キー操作関連
        //--------------------------------------------------------------------------
        Pad().UpdatePad();
		// 上下キーで輝度を1〜5に変更
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_UP))
		{
			light_level = uji::eva::backlight::UpLightLevel( light_level );
		}
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_DOWN))
		{
			light_level = uji::eva::backlight::DownLightLevel( light_level );
		}
		// LボタンでアクティブバックライトのON
		if( sys::Pad().IsButtonRepeat( Pad::BUTTON_L ) )
		{
			nn::gx::CTR::Backlight::EnableActiveBl(nn::gx::CTR::LCD_BOTH);
			abl_flag = true;
		}
		// RボタンでアクティブバックライトのOFF
		if( sys::Pad().IsButtonRepeat( Pad::BUTTON_R ) )
		{
			nn::gx::CTR::Backlight::DisableActiveBl(nn::gx::CTR::LCD_BOTH);
			abl_flag = false;
		}
		// STARTボタンで画像表示モード切替
		if( pad.IsButtonRepeat( Pad::BUTTON_START ) )
		{
			if( lcd_mode == 0 )
			{
				m_WindowManager.DestroyWindow( &m_LowerWindow );
			}
			else if( lcd_mode == 1 )
			{
				m_WindowManager.DestroyWindow( &m_UpperWindow );
			}
			lcd_mode++;
			loop_once = true;
			if( lcd_mode > 2 )	lcd_mode = 0;
		}

		// 上画面に画像表示時、Aボタンでモード切り替え(400→800→3D)
		if( lcd_mode == 0 || lcd_mode == 2 )
		{
			if( pad.IsButtonRepeat( Pad::BUTTON_A ) )
			{
				if( lcd_mode == 0 )
				{
					m_WindowManager.DestroyWindow( &m_LowerWindow );
				}
				loop_once = true;
				upper_mode ++;
				if( upper_mode > 2 )	upper_mode = 0;
			}
		}

        //--------------------------------------------------------------------------
        // 表示部分
        //--------------------------------------------------------------------------

		if( lcd_mode == 0 )
		{
			if( upper_mode == 0 )
			{
				// 開いて読み込んだ画像を上画面に表示 400モード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
			}
			else if( upper_mode == 1 )
			{
				// 800モード 2回書かないと更新されない？
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);

				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
			}
			else
			{
				// 3Dモード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_STEREO);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0_EXT );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop2,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();
				gfx->m_DrawFramework->SwapBuffers_ParallaxBarrier();
			}
			// 下画面に表示窓作成
			gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
			gfx->m_DrawFramework->Clear();
			if( loop_once )
			{
				m_WindowManager.CreateWindow( &m_LowerWindow, NN_GX_DISPLAY1, 0, 0 );
				loop_once = false;
			}
			m_LowerWindow.SetTitle( "HOW TO USE" );
			m_LowerWindow.Gotoxy(0,0);
			m_LowerWindow.Printf("    B:Back\n");
			m_LowerWindow.Printf("    A:Change Top LCD Mode\n");
			m_LowerWindow.Printf("    L:Active Backlight ON\n");
			m_LowerWindow.Printf("    R:Active Backlight OFF\n");
			m_LowerWindow.Printf("   UP:Increase Brightness Level\n");
			m_LowerWindow.Printf(" DOWN:Decrease Brightness Level\n");
			m_LowerWindow.Printf("START:Change Display Mode\n\n");
			m_LowerWindow.Printf("Brightness Level : %d\n", light_level );
			if( abl_flag )	m_LowerWindow.Printf("Active Backlight : ON\n");
			else			m_LowerWindow.Printf("Active Backlight : OFF\n");
			if( upper_mode == 0 )		m_LowerWindow.Printf("    Top LCD Mode : 2D(400LINE)\n");
			else if( upper_mode == 1 )	m_LowerWindow.Printf("    Top LCD Mode : 2D(800LINE)\n");
			else						m_LowerWindow.Printf("    Top LCD Mode : 3D\n");
			m_WindowManager.Update();
			m_WindowManager.DrawDisplay1();	
			gfx->m_DrawFramework->SwapBuffers();	    
			gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
		}
		else if( lcd_mode == 1 )
		{
			// 開いて読み込んだ画像を下画面に表示
			gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
			gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
			gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
			gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
														0.0f, 0.0f, 320.0f, 240.0f, 
														320.0f, 240.0f,
														s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
			gfx->m_DrawFramework->SwapBuffers();

			// 上画面に表示窓作成
			gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
			gfx->m_DrawFramework->Clear();
			if( loop_once )
			{
				m_WindowManager.CreateWindow( &m_UpperWindow, NN_GX_DISPLAY0, 0, 0 );
				loop_once = false;
			}
			m_UpperWindow.SetTitle( "HOW TO USE" );
			m_UpperWindow.Gotoxy(0,0);
			m_UpperWindow.Printf("    B:Back\n");
			m_UpperWindow.Printf("    L:Active Backlight ON\n");
			m_UpperWindow.Printf("    R:Active Backlight OFF\n");
			m_UpperWindow.Printf("   UP:Increase Brightness Level\n");
			m_UpperWindow.Printf(" DOWN:Decrease Brightness Level\n");
			m_UpperWindow.Printf("START:Change Display Mode\n\n");
			m_UpperWindow.Printf("Brightness Level : %d\n", light_level );
			if( abl_flag )	m_UpperWindow.Printf("Active Backlight : ON\n");
			else			m_UpperWindow.Printf("Active Backlight : OFF\n");
			m_WindowManager.Update();
			m_WindowManager.DrawDisplay0();	
			gfx->m_DrawFramework->SwapBuffers();	    
			gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
		}
		else if( lcd_mode == 2 )
		{
			if( upper_mode == 0 )
			{
				// 開いて読み込んだ画像を上画面に表示 400モード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
				gfx->m_DrawFramework->Clear();
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->m_DrawFramework->Clear();
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
			}
			else if( upper_mode == 1 )
			{
				// 800モード 2回書かないと更新されない？
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);

				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
			}
			else
			{
				// 3Dモード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_STEREO);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0_EXT );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop2,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop2->GetTextureWidth(), s_BmpTop2->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SwapBuffers_ParallaxBarrier();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
			}
		}
	// Bボタンでファイルリストに戻る
    }while( !Pad().IsButtonRepeat( Pad::BUTTON_B ) );
	// 画像消去
	if( s_TextureIdTop != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop );
	if( s_TextureIdTop2 != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop2 );
	if( flag_BmpTop )
	{
		s_BmpTop -> Close();
		delete s_BmpTop;
		s_BmpTop2 -> Close();
		delete s_BmpTop2;
		NN_LOG("BmpTop*2_deleted\n");
		flag_BmpTop = false;
	}
	if( s_TextureIdBottom  != 0 )gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
	if( flag_BmpBottom )
	{
		s_BmpBottom  -> Close();
		delete s_BmpBottom;
		NN_LOG("BmpBottom_deleted\n");
		flag_BmpBottom = false;
	}
	gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
	// バックライト輝度最大
	nn::gx::CTR::Backlight::SetLuminanceLevel(nn::gx::CTR::LCD_BOTH, 5);
	// アクティブバックライトOFF
	nn::gx::CTR::Backlight::DisableActiveBl(nn::gx::CTR::LCD_BOTH);
}

/*---------------------------------------------------------------------------
  name: NumberBmpViewer
  Desc: 番号順に画像(.bmp)を表示する

  Args: openPath = 開くファイルのパス
        
  Rtns: なし
---------------------------------------------------------------------------*/  
void TestLcd_RED::NumberBmpViewer( const wchar_t* rootPath )
{
	bool loop_once = true; 
	bool flag_BmpTop = false;
	bool flag_BmpBottom = false; 
	bool abl_flag = false;	// アクティブバックライトのON/OFF判定用
	int lcd_mode  = 0;		// 上下画面の表示切り替え
	int upper_mode = 0;		// 上画面の表示切り替え
	int light_level = 5;	// 輝度設定用
	int bmp_num = 0;
    wchar_t* tempPath  = new wchar_t[ 256 ];
    wchar_t* openPath  = new wchar_t[ 256 ];
    std::wcscpy( openPath, rootPath );
	swprintf( tempPath, 256, L"%d.bmp", bmp_num );
	int pathLength = std::wcslen( openPath ) - 1;  // パス名配列の末尾の添字
    do{
        openPath[ pathLength ] = L'\0';
        pathLength--;
	}while( openPath[ pathLength ] != L'/' );  // /の手前までを\0で埋める
	std::wcscat( openPath, tempPath );

	BmpTexture *s_BmpTop      = 0;
	GLuint s_TextureIdTop     = 0;
	BmpTexture *s_BmpBottom   = 0;
	GLuint s_TextureIdBottom  = 0;
	BmpTexture *s_BmpTop2     = 0;
	GLuint s_TextureIdTop2    = 0;

	uji::sys::GraphicsDrawing *gfx = uji::sys::GraphicsDrawing::GetInstance();
    uji::sys::Pad pad;

	// 上画面に画像表示
	s_BmpTop  = new BmpTexture();
	// openPathのファイルを開く
    if( !s_BmpTop -> Open( openPath ) )		return;
	// 3D用、第2の画像を読み込み・表示
	s_BmpTop2  = new BmpTexture();
	// 決まったファイルを開く
	s_BmpTop2 -> Open( L"rom:/bmp/ColorBar_400x240.bmp" );
	flag_BmpTop = true;
	// 下画面にも画像ロード
	s_BmpBottom  = new BmpTexture();
	s_BmpBottom -> Open( openPath );
	flag_BmpBottom = true;

	if( s_TextureIdTop != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpTop->INTERNAL_FORMAT,
											s_BmpTop->GetTextureWidth(),
											s_BmpTop->GetTextureHeight(),
											s_BmpTop->FORMAT,
											s_BmpTop->TYPE,
											s_BmpTop->GetTextureDataPointer(),
											s_TextureIdTop );
	if( s_TextureIdTop2 != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop2 );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpTop2->INTERNAL_FORMAT,
											s_BmpTop2->GetTextureWidth(),
											s_BmpTop2->GetTextureHeight(),
											s_BmpTop2->FORMAT,
											s_BmpTop2->TYPE,
											s_BmpTop2->GetTextureDataPointer(),
											s_TextureIdTop2 );
	if( s_TextureIdBottom != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
	gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
											s_BmpBottom->INTERNAL_FORMAT,
											s_BmpBottom->GetTextureWidth(),
											s_BmpBottom->GetTextureHeight(),
											s_BmpBottom->FORMAT,
											s_BmpBottom->TYPE,
											s_BmpBottom->GetTextureDataPointer(),
											s_TextureIdBottom );

    do{
		// スリープモードの有効
		if (sys::SleepHandler::IsSleepRequested())
		{
			// スリープモードに入る
			sys::SleepHandler::SleepSystem();
			// 再開後の処理
			nn::gx::StartLcdDisplay();
		}
        //--------------------------------------------------------------------------
        // キー操作関連
        //--------------------------------------------------------------------------
        Pad().UpdatePad();
		// 上下キーで輝度を1〜5に変更
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_UP))
		{
			light_level = uji::eva::backlight::UpLightLevel( light_level );
		}
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_DOWN))
		{
			light_level = uji::eva::backlight::DownLightLevel( light_level );
		}
		// 左キーで戻る
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_LEFT))
		{
			if( bmp_num == 0 )
			{
			}
			else
			{
				bmp_num --;
				swprintf( tempPath, 256, L"%d.bmp", bmp_num );
				pathLength = std::wcslen( openPath ) - 1;  // パス名配列の末尾の添字
				do{
					openPath[ pathLength ] = L'\0';
					pathLength--;
				}while( openPath[ pathLength ] != L'/' );  // /の手前までを\0で埋める
				std::wcscat( openPath, tempPath );
				// 戻ったファイルのパス設定完了

				// 読み込み直す前に現在ロードされてる画像を消去
				if( s_TextureIdTop != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop );
				if( flag_BmpTop )
				{
					s_BmpTop -> Close();
					delete s_BmpTop;
					NN_LOG("BmpTop_deleted\n");
					flag_BmpTop = false;
				}
				if( s_TextureIdBottom  != 0 )gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
				if( flag_BmpBottom )
				{
					s_BmpBottom  -> Close();
					delete s_BmpBottom;
					NN_LOG("BmpBottom_deleted\n");
					flag_BmpBottom = false;
				}

				// 上画面用の画像ロード
				s_BmpTop  = new BmpTexture();
				// openPathのファイルを開く
				if( !s_BmpTop -> Open( openPath ) )		return;
				flag_BmpTop = true;
				// 下画面にも画像ロード
				s_BmpBottom  = new BmpTexture();
				s_BmpBottom -> Open( openPath );
				flag_BmpBottom = true;
				// 上下画面にロードした画像セット
				if( s_TextureIdTop != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop );
				gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
														s_BmpTop->INTERNAL_FORMAT,
														s_BmpTop->GetTextureWidth(),
														s_BmpTop->GetTextureHeight(),
														s_BmpTop->FORMAT,
														s_BmpTop->TYPE,
														s_BmpTop->GetTextureDataPointer(),
														s_TextureIdTop );
				if( s_TextureIdBottom != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
				gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
														s_BmpBottom->INTERNAL_FORMAT,
														s_BmpBottom->GetTextureWidth(),
														s_BmpBottom->GetTextureHeight(),
														s_BmpBottom->FORMAT,
														s_BmpBottom->TYPE,
														s_BmpBottom->GetTextureDataPointer(),
														s_TextureIdBottom );
			}
		}
		// 右キーで進む
		if (sys::Pad().IsButtonDown(sys::Pad::BUTTON_RIGHT))
		{
			bmp_num ++;
			swprintf( tempPath, 256, L"%d.bmp", bmp_num );
			pathLength = std::wcslen( openPath ) - 1;  // パス名配列の末尾の添字
			do{
				openPath[ pathLength ] = L'\0';
				pathLength--;
			}while( openPath[ pathLength ] != L'/' );  // /の手前までを\0で埋める
			std::wcscat( openPath, tempPath );
			// 進んだファイルのパス設定完了

			// 画像消去
			if( s_TextureIdTop != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop );
			if( flag_BmpTop )
			{
				s_BmpTop -> Close();
				delete s_BmpTop;
				NN_LOG("BmpTop_deleted\n");
				flag_BmpTop = false;
			}
			if( s_TextureIdBottom  != 0 )gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
			if( flag_BmpBottom )
			{
				s_BmpBottom  -> Close();
				delete s_BmpBottom;
				NN_LOG("BmpBottom_deleted\n");
				flag_BmpBottom = false;
			}


			// 上画面に画像表示
			s_BmpTop  = new BmpTexture();
			// openPathのファイルを開く
			if( !s_BmpTop -> Open( openPath ) )
			{
				bmp_num --;
				swprintf( tempPath, 256, L"%d.bmp", bmp_num );
				pathLength = std::wcslen( openPath ) - 1;  // パス名配列の末尾の添字
				do{
					openPath[ pathLength ] = L'\0';
					pathLength--;
				}while( openPath[ pathLength ] != L'/' );  // /の手前までを\0で埋める
				std::wcscat( openPath, tempPath );
				if( !s_BmpTop -> Open( openPath ) )		return;
			}
			flag_BmpTop = true;
			// 下画面にも画像ロード
			s_BmpBottom  = new BmpTexture();
			s_BmpBottom -> Open( openPath );
			flag_BmpBottom = true;

			if( s_TextureIdTop != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdTop );
			gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
													s_BmpTop->INTERNAL_FORMAT,
													s_BmpTop->GetTextureWidth(),
													s_BmpTop->GetTextureHeight(),
													s_BmpTop->FORMAT,
													s_BmpTop->TYPE,
													s_BmpTop->GetTextureDataPointer(),
													s_TextureIdTop );
			if( s_TextureIdBottom != 0 )	gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
			gfx->m_DrawFramework->GenerateTexture(	GL_TEXTURE_2D, 
													s_BmpBottom->INTERNAL_FORMAT,
													s_BmpBottom->GetTextureWidth(),
													s_BmpBottom->GetTextureHeight(),
													s_BmpBottom->FORMAT,
													s_BmpBottom->TYPE,
													s_BmpBottom->GetTextureDataPointer(),
													s_TextureIdBottom );

		}
		// LボタンでアクティブバックライトのON
		if( sys::Pad().IsButtonRepeat( Pad::BUTTON_L ) )
		{
			nn::gx::CTR::Backlight::EnableActiveBl(nn::gx::CTR::LCD_BOTH);
			abl_flag = true;
		}
		// RボタンでアクティブバックライトのOFF
		if( sys::Pad().IsButtonRepeat( Pad::BUTTON_R ) )
		{
			nn::gx::CTR::Backlight::DisableActiveBl(nn::gx::CTR::LCD_BOTH);
			abl_flag = false;
		}
		// STARTボタンで画像表示モード切替
		if( pad.IsButtonRepeat( Pad::BUTTON_START ) )
		{
			if( lcd_mode == 0 )
			{
				m_WindowManager.DestroyWindow( &m_LowerWindow );
			}
			else if( lcd_mode == 1 )
			{
				m_WindowManager.DestroyWindow( &m_UpperWindow );
			}
			lcd_mode++;
			loop_once = true;
			if( lcd_mode > 2 )	lcd_mode = 0;
		}
		// 上画面に画像表示時、Aボタンでモード切り替え(400→800→3D)
		if( lcd_mode == 0 || lcd_mode == 2 )
		{
			if( pad.IsButtonRepeat( Pad::BUTTON_A ) )
			{
				if( lcd_mode == 0 )
				{
					m_WindowManager.DestroyWindow( &m_LowerWindow );
				}
				loop_once = true;
				upper_mode ++;
				if( upper_mode > 2 )	upper_mode = 0;
			}
		}

        //--------------------------------------------------------------------------
        // 表示部分
        //--------------------------------------------------------------------------

		if( lcd_mode == 0 )
		{
			if( upper_mode == 0 )
			{
				// 開いて読み込んだ画像を上画面に表示 400モード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
			}
			else if( upper_mode == 1 )
			{
				// 800モード 2回書かないと更新されない？
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);

				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
			}
			else
			{
				// 3Dモード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_STEREO);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0_EXT );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop2,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();
				gfx->m_DrawFramework->SwapBuffers_ParallaxBarrier();
			}
			// 下画面に表示窓作成
			gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
			gfx->m_DrawFramework->Clear();
			if( loop_once )
			{
				m_WindowManager.CreateWindow( &m_LowerWindow, NN_GX_DISPLAY1, 0, 0 );
				loop_once = false;
			}
			m_LowerWindow.SetTitle( "HOW TO USE" );
			m_LowerWindow.Gotoxy(0,0);
			m_LowerWindow.Printf(" LEFT:PREV\n");
			m_LowerWindow.Printf("RIGHT:NEXT\n");
			m_LowerWindow.Printf("    B:Back to List\n");
			m_LowerWindow.Printf("    A:Change Top LCD Mode\n");
			m_LowerWindow.Printf("    L:Active Backlight ON\n");
			m_LowerWindow.Printf("    R:Active Backlight OFF\n");
			m_LowerWindow.Printf("   UP:Increase Brightness Level\n");
			m_LowerWindow.Printf(" DOWN:Decrease Brightness Level\n");
			m_LowerWindow.Printf("START:Change Display Mode\n\n");
			m_LowerWindow.Printf("Brightness Level : %d\n", light_level );
			if( abl_flag )	m_LowerWindow.Printf("Active Backlight : ON\n");
			else			m_LowerWindow.Printf("Active Backlight : OFF\n");
			if( upper_mode == 0 )		m_LowerWindow.Printf("    Top LCD Mode : 2D(400LINE)\n");
			else if( upper_mode == 1 )	m_LowerWindow.Printf("    Top LCD Mode : 2D(800LINE)\n");
			else						m_LowerWindow.Printf("    Top LCD Mode : 3D\n");
			m_WindowManager.Update();
			m_WindowManager.DrawDisplay1();	
			gfx->m_DrawFramework->SwapBuffers();	    
			gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
		}
		else if( lcd_mode == 1 )
		{
			// 開いて読み込んだ画像を下画面に表示
			gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
			gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
			gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
			gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
														0.0f, 0.0f, 320.0f, 240.0f, 
														320.0f, 240.0f,
														s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
			gfx->m_DrawFramework->SwapBuffers();

			// 上画面に表示窓作成
			gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
			gfx->m_DrawFramework->Clear();
			if( loop_once )
			{
				m_WindowManager.CreateWindow( &m_UpperWindow, NN_GX_DISPLAY0, 0, 0 );
				loop_once = false;
			}
			m_UpperWindow.SetTitle( "HOW TO USE" );
			m_UpperWindow.Gotoxy(0,0);
			m_UpperWindow.Printf(" LEFT:PREV\n");
			m_UpperWindow.Printf("RIGHT:NEXT\n");
			m_UpperWindow.Printf("    B:Back to List\n");
			m_UpperWindow.Printf("    L:Active Backlight ON\n");
			m_UpperWindow.Printf("    R:Active Backlight OFF\n");
			m_UpperWindow.Printf("   UP:Increase Brightness Level\n");
			m_UpperWindow.Printf(" DOWN:Decrease Brightness Level\n");
			m_UpperWindow.Printf("START:Change Display Mode\n\n");
			m_UpperWindow.Printf("Brightness Level : %d\n", light_level );
			if( abl_flag )	m_UpperWindow.Printf("Active Backlight : ON\n");
			else			m_UpperWindow.Printf("Active Backlight : OFF\n");
			m_WindowManager.Update();
			m_WindowManager.DrawDisplay0();	
			gfx->m_DrawFramework->SwapBuffers();	    
			gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
		}
		else if( lcd_mode == 2 )
		{
			if( upper_mode == 0 )
			{
				// 開いて読み込んだ画像を上画面に表示 400モード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
				gfx->m_DrawFramework->Clear();
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->m_DrawFramework->Clear();
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
			}
			else if( upper_mode == 1 )
			{
				// 800モード 2回書かないと更新されない？
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);

				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_RESERVED);
				gfx->m_DrawFramework->SetRenderTarget( demo::RenderSystemUji::NN_GX_DISPLAY0_HD );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH*2, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 800.0f, 240.0f,
															800.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->SwapBuffers();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
			}
			else
			{
				// 3Dモード
				gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_STEREO);
				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0 );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop->GetTextureWidth(), s_BmpTop->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY0_EXT );
	            gfx->SetScreenSize( GraphicsDrawing::DISPLAY0_WIDTH, GraphicsDrawing::DISPLAY0_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdTop2,
															0.0f, 0.0f, 400.0f, 240.0f,
															400.0f, 240.0f,
															s_BmpTop2->GetTextureWidth(), s_BmpTop2->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SetRenderTarget( NN_GX_DISPLAY1 );
				gfx->SetScreenSize( GraphicsDrawing::DISPLAY1_WIDTH, GraphicsDrawing::DISPLAY1_HEIGHT );
				gfx->m_DrawFramework->FillTexturedRectangle(s_TextureIdBottom,
															0.0f, 0.0f, 320.0f, 240.0f, 
															320.0f, 240.0f,
															s_BmpBottom->GetTextureWidth(), s_BmpBottom->GetTextureHeight() );
				gfx->m_DrawFramework->Transfer();

				gfx->m_DrawFramework->SwapBuffers_ParallaxBarrier();
				gfx->m_DrawFramework->WaitVsync(NN_GX_DISPLAY_BOTH);
			}
		}
	// Bボタンでファイルリストに戻る
    }while( !Pad().IsButtonRepeat( Pad::BUTTON_B ) );
	// 画像消去
	if( s_TextureIdTop != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop );
	if( s_TextureIdTop2 != 0 )gfx->m_DrawFramework->DeleteTexture( s_TextureIdTop2 );
	if( flag_BmpTop )
	{
		s_BmpTop -> Close();
		delete s_BmpTop;
		s_BmpTop2 -> Close();
		delete s_BmpTop2;
		NN_LOG("BmpTop*2_deleted\n");
		flag_BmpTop = false;
	}
	if( s_TextureIdBottom  != 0 )gfx->m_DrawFramework->DeleteTexture(s_TextureIdBottom );
	if( flag_BmpBottom )
	{
		s_BmpBottom  -> Close();
		delete s_BmpBottom;
		NN_LOG("BmpBottom_deleted\n");
		flag_BmpBottom = false;
	}
	gfx->m_DrawFramework->SetLcdModeUji(NN_GX_DISPLAYMODE_NORMAL);
	// バックライト輝度最大
	nn::gx::CTR::Backlight::SetLuminanceLevel(nn::gx::CTR::LCD_BOTH, 5);
	// アクティブバックライトOFF
	nn::gx::CTR::Backlight::DisableActiveBl(nn::gx::CTR::LCD_BOTH);
}
