bkaradzic 12 years ago
parent
commit
50a92db146
2 changed files with 57 additions and 71 deletions
  1. 36 40
      examples/10-font/font.cpp
  2. 21 31
      examples/11-fontsdf/fontsdf.cpp

+ 36 - 40
examples/10-font/font.cpp

@@ -1,7 +1,7 @@
 /*
 /*
- * Copyright 2013 Jeremie Roy. All rights reserved.
- * License: http://www.opensource.org/licenses/BSD-2-Clause
- */
+* Copyright 2013 Jeremie Roy. All rights reserved.
+* License: http://www.opensource.org/licenses/BSD-2-Clause
+*/
 
 
 #include <bgfx.h>
 #include <bgfx.h>
 #include <bx/bx.h>
 #include <bx/bx.h>
@@ -34,17 +34,17 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 
 
 	// Set view 0 clear state.
 	// Set view 0 clear state.
 	bgfx::setViewClear(0
 	bgfx::setViewClear(0
-	                  , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
-	                  , 0x303030ff
-	                  , 1.0f
-	                  , 0
-	                  );
+		, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
+		, 0x303030ff
+		, 1.0f
+		, 0
+		);
 
 
-	//init the text rendering system
+	// Init the text rendering system.
 	FontManager* fontManager = new FontManager(512);
 	FontManager* fontManager = new FontManager(512);
 	TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
 	TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
 
 
-	//load some truetype files
+	// Load some TTF files.
 	const char* fontNames[7] =
 	const char* fontNames[7] =
 	{
 	{
 		"font/droidsans.ttf",
 		"font/droidsans.ttf",
@@ -56,83 +56,79 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		"font/five_minutes.otf"
 		"font/five_minutes.otf"
 	};
 	};
 
 
-	const uint32_t fontCount = sizeof(fontNames) / sizeof(const char*);
+	const uint32_t fontCount = countof(fontNames);
 
 
 	TrueTypeHandle fontFiles[fontCount];
 	TrueTypeHandle fontFiles[fontCount];
 	FontHandle fonts[fontCount];
 	FontHandle fonts[fontCount];
 	for (uint32_t ii = 0; ii < fontCount; ++ii)
 	for (uint32_t ii = 0; ii < fontCount; ++ii)
 	{
 	{
-		//instantiate a usable font
+		// Instantiate a usable font.
 		fontFiles[ii] = fontManager->loadTrueTypeFromFile(fontNames[ii]);
 		fontFiles[ii] = fontManager->loadTrueTypeFromFile(fontNames[ii]);
 		fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
 		fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
-		//preload glyphs and blit them to atlas
+
+		// Preload glyphs and blit them to atlas.
 		fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
 		fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
-		//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 glyph
+
+		// 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 
+		// glyph.
 		fontManager->unloadTrueType(fontFiles[ii]);
 		fontManager->unloadTrueType(fontFiles[ii]);
 	}
 	}
 
 
 	TrueTypeHandle console_tt = fontManager->loadTrueTypeFromFile("font/visitor1.ttf");
 	TrueTypeHandle console_tt = fontManager->loadTrueTypeFromFile("font/visitor1.ttf");
 
 
-	//this font doesn't have any preloaded glyph's but the truetype file is loaded
-	//so glyph will be generated as needed
+	// This font doesn't have any preloaded glyph's but the truetype file 
+	// is loaded so glyph will be generated as needed.
 	FontHandle consola_16 = fontManager->createFontByPixelSize(console_tt, 0, 10);
 	FontHandle consola_16 = fontManager->createFontByPixelSize(console_tt, 0, 10);
 
 
 	//create a static text buffer compatible with alpha font
 	//create a static text buffer compatible with alpha font
 	//a static text buffer content cannot be modified after its first submit.
 	//a static text buffer content cannot be modified after its first submit.
 	TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, STATIC);
 	TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, STATIC);
 
 
-	//the pen position represent the top left of the box of the first line of text
+	// The pen position represent the top left of the box of the first line 
+	// of text.
 	textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
 	textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
 
 
 	for (uint32_t ii = 0; ii < fontCount; ++ii)
 	for (uint32_t ii = 0; ii < fontCount; ++ii)
 	{
 	{
-		//add some text to the buffer
+		// Add some text to the buffer.
+		// The position of the pen is adjusted when there is an endline.
 		textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
 		textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
-		//the position of the pen is adjusted when there is an endline
 	}
 	}
 
 
-	// Now write some styled text
+	// Now write some styled text.
 
 
-	//setup style colors
+	// Setup style colors.
 	textBufferManager->setBackgroundColor(staticText, 0x551111FF);
 	textBufferManager->setBackgroundColor(staticText, 0x551111FF);
 	textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
 	textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
 	textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
 	textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
 	textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
 	textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
 
 
-	//text + bkg
+	// Background.
 	textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
 	textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
 	textBufferManager->appendText(staticText, fonts[0], L"The quick ");
 	textBufferManager->appendText(staticText, fonts[0], L"The quick ");
 
 
-	//text + strike-through
+	// Strike-through.
 	textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
 	textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
 	textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
 	textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
 
 
-	//text + overline
+	// Overline.
 	textBufferManager->setStyle(staticText, STYLE_OVERLINE);
 	textBufferManager->setStyle(staticText, STYLE_OVERLINE);
 	textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
 	textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
 
 
-	//text + underline
+	// Underline.
 	textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
 	textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
 	textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
 	textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
 
 
-	//text + bkg + strike-through
+	// Background + strike-through.
 	textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
 	textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
 	textBufferManager->appendText(staticText, fonts[0], L"dog\n");
 	textBufferManager->appendText(staticText, fonts[0], L"dog\n");
 
 
-	//create a transient buffer for realtime data
+	// Create a transient buffer for real-time data.
 	TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, TRANSIENT);
 	TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, TRANSIENT);
 
 
-	uint32_t w = 0, h = 0;
 	while (!processEvents(width, height, debug, reset) )
 	while (!processEvents(width, height, debug, reset) )
 	{
 	{
-		if (w != width
-		   || h != height)
-		{
-			w = width;
-			h = height;
-			printf("ri: %d,%d\n", width, height);
-		}
-
 		// Set view 0 default viewport.
 		// Set view 0 default viewport.
 		bgfx::setViewRect(0, 0, 0, width, height);
 		bgfx::setViewRect(0, 0, 0, width, height);
 
 
@@ -156,7 +152,6 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		//Use transient text to display debug information
 		//Use transient text to display debug information
 		//Code below is similar to commented code above
 		//Code below is similar to commented code above
 		wchar_t fpsText[64];
 		wchar_t fpsText[64];
-		//swprintf(fpsText,L"Frame: % 7.3f[ms]", double(frameTime)*toMs);
 		swprintf(fpsText, countof(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
 		swprintf(fpsText, countof(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
 
 
 		textBufferManager->clearTextBuffer(transientText);
 		textBufferManager->clearTextBuffer(transientText);
@@ -178,10 +173,10 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		// Set view and projection matrix for view 0.
 		// Set view and projection matrix for view 0.
 		bgfx::setViewTransform(0, view, proj);
 		bgfx::setViewTransform(0, view, proj);
 
 
-		//submit the debug text
+		// Submit the debug text.
 		textBufferManager->submitTextBuffer(transientText, 0);
 		textBufferManager->submitTextBuffer(transientText, 0);
 
 
-		//submit the static text
+		// Submit the static text.
 		textBufferManager->submitTextBuffer(staticText, 0);
 		textBufferManager->submitTextBuffer(staticText, 0);
 
 
 		// Advance to next frame. Rendering thread will be kicked to
 		// Advance to next frame. Rendering thread will be kicked to
@@ -190,7 +185,8 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 	}
 	}
 
 
 	fontManager->unloadTrueType(console_tt);
 	fontManager->unloadTrueType(console_tt);
-	//destroy the fonts
+
+	// Destroy the fonts.
 	fontManager->destroyFont(consola_16);
 	fontManager->destroyFont(consola_16);
 	for (uint32_t ii = 0; ii < fontCount; ++ii)
 	for (uint32_t ii = 0; ii < fontCount; ++ii)
 	{
 	{

+ 21 - 31
examples/11-fontsdf/fontsdf.cpp

@@ -1,7 +1,7 @@
 /*
 /*
- * Copyright 2013 Jeremie Roy. All rights reserved.
- * License: http://www.opensource.org/licenses/BSD-2-Clause
- */
+* Copyright 2013 Jeremie Roy. All rights reserved.
+* License: http://www.opensource.org/licenses/BSD-2-Clause
+*/
 
 
 #include <bgfx.h>
 #include <bgfx.h>
 #include <bx/bx.h>
 #include <bx/bx.h>
@@ -48,46 +48,35 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 
 
 	// Set view 0 clear state.
 	// Set view 0 clear state.
 	bgfx::setViewClear(0
 	bgfx::setViewClear(0
-	                  , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
-	                   //, 0x303030ff
-	                   //, 0xffffffff
-	                  , 0x000000FF
-	                  , 1.0f
-	                  , 0
-	                  );
-
-	//init the text rendering system
+		, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
+		, 0x303030ff
+		, 1.0f
+		, 0
+		);
+
+	// Init the text rendering system.
 	FontManager* fontManager = new FontManager(512);
 	FontManager* fontManager = new FontManager(512);
 	TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
 	TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
 
 
-	//load a truetype files
-	/*
-	"font/droidsans.ttf",
-	"font/chp-fire.ttf",
-	"font/bleeding_cowboys.ttf",
-	"font/mias_scribblings.ttf",
-	"font/ruritania.ttf",
-	"font/signika-regular.ttf",
-	"font/five_minutes.otf"
-	*/
 	TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("font/bleeding_cowboys.ttf");
 	TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("font/bleeding_cowboys.ttf");
 
 
-	//create a distance field font
+	// Create a distance field font.
 	FontHandle distance_font = fontManager->createFontByPixelSize(times_tt, 0, 48, FONT_TYPE_DISTANCE);
 	FontHandle distance_font = fontManager->createFontByPixelSize(times_tt, 0, 48, FONT_TYPE_DISTANCE);
-	//create a scalled down version of the same font (without adding anything to the atlas)
+
+	// Create a scalled down version of the same font (without adding 
+	// anything to the atlas).
 	FontHandle smaller_font = fontManager->createScaledFontToPixelSize(distance_font, 32);
 	FontHandle smaller_font = fontManager->createScaledFontToPixelSize(distance_font, 32);
 
 
-	//preload glyph and generate (generate bitmap's)
+	// Preload glyph and generate (generate bitmap's).
 	fontManager->preloadGlyph(distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,\" \n");
 	fontManager->preloadGlyph(distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,\" \n");
 
 
-	//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 glyph
+	// You can unload the TTF files at this stage, but in that case, the 
+	// set of glyph's will be limited to the set of preloaded glyph.
 	fontManager->unloadTrueType(times_tt);
 	fontManager->unloadTrueType(times_tt);
 
 
 	TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, STATIC);
 	TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, STATIC);
 	textBufferManager->setTextColor(staticText, 0xDD0000FF);
 	textBufferManager->setTextColor(staticText, 0xDD0000FF);
 
 
-	//textBufferManager->appendText(staticText, distance_font, L"The quick brown fox jumps over the lazy dog\n");
-	//textBufferManager->appendText(staticText, distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
 	textBufferManager->appendText(staticText, distance_font, L"BGFX ");
 	textBufferManager->appendText(staticText, distance_font, L"BGFX ");
 	textBufferManager->appendText(staticText, smaller_font, L"bgfx");
 	textBufferManager->appendText(staticText, smaller_font, L"bgfx");
 
 
@@ -122,7 +111,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		float proj[16];
 		float proj[16];
 		mtxLookAt(view, eye, at);
 		mtxLookAt(view, eye, at);
 		float centering = 0.5f;
 		float centering = 0.5f;
-		//setup a top-left ortho matrix for screen space drawing
+		// Setup a top-left ortho matrix for screen space drawing.
 		mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
 		mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
 
 
 		// Set view and projection matrix for view 0.
 		// Set view and projection matrix for view 0.
@@ -147,7 +136,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		// Set model matrix for rendering.
 		// Set model matrix for rendering.
 		bgfx::setTransform(mtxA);
 		bgfx::setTransform(mtxA);
 
 
-		//draw your text
+		// Draw your text.
 		textBufferManager->submitTextBuffer(staticText, 0);
 		textBufferManager->submitTextBuffer(staticText, 0);
 
 
 		// Advance to next frame. Rendering thread will be kicked to
 		// Advance to next frame. Rendering thread will be kicked to
@@ -155,7 +144,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		bgfx::frame();
 		bgfx::frame();
 	}
 	}
 
 
-	//destroy the fonts
+	// Destroy the fonts.
 	fontManager->destroyFont(distance_font);
 	fontManager->destroyFont(distance_font);
 	fontManager->destroyFont(smaller_font);
 	fontManager->destroyFont(smaller_font);
 
 
@@ -163,6 +152,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 
 
 	delete textBufferManager;
 	delete textBufferManager;
 	delete fontManager;
 	delete fontManager;
+
 	// Shutdown bgfx.
 	// Shutdown bgfx.
 	bgfx::shutdown();
 	bgfx::shutdown();