Przeglądaj źródła

convert assert to BX_CHECK

Jeremie Roy 12 lat temu
rodzic
commit
d4a695a181

+ 14 - 10
examples/common/cube_atlas.cpp

@@ -2,10 +2,11 @@
  * License: http://www.opensource.org/licenses/BSD-2-Clause
 */
 #pragma once
+#include "cube_atlas.h"
+#include <bx/bx.h>
 #include <bgfx.h> 
-#include <assert.h>
 #include <vector>
-#include "cube_atlas.h"
+
 
 //********** Rectangle packer implementation ************
 class RectanglePacker
@@ -68,8 +69,8 @@ RectanglePacker::RectanglePacker(uint32_t _width, uint32_t _height):m_width(_wid
 
 void RectanglePacker::init(uint32_t _width, uint32_t _height)
 {
-	assert(_width > 2);
-	assert(_height > 2);
+	BX_CHECK(_width > 2, "_width must be > 2");
+	BX_CHECK(_height > 2, "_height must be > 2");
 	m_width = _width;
 	m_height = _height;
 	m_usedSpace = 0;
@@ -231,8 +232,8 @@ struct Atlas::PackedLayer
 
 Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount )
 {
-	assert(_textureSize >= 64 && _textureSize <= 4096 && "suspicious texture size" );
-	assert(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000 && "suspicious _regions count" );
+	BX_CHECK(_textureSize >= 64 && _textureSize <= 4096, "suspicious texture size" );
+	BX_CHECK(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "suspicious _regions count" );
 	m_layers = new PackedLayer[24];
 	for(int ii=0; ii<24;++ii)
 	{
@@ -267,7 +268,7 @@ Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount )
 
 Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer , uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount)
 {
-	assert(_regionCount <= 64 && _maxRegionsCount <= 4096);
+	BX_CHECK(_regionCount <= 64 && _maxRegionsCount <= 4096, "suspicious initialization");
 	//layers are frozen
 	m_usedLayers = 24;
 	m_usedFaces = 6;
@@ -275,7 +276,10 @@ Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer , uint16_t _re
 	m_textureSize = _textureSize;
 	m_regionCount = _regionCount;
 	//regions are frozen
-	m_maxRegionCount = _regionCount;
+	if(_regionCount < _maxRegionsCount)
+		m_maxRegionCount = _regionCount;
+	else
+		m_maxRegionCount = _maxRegionsCount;
 	m_regions = new AtlasRegion[_regionCount];
 	m_textureBuffer = new uint8_t[getTextureBufferSize()];
 	
@@ -309,7 +313,7 @@ uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bit
 		return UINT16_MAX;
 	}
 	
-	uint16_t x,y;
+	uint16_t x=0,y=0;
 	// We want each bitmap to be separated by at least one black pixel
 	// TODO manage mipmaps
 	uint32_t idx = 0;
@@ -378,7 +382,7 @@ void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffe
 	}else
 	{
 		uint32_t layer = _region.getComponentIndex();
-		uint32_t face = _region.getFaceIndex();
+		//uint32_t face = _region.getFaceIndex();
 		const uint8_t* inLineBuffer = _bitmapBuffer;
 		uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize*m_textureSize*4) + (((_region.m_y *m_textureSize)+_region.m_x)*4));
 		

+ 31 - 30
examples/common/font/font_manager.cpp

@@ -15,7 +15,7 @@
 #include "../../../3rdparty/edtaa3/edtaa3func.h"
 #include "../../../3rdparty/edtaa3/edtaa3func.cpp"
 #include <math.h>
-#include <assert.h>
+#include <bx/bx.h>
 
 
 #if BGFX_CONFIG_USE_TINYSTL
@@ -96,10 +96,10 @@ FontManager::TrueTypeFont::~TrueTypeFont()
 
 bool FontManager::TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight)
 {
-	assert((_bufferSize > 256 && _bufferSize < 100000000) && "TrueType buffer size is suspicious");
-	assert((_pixelHeight > 4 && _pixelHeight < 128) && "TrueType buffer size is suspicious");
+	BX_CHECK((_bufferSize > 256 && _bufferSize < 100000000), "TrueType buffer size is suspicious");
+	BX_CHECK((_pixelHeight > 4 && _pixelHeight < 128), "TrueType buffer size is suspicious");
 	
-	assert(m_font == NULL && "TrueTypeFont already initialized" );
+	BX_CHECK(m_font == NULL, "TrueTypeFont already initialized" );
 	
 	FTHolder* holder = new FTHolder();	
 
@@ -152,14 +152,15 @@ bool FontManager::TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSiz
 
 FontInfo FontManager::TrueTypeFont::getFontInfo()
 {
-	assert(m_font != NULL && "TrueTypeFont not initialized" );
+	BX_CHECK(m_font != NULL, "TrueTypeFont not initialized" );
 	FTHolder* holder = (FTHolder*) m_font;
 	
-	assert(FT_IS_SCALABLE (holder->m_face));
+	//todo manage unscalable font
+	BX_CHECK(FT_IS_SCALABLE (holder->m_face), "Font is unscalable");
 
 	FT_Size_Metrics metrics = holder->m_face->size->metrics;
 
-	//todo manage unscalable font
+	
 	FontInfo outFontInfo;
 	outFontInfo.m_scale = 1.0f;
 	outFontInfo.m_ascender = metrics.ascender /64.0f;
@@ -173,7 +174,7 @@ FontInfo FontManager::TrueTypeFont::getFontInfo()
 
 bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
 {	
-	assert(m_font != NULL && "TrueTypeFont not initialized" );
+	BX_CHECK(m_font != NULL, "TrueTypeFont not initialized" );
 	FTHolder* holder = (FTHolder*) m_font;
 	
 	_glyphInfo.m_glyphIndex = FT_Get_Char_Index( holder->m_face, _codePoint );
@@ -217,7 +218,7 @@ bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo
 
 bool FontManager::TrueTypeFont::bakeGlyphSubpixel(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
 {
-	assert(m_font != NULL && "TrueTypeFont not initialized" );
+	BX_CHECK(m_font != NULL, "TrueTypeFont not initialized" );
 	FTHolder* holder = (FTHolder*) m_font;
 	
 	_glyphInfo.m_glyphIndex = FT_Get_Char_Index( holder->m_face, _codePoint );
@@ -337,7 +338,7 @@ void make_distance_map( unsigned char *img, unsigned char *outImg, unsigned int
 
 bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
 {	
-	assert(m_font != NULL && "TrueTypeFont not initialized" );
+	BX_CHECK(m_font != NULL, "TrueTypeFont not initialized" );
 	FTHolder* holder = (FTHolder*) m_font;
 	
 	_glyphInfo.m_glyphIndex = FT_Get_Char_Index( holder->m_face, _codePoint );
@@ -391,7 +392,7 @@ bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t _codePoint, GlyphI
 	
 		uint32_t nw = w + dw*2;
 		uint32_t nh = h + dh*2;
-		assert(nw*nh < 128*128);
+		BX_CHECK(nw*nh < 128*128, "buffer overflow");
 		uint32_t buffSize = nw*nh*sizeof(uint8_t);
 	
 		uint8_t * alphaImg = (uint8_t *)  malloc( buffSize );
@@ -465,7 +466,7 @@ void FontManager::init()
 
 	m_blackGlyph.m_width=3;
 	m_blackGlyph.m_height=3;
-	assert( addBitmap(m_blackGlyph, buffer) );
+	BX_CHECK( addBitmap(m_blackGlyph, buffer), "unable to add white glyph" );
 	//make sure the black glyph doesn't bleed
 	
 	/*int16_t texUnit = 65535 / m_textureWidth;
@@ -478,10 +479,10 @@ void FontManager::init()
 
 FontManager::~FontManager()
 {
-	assert(m_fontHandles.getNumHandles() == 0 && "All the fonts must be destroyed before destroying the manager");
+	BX_CHECK(m_fontHandles.getNumHandles() == 0, "All the fonts must be destroyed before destroying the manager");
 	delete [] m_cachedFonts;
 
-	assert(m_filesHandles.getNumHandles() == 0 && "All the font files must be destroyed before destroying the manager");
+	BX_CHECK(m_filesHandles.getNumHandles() == 0, "All the font files must be destroyed before destroying the manager");
 	delete [] m_cachedFiles;
 	
 	delete [] m_buffer;
@@ -533,7 +534,7 @@ TrueTypeHandle FontManager::loadTrueTypeFromFile(const char* _fontPath)
 		fclose(pFile);
 
 		uint16_t id = m_filesHandles.alloc();
-		assert(id != bx::HandleAlloc::invalid);
+		BX_CHECK(id != bx::HandleAlloc::invalid, "No more room for files");
 		m_cachedFiles[id].buffer = buffer;
 		m_cachedFiles[id].bufferSize = bufsize;
 		TrueTypeHandle ret = {id};
@@ -546,8 +547,8 @@ TrueTypeHandle FontManager::loadTrueTypeFromFile(const char* _fontPath)
 
 TrueTypeHandle FontManager::loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size)
 {	
-	uint16_t id = m_filesHandles.alloc();
-	assert(id != bx::HandleAlloc::invalid);
+	uint16_t id = m_filesHandles.alloc();	
+	BX_CHECK(id != bx::HandleAlloc::invalid, "Invalid handle used");
 	m_cachedFiles[id].buffer = new uint8_t[_size];
 	m_cachedFiles[id].bufferSize = _size;
 	memcpy(m_cachedFiles[id].buffer, _buffer, _size);
@@ -559,7 +560,7 @@ TrueTypeHandle FontManager::loadTrueTypeFromMemory(const uint8_t* _buffer, uint3
 
 void FontManager::unloadTrueType(TrueTypeHandle _handle)
 {
-	assert(bgfx::invalidHandle != _handle.idx);
+	BX_CHECK(bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	delete m_cachedFiles[_handle.idx].buffer;
 	m_cachedFiles[_handle.idx].bufferSize = 0;
 	m_cachedFiles[_handle.idx].buffer = NULL;
@@ -568,7 +569,7 @@ void FontManager::unloadTrueType(TrueTypeHandle _handle)
 
 FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _tt_handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType)
 {
-	assert(bgfx::invalidHandle != _tt_handle.idx);
+	BX_CHECK(bgfx::invalidHandle != _tt_handle.idx, "Invalid handle used");
 
 	TrueTypeFont* ttf = new TrueTypeFont();
 	if(!ttf->init(  m_cachedFiles[_tt_handle.idx].buffer,  m_cachedFiles[_tt_handle.idx].bufferSize, _typefaceIndex, _pixelSize))
@@ -579,7 +580,7 @@ FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _tt_handle, uint32_
 	}
 	
 	uint16_t fontIdx = m_fontHandles.alloc();
-	assert(fontIdx != bx::HandleAlloc::invalid);	
+	BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");	
 	
 	m_cachedFonts[fontIdx].m_trueTypeFont = ttf;
 	m_cachedFonts[fontIdx].m_fontInfo = ttf->getFontInfo();
@@ -593,7 +594,7 @@ FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _tt_handle, uint32_
 
 FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize)
 {
-	assert(bgfx::invalidHandle != _baseFontHandle.idx);
+	BX_CHECK(bgfx::invalidHandle != _baseFontHandle.idx, "Invalid handle used");
 	CachedFont& font = m_cachedFonts[_baseFontHandle.idx];
 	FontInfo& fontInfo = font.m_fontInfo;
 
@@ -608,7 +609,7 @@ FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle,
 
 
 	uint16_t fontIdx = m_fontHandles.alloc();
-	assert(fontIdx != bx::HandleAlloc::invalid);
+	BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");
 	m_cachedFonts[fontIdx].m_cachedGlyphs.clear();
 	m_cachedFonts[fontIdx].m_fontInfo = newFontInfo;
 	m_cachedFonts[fontIdx].m_trueTypeFont = NULL;
@@ -619,21 +620,21 @@ FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle,
 
 FontHandle FontManager::loadBakedFontFromFile(const char* /*fontPath*/,  const char* /*descriptorPath*/)
 {
-	assert(false); //TODO implement
+	//assert(false); //TODO implement
 	FontHandle invalid = BGFX_INVALID_HANDLE;
 	return invalid;
 }
 
 FontHandle FontManager::loadBakedFontFromMemory(const uint8_t* /*imageBuffer*/, uint32_t /*imageSize*/, const uint8_t* /*descriptorBuffer*/, uint32_t /*descriptorSize*/)
 {
-	assert(false); //TODO implement
+	//assert(false); //TODO implement
 	FontHandle invalid = BGFX_INVALID_HANDLE;
 	return invalid;
 }
 
 void FontManager::destroyFont(FontHandle _handle)
 {
-	assert(bgfx::invalidHandle != _handle.idx);
+	BX_CHECK(bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 
 	if(m_cachedFonts[_handle.idx].m_trueTypeFont != NULL)
 	{
@@ -645,8 +646,8 @@ void FontManager::destroyFont(FontHandle _handle)
 }
 
 bool FontManager::preloadGlyph(FontHandle _handle, const wchar_t* _string)
-{
-	assert(bgfx::invalidHandle != _handle.idx);
+{	
+	BX_CHECK(bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	CachedFont& font = m_cachedFonts[_handle.idx];
 
 	//if truetype present
@@ -670,7 +671,7 @@ bool FontManager::preloadGlyph(FontHandle _handle, const wchar_t* _string)
 
 bool FontManager::preloadGlyph(FontHandle _handle, CodePoint_t _codePoint)
 {
-	assert(bgfx::invalidHandle != _handle.idx);
+	BX_CHECK(bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	CachedFont& font = m_cachedFonts[_handle.idx];
 	FontInfo& fontInfo = font.m_fontInfo;
 	//check if glyph not already present
@@ -701,7 +702,7 @@ bool FontManager::preloadGlyph(FontHandle _handle, CodePoint_t _codePoint)
 			font.m_trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
 			break;
 		default:
-			assert(false && "TextureType not supported yet");
+			BX_CHECK(false, "TextureType not supported yet");
 		};
 
 		//copy bitmap to texture
@@ -749,7 +750,7 @@ bool FontManager::preloadGlyph(FontHandle _handle, CodePoint_t _codePoint)
 
 const FontInfo& FontManager::getFontInfo(FontHandle _handle)
 { 
-	assert(_handle.idx != bgfx::invalidHandle);
+	BX_CHECK(bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	return m_cachedFonts[_handle.idx].m_fontInfo;
 }
 

+ 17 - 17
examples/common/font/text_buffer_manager.cpp

@@ -4,7 +4,7 @@
 #include "text_buffer_manager.h"
 #include "../cube_atlas.h"
 
-#include <assert.h>
+#include <bx/bx.h>
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
@@ -261,7 +261,7 @@ void TextBuffer::appendText(FontHandle _fontHandle, const char * _string)
 				appendGlyph((CodePoint_t)codepoint, font, glyph);
 			}else
 			{
-				assert(false && "Glyph not found");
+				BX_CHECK(false, "Glyph not found");
 			}
 		}
 	  //printf("U+%04X\n", codepoint);
@@ -297,7 +297,7 @@ void TextBuffer::appendText(FontHandle _fontHandle, const wchar_t * _string)
 			appendGlyph(_codePoint, font, glyph);
 		}else
 		{
-			assert(false && "Glyph not found");
+			BX_CHECK(false, "Glyph not found");
 		}
 	}
 }
@@ -515,7 +515,7 @@ TextBufferManager::TextBufferManager(FontManager* _fontManager):m_fontManager(_f
 
 TextBufferManager::~TextBufferManager()
 {
-	assert(m_textBufferHandles.getNumHandles() == 0 && "All the text buffers must be destroyed before destroying the manager");
+	BX_CHECK(m_textBufferHandles.getNumHandles() == 0, "All the text buffers must be destroyed before destroying the manager");
 	delete[] m_textBuffers;
 
 	bgfx::destroyUniform(m_u_texColor);
@@ -580,7 +580,7 @@ TextBufferHandle TextBufferManager::createTextBuffer(FontType _type, BufferType
 
 void TextBufferManager::destroyTextBuffer(TextBufferHandle _handle)
 {	
-	assert( bgfx::invalidHandle != _handle.idx);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	m_textBufferHandles.free(_handle.idx);
@@ -618,7 +618,7 @@ void TextBufferManager::destroyTextBuffer(TextBufferHandle _handle)
 
 void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id, int32_t _depth)
 {
-	assert(bgfx::invalidHandle != _handle.idx);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	
 	uint32_t indexSize = bc.m_textBuffer->getIndexCount() * bc.m_textBuffer->getIndexSize();
@@ -725,75 +725,75 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id,
 void TextBufferManager::submitTextBufferMask(TextBufferHandle /*_handle*/, uint32_t /*_viewMask*/, int32_t /*_depth*/)
 {
 	//TODO
-	assert(false);
+	BX_CHECK(false, "TODO TODO");
 }
 
 void TextBufferManager::setStyle(TextBufferHandle _handle, uint32_t _flags ) 
 { 
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setStyle(_flags); 
 }
 
 void TextBufferManager::setTextColor(TextBufferHandle _handle, uint32_t _rgba ) 
 { 
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setTextColor(_rgba); 
 }
 
 void TextBufferManager::setBackgroundColor(TextBufferHandle _handle, uint32_t _rgba ) 
 { 
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setBackgroundColor(_rgba); 
 }
 
 void TextBufferManager::setOverlineColor(TextBufferHandle _handle, uint32_t _rgba ) 
 { 
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setOverlineColor(_rgba); 
 }
 
 void TextBufferManager::setUnderlineColor(TextBufferHandle _handle, uint32_t _rgba ) 
 {
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setUnderlineColor(_rgba); 
 }
 
 void TextBufferManager::setStrikeThroughColor(TextBufferHandle _handle, uint32_t _rgba ) 
 { 
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setStrikeThroughColor(_rgba); 
 }
 	
 void TextBufferManager::setPenPosition(TextBufferHandle _handle, float _x, float _y) 
 {
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	 bc.m_textBuffer->setPenPosition(_x,_y); 
 }
 
 void TextBufferManager::appendText(TextBufferHandle _handle, FontHandle _fontHandle, const char * _string)
 {
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	bc.m_textBuffer->appendText(_fontHandle, _string);
 }
 
 void TextBufferManager::appendText(TextBufferHandle _handle, FontHandle _fontHandle, const wchar_t * _string)
 {
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	bc.m_textBuffer->appendText(_fontHandle, _string);
 }
 
 void TextBufferManager::clearTextBuffer(TextBufferHandle _handle)
 {
-	assert( _handle.idx != bgfx::invalidHandle);
+	BX_CHECK( bgfx::invalidHandle != _handle.idx, "Invalid handle used");
 	BufferCache& bc = m_textBuffers[_handle.idx];
 	bc.m_textBuffer->clearTextBuffer();
 }