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