forked from pent0/ka3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.cpp
653 lines (564 loc) · 16.4 KB
/
Console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include <hgr/Console.h>
#include <gr/Shader.h>
#include <gr/Context.h>
#include <gr/Primitive.h>
#include <gr/VertexFormat.h>
#include <lang/Debug.h>
#include <lang/Character.h>
#include <lang/UTFConverter.h>
#include <math/float2.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <config.h>
#include <lang/pp.h>
#ifdef PLATFORM_NGI
# include <errorcodes.h>
# include <runtime.h>
# include <fonts.h>
# include <ngibitmap.h>
# include <backbuffer.h>
# include <gr/n3d/N3D_Context.h>
# include <NLibs/NRenderBuffer/NRenderBackBuffer.h>
USING_NAMESPACE(ngi)
#endif
USING_NAMESPACE(gr)
USING_NAMESPACE(lang)
USING_NAMESPACE(math)
BEGIN_NAMESPACE(hgr)
Console::Console( Context* context,
Texture* isolatin1, int fontwidth, int fontheight, int textoffset,
const String& shadername ) :
#ifndef PLATFORM_NGI
m_shader( context->createShader(shadername) ),
#endif
m_fontWidth( fontwidth ),
m_fontHeight( fontheight ),
m_textOffset( textoffset ),
m_cursorX( 0.f ),
m_cursorY( 0.f )
{
setClassId( NODE_CONSOLE );
clear();
addPage( isolatin1, 0 );
#ifdef PLATFORM_NGI
USING_NAMESPACE(ngi)
ReturnCode rc;
rc = rc; // To silence compiler warning on release build
m_fontFactory = 0;
m_font = 0;
m_fontBitmap = 0;
rc = CRuntime::CreateInstance( m_fontFactory );
assert( OK == rc );
// Get the number of fonts already installed on the system.
uint32 numberOfFontsInstalled = m_fontFactory->GetInstalledFontCount();
assert ( numberOfFontsInstalled != 0 );
// Get information of the Font at index 0.
IFontFactory::CFontInformation fontInfo;
m_fontFactory->GetFontInformation( 0, fontInfo );
// Create a Font Object using the Font name (of the font at index 0)
rc = m_fontFactory->CreateFont( m_font, fontInfo.mTypeFaceName, fontheight*8 ); // size in twips; use arbitrary number 8 twips per pixel
assert( OK == rc );
// Create an empty Bitmap object
rc = CRuntime::CreateInstance( m_fontBitmap );
assert( OK == rc );
#endif
}
Console::~Console()
{
#ifdef PLATFORM_NGI
m_fontBitmap->Release();
m_font->Release();
m_fontFactory->Release();
#endif
}
Console::Console( const Console& other ) :
Visual( other ),
m_shader( other.m_shader ),
m_fontWidth( other.m_fontWidth ),
m_fontHeight( other.m_fontHeight ),
m_textOffset( other.m_textOffset ),
m_cursorX( other.m_cursorX ),
m_cursorY( other.m_cursorY ),
m_text( other.m_text ),
m_pages( other.m_pages ),
m_images( other.m_images )
#ifdef PLATFORM_NGI
, m_fontFactory( other.m_fontFactory ),
m_font( other.m_font ),
m_fontBitmap( other.m_fontBitmap )
#endif
{
}
Node* Console::clone() const
{
return new Console( *this );
}
void Console::drawImage( int x, int y, Texture* tex )
{
Rect sourcerect( 0, 0, tex->width(), tex->height() );
drawImage( x, y, sourcerect, tex );
}
void Console::drawImage( int x, int y, const Rect& subimage, Texture* tex )
{
stretchImage( x, y, subimage.width(), subimage.height(), subimage, tex );
}
void Console::stretchImage( int x, int y, int w, int h, Texture* tex )
{
stretchImage( x, y, w, h, Rect(0,0,tex->width(),tex->height()), tex );
}
void Console::stretchImage( int x, int y, int w, int h, const Rect& subimage, Texture* tex )
{
Image img;
img.pos = float3( float(x), float(y), 0.f );
img.tex = tex;
img.x0 = (float)subimage.left();
img.y0 = (float)subimage.top();
img.x1 = (float)subimage.right();
img.y1 = (float)subimage.bottom();
img.width = (float)w;
img.height = (float)h;
m_images.add( img );
}
void Console::printf( int x, int y, const char* fmt, ... )
{
// format variable arguments
const unsigned MAX_MSG = 2000;
char msg[MAX_MSG+4];
va_list marker;
va_start( marker, fmt );
vsprintf( msg, fmt, marker );
va_end( marker );
assert( strlen(msg) < MAX_MSG );
put( (float)x, (float)y, msg );
}
void Console::printf( const char* fmt, ... )
{
// format variable arguments
const unsigned MAX_MSG = 2000;
char msg[MAX_MSG+4];
va_list marker;
va_start( marker, fmt );
vsprintf( msg, fmt, marker );
va_end( marker );
assert( strlen(msg) < MAX_MSG );
put( m_cursorX, m_cursorY, msg );
}
void Console::put( float x, float y, const char* msg )
{
UTFConverter conv( UTFConverter::ENCODING_UTF8 );
m_cursorX = x;
m_cursorY = y;
for ( int i = 0 ; msg[i] ; )
{
TextChar ch;
ch.x0 = m_cursorX;
ch.y0 = m_cursorY;
ch.code = 0;
int bytes = 1;
if ( conv.decode(&msg[i], m_text.end(), &bytes, &ch.code) )
{
if ( Character::isWhitespace(ch.code) )
{
if ( ch.code == '\n' )
{
m_cursorX = x;
m_cursorY += m_fontHeight;
}
else
{
m_cursorX += (float)( m_fontWidth + m_textOffset );
}
ch.code = 0; // don't bother rendering whitespace
}
else
{
m_cursorX += (float)( m_fontWidth + m_textOffset );
}
}
i += bytes;
m_text.add( ch );
}
}
void Console::clear()
{
m_text.clear();
m_images.clear();
m_cursorX = 0;
m_cursorY = 0;
}
void Console::render( Context* context )
{
#ifdef PLATFORM_NGI
#ifdef EMULATOR_BUILD
return; // Does not work on emulator since upgrade from SDK 1.0 to 1.1
#endif
// TODO: replace with real implementation for NGI
// Kludge, uses NGI font API instead of 3D primitives from font texture
// Does not support images
// Does not adhere to specified font size (uses twips instead of pixels)
// Does not use font positions yet
// NOTE: Non-Latin-1 characters are probably assumed to be encoded as UCS-2 instead of UTF-16
// - NGI documentation does not specify the encoding for the font API, but UCS-2 is used in other APIs
// - UCS-2 only covers characters in the Basic Multilingual Plane, and is 16bit fixed-width
ReturnCode rc;
rc = rc; // To silence compiler warning on release build
USING_NAMESPACE(ngi)
char16 text[1024];
int i;
for ( i = 0 ; i < m_text.size() && i < 1024; ++i )
{
int ch = m_text[i].code;
if( 0 == ch )
{
text[i] = 32; // replace whitespace (encoded as 0) with space (ASCII 32)
}
else
{
text[i] = ch;
}
}
text[i] = 0; // null terminate
// Grab bitmap of backbuffer
nlibs::CNRenderBackBuffer* rbb = reinterpret_cast<N3D_Context*>(context)->n3dRenderBackBuffer();
IBackBuffer* bb = rbb->GetBackBuffer();
rc = m_fontBitmap->CreateFromMemory( bb->GetSize(),
bb->GetColorFormat(),
bb->GetStride(),
bb->GetAddress(),
false ); // Do not take ownership of backbuffer data
assert( OK == rc );
// Draw text onto backbuffer
rc = m_font->DrawText( m_fontBitmap, // The Bitmap to draw the text into.
text, // The string to be drawn.
0x0, // Color of the text.
TEXTJUSTIFICATION_TOP_LEFT, // Justification of the text inside the Bitmap
TEXTEFFECTS_NONE // No text effects
);
assert( OK == rc );
return;
#endif
// draw text
if ( m_text.size() > 0 )
{
assert( m_pages.size() > 0 );
// font params
const float fontw = (float)m_fontWidth;
const float fonth = (float)m_fontHeight;
// find out which texture pages are used
int texpagelist[1024];
int texpages = 0;
for ( int i = 0 ; i < m_text.size() ; ++i )
{
int ch = m_text[i].code;
int page = ch >> 8;
for ( int k = 0 ; k < texpages ; ++k )
{
if ( texpagelist[k] == page )
{
page = -1;
break;
}
}
if ( -1 != page )
{
assert( texpages < 256 );
texpagelist[texpages++] = page;
}
}
// get temporary dynamic primitive from rendering context
VertexFormat vf;
vf.addTransformedPosition().addDiffuse().addTextureCoordinate( VertexFormat::DF_V2_32 );
Primitive* prim = context->getDynamicPrimitive( Primitive::PRIM_TRI, vf, m_text.size()*6, 0 );
// render / font texpage
for ( int k = 0 ; k < texpages ; ++k )
{
const int pageindex = texpagelist[k];
// set vertex data
{
Primitive::Lock lk( prim, Primitive::LOCK_WRITE );
int vi = 0;
// get data pointers
assert( VertexFormat::DF_V4_32 == prim->vertexFormat().getDataFormat(VertexFormat::DT_POSITIONT) );
assert( VertexFormat::DF_V4_8 == prim->vertexFormat().getDataFormat(VertexFormat::DT_DIFFUSE) );
assert( VertexFormat::DF_V2_32 == prim->vertexFormat().getDataFormat(VertexFormat::DT_TEX0) );
float* vpos;
int vpospitch;
prim->getVertexDataPtr( VertexFormat::DT_POSITIONT, reinterpret_cast<uint8_t**>(&vpos), &vpospitch );
assert( (vpospitch&3) == 0 );
vpospitch >>= 2;
uint32_t* vcolor;
int vcolorpitch;
prim->getVertexDataPtr( VertexFormat::DT_DIFFUSE, reinterpret_cast<uint8_t**>(&vcolor), &vcolorpitch );
assert( (vcolorpitch&3) == 0 );
vcolorpitch >>= 2;
float* vtexcoord;
int vtexcoordpitch;
prim->getVertexDataPtr( VertexFormat::DT_TEX0, reinterpret_cast<uint8_t**>(&vtexcoord), &vtexcoordpitch );
assert( (vtexcoordpitch&3) == 0 );
vtexcoordpitch >>= 2;
// prepare sprite quads
for ( int i = 0 ; i < m_text.size() ; ++i )
{
int ch = m_text[i].code;
if ( ch > 0 )
{
int page = ch >> 8;
ch &= 0xFF;
if ( page >= 0 && page < m_pages.size() && m_pages[page] != 0 )
{
if ( page == pageindex )
{
Texture* tex = m_pages[pageindex];
// prepare font character data
float x = float(ch&0xF) * fontw;
float y = float((ch>>4)&0xF) * fonth;
float du = 1.f/float(tex->width());
float dv = 1.f/float(tex->height());
float u0 = du * float(x);
float v0 = dv * float(y);
float u1 = u0 + du * fontw;
float v1 = v0 + dv * fonth;
const float smallz = 0.0001f;
float3 projpoint(0,0,smallz);
float rhw = 1.f;
float2 cursor( m_text[i].x0+.5f, m_text[i].y0+.5f );
float2 p0( cursor );
float2 p1( cursor + float2(fontw,0) );
float2 p2( cursor + float2(fontw,fonth) );
float2 p3( cursor + float2(0,fonth) );
// set vertex positions (as two-triangle quad)
vpos[0] = p0.x;
vpos[1] = p0.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p1.x;
vpos[1] = p1.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p2.x;
vpos[1] = p2.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
// tri 0,2,3
vpos[0] = p0.x;
vpos[1] = p0.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p2.x;
vpos[1] = p2.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p3.x;
vpos[1] = p3.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
// set color0
uint32_t color = 0xFFFFFFFF;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
// set texcoords
vtexcoord[0] = u0;
vtexcoord[1] = v0;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u1;
vtexcoord[1] = v0;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u1;
vtexcoord[1] = v1;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u0;
vtexcoord[1] = v0;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u1;
vtexcoord[1] = v1;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u0;
vtexcoord[1] = v1;
vtexcoord += vtexcoordpitch;
vi += 6;
}
}
}
}
prim->setVertexRangeEnd( vi );
}
// render prepared primitive
if ( prim->vertexRangeEnd() > 0 && m_pages[pageindex] != 0 )
draw( prim, m_pages[pageindex] );
}
}
// draw images
if ( m_images.size() > 0 )
{
// get temporary dynamic primitive from rendering context
VertexFormat vf;
vf.addTransformedPosition().addDiffuse().addTextureCoordinate( VertexFormat::DF_V2_32 );
Primitive* prim = context->getDynamicPrimitive( Primitive::PRIM_TRI, vf, m_images.size()*6, 0 );
for ( int k = 0 ; k < m_images.size() ; ++k )
{
// image props
const Image& img = m_images[k];
const float imgw = m_images[k].width;
const float imgh = m_images[k].height;
const float2 cursor( img.pos.x, img.pos.y );
// set vertex data
{
Primitive::Lock lk( prim, Primitive::LOCK_WRITE );
int vi = 0;
// get data pointers
assert( VertexFormat::DF_V4_32 == prim->vertexFormat().getDataFormat(VertexFormat::DT_POSITIONT) );
assert( VertexFormat::DF_V4_8 == prim->vertexFormat().getDataFormat(VertexFormat::DT_DIFFUSE) );
assert( VertexFormat::DF_V2_32 == prim->vertexFormat().getDataFormat(VertexFormat::DT_TEX0) );
float* vpos;
int vpospitch;
prim->getVertexDataPtr( VertexFormat::DT_POSITIONT, reinterpret_cast<uint8_t**>(&vpos), &vpospitch );
assert( (vpospitch&3) == 0 );
vpospitch >>= 2;
uint32_t* vcolor;
int vcolorpitch;
prim->getVertexDataPtr( VertexFormat::DT_DIFFUSE, reinterpret_cast<uint8_t**>(&vcolor), &vcolorpitch );
assert( (vcolorpitch&3) == 0 );
vcolorpitch >>= 2;
float* vtexcoord;
int vtexcoordpitch;
prim->getVertexDataPtr( VertexFormat::DT_TEX0, reinterpret_cast<uint8_t**>(&vtexcoord), &vtexcoordpitch );
assert( (vtexcoordpitch&3) == 0 );
vtexcoordpitch >>= 2;
// prepare image data
float u0 = img.x0 / (float)img.tex->width();
float v0 = img.y0 / (float)img.tex->height();
float u1 = img.x1 / (float)img.tex->width();
float v1 = img.y1 / (float)img.tex->height();
const float smallz = 0.0001f;
float3 projpoint(0,0,smallz);
float rhw = 1.f;
float2 p0( cursor );
float2 p1( cursor + float2(imgw,0) );
float2 p2( cursor + float2(imgw,imgh) );
float2 p3( cursor + float2(0,imgh) );
// set vertex positions (as two-triangle quad)
vpos[0] = p0.x;
vpos[1] = p0.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p1.x;
vpos[1] = p1.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p2.x;
vpos[1] = p2.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
// tri 0,2,3
vpos[0] = p0.x;
vpos[1] = p0.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p2.x;
vpos[1] = p2.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
vpos[0] = p3.x;
vpos[1] = p3.y;
vpos[2] = projpoint.z;
vpos[3] = rhw;
vpos += vpospitch;
// set color0
uint32_t color = 0xFFFFFFFF;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
*vcolor = color;
vcolor += vcolorpitch;
// set texcoords
vtexcoord[0] = u0;
vtexcoord[1] = v0;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u1;
vtexcoord[1] = v0;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u1;
vtexcoord[1] = v1;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u0;
vtexcoord[1] = v0;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u1;
vtexcoord[1] = v1;
vtexcoord += vtexcoordpitch;
vtexcoord[0] = u0;
vtexcoord[1] = v1;
vtexcoord += vtexcoordpitch;
vi += 6;
prim->setVertexRangeEnd( vi );
}
// render prepared primitive
if ( prim->vertexRangeEnd() > 0 )
draw( prim, img.tex );
}
}
}
void Console::render( Context* context, Camera* /*camera*/, int priority )
{
if ( priority == m_shader->priority() )
render( context );
}
void Console::draw( Primitive* prim, Texture* tex )
{
if ( tex != 0 )
{
m_shader->setTexture( "BASEMAP", tex );
Shader::Begin use( m_shader );
for ( int k = 0 ; k < use.passes() ; ++k )
{
Shader::Pass pass( m_shader, k );
prim->render();
}
}
}
void Console::getShaders( Array<Shader*>& shaders )
{
shaders.add( m_shader );
}
void Console::computeBound()
{
setBoundInfinity();
}
void Console::addPage( Texture* tex, int page )
{
assert( page >= 0 && page < 1024 );
if ( page >= m_pages.size() )
m_pages.resize( page+1 );
m_pages[page] = tex;
}
END_NAMESPACE() // hgr
// Copyright (C) 2004-2006 Pixelgene Ltd. All rights reserved. Consult your license regarding permissions and restrictions.