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