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