1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     SimpleApp.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision:$
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include "SimpleApp.h"
19 #include "simple.csid"
20 
21 namespace
22 {
23 
24 const s32 SOUND_THREAD_PRIORITY = 4;
25 const s32 LOAD_THREAD_PRIORITY = 3;
26 const s32 SOUND_HEAP_SIZE = 1 * 1024 * 1024;
27 const char SOUND_ARC_PATH[] = NW_SND_DEMO_PATH_PREFIX "simple.bcsar";
28 const char DEMO_TITLE[] = "Simple";
29 
30 } // anonymous namespace
31 
OnInitialize()32 void SimpleApp::OnInitialize()
33 {
34     InitializeSoundSystem();
35 
36     // サウンドデータのロード
37     if ( ! m_DataManager.LoadData( SEQ_MARIOKART, &m_Heap ) )
38     {
39         NW_ASSERTMSG( false, "LoadData(SEQ_MARIOKART) failed." );
40     }
41     if ( ! m_DataManager.LoadData( SE_YOSHI, &m_Heap ) )
42     {
43         NW_ASSERTMSG( false, "LoadData(SE_YOSHI) failed." );
44     }
45 }
46 
InitializeSoundSystem()47 void SimpleApp::InitializeSoundSystem()
48 {
49     // サウンドシステムの初期化
50     {
51         nw::snd::SoundSystem::SoundSystemParam param;
52         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
53         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
54 
55         nw::snd::SoundSystem::Initialize(
56                 param,
57                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
58                 workMemSize );
59     }
60 
61     // サウンドアーカイブの初期化
62     if ( ! m_Archive.Open( SOUND_ARC_PATH ) )
63     {
64         NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", SOUND_ARC_PATH );
65     }
66 
67     // INFO ブロックのロード
68     {
69         size_t infoBlockSize = m_Archive.GetHeaderSize();
70         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
71         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
72         {
73             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH );
74         }
75     }
76 
77     // サウンドデータマネージャーの初期化
78     {
79         size_t setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
80         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
81         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
82     }
83 
84     // サウンドアーカイブプレイヤーの初期化
85     {
86         size_t setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
87         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
88         size_t setupStrmBufferSize =
89             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
90         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
91         bool result = m_ArchivePlayer.Initialize(
92                 &m_Archive,
93                 &m_DataManager,
94                 m_pMemoryForSoundArchivePlayer, setupSize,
95                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
96         NW_ASSERT( result );
97     }
98 
99     // サウンドヒープの構築
100     {
101         m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE );
102         bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE );
103         NW_ASSERT( result );
104     }
105 }
106 
OnFinalize()107 void SimpleApp::OnFinalize()
108 {
109     nw::snd::SoundSystem::Finalize();
110 
111     MemFree( m_pMemoryForSoundSystem );
112     MemFree( m_pMemoryForInfoBlock );
113     MemFree( m_pMemoryForSoundDataManager );
114     MemFree( m_pMemoryForSoundArchivePlayer );
115     MemFree( m_pMemoryForSoundHeap );
116     MemFree( m_pMemoryForStreamBuffer );
117 }
118 
OnDrawUpLCD(nw::font::TextWriter & writer)119 void SimpleApp::OnDrawUpLCD( nw::font::TextWriter& writer )
120 {
121     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE);
122 
123     writer.Print ("    -- usage --\n\n");
124     writer.Print ("    [A] Play Sequence Sound\n");
125     writer.Print ("    [X] Play Wave Sound\n");
126     writer.Print ("    [Y] Play Stream Sound\n");
127     writer.Print ("    [B] Stop Sound\n\n");
128 }
129 
OnDrawDownLCD(nw::font::TextWriter & writer)130 void SimpleApp::OnDrawDownLCD( nw::font::TextWriter& writer )
131 {
132     (void)writer;
133 }
134 
OnUpdatePad(nw::demo::Pad & pad)135 void SimpleApp::OnUpdatePad( nw::demo::Pad& pad )
136 {
137     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_A ) )
138     {
139         m_Handle.Stop( 0 );
140         bool result = m_ArchivePlayer.StartSound( &m_Handle, SEQ_MARIOKART ).IsSuccess();
141         NN_LOG("[SEQ] SEQ_MARIOKART ... (%d)\n", result);
142     }
143 
144     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_X ) )
145     {
146         m_Handle.Stop( 0 );
147         bool result = m_ArchivePlayer.StartSound( &m_Handle, SE_YOSHI ).IsSuccess();
148         NN_LOG("[WSD] SE_YOSHI ... (%d)\n", result);
149     }
150 
151     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_Y ) )
152     {
153         m_Handle.Stop( 0 );
154         bool result = m_ArchivePlayer.StartSound( &m_Handle, STRM_MARIOKART ).IsSuccess();
155         NN_LOG("[STRM] STRM_MARIOKART ... (%d)\n", result );
156     }
157 
158     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) )
159     {
160         m_Handle.Stop( 3 );
161     }
162 }
163 
OnUpdate()164 void SimpleApp::OnUpdate()
165 {
166     m_ArchivePlayer.Update();
167 }
168