1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_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: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nw/types.h>
19 #include <nw/demo.h>
20 
21 namespace nw
22 {
23 namespace demo
24 {
25 
26 SimpleApp* SimpleApp::s_pInstance = NULL;
27 
28 //----------------------------------------
29 void
Initialize()30 SimpleApp::Initialize()
31 {
32     m_CurrentDisplay = DISPLAY_MAX;
33 
34     nw::demo::InitializeGraphicsSystem(&m_DeviceAllocator);
35 
36     nw::demo::PadFactory::Initialize(&m_DeviceAllocator);
37 
38     nw::demo::RenderSystem::Description renderDesc;
39 
40     nw::demo::DisplayBufferSwapper::Description& upperScreenDesc =
41         renderDesc.upperScreenDescription;
42 
43     nw::demo::DisplayBufferSwapper::Description& lowerScreenDesc =
44         renderDesc.lowerScreenDescription;
45 
46     m_pRenderSystem = nw::demo::RenderSystem::Create(
47         &m_DeviceAllocator, renderDesc);
48 
49     NW_NULL_ASSERT(m_pRenderSystem);
50 
51     m_pRenderTargetUpper = nw::gfx::IRenderTarget::Builder()
52         .BufferSize(upperScreenDesc.height, upperScreenDesc.width)
53         .ColorFormat(renderDesc.renderColorFormat)
54         .Create(&m_DeviceAllocator);
55 
56     NW_NULL_ASSERT(m_pRenderTargetUpper);
57 
58     m_pRenderTargetLower = nw::gfx::IRenderTarget::Builder()
59         .BufferSize(lowerScreenDesc.height, lowerScreenDesc.width)
60         .ColorFormat(renderDesc.renderColorFormat)
61         .Create(&m_DeviceAllocator);
62 
63     NW_NULL_ASSERT(m_pRenderTargetLower);
64 
65     NW_GL_ASSERT();
66 }
67 
68 //----------------------------------------
69 void
Finalize()70 SimpleApp::Finalize()
71 {
72     nw::demo::PadFactory::Finalize();
73     nw::gfx::SafeDestroy(m_pRenderSystem);
74     nw::gfx::SafeDestroy(m_pRenderTargetUpper);
75     nw::gfx::SafeDestroy(m_pRenderTargetLower);
76 }
77 
78 //----------------------------------------
79 void
TransferBuffer()80 SimpleApp::TransferBuffer()
81 {
82     switch (m_CurrentDisplay)
83     {
84     case DISPLAY0:
85         m_pRenderSystem->TransferBuffer(nw::demo::UPPER_SCREEN);
86         break;
87 
88     case DISPLAY1:
89         m_pRenderSystem->TransferBuffer(nw::demo::LOWER_SCREEN);
90         break;
91 
92     default:
93         break;
94     }
95 }
96 
97 //----------------------------------------
98 void
SetRenderingTarget(Display display)99 SimpleApp::SetRenderingTarget(Display display)
100 {
101     NW_ASSERT(display == DISPLAY0 || display == DISPLAY1);
102 
103     this->TransferBuffer();
104 
105     switch (display)
106     {
107     case DISPLAY0:
108         m_pRenderSystem->SetRenderTarget(m_pRenderTargetUpper);
109         m_CurrentDisplay = DISPLAY0;
110         break;
111 
112     case DISPLAY1:
113         m_pRenderSystem->SetRenderTarget(m_pRenderTargetLower);
114         m_CurrentDisplay = DISPLAY1;
115         break;
116 
117     default:
118         break;
119     }
120 }
121 
122 //----------------------------------------
123 const gfx::FrameBufferObject&
GetFrameBufferObject() const124 SimpleApp::GetFrameBufferObject() const
125 {
126     return m_pRenderSystem->GetRenderContext()->GetRenderTarget()->GetBufferObject();
127 }
128 
129 //----------------------------------------
130 void
SwapBuffer(Display display)131 SimpleApp::SwapBuffer(Display display)
132 {
133     this->TransferBuffer();
134     m_CurrentDisplay = DISPLAY_MAX;
135 
136     s32 screenKind = 0;
137     switch (display)
138     {
139     case DISPLAY0:
140         screenKind = nw::demo::UPPER_SCREEN;
141         break;
142 
143     case DISPLAY1:
144         screenKind = nw::demo::LOWER_SCREEN;
145         break;
146 
147     case DISPLAY_BOTH:
148         screenKind = nw::demo::UPPER_SCREEN | nw::demo::LOWER_SCREEN;
149         break;
150 
151     default:
152         return;
153     }
154 
155     m_pRenderSystem->PresentBuffer(screenKind);
156     NW_GL_ASSERT();
157 }
158 
159 //----------------------------------------
160 void*
Allocate(size_t size,u8 alignment)161 SimpleApp::Allocate(size_t size, u8 alignment)
162 {
163     return nw::demo::Alloc(size, alignment);
164 }
165 
166 //----------------------------------------
167 void*
AllocateDeviceMemory(size_t size,u8 alignment)168 SimpleApp::AllocateDeviceMemory(size_t size, u8 alignment)
169 {
170     return nw::demo::Alloc(size, alignment);
171 }
172 
173 //----------------------------------------
174 void
Free(void * ptr)175 SimpleApp::Free(void* ptr)
176 {
177     nw::demo::Free(ptr);
178 }
179 
180 } /* namespace demo */
181 } /* namespace nw   */
182 
183