2013-05-15 15:07:04 +02:00
/*
2013-05-16 20:37:54 -07:00
* Copyright 2013 Jeremie Roy . All rights reserved .
2016-01-01 00:11:04 -08:00
* License : https : //github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-05-16 20:37:54 -07:00
*/
2013-05-15 15:07:04 +02:00
2013-05-23 22:07:54 -07:00
# include "common.h"
2015-10-23 20:52:22 -07:00
# include "bgfx_utils.h"
2013-05-18 22:12:40 -07:00
2013-04-22 22:42:11 +02:00
# include <bx/timer.h>
2013-05-17 22:13:32 -07:00
# include <bx/string.h>
2014-05-26 19:31:37 -07:00
# include <bx/fpumath.h>
2013-04-22 22:42:11 +02:00
2013-05-23 22:07:54 -07:00
# include "font/font_manager.h"
# include "font/text_buffer_manager.h"
2014-11-08 20:57:47 -08:00
# include "entry/input.h"
2013-04-22 22:42:11 +02:00
2016-02-10 22:37:09 -08:00
# include <iconfontheaders/icons_font_awesome.h>
# include <iconfontheaders/icons_kenney.h>
2013-04-22 22:42:11 +02:00
# include <stdio.h>
2013-05-16 09:03:56 -07:00
# include <wchar.h>
2013-04-22 22:42:11 +02:00
2013-08-08 22:18:19 -07:00
long int fsize ( FILE * _file )
2013-05-29 21:53:19 -07:00
{
2013-08-08 22:18:19 -07:00
long int pos = ftell ( _file ) ;
fseek ( _file , 0L , SEEK_END ) ;
long int size = ftell ( _file ) ;
fseek ( _file , pos , SEEK_SET ) ;
return size ;
}
TrueTypeHandle loadTtf ( FontManager * _fm , const char * _filePath )
{
FILE * file = fopen ( _filePath , " rb " ) ;
if ( NULL ! = file )
2013-05-29 21:53:19 -07:00
{
2013-08-08 22:18:19 -07:00
uint32_t size = ( uint32_t ) fsize ( file ) ;
uint8_t * mem = ( uint8_t * ) malloc ( size + 1 ) ;
size_t ignore = fread ( mem , 1 , size , file ) ;
BX_UNUSED ( ignore ) ;
fclose ( file ) ;
mem [ size - 1 ] = ' \0 ' ;
TrueTypeHandle handle = _fm - > createTtf ( mem , size ) ;
free ( mem ) ;
return handle ;
2013-05-29 21:53:19 -07:00
}
TrueTypeHandle invalid = BGFX_INVALID_HANDLE ;
return invalid ;
}
2015-10-23 20:52:22 -07:00
int _main_ ( int _argc , char * * _argv )
2013-04-22 22:42:11 +02:00
{
2015-10-23 20:52:22 -07:00
Args args ( _argc , _argv ) ;
2013-05-15 15:21:23 +02:00
uint32_t width = 1280 ;
2013-04-22 22:42:11 +02:00
uint32_t height = 720 ;
uint32_t debug = BGFX_DEBUG_TEXT ;
2013-05-17 08:45:12 -07:00
uint32_t reset = BGFX_RESET_VSYNC ;
2013-04-22 22:42:11 +02:00
2015-10-23 20:52:22 -07:00
bgfx : : init ( args . m_type , args . m_pciId ) ;
2013-05-17 08:45:12 -07:00
bgfx : : reset ( width , height , reset ) ;
2013-04-22 22:42:11 +02:00
// Enable debug text.
bgfx : : setDebug ( debug ) ;
// Set view 0 clear state.
bgfx : : setViewClear ( 0
2015-01-10 21:39:45 -08:00
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
2013-05-16 20:35:08 -07:00
, 0x303030ff
, 1.0f
, 0
) ;
2013-05-15 15:21:23 +02:00
2013-05-16 20:35:08 -07:00
// Init the text rendering system.
2013-04-23 17:18:54 +02:00
FontManager * fontManager = new FontManager ( 512 ) ;
TextBufferManager * textBufferManager = new TextBufferManager ( fontManager ) ;
2013-05-07 15:51:19 +02:00
2013-05-16 20:35:08 -07:00
// Load some TTF files.
2013-10-23 20:32:48 -07:00
const char * fontFilePath [ 7 ] =
2013-05-15 15:21:23 +02:00
{
2013-05-08 19:55:20 +02:00
" font/droidsans.ttf " ,
2013-05-15 15:21:23 +02:00
" font/chp-fire.ttf " ,
2013-05-08 19:55:20 +02:00
" font/bleeding_cowboys.ttf " ,
" font/mias_scribblings.ttf " ,
" font/ruritania.ttf " ,
" font/signika-regular.ttf " ,
2013-10-23 20:32:48 -07:00
" font/five_minutes.otf " ,
2013-05-08 19:55:20 +02:00
} ;
2013-10-23 20:32:48 -07:00
const uint32_t numFonts = BX_COUNTOF ( fontFilePath ) ;
2013-05-15 15:21:23 +02:00
2013-10-23 20:32:48 -07:00
TrueTypeHandle fontFiles [ numFonts ] ;
FontHandle fonts [ numFonts ] ;
for ( uint32_t ii = 0 ; ii < numFonts ; + + ii )
2013-05-08 19:55:20 +02:00
{
2013-05-16 20:35:08 -07:00
// Instantiate a usable font.
2013-10-23 20:32:48 -07:00
fontFiles [ ii ] = loadTtf ( fontManager , fontFilePath [ ii ] ) ;
2013-05-08 19:55:20 +02:00
fonts [ ii ] = fontManager - > createFontByPixelSize ( fontFiles [ ii ] , 0 , 32 ) ;
2013-05-16 20:35:08 -07:00
// Preload glyphs and blit them to atlas.
2013-05-08 19:55:20 +02:00
fontManager - > preloadGlyph ( fonts [ ii ] , L " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n " ) ;
2013-05-16 20:35:08 -07:00
2015-07-22 18:05:11 -07:00
// You can unload the truetype files at this stage, but in that
// case, the set of glyph's will be limited to the set of preloaded
2013-05-16 20:35:08 -07:00
// glyph.
2013-05-29 21:53:19 -07:00
fontManager - > destroyTtf ( fontFiles [ ii ] ) ;
2013-05-08 19:55:20 +02:00
}
2013-05-15 15:21:23 +02:00
2013-10-23 20:32:48 -07:00
TrueTypeHandle fontAwesomeTtf = loadTtf ( fontManager , " font/fontawesome-webfont.ttf " ) ;
2016-02-10 22:37:09 -08:00
TrueTypeHandle fontKenneyTtf = loadTtf ( fontManager , " font/kenney-icon-font.ttf " ) ;
2013-04-22 22:42:11 +02:00
2015-07-22 18:05:11 -07:00
// This font doesn't have any preloaded glyph's but the truetype file
2013-05-16 20:35:08 -07:00
// is loaded so glyph will be generated as needed.
2013-10-23 20:32:48 -07:00
FontHandle fontAwesome72 = fontManager - > createFontByPixelSize ( fontAwesomeTtf , 0 , 72 ) ;
2016-02-10 22:37:09 -08:00
FontHandle fontKenney64 = fontManager - > createFontByPixelSize ( fontKenneyTtf , 0 , 64 ) ;
2013-10-23 20:32:48 -07:00
TrueTypeHandle visitorTtf = loadTtf ( fontManager , " font/visitor1.ttf " ) ;
2015-07-22 18:05:11 -07:00
// This font doesn't have any preloaded glyph's but the truetype file
2013-10-23 20:32:48 -07:00
// is loaded so glyph will be generated as needed.
FontHandle visitor10 = fontManager - > createFontByPixelSize ( visitorTtf , 0 , 10 ) ;
2013-05-15 15:21:23 +02:00
2013-04-22 22:42:11 +02:00
//create a static text buffer compatible with alpha font
//a static text buffer content cannot be modified after its first submit.
2013-06-03 23:16:02 -07:00
TextBufferHandle staticText = textBufferManager - > createTextBuffer ( FONT_TYPE_ALPHA , BufferType : : Static ) ;
2013-05-15 15:21:23 +02:00
2015-07-22 18:05:11 -07:00
// The pen position represent the top left of the box of the first line
2013-05-16 20:35:08 -07:00
// of text.
2013-05-08 19:55:20 +02:00
textBufferManager - > setPenPosition ( staticText , 24.0f , 100.0f ) ;
2013-10-23 20:32:48 -07:00
for ( uint32_t ii = 0 ; ii < numFonts ; + + ii )
2013-05-08 19:55:20 +02:00
{
2013-05-16 20:35:08 -07:00
// Add some text to the buffer.
// The position of the pen is adjusted when there is an endline.
2013-05-08 19:55:20 +02:00
textBufferManager - > appendText ( staticText , fonts [ ii ] , L " The quick brown fox jumps over the lazy dog \n " ) ;
}
2013-05-16 20:35:08 -07:00
// Now write some styled text.
2013-05-15 15:21:23 +02:00
2013-05-16 20:35:08 -07:00
// Setup style colors.
2013-10-23 20:32:48 -07:00
textBufferManager - > setBackgroundColor ( staticText , 0x551111ff ) ;
textBufferManager - > setUnderlineColor ( staticText , 0xff2222ff ) ;
textBufferManager - > setOverlineColor ( staticText , 0x2222ffff ) ;
textBufferManager - > setStrikeThroughColor ( staticText , 0x22ff22ff ) ;
2013-04-22 22:42:11 +02:00
2013-05-16 20:35:08 -07:00
// Background.
2013-04-23 17:18:54 +02:00
textBufferManager - > setStyle ( staticText , STYLE_BACKGROUND ) ;
2013-05-08 19:55:20 +02:00
textBufferManager - > appendText ( staticText , fonts [ 0 ] , L " The quick " ) ;
2013-05-15 15:21:23 +02:00
2013-05-16 20:35:08 -07:00
// Strike-through.
2013-04-23 17:18:54 +02:00
textBufferManager - > setStyle ( staticText , STYLE_STRIKE_THROUGH ) ;
2013-05-08 19:55:20 +02:00
textBufferManager - > appendText ( staticText , fonts [ 0 ] , L " brown fox " ) ;
2013-05-15 15:21:23 +02:00
2013-05-16 20:35:08 -07:00
// Overline.
2013-04-23 17:18:54 +02:00
textBufferManager - > setStyle ( staticText , STYLE_OVERLINE ) ;
2013-05-08 19:55:20 +02:00
textBufferManager - > appendText ( staticText , fonts [ 0 ] , L " jumps over " ) ;
2013-05-16 20:35:08 -07:00
// Underline.
2013-04-23 17:18:54 +02:00
textBufferManager - > setStyle ( staticText , STYLE_UNDERLINE ) ;
2013-05-08 19:55:20 +02:00
textBufferManager - > appendText ( staticText , fonts [ 0 ] , L " the lazy " ) ;
2013-05-15 15:21:23 +02:00
2013-05-16 20:35:08 -07:00
// Background + strike-through.
2013-05-15 15:21:23 +02:00
textBufferManager - > setStyle ( staticText , STYLE_BACKGROUND | STYLE_STRIKE_THROUGH ) ;
2013-05-08 19:55:20 +02:00
textBufferManager - > appendText ( staticText , fonts [ 0 ] , L " dog \n " ) ;
2013-05-15 15:21:23 +02:00
2013-10-23 20:32:48 -07:00
textBufferManager - > setStyle ( staticText , STYLE_NORMAL ) ;
2016-02-10 22:37:09 -08:00
textBufferManager - > appendText ( staticText , fontAwesome72 ,
" " ICON_FA_POWER_OFF
" " ICON_FA_TWITTER_SQUARE
" " ICON_FA_CERTIFICATE
" " ICON_FA_FLOPPY_O
" " ICON_FA_GITHUB
" " ICON_FA_GITHUB_ALT
" \n "
) ;
textBufferManager - > appendText ( staticText , fontKenney64 ,
" " ICON_KI_COMPUTER
" " ICON_KI_JOYSTICK
" " ICON_KI_EXLAMATION
" " ICON_KI_STAR
" " ICON_KI_BUTTON_START
" " ICON_KI_DOWNLOAD
" \n "
) ;
2013-10-23 20:32:48 -07:00
2013-05-16 20:35:08 -07:00
// Create a transient buffer for real-time data.
2013-06-03 23:16:02 -07:00
TextBufferHandle transientText = textBufferManager - > createTextBuffer ( FONT_TYPE_ALPHA , BufferType : : Transient ) ;
2013-05-15 15:21:23 +02:00
2013-08-07 21:45:56 -07:00
while ( ! entry : : processEvents ( width , height , debug , reset ) )
2013-04-22 22:42:11 +02:00
{
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
2015-07-22 18:05:11 -07:00
bgfx : : touch ( 0 ) ;
2013-04-22 22:42:11 +02:00
int64_t now = bx : : getHPCounter ( ) ;
static int64_t last = now ;
const int64_t frameTime = now - last ;
last = now ;
const double freq = double ( bx : : getHPFrequency ( ) ) ;
2013-05-15 15:21:23 +02:00
const double toMs = 1000.0 / freq ;
2013-04-23 14:29:35 +02:00
2013-08-08 22:18:19 -07:00
// Use debug font to print information about this example.
bgfx : : dbgTextClear ( ) ;
bgfx : : dbgTextPrintf ( 0 , 1 , 0x4f , " bgfx/examples/10-font " ) ;
bgfx : : dbgTextPrintf ( 0 , 2 , 0x6f , " Description: Use the font system to display text and styled text. " ) ;
bgfx : : dbgTextPrintf ( 0 , 3 , 0x0f , " Frame: % 7.3f[ms] " , double ( frameTime ) * toMs ) ;
2013-05-16 20:37:54 -07:00
// Use transient text to display debug information.
2013-04-23 14:29:35 +02:00
wchar_t fpsText [ 64 ] ;
2013-08-06 21:09:02 -07:00
bx : : swnprintf ( fpsText , BX_COUNTOF ( fpsText ) , L " Frame: % 7.3f[ms] " , double ( frameTime ) * toMs ) ;
2013-05-15 15:21:23 +02:00
2013-04-23 14:29:35 +02:00
textBufferManager - > clearTextBuffer ( transientText ) ;
2013-08-08 22:18:19 -07:00
textBufferManager - > setPenPosition ( transientText , width - 150.0f , 10.0f ) ;
2013-10-23 20:32:48 -07:00
textBufferManager - > appendText ( transientText , visitor10 , L " Transient \n " ) ;
textBufferManager - > appendText ( transientText , visitor10 , L " text buffer \n " ) ;
textBufferManager - > appendText ( transientText , visitor10 , fpsText ) ;
2013-04-23 14:29:35 +02:00
2014-11-08 20:57:47 -08:00
float at [ 3 ] = { 0 , 0 , 0.0f } ;
float eye [ 3 ] = { 0 , 0 , - 1.0f } ;
2013-05-15 15:21:23 +02:00
2013-04-22 22:42:11 +02:00
float view [ 16 ] ;
2014-05-26 19:31:37 -07:00
bx : : mtxLookAt ( view , eye , at ) ;
2013-05-16 20:37:54 -07:00
2014-11-03 19:11:08 -08:00
const float centering = 0.5f ;
2013-04-22 22:42:11 +02:00
2014-11-03 19:11:08 -08:00
// Setup a top-left ortho matrix for screen space drawing.
const bgfx : : HMD * hmd = bgfx : : getHMD ( ) ;
2015-07-16 20:28:43 -07:00
if ( NULL ! = hmd & & 0 ! = ( hmd - > flags & BGFX_HMD_RENDERING ) )
2014-11-03 19:11:08 -08:00
{
float proj [ 16 ] ;
bx : : mtxProj ( proj , hmd - > eye [ 0 ] . fov , 0.1f , 100.0f ) ;
static float time = 0.0f ;
time + = 0.05f ;
const float dist = 10.0f ;
2014-11-08 20:57:47 -08:00
const float offset0 = - proj [ 8 ] + ( hmd - > eye [ 0 ] . viewOffset [ 0 ] / dist * proj [ 0 ] ) ;
const float offset1 = - proj [ 8 ] + ( hmd - > eye [ 1 ] . viewOffset [ 0 ] / dist * proj [ 0 ] ) ;
2014-11-03 19:11:08 -08:00
float ortho [ 2 ] [ 16 ] ;
const float offsetx = width / 2.0f ;
bx : : mtxOrtho ( ortho [ 0 ] , centering , offsetx + centering , height + centering , centering , - 1.0f , 1.0f , offset0 ) ;
bx : : mtxOrtho ( ortho [ 1 ] , centering , offsetx + centering , height + centering , centering , - 1.0f , 1.0f , offset1 ) ;
bgfx : : setViewTransform ( 0 , view , ortho [ 0 ] , BGFX_VIEW_STEREO , ortho [ 1 ] ) ;
bgfx : : setViewRect ( 0 , 0 , 0 , hmd - > width , hmd - > height ) ;
}
else
{
float ortho [ 16 ] ;
bx : : mtxOrtho ( ortho , centering , width + centering , height + centering , centering , - 1.0f , 1.0f ) ;
bgfx : : setViewTransform ( 0 , view , ortho ) ;
bgfx : : setViewRect ( 0 , 0 , 0 , width , height ) ;
}
2013-04-22 22:42:11 +02:00
2013-05-16 20:35:08 -07:00
// Submit the debug text.
2013-04-23 14:29:35 +02:00
textBufferManager - > submitTextBuffer ( transientText , 0 ) ;
2013-05-16 20:35:08 -07:00
// Submit the static text.
2013-05-15 15:21:23 +02:00
textBufferManager - > submitTextBuffer ( staticText , 0 ) ;
// Advance to next frame. Rendering thread will be kicked to
2013-04-22 22:42:11 +02:00
// process submitted rendering primitives.
bgfx : : frame ( ) ;
}
2013-05-15 15:21:23 +02:00
2016-02-10 22:37:09 -08:00
fontManager - > destroyTtf ( fontKenneyTtf ) ;
2013-10-23 20:32:48 -07:00
fontManager - > destroyTtf ( fontAwesomeTtf ) ;
fontManager - > destroyTtf ( visitorTtf ) ;
2013-05-16 20:35:08 -07:00
// Destroy the fonts.
2016-02-10 22:37:09 -08:00
fontManager - > destroyFont ( fontKenney64 ) ;
2013-10-23 20:32:48 -07:00
fontManager - > destroyFont ( fontAwesome72 ) ;
fontManager - > destroyFont ( visitor10 ) ;
for ( uint32_t ii = 0 ; ii < numFonts ; + + ii )
2013-05-08 19:55:20 +02:00
{
fontManager - > destroyFont ( fonts [ ii ] ) ;
}
2013-04-22 22:42:11 +02:00
textBufferManager - > destroyTextBuffer ( staticText ) ;
2013-05-15 15:21:23 +02:00
textBufferManager - > destroyTextBuffer ( transientText ) ;
2013-04-22 22:42:11 +02:00
delete textBufferManager ;
2013-05-15 15:21:23 +02:00
delete fontManager ;
2013-04-22 22:42:11 +02:00
// Shutdown bgfx.
2013-05-15 15:21:23 +02:00
bgfx : : shutdown ( ) ;
2013-04-22 22:42:11 +02:00
return 0 ;
}