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