1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     extrapad_demo.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev:$
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn.h>
17 #include <nn/extrapad.h>
18 
19 #include "demo.h"
20 
21 //----------------------------------------------------------------
22 
23 namespace
24 {
25     demo::RenderSystemDrawing df;
26 
27     s32 loopCnt = 0;
28 
SetupRenderSystem()29     void SetupRenderSystem()
30     {
31         demo::RenderBufferDescription colorBufferDesc;
32         {
33             colorBufferDesc.m_Format     = GL_RGBA8_OES;
34             colorBufferDesc.m_Width      = nn::gx::DISPLAY0_WIDTH;
35             colorBufferDesc.m_Height     = nn::gx::DISPLAY0_HEIGHT;
36             colorBufferDesc.m_Area       = NN_GX_MEM_VRAMA;
37             colorBufferDesc.m_Attachment = GL_COLOR_ATTACHMENT0;
38         }
39         demo::RenderBufferDescription depthStencilBufferDesc;
40         {
41             depthStencilBufferDesc.m_Format     = GL_DEPTH24_STENCIL8_EXT;
42             depthStencilBufferDesc.m_Width      = nn::gx::DISPLAY0_WIDTH;
43             depthStencilBufferDesc.m_Height     = nn::gx::DISPLAY0_HEIGHT;
44             depthStencilBufferDesc.m_Area       = NN_GX_MEM_VRAMB;
45             depthStencilBufferDesc.m_Attachment = GL_DEPTH_STENCIL_ATTACHMENT;
46         }
47 
48         demo::FrameBufferDescription frameBufferDesc;
49         {
50             frameBufferDesc.m_RenderBufferDescriptionArray[demo::DEMO_COLOR_BUFFER_INDEX] = colorBufferDesc;
51             frameBufferDesc.m_RenderBufferDescriptionArray[demo::DEMO_DEPTH_STENCIL_BUFFER_INDEX] = depthStencilBufferDesc;
52         }
53 
54         demo::DisplayBuffersDescription display0BuffersDesc;
55         {
56             display0BuffersDesc.m_TargetDisplay    = NN_GX_DISPLAY0;
57             display0BuffersDesc.m_DisplayBufferNum = demo::DEMO_DISPLAY_BUFFER_NUM;
58             display0BuffersDesc.m_Format           = GL_RGB8_OES;
59             display0BuffersDesc.m_Width            = nn::gx::DISPLAY0_WIDTH;
60             display0BuffersDesc.m_Height           = nn::gx::DISPLAY0_HEIGHT;
61             display0BuffersDesc.m_Area             = NN_GX_MEM_FCRAM;
62 //            display0BuffersDesc.m_Area             = NN_GX_MEM_VRAMA;
63         }
64 
65         demo::DisplayBuffersDescription display1BuffersDesc;
66         {
67             display1BuffersDesc.m_TargetDisplay    = NN_GX_DISPLAY1;
68             display1BuffersDesc.m_DisplayBufferNum = demo::DEMO_DISPLAY_BUFFER_NUM;
69             display1BuffersDesc.m_Format           = GL_RGB8_OES;
70             display1BuffersDesc.m_Width            = nn::gx::DISPLAY1_WIDTH;
71             display1BuffersDesc.m_Height           = nn::gx::DISPLAY1_HEIGHT;
72             display1BuffersDesc.m_Area             = NN_GX_MEM_FCRAM;
73 //            display1BuffersDesc.m_Area             = NN_GX_MEM_VRAMB;
74         }
75 
76         df.Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(),
77                                   0x40000, 128, true,
78                                   display0BuffersDesc, display1BuffersDesc,
79                                   frameBufferDesc
80                                   );
81     }
82 
ReleaseRenderSystem()83     void ReleaseRenderSystem()
84     {
85         df.Finalize();
86     }
87 
DrawText()88     void DrawText()
89     {
90         // display 0
91         df.SetRenderTarget(NN_GX_DISPLAY0);
92         df.Clear();
93         {
94             df.SetColor(0.0f, 0.0f, 0.5f);
95             df.FillRectangle( 0, 0, 399, 255 );
96             df.SetColor(0.5f, 0.4f, 0.0f);
97             df.FillRectangle( 0, 0, 399, 40 );
98             df.SetColor(0.5f, 0.0f, 0.4f);
99             df.FillRectangle( 0, 180, 399, 40 );
100             df.SetColor(1.0f, 1.0f, 1.0f);
101             df.DrawText( 100, 50, "** DEMO APPLICATION **" );
102 
103             df.SetColor(1.0f, 1.0f, 1.0f);
104             df.DrawText( 30,  80, "[A]   extra pad applet." );
105 
106             // Count
107             df.DrawText( 340, 50, "%d", loopCnt );
108             df.DrawText( 340, 82, "%d", nngxCheckVSync(NN_GX_DISPLAY0) );
109         }
110         df.SwapBuffers();
111 
112         // display 1
113         df.SetRenderTarget(NN_GX_DISPLAY1);
114         df.Clear();
115         {
116             df.SetColor(0.0f, 0.5f, 0.0f);
117             df.FillRectangle( 0, 0, 399, 255 );
118             df.SetColor(1.0f, 1.0f, 1.0f);
119             df.DrawText( 80, 50, "** DEMO APPLICATION **" );
120         }
121         df.SwapBuffers();
122     }
DrawDisplay()123     void DrawDisplay()
124     {
125         DrawText();
126     }
RestoreGraphics()127     void RestoreGraphics()
128     {
129         // Recover the GPU register settings
130         nngxUpdateState(NN_GX_STATE_ALL);
131         nngxValidateState(NN_GX_STATE_ALL,GL_TRUE);
132     }
133 }
134 
135 //----------------------------------------------------------------
myReceiveSleepQueryNotification(uptr arg)136 AppletQueryReply myReceiveSleepQueryNotification( uptr arg )
137 {
138     NN_UNUSED_VAR( arg );
139     NN_LOG("*** Application sleepQuery \n");
140     return /*applet::IsActive()? applet::REPLY_LATER:*/ nn::applet::REPLY_ACCEPT;
141 }
142 
143 //----------------------------------------------------------------
myReceiveAwakeNotification(uptr arg)144 void myReceiveAwakeNotification( uptr arg )
145 {
146     NN_UNUSED_VAR( arg );
147     NN_LOG("*** Application awake \n");
148     if ( nn::applet::IsActive() )
149     {
150         nngxStartLcdDisplay();
151     }
152 }
153 
154 //----------------------------------------------------------------
nnMain()155 extern "C" void nnMain()
156 {
157     NN_TLOG_("----Application start\n");
158 
159     nn::Result result;
160 
161     // applet declarations
162     nn::applet::SetSleepQueryCallback( myReceiveSleepQueryNotification, 0 );
163     nn::applet::SetAwakeCallback( myReceiveAwakeNotification, 0 );
164     nn::applet::Enable();
165     if(nn::applet::IsExpectedToCloseApplication())
166     {
167         // Quickly end the application
168         nn::applet::PrepareToCloseApplication();
169         nn::applet::CloseApplication();
170 
171         // Never reached
172         return;
173 
174     }
175 
176     // hid
177     result = nn::hid::Initialize();
178     NN_UTIL_PANIC_IF_FAILED(result);
179 
180     // fs
181     nn::fs::Initialize();
182 
183     // Render class initialization
184     SetupRenderSystem();
185 
186     nn::hid::PadReader padReader;
187     nn::hid::PadStatus ps;
188 
189     while(1)
190     {
191         loopCnt++;
192 
193         padReader.ReadLatest( &ps );
194 
195         // Render
196         DrawDisplay();
197 
198         // Start library applet with A Button
199         if ( ps.trigger & nn::hid::BUTTON_A )
200         {
201             nn::applet::WakeupState wstate;
202 
203             // ExtraPad settings structure
204             nn::extrapad::Parameter expad_param;
205 
206             // Initialize settings struct
207             nn::extrapad::InitializeConfig( &expad_param.config );
208 
209             nn::extrapad::StartExtraPadApplet( &wstate, &expad_param );
210             if(nn::applet::IsExpectedToCloseApplication())
211             {
212                 break;
213             }
214 
215             // Recover the GPU register settings
216             RestoreGraphics();
217             NN_LOG( "Return Code : %d\n", expad_param.config.returnCode );
218         }
219 
220         // Process HOME Button
221         if ( nn::applet::IsExpectedToProcessHomeButton() )
222         {
223             NN_LOG("HomeBtn.\n");
224             nn::applet::ProcessHomeButtonAndWait();
225 
226             if(nn::applet::IsExpectedToCloseApplication())
227             {
228                 break;
229             }
230 
231             RestoreGraphics();
232 
233         }
234 
235         // POWER button process
236         if ( nn::applet::IsExpectedToProcessPowerButton() )
237         {
238             NN_LOG("PowerBtn.\n");
239             nn::applet::ProcessPowerButtonAndWait();
240 
241             if(nn::applet::IsExpectedToCloseApplication())
242             {
243                 break;
244             }
245 
246             RestoreGraphics();
247         }
248 
249     }
250 
251 
252     NN_TLOG_("----Application is to be end\n");
253 
254     // Free the render class
255     ReleaseRenderSystem();
256 
257     // Finalize hid
258     nn::hid::Finalize();
259 
260     // Exit application
261     nn::applet::PrepareToCloseApplication();
262     nn::applet::CloseApplication();
263 
264     // No need to call Finalize on applet
265     // Execution will probably not reach here.
266     NN_TLOG_("----Application end\n");
267 }
268