1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_GraphicsDrawing.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: 24774 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nw/os.h>
17 #include <nw/demo.h>
18 #include <nw/demo/demo_GraphicsDrawing.h>
19 #include <nw/gfx/gfx_Common.h>
20 #include <GLES2/gl2.h>
21 #include <GLES2/gl2ext.h>
22 
23 namespace nw {
24 namespace demo {
25 
26 enum
27 {
28     VTXATTR_POSITION  = 0,
29     VTXATTR_COLOR,
30     VTXATTR_TEXCOORD0
31 };
32 
33 //--------------------------------------------------------------------------
34 /* ctor */
GraphicsDrawing()35 GraphicsDrawing::GraphicsDrawing()
36  : m_ScreenSize( f32(DEFAULT_SCREEN_WIDTH), f32(DEFAULT_SCREEN_HEIGHT) ),
37    m_LineWidth( 2.0f ),
38    m_ProgramID( 0 ),
39    m_ShaderID( 0 ),
40    m_pShaderBinary( NULL ),
41    m_MaxTextCount( DEFAULT_MAX_TEXT_COUNT ),
42    m_FontCommandBuffer( NULL ),
43    m_FontAllocator( NULL ),
44    m_ShapeAllocator( NULL )
45 {
46 }
47 
48 
49 //--------------------------------------------------------------------------
~GraphicsDrawing()50 GraphicsDrawing::~GraphicsDrawing()
51 {
52 }
53 
54 //--------------------------------------------------------------------------
55 void
Finalize()56 GraphicsDrawing::Finalize()
57 {
58     this->DestroyFont();
59 
60     if ( m_pShaderBinary && m_ShapeAllocator )
61     {
62         os::SafeFree(m_pShaderBinary, m_ShapeAllocator);
63     }
64 
65     m_FontAllocator = NULL;
66     m_ShapeAllocator = NULL;
67 }
68 
69 //--------------------------------------------------------------------------
70 bool
InitializeShader(void * pShaderBinary,size_t binarySize)71 GraphicsDrawing::InitializeShader(void* pShaderBinary, size_t binarySize)
72 {
73     bool result = true;
74     const void* prev = m_pShaderBinary;
75     m_pShaderBinary = pShaderBinary;
76 
77     NW_GL_ASSERT();
78 
79     if ( m_ProgramID == 0 )
80     {
81         m_ProgramID = glCreateProgram();
82     }
83 
84     if ( m_ShaderID == 0 )
85     {
86         m_ShaderID = glCreateShader( GL_VERTEX_SHADER );
87     }
88 
89     glShaderBinary(1, &m_ShaderID, GL_PLATFORM_BINARY_DMP, pShaderBinary, binarySize);
90 
91     NW_GL_ASSERT();
92 
93     glAttachShader(m_ProgramID, m_ShaderID);
94     glAttachShader(m_ProgramID, GL_DMP_FRAGMENT_SHADER_DMP);
95 
96     glBindAttribLocation(m_ProgramID, VTXATTR_POSITION,  "aPosition");
97     glBindAttribLocation(m_ProgramID, VTXATTR_COLOR,     "aColor");
98 
99     glLinkProgram(m_ProgramID);
100     glUseProgram(m_ProgramID);
101 
102     glEnableVertexAttribArray( VTXATTR_POSITION  );
103     glDisableVertexAttribArray( VTXATTR_COLOR     );
104     glDisableVertexAttribArray( VTXATTR_TEXCOORD0 );
105 
106     NW_GL_ASSERT();
107 
108     return true;
109 }
110 
111 //--------------------------------------------------------------------------
112 bool
InitializeShader(os::IAllocator * allocator,const wchar_t * shaderPath)113 GraphicsDrawing::InitializeShader( os::IAllocator* allocator, const wchar_t* shaderPath )
114 {
115     m_ShapeAllocator = allocator;
116 
117     ut::MoveArray<u8> buffer = nw::demo::Utility::LoadFile( allocator, shaderPath );
118 
119     if ( buffer.empty() )
120     {
121         return false;
122     }
123 
124     s32 size = buffer.size();
125     void* pBuffer = buffer.release();
126 
127     return InitializeShader( pBuffer, size );
128 }
129 
130 //--------------------------------------------------------------------------
131 void
Setup()132 GraphicsDrawing::Setup()
133 {
134     glUseProgram(m_ProgramID);
135 
136     this->SetupMaterial();
137     this->SetupTexEnv();
138 }
139 
140 //--------------------------------------------------------------------------
141 void
SetupMaterial()142 GraphicsDrawing::SetupMaterial()
143 {
144     glDisable(GL_CULL_FACE);
145     glDisable(GL_DEPTH_TEST);
146     glDisable(GL_STENCIL_TEST);
147 
148     glEnable(GL_BLEND);
149     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 }
151 
152 //--------------------------------------------------------------------------
153 void
SetupTexEnv()154 GraphicsDrawing::SetupTexEnv()
155 {
156 #define TEXENV_LAST_STAGE "dmp_TexEnv[2]"
157 
158     // プライマリカラーの設定をそのまま出力します。
159 
160     glUniform3i(
161         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".srcRgb"),
162         GL_PRIMARY_COLOR, GL_PREVIOUS, GL_PREVIOUS
163     );
164     glUniform3i(
165         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".srcAlpha"),
166         GL_PRIMARY_COLOR, GL_PREVIOUS, GL_PREVIOUS
167     );
168     glUniform1i(
169         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".combineRgb"),
170         GL_REPLACE
171     );
172     glUniform1i(
173         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".combineAlpha"),
174         GL_REPLACE
175     );
176     glUniform3i(
177         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".operandRgb"),
178         GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_COLOR
179     );
180     glUniform3i(
181         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".operandAlpha"),
182         GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA
183     );
184     glUniform1f(
185         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".scaleRgb"),
186         1.0f
187     );
188     glUniform1f(
189         glGetUniformLocation( m_ProgramID, TEXENV_LAST_STAGE ".scaleAlpha"),
190         1.0f
191     );
192 #undef TEXENV_LAST_STAGE
193 }
194 
195 //--------------------------------------------------------------------------
196 void
BeginDrawingShape()197 GraphicsDrawing::BeginDrawingShape()
198 {
199     glUseProgram(m_ProgramID);
200 
201     this->SendMatrix();
202 }
203 
204 //--------------------------------------------------------------------------
205 void
SendMatrix()206 GraphicsDrawing::SendMatrix()
207 {
208     nw::math::MTX44 projMtx;
209     nw::math::MTX44 viewMtx;
210     const nw::math::VEC3 camPos(0.0f, 0.0f, 1.0f);
211     const nw::math::VEC3 aimPos(0.0f, 0.0f, 0.0f);
212     const nw::math::VEC3 camUp(0.0f, 1.0f, 0.0f);
213 
214     const f32 width = m_ScreenSize.y;
215     const f32 height = m_ScreenSize.x;
216 
217     const f32 left = 0.f;
218     const f32 right = m_ScreenSize.x;
219     const f32 bottom =  m_ScreenSize.y;
220     const f32 top = 0.f;
221 
222     math::MTX44OrthoPivot(&projMtx, left, right, bottom, top, -100.f, 100.f, math::PIVOT_UPSIDE_TO_TOP);
223 
224     math::MTX44Identity(&viewMtx);
225     math::MTX34LookAt(reinterpret_cast<nw::math::MTX34*>(&viewMtx), &camPos, &camUp, &aimPos);
226 
227     GLint location;
228 
229     location = glGetUniformLocation( m_ProgramID, "uProjection" );
230     glUniform4fv(location, 4, projMtx);
231 
232     location = glGetUniformLocation( m_ProgramID, "uModelView" );
233     glUniform4fv(location, 4, viewMtx);
234     NW_GL_ASSERT();
235 
236     glViewport(0, 0, static_cast<s32>(width), static_cast<s32>(height));
237 }
238 
239 //--------------------------------------------------------------------------
240 void
DrawLine(const math::VEC2 & p1,const math::VEC2 & p2,ut::Color8 color)241 GraphicsDrawing::DrawLine( const math::VEC2& p1, const math::VEC2& p2, ut::Color8 color )
242 {
243     ut::FloatColor floatColor = color;
244 
245     glVertexAttrib4fv( VTXATTR_COLOR, floatColor );
246 
247     s32 count = this->BuildLine(p1, p2, m_LineWidth, &m_PositionStream[0]);
248 
249     glVertexAttribPointer( VTXATTR_POSITION, 2, GL_FLOAT, GL_FALSE, 0, static_cast<const f32*>(m_PositionStream[0]) );
250 
251     glDrawArrays ( GL_TRIANGLES, 0, count);
252 }
253 
254 //--------------------------------------------------------------------------
255 void
DrawRectangle(const math::VEC2 & pos,const math::VEC2 & size,ut::Color8 color)256 GraphicsDrawing::DrawRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color )
257 {
258     ut::FloatColor floatColor = color;
259 
260     glVertexAttrib4fv( VTXATTR_COLOR, floatColor );
261 
262     s32 count = 0;
263 
264     count += this->BuildLine(pos, math::VEC2(pos.x + size.x, pos.y),        m_LineWidth, &m_PositionStream[count]);
265     count += this->BuildLine(math::VEC2(pos.x + size.x, pos.y), pos + size, m_LineWidth, &m_PositionStream[count]);
266     count += this->BuildLine(pos + size, math::VEC2(pos.x, pos.y + size.y), m_LineWidth, &m_PositionStream[count]);
267     count += this->BuildLine(math::VEC2(pos.x, pos.y + size.y), pos,        m_LineWidth, &m_PositionStream[count]);
268 
269     NW_ASSERT( count <= VERTEX_STREAM_SIZE );
270 
271     glVertexAttribPointer( VTXATTR_POSITION, 2, GL_FLOAT, GL_FALSE, 0, static_cast<const f32*>(m_PositionStream[0]) );
272 
273     glDrawArrays ( GL_TRIANGLES, 0, count);
274 }
275 
276 //--------------------------------------------------------------------------
277 void
FillRectangle(const math::VEC2 & pos,const math::VEC2 & size,ut::Color8 color)278 GraphicsDrawing::FillRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color )
279 {
280     ut::FloatColor floatColor = color;
281 
282     glVertexAttrib4fv( VTXATTR_COLOR, floatColor );
283 
284     s32 count = this->BuildRectangle(pos, size, &m_PositionStream[0]);
285 
286     glEnableVertexAttribArray( VTXATTR_POSITION );
287     glVertexAttribPointer( VTXATTR_POSITION, 2, GL_FLOAT, GL_FALSE, 0, static_cast<const f32*>(m_PositionStream[0]) );
288 
289     glDrawArrays ( GL_TRIANGLES, 0, count);
290 }
291 
292 //--------------------------------------------------------------------------
293 void
FillTriangle(const math::VEC2 & pt1,const math::VEC2 & pt2,const math::VEC2 & pt3,ut::Color8 color)294 GraphicsDrawing::FillTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, ut::Color8 color )
295 {
296     ut::FloatColor floatColor = color;
297 
298     glVertexAttrib4fv( VTXATTR_COLOR, floatColor );
299 
300     s32 count = this->BuildTriangle(pt1, pt2, pt3, &m_PositionStream[0]);
301 
302     glVertexAttribPointer( VTXATTR_POSITION, 2, GL_FLOAT, GL_FALSE, 0, static_cast<const f32*>(m_PositionStream[0]) );
303 
304     glDrawArrays ( GL_TRIANGLES, 0, count);
305 }
306 
307 //--------------------------------------------------------------------------
308 void
FillTriangles(const math::VEC2 * pPointArray,s32 pointCount,ut::Color8 color)309 GraphicsDrawing::FillTriangles( const math::VEC2* pPointArray, s32 pointCount, ut::Color8 color )
310 {
311     NW_NULL_ASSERT( pPointArray );
312     NW_ASSERT( pointCount > 0 );
313 
314     ut::FloatColor floatColor = color;
315 
316     glVertexAttrib4fv( VTXATTR_COLOR, floatColor );
317 
318     glVertexAttribPointer( VTXATTR_POSITION, 2, GL_FLOAT, GL_FALSE, 0, static_cast<const f32*>(pPointArray[0]) );
319 
320     glDrawArrays ( GL_TRIANGLES, 0, pointCount);
321 }
322 
323 //--------------------------------------------------------------------------
324 s32
BuildTriangle(const math::VEC2 & pt1,const math::VEC2 & pt2,const math::VEC2 & pt3,math::VEC2 * pVecArray)325 GraphicsDrawing::BuildTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, math::VEC2 *pVecArray )
326 {
327     NW_NULL_ASSERT( pVecArray );
328 
329     pVecArray[ 0 ] = pt1;
330     pVecArray[ 1 ] = pt2;
331     pVecArray[ 2 ] = pt3;
332 
333     return 3;
334 }
335 
336 //--------------------------------------------------------------------------
337 s32
BuildRectangle(const math::VEC2 & pt1,const math::VEC2 & pt2,const math::VEC2 & pt3,const math::VEC2 & pt4,math::VEC2 * pVecArray)338 GraphicsDrawing::BuildRectangle(
339     const math::VEC2& pt1,
340     const math::VEC2& pt2,
341     const math::VEC2& pt3,
342     const math::VEC2& pt4,
343     math::VEC2 *pVecArray )
344 {
345     NW_NULL_ASSERT( pVecArray );
346     s32 count = 0;
347 
348     count += this->BuildTriangle( pt1, pt2, pt3, &pVecArray[count] );
349     count += this->BuildTriangle( pt1, pt3, pt4, &pVecArray[count] );
350 
351     return count;
352 }
353 
354 //--------------------------------------------------------------------------
355 s32
BuildRectangle(const math::VEC2 & pos,const math::VEC2 & size,math::VEC2 * pVecArray)356 GraphicsDrawing::BuildRectangle( const math::VEC2& pos, const math::VEC2& size, math::VEC2 *pVecArray )
357 {
358     NW_NULL_ASSERT( pVecArray );
359     s32 count = 0;
360 
361     count += this->BuildRectangle(
362         pos,
363         math::VEC2(pos.x + size.x, pos.y),
364         pos + size,
365         math::VEC2(pos.x, pos.y + size.y),
366         pVecArray );
367 
368     return count;
369 }
370 
371 //--------------------------------------------------------------------------
372 s32
BuildLine(const math::VEC2 & pt1,const math::VEC2 & pt2,f32 lineWidth,math::VEC2 * pVecArray)373 GraphicsDrawing::BuildLine( const math::VEC2& pt1, const math::VEC2& pt2, f32 lineWidth, math::VEC2 *pVecArray )
374 {
375     NW_ASSERT( lineWidth >= 0.f );
376     NW_NULL_ASSERT( pVecArray );
377 
378     math::VEC2 direction  = pt1 + pt2;
379     math::VEC2 ortho      = math::VEC2(direction.y, -direction.x);
380 
381     VEC2Normalize( &ortho, &ortho );
382     ortho *= lineWidth;
383 
384     s32 count = 0;
385 
386     count += this->BuildRectangle( pt1 - ortho / 2, pt1 + ortho / 2, pt2 + ortho / 2, pt2 - ortho / 2, pVecArray );
387 
388     return count;
389 }
390 
391 //--------------------------------------------------------------------------
392 bool
InitializeFont(os::IAllocator * allocator,const wchar_t * fontShaderPath,const wchar_t * fontPath)393 GraphicsDrawing::InitializeFont(
394     os::IAllocator* allocator,
395     const wchar_t* fontShaderPath,
396     const wchar_t* fontPath
397 )
398 {
399     m_FontAllocator = allocator;
400 
401     // フォントリソースをロードします。
402     ut::MoveArray<u8> fontData = nw::demo::Utility::LoadFile( allocator, fontPath, nw::font::GlyphDataAlignment );
403     ut::MoveArray<u8> fontShaderData = nw::demo::Utility::LoadFile( allocator, fontShaderPath );
404 
405     s32 fontSize      = fontData.size();
406     void* fontBuffer = fontData.release();
407 
408     s32 shaderSize      = fontShaderData.size();
409     void* shaderBuffer = fontShaderData.release();
410     const u32 vtxBufCmdBufSize =
411         nw::font::RectDrawer::GetVertexBufferCommandBufferSize(shaderBuffer, shaderSize);
412     m_FontCommandBuffer = allocator->Alloc(vtxBufCmdBufSize);
413     NN_NULL_ASSERT(m_FontCommandBuffer);
414 
415     const u32 drawBufferSize = nw::font::ResFont::GetDrawBufferSize(fontBuffer);
416     void* drawBuffer = allocator->Alloc(drawBufferSize, 4);
417     NW_NULL_ASSERT(drawBuffer);
418 
419     const u32 stringBufferSize = nw::font::CharWriter::GetDispStringBufferSize(m_MaxTextCount);
420     void *const stringBuffer = allocator->Alloc(stringBufferSize);
421     NN_NULL_ASSERT(stringBuffer);
422 
423     bool result = false;
424     if ( fontBuffer != NULL && shaderBuffer != NULL )
425     {
426         result = this->InitializeFont( shaderBuffer, shaderSize, fontBuffer, fontSize, drawBuffer, stringBuffer );
427     }
428 
429     os::SafeFree(shaderBuffer, allocator);
430 
431     if ( ! result )
432     {
433         os::SafeFree(fontBuffer, allocator);
434     }
435 
436     return result;
437 }
438 
439 //--------------------------------------------------------------------------
440 bool
InitializeFont(void * shaderBinary,size_t shaderSize,void * fontBinary,size_t fontSize,void * drawBuffer,void * stringBuffer)441 GraphicsDrawing::InitializeFont(
442     void* shaderBinary,
443     size_t shaderSize,
444     void* fontBinary,
445     size_t fontSize,
446     void* drawBuffer,
447     void* stringBuffer
448 )
449 {
450     if ( ! this->InitializeFontResource( fontBinary, fontSize ) )
451     {
452         return false;
453     }
454 
455     if ( ! this->InitializeFontShader( shaderBinary, shaderSize ) )
456     {
457         return false;
458     }
459 
460     // 描画用バッファを設定します。
461     m_Font.SetDrawBuffer(drawBuffer);
462 
463     m_TextWriter.SetFont( &m_Font );
464     m_TextWriter.EnableFixedWidth( true );
465     m_TextWriter.SetFixedWidth( 8.5f );
466     nw::font::DispStringBuffer *const pDrawStringBuf = nw::font::CharWriter::InitDispStringBuffer(stringBuffer, m_MaxTextCount);
467     m_TextWriter.SetDispStringBuffer(pDrawStringBuf);
468 
469     return true;
470 }
471 
472 //--------------------------------------------------------------------------
473 bool
InitializeFontResource(void * fontData,size_t fontSize)474 GraphicsDrawing::InitializeFontResource( void* fontData, size_t fontSize )
475 {
476     NW_UNUSED_VARIABLE( fontSize );
477 
478     // フォントリソースをセットします。
479     return m_Font.SetResource( fontData );
480 }
481 
482 //--------------------------------------------------------------------------
483 bool
InitializeFontShader(void * shaderBinary,size_t shaderSize)484 GraphicsDrawing::InitializeFontShader( void* shaderBinary, size_t shaderSize )
485 {
486     m_Drawer.Initialize(m_FontCommandBuffer, shaderBinary, shaderSize);
487     return true;
488 }
489 
490 //--------------------------------------------------------------------------
491 void
DestroyFont()492 GraphicsDrawing::DestroyFont()
493 {
494     m_Drawer.Finalize();
495 
496     os::SafeFree(m_FontCommandBuffer, m_FontAllocator);
497 
498     nw::font::DispStringBuffer* stringBuffer = m_TextWriter.GetDispStringBuffer();
499     os::SafeFree(stringBuffer, m_FontAllocator);
500 
501     // フォントの破棄
502     if (!m_Font.IsManaging(NULL))
503     {
504         void* drawBuffer = m_Font.SetDrawBuffer(NULL);
505         void* resource = m_Font.RemoveResource();
506 
507         os::SafeFree(resource, m_FontAllocator);
508         os::SafeFree(drawBuffer, m_FontAllocator);
509     }
510 }
511 
512 //--------------------------------------------------------------------------
513 void
SendFontMatrix()514 GraphicsDrawing::SendFontMatrix()
515 {
516     //---- 射影行列を正射影に設定
517     {
518         // 左上原点とし、Y軸とZ軸の向きが逆になるように設定します。
519         math::MTX44 proj;
520         f32 znear   =  0.0f;
521         f32 zfar    = -1.0f;
522         f32 t       =  0;
523         f32 b       =  static_cast<f32>(m_ScreenSize.y);
524         f32 l       =  0;
525         f32 r       =  static_cast<f32>(m_ScreenSize.x);
526 
527         math::MTX44OrthoPivot(&proj, l, r, b, t, znear, zfar, math::PIVOT_UPSIDE_TO_TOP);
528 
529         m_Drawer.SetProjectionMtx( proj );
530     }
531 
532     //---- モデルビュー行列を単位行列に設定
533     {
534         nn::math::MTX34 mv;
535         nn::math::MTX34Identity(&mv);
536         m_Drawer.SetViewMtxForText(mv);
537     }
538 }
539 
540 //--------------------------------------------------------------------------
541 void
BeginDrawingString()542 GraphicsDrawing::BeginDrawingString()
543 {
544     // カラーバッファ情報
545     // LCDの向きに合わせて、幅と高さを入れ替えています。
546     const nw::font::ColorBufferInfo colBufInfo = { m_ScreenSize.y, m_ScreenSize.x, PICA_DATA_DEPTH24_STENCIL8_EXT };
547 
548     const u32 screenSettingCommands[] =
549     {
550         // ビューポートの設定
551         NW_FONT_CMD_SET_VIEWPORT( 0, 0, colBufInfo.width, colBufInfo.height ),
552 
553         // シザー処理を無効
554         NW_FONT_CMD_SET_DISABLE_SCISSOR( colBufInfo ),
555 
556         // wバッファの無効化
557         // デプスレンジの設定
558         // ポリゴンオフセットの無効化
559         NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET(
560             0.0f,           // wScale : 0.0 でWバッファが無効
561             0.0f,           // depth range near
562             1.0f,           // depth range far
563             0,              // polygon offset units : 0.0 で ポリゴンオフセットが無効
564             colBufInfo),
565     };
566 
567     nngxAdd3DCommand(screenSettingCommands, sizeof(screenSettingCommands), true);
568 
569     static const u32 s_InitCommands[] =
570     {
571         // カリングを無効
572         NW_FONT_CMD_SET_CULL_FACE( NW_FONT_CMD_CULL_FACE_DISABLE ),
573 
574         // ステンシルテストを無効
575         NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(),
576 
577         // デプステストを無効
578         // カラーバッファの全ての成分を書き込み可
579         NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK(
580             false,  // isDepthTestEnabled
581             0,      // depthFunc
582             true,   // depthMask
583             true,   // red
584             true,   // green
585             true,   // blue
586             true),  // alpha
587 
588         // アーリーデプステストを無効
589         NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST( false ),
590 
591         // フレームバッファアクセス制御
592         NW_FONT_CMD_SET_FBACCESS(
593             true,   // colorRead
594             true,   // colorWrite
595             false,  // depthRead
596             false,  // depthWrite
597             false,  // stencilRead
598             false), // stencilWrite
599     };
600 
601     nngxAdd3DCommand(s_InitCommands, sizeof(s_InitCommands), true);
602 
603     m_Drawer.DrawBegin();
604 
605     this->SendFontMatrix();
606 
607     m_TextWriter.SetCursor( 0, 0 );
608 }
609 
610 //--------------------------------------------------------------------------
611 f32
DrawString(const char * format,...)612 GraphicsDrawing::DrawString( const char* format, ... )
613 {
614     NW_NULL_ASSERT( format );
615 
616     std::va_list vargs;
617     va_start(vargs, format);
618 
619     m_TextWriter.StartPrint();
620         f32 width = m_TextWriter.VPrintf(format, vargs);
621     m_TextWriter.EndPrint();
622 
623     m_Drawer.BuildTextCommand(&m_TextWriter);
624     m_TextWriter.UseCommandBuffer();
625 
626     va_end(vargs);
627 
628     return width;
629 }
630 
631 //--------------------------------------------------------------------------
632 f32
DrawString(s32 posh,s32 posv,const char * format,...)633 GraphicsDrawing::DrawString( s32 posh, s32 posv, const char* format, ... )
634 {
635     NW_NULL_ASSERT( format );
636 
637     m_TextWriter.SetCursor( static_cast<f32>(posh), static_cast<f32>(posv) );
638 
639     std::va_list vargs;
640     va_start(vargs, format);
641 
642     f32 width = this->DrawStringArgs( posh, posv, format, vargs);
643 
644     va_end(vargs);
645 
646     return width;
647 }
648 
649 //--------------------------------------------------------------------------
650 f32
DrawStringArgs(s32 posh,s32 posv,const char * format,std::va_list args)651 GraphicsDrawing::DrawStringArgs( s32 posh, s32 posv, const char* format, std::va_list args )
652 {
653     NW_NULL_ASSERT( format );
654 
655     m_TextWriter.SetCursor( static_cast<f32>(posh), static_cast<f32>(posv) );
656 
657     m_TextWriter.StartPrint();
658         f32 width = m_TextWriter.VPrintf(format, args);
659     m_TextWriter.EndPrint();
660 
661     m_Drawer.BuildTextCommand(&m_TextWriter);
662     m_TextWriter.UseCommandBuffer();
663 
664     return width;
665 }
666 
667 //--------------------------------------------------------------------------
668 void
FlushDrawing()669 GraphicsDrawing::FlushDrawing()
670 {
671     m_Drawer.DrawEnd();
672 }
673 
674 } /* namespace demo */
675 } /* namespace nw   */
676