1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     main.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: 23867 $
14  *---------------------------------------------------------------------------*/
15 
16 //------------------------------------------------------------------
17 // デモ: simple2
18 //
19 // 概要
20 //   プログラムでピクチャペインを追加するデモです。
21 //
22 //------------------------------------------------------------------
23 
24 #include <nw/lyt.h>
25 #include <nw/demo.h>
26 #include <new>
27 
28 namespace
29 {
30 
31 typedef nw::ut::MoveArray<u8> File;
32 
33 /*---------------------------------------------------------------------------*
34   @brief 描画の初期設定を行います。
35 
36   @param width  画面の幅。
37   @param height  画面の高さ。
38  *---------------------------------------------------------------------------*/
39 void
InitDraw(int width,int height)40 InitDraw(int width, int height)
41 {
42     // カラーバッファ情報
43     // LCDの向きに合わせて、幅と高さを入れ替えています。
44     const nw::font::ColorBufferInfo colBufInfo =
45     {
46         height, width, PICA_DATA_DEPTH24_STENCIL8_EXT
47     };
48 
49     const u32 commands[] =
50     {
51         // ビューポートの設定
52         NW_FONT_CMD_SET_VIEWPORT(0, 0, colBufInfo.width, colBufInfo.height),
53 
54         // シザー処理を無効
55         NW_FONT_CMD_SET_DISABLE_SCISSOR(colBufInfo),
56 
57         // wバッファの無効化
58         // デプスレンジの設定
59         // ポリゴンオフセットの無効化
60         NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET(
61             0.0f,           // wScale : 0.0 でWバッファが無効
62             0.0f,           // depth range near
63             1.0f,           // depth range far
64             0,              // polygon offset units : 0.0 で ポリゴンオフセットが無効
65             colBufInfo),
66     };
67 
68     nngxAdd3DCommand(commands, sizeof(commands), true);
69 
70     static const u32 constCommands[] =
71     {
72         // カリングを無効
73         NW_FONT_CMD_SET_CULL_FACE(NW_FONT_CMD_CULL_FACE_DISABLE),
74 
75         // ステンシルテストを無効
76         NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(),
77 
78         // デプステストを無効
79         // カラーバッファの全ての成分を書き込み可
80         NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK(
81             false,  // isDepthTestEnabled
82             0,      // depthFunc
83             true,   // depthMask
84             true,   // red
85             true,   // green
86             true,   // blue
87             true),  // alpha
88 
89         // アーリーデプステストを無効
90         NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST(false),
91 
92         // フレームバッファアクセス制御
93         NW_FONT_CMD_SET_FBACCESS(
94             true,   // colorRead
95             true,   // colorWrite
96             false,  // depthRead
97             false,  // depthWrite
98             false,  // stencilRead
99             false), // stencilWrite
100     };
101 
102     nngxAdd3DCommand(constCommands, sizeof(constCommands), true);
103 }
104 
105 
106 /*---------------------------------------------------------------------------*
107   @brief モデルビュー行列と射影行列を設定します。
108 
109   @param drawInfo 描画情報です。
110   @param layout レイアウトです。
111  *---------------------------------------------------------------------------*/
112 void
SetupCamera(nw::lyt::DrawInfo & drawInfo,const nw::lyt::Layout & layout)113 SetupCamera(nw::lyt::DrawInfo& drawInfo, const nw::lyt::Layout& layout)
114 {
115     nw::ut::Rect layoutRect = layout.GetLayoutRect();
116 
117     f32 znear = 0.f;
118     f32 zfar = 500.f;
119 
120     // 射影行列を正射影に設定
121     // (Layoutデータは横向きなので向きを変換する)
122     nw::math::MTX44 projMtx;
123     nw::math::MTX44OrthoPivot(
124         &projMtx,
125         layoutRect.left,   // left
126         layoutRect.right,  // right
127         layoutRect.bottom, // bottom
128         layoutRect.top,    // top
129         znear,
130         zfar,
131         nw::math::PIVOT_UPSIDE_TO_TOP);
132     drawInfo.SetProjectionMtx(projMtx);
133 
134     // モデルビュー行列を設定
135     // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向)
136     nw::math::VEC3 pos(0, 0, 1);
137     nw::math::VEC3 up(0, 1, 0);
138     nw::math::VEC3 target(0, 0, 0);
139 
140     nw::math::MTX34 viewMtx;
141     nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target);
142     drawInfo.SetViewMtx(viewMtx);
143 }
144 
145 }   // namespace
146 
147 /*---------------------------------------------------------------------------*
148   @brief サンプルのメイン関数です。
149  *---------------------------------------------------------------------------*/
150 void
nnMain()151 nnMain()
152 {
153     nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance();
154     demoApp.Initialize();
155 
156     nw::lyt::Initialize(&demoApp.GetAllocator(), &demoApp.GetDeviceAllocator());
157 
158     // レイアウトのバイナリリソース(アーカイブ)を読み込み。
159     File fileLayout = nw::demo::Utility::LoadFile(
160         &demoApp.GetDeviceAllocator(), NW_DEMO_FILE_PATH(L"layout.arc"), 128);
161 
162     if (fileLayout.empty())
163     {
164         NW_FATAL_ERROR("can not open layout archive.\n");
165     }
166 
167     // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成。
168     nw::lyt::ArcResourceAccessor* pResAccessor = new nw::lyt::ArcResourceAccessor;
169     if (!pResAccessor->Attach(fileLayout.begin(), "."))
170     {
171         NW_FATAL_ERROR("can not attach layout archive.\n");
172     }
173 
174     nw::lyt::Layout* pLayout = new nw::lyt::Layout();
175 
176     // レイアウトリソースの読み込み
177     {
178         const void* lytRes = pResAccessor->GetResource(0, "simple2.bclyt");
179         NW_NULL_ASSERT(lytRes);
180         pLayout->Build(lytRes, pResAccessor);
181     }
182 
183     /*
184         レイアウトリソースには3つのピクチャペインを持っています。
185         この後、プログラムで3つのピクチャペインを追加します。
186      */
187 
188     // テクスチャイメージリソースからPictureペインを作成
189     u32 size = 0;
190     if (void* pImg = pResAccessor->GetResource('timg', "mark_1.bclim", &size))
191     {
192         if (void* pMemPicture = nw::lyt::Layout::AllocMemory(sizeof(nw::lyt::Picture)))
193         {
194             // イメージをOpenGLに読み込み。
195             nw::lyt::TextureInfo textureInfo = nw::lyt::LoadTexture(pImg, size);
196 
197             // Picture ペインを生成。
198             nw::lyt::Picture* pPic = new (pMemPicture) nw::lyt::Picture(textureInfo);
199             pPic->SetTranslate(nw::math::VEC2(-128, -64));
200             pLayout->GetRootPane()->AppendChild(pPic);
201 
202             // 廃棄のために登録します。
203             pResAccessor->RegistTexture("mark_1.bclim", textureInfo);
204         }
205     }
206 
207     // テクスチャイメージリソースからPictureペインを作成
208     if (void* pImg = pResAccessor->GetResource('timg', "mark_2.bclim", &size))
209     {
210         if (void* pMemPicture = nw::lyt::Layout::AllocMemory(sizeof(nw::lyt::Picture)))
211         {
212             // イメージをOpenGLに読み込み。
213             nw::lyt::TextureInfo textureInfo = nw::lyt::LoadTexture(pImg, size);
214 
215             // Picture ペインを生成。
216             nw::lyt::Picture *pPic = new (pMemPicture) nw::lyt::Picture(textureInfo);
217             pPic->SetTranslate(nw::math::VEC2(0, -64));
218             pLayout->GetRootPane()->AppendChild(pPic);
219 
220             // 廃棄のために登録します。
221             pResAccessor->RegistTexture("mark_2.bclim", textureInfo);
222         }
223     }
224 
225     // コンストラクタでメモリのみ確保し、後でテクスチャを登録
226     if (void* pImg = pResAccessor->GetResource('timg', "mark_3.bclim", &size))
227     {
228         if (void* pMemPicture = nw::lyt::Layout::AllocMemory(sizeof(nw::lyt::Picture)))
229         {
230             // イメージをOpenGLに読み込み。
231             nw::lyt::TextureInfo textureInfo = nw::lyt::LoadTexture(pImg, size);
232 
233             // 1枚分のテクスチャ用メモリを確保
234             nw::lyt::Picture *pPic = new (pMemPicture) nw::lyt::Picture(1);
235 
236             pPic->Append(textureInfo);   // テクスチャの登録
237             pPic->SetTranslate(nw::math::VEC2(128, -64));
238             pLayout->GetRootPane()->AppendChild(pPic);
239 
240             // 廃棄のために登録します。
241             pResAccessor->RegistTexture("mark_3.bclim", textureInfo);
242         }
243     }
244 
245     nw::lyt::GraphicsResource graphicsResource;
246     // グローバルなリソースファイルを読み込みます。
247     {
248         graphicsResource.StartSetup();
249         const wchar_t* resourcePath = 0;
250         for (int i = 0;
251              (resourcePath = graphicsResource.GetResourcePath(i)) != NULL;
252              ++i)
253         {
254             File file = nw::demo::Utility::LoadFile(&demoApp.GetAllocator(), resourcePath);
255 
256             if (file.empty())
257             {
258                 NW_FATAL_ERROR("can not read lyt resource file.");
259             }
260 
261             graphicsResource.SetResource(i, file.begin(), file.size(), false);
262         }
263 
264         graphicsResource.FinishSetup();
265     }
266 
267     nw::lyt::DrawInfo drawInfo;
268     drawInfo.SetGraphicsResource(&graphicsResource);
269 #if ! defined(NW_RELEASE)
270     drawInfo.SetDebugDrawMode(true);
271 #endif
272 
273     nw::lyt::Drawer drawer;
274     drawer.Initialize(graphicsResource);
275 
276     const nw::ut::FloatColor clearColor(0.3f, 0.3f, 0.3f, 1.0f);
277     const f32 clearDepth = 0.0f;
278 
279     bool loop = true;
280     while (loop)
281     {
282         demoApp.SetRenderingTarget(demoApp.DISPLAY0);
283         {
284             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
285 
286             InitDraw(demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT);
287 
288             SetupCamera(drawInfo, *pLayout);
289 
290             pLayout->CalculateMtx(drawInfo);
291 
292             drawer.DrawBegin(drawInfo);
293             drawer.Draw(pLayout, drawInfo);
294             drawer.DrawEnd(drawInfo);
295         }
296 
297         demoApp.SetRenderingTarget(demoApp.DISPLAY1);
298         {
299             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
300         }
301 
302         demoApp.SwapBuffer(demoApp.DISPLAY_BOTH);
303     }
304 
305     delete pLayout;
306 
307     delete pResAccessor;
308 }
309 
310