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