DOMArrayPiece::DOMArrayPiece(const ArrayBufferOrArrayBufferView& arrayBufferOrView, InitWithUnionOption option)
{
    if (arrayBufferOrView.isArrayBuffer()) {
        RefPtr<DOMArrayBuffer> arrayBuffer = arrayBufferOrView.getAsArrayBuffer();
        initWithData(arrayBuffer->data(), arrayBuffer->byteLength());
    } else if (arrayBufferOrView.isArrayBufferView()) {
        RefPtr<DOMArrayBufferView> arrayBufferView =
            arrayBufferOrView.getAsArrayBufferView();
        initWithData(arrayBufferView->baseAddress(), arrayBufferView->byteLength());
    } else if (arrayBufferOrView.isNull() && option == AllowNullPointToNullWithZeroSize) {
        initWithData(nullptr, 0);
    } // Otherwise, leave the obejct as null.
}
Exemplo n.º 2
0
void FontAtlas::reinit()
{
    if (_currentPageData)
    {
        delete []_currentPageData;
        _currentPageData = nullptr;
    }
    
    auto texture = new (std::nothrow) Texture2D;
    
    _currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
    
    auto outlineSize = _fontFreeType->getOutlineSize();
    if(outlineSize > 0)
    {
        _lineHeight += 2 * outlineSize;
        _currentPageDataSize *= 2;
    }
    
    _currentPageData = new (std::nothrow) unsigned char[_currentPageDataSize];
    memset(_currentPageData, 0, _currentPageDataSize);
    
    auto  pixelFormat = outlineSize > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
    texture->initWithData(_currentPageData, _currentPageDataSize,
                          pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
    
    addTexture(texture,0);
    texture->release();
}
Exemplo n.º 3
0
bool Texture::init(const std::string & fileName)
{
    destroy();

    stbi_set_flip_vertically_on_load(true);
    int width, height, components;
    uint8_t * imageData = stbi_load(fileName.c_str(), &width, &height, &components, STBI_rgb_alpha);
    if (imageData == nullptr)
    {
        common::Logger::toLogWithFormat("Error: could not load file '%s'.\n", fileName.c_str());
        return false;
    }

    m_format = findFormat(components);
    if (m_format == -1)
    {
        common::Logger::toLogWithFormat("Error: format of file '%s' is unknown.\n", fileName.c_str());
        if (imageData != nullptr)
            stbi_image_free(imageData);
        return false;
    }

    bool result = initWithData(m_format, imageData, width, height, true, -1);

    if (imageData != nullptr)
        stbi_image_free(imageData);

    return result;
}
Exemplo n.º 4
0
ArrayPiece::ArrayPiece(ArrayBuffer* buffer)
{
    if (buffer) {
        initWithData(buffer->data(), buffer->byteLength());
    } else {
        initNull();
    }
}
Exemplo n.º 5
0
ArrayPiece::ArrayPiece(ArrayBufferView* buffer)
{
    if (buffer) {
        initWithData(buffer->baseAddress(), buffer->byteLength());
    } else {
        initNull();
    }
}
CSSM_RETURN CertParser::initWithCFData(
	CFDataRef			cfData)
{
	CSSM_DATA   cdata;
	
	cdata.Data = (uint8 *)CFDataGetBytePtr(cfData);
	cdata.Length = CFDataGetLength(cfData);
	return initWithData(cdata);
}
Exemplo n.º 7
0
bool BitmapData::initWithFile(const std::string &path) {
    _filePath = FileUtils::getInstance()->getFullPath(path);
    ByteArray *data = FileUtils::getInstance()->getFileData(_filePath, "rb");
    bool result = false;
    if (!data->isNull()) {
        result = initWithData(data->getBytes(), data->getLength());
    }
    delete data;
    return result;
}
Exemplo n.º 8
0
    void assignArray(const StaticArray<T>& array) {
      _size = 0;

      if (_data) {
        delete[] _data;
        _data = 0;
      }

      initWithData(array.cArray(), array.size());
    }
Exemplo n.º 9
0
FontAtlas::FontAtlas(Font &theFont) 
: _font(&theFont)
, _fontFreeType(nullptr)
, _iconv(nullptr)
, _currentPageData(nullptr)
, _fontAscender(0)
//, _rendererRecreatedListener(nullptr)
, _antialiasEnabled(true)
, _currLineHeight(0)
{
    _font->retain();

    _fontFreeType = dynamic_cast<FontFreeType*>(_font);
    if (_fontFreeType)
    {
        _lineHeight = _font->getFontMaxHeight();
        _fontAscender = _fontFreeType->getFontAscender();
        auto texture = new (std::nothrow) Texture2D;
        _currentPage = 0;
        _currentPageOrigX = 0;
        _currentPageOrigY = 0;
        _letterEdgeExtend = 2;
        _letterPadding = 0;

        if (_fontFreeType->isDistanceFieldEnabled())
        {
            _letterPadding += 2 * FontFreeType::DistanceMapSpread;    
        }
        _currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
        auto outlineSize = _fontFreeType->getOutlineSize();
        if(outlineSize > 0)
        {
            _lineHeight += 2 * outlineSize;
            _currentPageDataSize *= 2;
        }

        _currentPageData = new unsigned char[_currentPageDataSize];
        memset(_currentPageData, 0, _currentPageDataSize);

        auto  pixelFormat = outlineSize > 0 ? CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88 : CCTexture2DPixelFormat::kCCTexture2DPixelFormat_A8;
        texture->initWithData(_currentPageData, _currentPageDataSize, 
            pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );

        addTexture(texture,0);
        texture->release();

#if CC_ENABLE_CACHE_TEXTURE_DATA
        auto eventDispatcher = Director::getInstance()->getEventDispatcher();

        _rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(FontAtlas::listenRendererRecreated, this));
        eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1);
#endif
    }
}
// TODO: need to handle exception here!
bool StageDataManager::initFromLocalData(const std::string &stageDataFile,
                                         const std::string &scoreRocordsFile)
{
    auto stageData = StageData::loadStageDataFromCSV(stageDataFile);
    auto scoreRecord = StageScoreRecord::loadStageScoreRecordFromCSV(scoreRocordsFile,
                                                                     stageData);
    
    _scoreRecordsFilename = scoreRocordsFile;
    
    return initWithData(stageData, scoreRecord);
}
CertParser::CertParser(
	CSSM_CL_HANDLE		clHand,
	const CSSM_DATA 	&certData)
{
	initFields();
	mClHand = clHand;
	CSSM_RETURN crtn = initWithData(certData);
	if(crtn) {
		throw ((int)crtn);
	}
}
Exemplo n.º 12
0
void DynamicSprite::setTexture(const Bitmap &bmp, const Rect &rect) {
	Rect texRect;
	if (rect.equals(Rect::ZERO)) {
		texRect = cocos2d::Rect(0, 0, bmp.width(), bmp.height());
	} else {
		texRect = rect;
	}

	auto tex = Rc<cocos2d::Texture2D>::alloc();
	tex->initWithData(bmp.dataPtr(), bmp.size(), TextureCache::getPixelFormat(bmp.format()), bmp.width(), bmp.height());

	setTexture(tex, texRect);
}
Exemplo n.º 13
0
DOMArrayPiece::DOMArrayPiece(const BufferSource& bufferSource)
{
    void* data = nullptr;
    unsigned len = 0;

    if (bufferSource.isArrayBuffer()) {
        data = bufferSource.getAsArrayBuffer()->data();
        len = bufferSource.getAsArrayBuffer()->byteLength();
    } else if (bufferSource.isArrayBufferView()) {
        data = bufferSource.getAsArrayBufferView()->baseAddress();
        len = bufferSource.getAsArrayBufferView()->byteLength();
    }
    initWithData(data, len);
}
Exemplo n.º 14
0
FontAtlas::FontAtlas(Font &theFont) 
: _font(&theFont)
, _currentPageData(nullptr)
, _fontAscender(0)
, _toForegroundListener(nullptr)
, _toBackgroundListener(nullptr)
, _antialiasEnabled(true)
{
    _font->retain();

    FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
    if (fontTTf)
    {
        _commonLineHeight = _font->getFontMaxHeight();
        _fontAscender = fontTTf->getFontAscender();
        auto texture = new Texture2D;
        _currentPage = 0;
        _currentPageOrigX = 0;
        _currentPageOrigY = 0;
        _letterPadding = 0;

        if(fontTTf->isDistanceFieldEnabled())
        {
            _letterPadding += 2 * FontFreeType::DistanceMapSpread;    
        }
        _currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
        if(fontTTf->getOutlineSize() > 0)
        {
            _currentPageDataSize *= 2;
        }    

        _currentPageData = new unsigned char[_currentPageDataSize];
        memset(_currentPageData, 0, _currentPageDataSize);

        auto  pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; 
        texture->initWithData(_currentPageData, _currentPageDataSize, 
            pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );

        addTexture(texture,0);
        texture->release();
#if CC_ENABLE_CACHE_TEXTURE_DATA
        auto eventDispatcher = Director::getInstance()->getEventDispatcher();
        _toBackgroundListener = EventListenerCustom::create(EVENT_COME_TO_BACKGROUND, CC_CALLBACK_1(FontAtlas::listenToBackground, this));
        eventDispatcher->addEventListenerWithFixedPriority(_toBackgroundListener, 1);
        _toForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(FontAtlas::listenToForeground, this));
        eventDispatcher->addEventListenerWithFixedPriority(_toForegroundListener, 1);
#endif
    }
}
OSStatus CertParser::initWithSecCert(
	SecCertificateRef 	secCert)
{
	OSStatus ortn;
	CSSM_DATA certData;
	
	assert(mClHand == 0);
	ortn = SecCertificateGetCLHandle(secCert, &mClHand);
	if(ortn) {
		return ortn;
	}
	ortn = SecCertificateGetData(secCert, &certData);
	if(ortn) {
		return ortn;
	}
	return (OSStatus)initWithData(certData);
}
Exemplo n.º 16
0
SoundBuffer::SoundBuffer(unsigned frequency, unsigned length)
{
	// The easiest way to do a square wave: Alternate min and max bytes for as
	// long as the buffer goes. Notice that we need to have twice the sampling
	// rate to make this work, in accordance with sampling rate laws in general.
	
	unsigned numSamples = (frequency * length * 2) / 1000 * 1000;
	uint8_t *samples = new uint8_t [numSamples];
	for (unsigned i = 0; i < numSamples/2; i++)
	{
		samples[i*2+0] = std::numeric_limits<uint8_t>::min();
		samples[i*2+1] = std::numeric_limits<uint8_t>::max();
	}
	
	initWithData(samples, numSamples, frequency*2, false, false);
	
	delete samples;
}
Exemplo n.º 17
0
 const Blob& operator=(const Blob& v) {
   initWithData(v.m_data,v.m_length);
   return *this;
 }
Exemplo n.º 18
0
Texture::Texture(Image *image)
{
    initWithData((const char *)image->getImageData(), image->getImageSize().width, image->getImageSize().height);
}
Exemplo n.º 19
0
SkeletonRenderer::SkeletonRenderer (spSkeletonData *skeletonData, bool ownsSkeletonData)
	: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
	initWithData(skeletonData, ownsSkeletonData);
}
Exemplo n.º 20
0
 StaticArray(const StaticArray<T>& array) : _size(0), _data(0) {
   initWithData(array.cArray(), array.size());
 }
Exemplo n.º 21
0
 StaticArray(const T* array, index_t size) : _size(0), _data(0) {
   initWithData(array, size);
 }
Exemplo n.º 22
0
bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16Text)
{
    if (_fontFreeType == nullptr)
    {
        return false;
    } 
    
    std::unordered_map<unsigned short, unsigned short> codeMapOfNewChar;
    findNewCharacters(utf16Text, codeMapOfNewChar);
    if (codeMapOfNewChar.empty())
    {
        return false;
    }

    int adjustForDistanceMap = _letterPadding / 2;
    int adjustForExtend = _letterEdgeExtend / 2;
    long bitmapWidth;
    long bitmapHeight;
    Rect tempRect;
    FontLetterDefinition tempDef;

    auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
    auto  pixelFormat = _fontFreeType->getOutlineSize() > 0 ? CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88 : CCTexture2DPixelFormat::kCCTexture2DPixelFormat_A8;

    float startY = _currentPageOrigY;

    for (auto&& it : codeMapOfNewChar)
    {
        auto bitmap = _fontFreeType->getGlyphBitmap(it.second, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance);
        if (bitmap && bitmapWidth > 0 && bitmapHeight > 0)
        {
            tempDef.validDefinition = true;
            tempDef.width = tempRect.size.width + _letterPadding + _letterEdgeExtend;
            tempDef.height = tempRect.size.height + _letterPadding + _letterEdgeExtend;
            tempDef.offsetX = tempRect.origin.x + adjustForDistanceMap + adjustForExtend;
            tempDef.offsetY = _fontAscender + tempRect.origin.y - adjustForDistanceMap - adjustForExtend;

            if (bitmapHeight > _currLineHeight)
            {
                _currLineHeight = static_cast<int>(bitmapHeight) + _letterPadding + _letterEdgeExtend + 1;
            }
            if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
            {
                _currentPageOrigY += _currLineHeight;
                _currLineHeight = 0;
                _currentPageOrigX = 0;
                if (_currentPageOrigY + _lineHeight >= CacheTextureHeight)
                {
                    unsigned char *data = nullptr;
                    if (pixelFormat == CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88)
                    {
                        data = _currentPageData + CacheTextureWidth * (int)startY * 2;
                    }
                    else
                    {
                        data = _currentPageData + CacheTextureWidth * (int)startY;
                    }
                    _atlasTextures[_currentPage]->updateWithData(data, 0, startY,
                        CacheTextureWidth, CacheTextureHeight - startY);

                    startY = 0.0f;

                    _currentPageOrigY = 0;
                    memset(_currentPageData, 0, _currentPageDataSize);
                    _currentPage++;
                    auto tex = new (std::nothrow) Texture2D;
                    if (_antialiasEnabled)
                    {
                        tex->setAntiAliasTexParameters();
                    }
                    else
                    {
                        tex->setAliasTexParameters();
                    }
                    tex->initWithData(_currentPageData, _currentPageDataSize,
                        pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth, CacheTextureHeight));
                    addTexture(tex, _currentPage);
                    tex->release();
                }
            }
            _fontFreeType->renderCharAt(_currentPageData, _currentPageOrigX + adjustForExtend, _currentPageOrigY + adjustForExtend, bitmap, bitmapWidth, bitmapHeight);

            tempDef.U = _currentPageOrigX;
            tempDef.V = _currentPageOrigY;
            tempDef.textureID = _currentPage;
            _currentPageOrigX += tempDef.width + 1;
            // take from pixels to points
            tempDef.width = tempDef.width / scaleFactor;
            tempDef.height = tempDef.height / scaleFactor;
            tempDef.U = tempDef.U / scaleFactor;
            tempDef.V = tempDef.V / scaleFactor;
        }
        else{
            if (tempDef.xAdvance)
                tempDef.validDefinition = true;
            else
                tempDef.validDefinition = false;

            tempDef.width = 0;
            tempDef.height = 0;
            tempDef.U = 0;
            tempDef.V = 0;
            tempDef.offsetX = 0;
            tempDef.offsetY = 0;
            tempDef.textureID = 0;
            _currentPageOrigX += 1;
        }

        _letterDefinitions[it.first] = tempDef;
    }

    unsigned char *data = nullptr;
    if (pixelFormat == CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88)
    {
        data = _currentPageData + CacheTextureWidth * (int)startY * 2;
    }
    else
    {
        data = _currentPageData + CacheTextureWidth * (int)startY;
    }
    _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, _currentPageOrigY - startY + _lineHeight);

    return true;
}
Exemplo n.º 23
0
bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16String)
{
    if (_fontFreeType == nullptr)
    {
        return false;
    }  
    
    std::unordered_map<unsigned short, unsigned short> newCharsMap;
    findNewCharacters(utf16String, newCharsMap);
    if (newCharsMap.empty())
    {
        return false;
    }

    float offsetAdjust = _letterPadding / 2;  
    long bitmapWidth;
    long bitmapHeight;
    Rect tempRect;
    FontLetterDefinition tempDef;

    auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
    auto  pixelFormat = _fontFreeType->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;

    int bottomHeight = _commonLineHeight - _fontAscender;
    float startY = _currentPageOrigY;

    
    for (auto&& it : newCharsMap)
    {
        auto bitmap = _fontFreeType->getGlyphBitmap(it.second, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance);
        if (bitmap)
        {
            tempDef.validDefinition = true;
            tempDef.letteCharUTF16 = it.first;
            tempDef.width = tempRect.size.width + _letterPadding;
            tempDef.height = tempRect.size.height + _letterPadding;
            tempDef.offsetX = tempRect.origin.x + offsetAdjust;
            tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust;
            tempDef.clipBottom = bottomHeight - (tempDef.height + tempRect.origin.y + offsetAdjust);

            if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
            {
                _currentPageOrigY += _commonLineHeight;
                _currentPageOrigX = 0;
                if (_currentPageOrigY + _commonLineHeight >= CacheTextureHeight)
                {
                    unsigned char *data = nullptr;
                    if (pixelFormat == Texture2D::PixelFormat::AI88)
                    {
                        data = _currentPageData + CacheTextureWidth * (int)startY * 2;
                    }
                    else
                    {
                        data = _currentPageData + CacheTextureWidth * (int)startY;
                    }
                    _atlasTextures[_currentPage]->updateWithData(data, 0, startY,
                        CacheTextureWidth, CacheTextureHeight - startY);

                    startY = 0.0f;

                    _currentPageOrigY = 0;
                    memset(_currentPageData, 0, _currentPageDataSize);
                    _currentPage++;
                    auto tex = new (std::nothrow) Texture2D;
                    if (_antialiasEnabled)
                    {
                        tex->setAntiAliasTexParameters();
                    }
                    else
                    {
                        tex->setAliasTexParameters();
                    }
                    tex->initWithData(_currentPageData, _currentPageDataSize,
                        pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth, CacheTextureHeight));
                    addTexture(tex, _currentPage);
                    tex->release();
                }
            }
            _fontFreeType->renderCharAt(_currentPageData, _currentPageOrigX, _currentPageOrigY, bitmap, bitmapWidth, bitmapHeight);

            tempDef.U = _currentPageOrigX;
            tempDef.V = _currentPageOrigY;
            tempDef.textureID = _currentPage;
            _currentPageOrigX += tempDef.width + 1;
            // take from pixels to points
            tempDef.width = tempDef.width / scaleFactor;
            tempDef.height = tempDef.height / scaleFactor;
            tempDef.U = tempDef.U / scaleFactor;
            tempDef.V = tempDef.V / scaleFactor;
        }
        else{
            if (tempDef.xAdvance)
                tempDef.validDefinition = true;
            else
                tempDef.validDefinition = false;

            tempDef.letteCharUTF16 = it.first;
            tempDef.width = 0;
            tempDef.height = 0;
            tempDef.U = 0;
            tempDef.V = 0;
            tempDef.offsetX = 0;
            tempDef.offsetY = 0;
            tempDef.textureID = 0;
            tempDef.clipBottom = 0;
            _currentPageOrigX += 1;
        }

        _fontLetterDefinitions[tempDef.letteCharUTF16] = tempDef;
    }

    unsigned char *data = nullptr;
    if (pixelFormat == Texture2D::PixelFormat::AI88)
    {
        data = _currentPageData + CacheTextureWidth * (int)startY * 2;
    }
    else
    {
        data = _currentPageData + CacheTextureWidth * (int)startY;
    }
    _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, 
        _currentPageOrigY - startY + _commonLineHeight);

    return true;
}
Exemplo n.º 24
0
 Blob(const void* data, size_t length) : m_data(NULL), m_length(0)
 {
   initWithData((u8_t*)data,length);
 };
Exemplo n.º 25
0
ArrayPiece::ArrayPiece(void* data, unsigned byteLength)
{
    initWithData(data, byteLength);
}
Exemplo n.º 26
0
SoundBuffer::SoundBuffer(const char *filename) throw(std::runtime_error)
{
	std::ifstream file(filename, std::ios::binary);
	if (!file) throw std::runtime_error("Could not open sound file!");
		
	char actualGroupID[4];
	file.read(actualGroupID, 4);
	if (memcmp(actualGroupID, "RIFF", 4) != 0) throw std::runtime_error("Not a WAV file.");
	
	// File length minus eight. Ignored here.
	uint32_t fileLength;
	file.read(reinterpret_cast<char *> (&fileLength), 4);
    fileLength = SwapLittleToHost(fileLength);
	
	char actualRiffType[4];
	file.read(actualRiffType, 4);
	if (memcmp(actualRiffType, "WAVE", 4) != 0) throw std::runtime_error("Not a WAV file.");
	
	// Information about the file
	bool haveFmt = false;
	bool haveData = false;
	
	uint32_t dataLength;
	uint16_t numChannels;
	uint32_t samplesPerSecond;
	uint16_t bitsPerSample;
	char *data = NULL;
	
	// Scan chunks
	while (file.good() && !(haveFmt && haveData))
	{
		char chunkType[4];
		file.read(chunkType, 4);
		
		if (memcmp(chunkType, "fmt ", 4) == 0)
		{
			// Read Format header
			if (haveFmt) throw std::runtime_error("Format data specified more than once.");
			haveFmt = true;
			
			uint32_t chunkLength;
			file.read(reinterpret_cast<char *> (&chunkLength), 4);
            chunkLength = SwapLittleToHost(chunkLength);
			if (chunkLength != 16) throw std::runtime_error("Sound file format not supported.");
			
			uint16_t format;
			file.read(reinterpret_cast<char *> (&format), 2);
            format = SwapLittleToHost(format);
			if (format != 0x0001) throw std::runtime_error("Format is not PCM.");
			
			file.read(reinterpret_cast<char *> (&numChannels), 2);
            numChannels = SwapLittleToHost(numChannels);
			if (numChannels > 2) throw std::runtime_error("More than two channels.");
			
			file.read(reinterpret_cast<char *> (&samplesPerSecond), 4);
            samplesPerSecond = SwapLittleToHost(samplesPerSecond);
			
			// Bytes per second and block align.
			file.seekg(6, std::ios::cur);
			
			file.read(reinterpret_cast<char *> (&bitsPerSample), 2);
            bitsPerSample = SwapLittleToHost(bitsPerSample);
			if (bitsPerSample != 8 && bitsPerSample != 16) throw std::runtime_error("Bits per sample has unusual value.");
		}
		else if (memcmp(chunkType, "data", 4) == 0)
		{
			// Read actual data
			if (haveData) throw std::runtime_error("Sound data specified more than once.");
			haveData = true;
			
			file.read(reinterpret_cast<char *> (&dataLength), 4);
            dataLength = SwapLittleToHost(dataLength);
			data = new char [dataLength];
			file.read(data, dataLength);
		}
		else
		{
			// Skip unknown chunk
			uint32_t chunkLength;
			file.read(reinterpret_cast<char *> (&chunkLength), 4);
			chunkLength += chunkLength % 2; // RIFF-Files are word aligned
			file.seekg(chunkLength, std::ios::cur);
		}
	}
    
    if (bitsPerSample == 16)
        SwapU16LittleToHost(reinterpret_cast<uint16_t *>(data), dataLength/sizeof(uint16_t));
	
	initWithData(data, dataLength, samplesPerSecond, bitsPerSample == 16, numChannels == 2);
	
	delete [] data;
}
// TODO: for debug, remove later
bool StatusDataManager::initWithDebugData()
{
    PlayerStatusData* playerData = new PlayerStatusData(0,
                                                        "character_icon_0.png",
                                                        "Username_0",
                                                        100,
                                                        1000);
    
    std::vector<UnitOfPlayerRecord*> unitRecords;
    UnitData *unitDataTemp;
    UnitOfPlayerRecord* unitRecordTemp;
    // unit 0
    unitDataTemp = new UnitData(0,
                                "element_red.png",
                                ElementType::RED,
                                "UnitName_0_red",
                                10,
                                1);
    unitRecordTemp = new UnitOfPlayerRecord(unitDataTemp,
                                            time(nullptr),
                                            1,
                                            0);
    unitRecords.push_back(unitRecordTemp);
    // unit 1
    unitDataTemp = new UnitData(1,
                                "element_blue.png",
                                ElementType::BLUE,
                                "UnitName_1_blue",
                                10,
                                1);
    unitRecordTemp = new UnitOfPlayerRecord(unitDataTemp,
                                            time(nullptr),
                                            1,
                                            0);
    unitRecords.push_back(unitRecordTemp);
    // unit 2
    unitDataTemp = new UnitData(2,
                                "element_yellow.png",
                                ElementType::YELLOW,
                                "UnitName_2_yellow",
                                10,
                                1);
    unitRecordTemp = new UnitOfPlayerRecord(unitDataTemp,
                                            time(nullptr),
                                            1,
                                            0);
    unitRecords.push_back(unitRecordTemp);
    // unit 3
    unitDataTemp = new UnitData(3,
                                "element_green.png",
                                ElementType::GREEN,
                                "UnitName_3_green",
                                10,
                                1);
    unitRecordTemp = new UnitOfPlayerRecord(unitDataTemp,
                                            time(nullptr),
                                            1,
                                            0);
    unitRecords.push_back(unitRecordTemp);
    // unit 4
    unitDataTemp = new UnitData(1,
                                "element_blue.png",
                                ElementType::BLUE,
                                "UnitName_1_blue",
                                10,
                                1);
                      
    unitRecordTemp = new UnitOfPlayerRecord(unitDataTemp,
                                            time(nullptr),
                                            1,
                                            0);
    unitRecords.push_back(unitRecordTemp);
    // unit 5
    unitDataTemp = new UnitData(1,
                                "element_blue.png",
                                ElementType::BLUE,
                                "UnitName_1_blue",
                                10,
                                1);
                      
    unitRecordTemp = new UnitOfPlayerRecord(unitDataTemp,
                                            time(nullptr),
                                            1,
                                            0);
    unitRecords.push_back(unitRecordTemp);
    
    return initWithData(playerData, _unitRecords);
}
Exemplo n.º 28
0
FBTexture::FBTexture(FBImage* image)
{
    initWithData(image->getImageInfo().data, image->getImageInfo().size);
}
Exemplo n.º 29
0
 Blob(const Blob & b) : m_data(NULL)
 { 
   initWithData(b.m_data,b.m_length);
 };
Exemplo n.º 30
0
bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16String)
{
    FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
    if(fontTTf == nullptr)
        return false;
    
    size_t length = utf16String.length();

    float offsetAdjust = _letterPadding / 2;  
    long bitmapWidth;
    long bitmapHeight;
    Rect tempRect;
    FontLetterDefinition tempDef;

    auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
    auto  pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; 

    bool existNewLetter = false;
    int bottomHeight = _commonLineHeight - _fontAscender;

    float startY = _currentPageOrigY;

    for (size_t i = 0; i < length; ++i)
    {
        auto outIterator = _fontLetterDefinitions.find(utf16String[i]);

        if (outIterator == _fontLetterDefinitions.end())
        {  
            existNewLetter = true;

            auto bitmap = fontTTf->getGlyphBitmap(utf16String[i],bitmapWidth,bitmapHeight,tempRect,tempDef.xAdvance);
            if (bitmap)
            {
                tempDef.validDefinition = true;
                tempDef.letteCharUTF16   = utf16String[i];
                tempDef.width            = tempRect.size.width + _letterPadding;
                tempDef.height           = tempRect.size.height + _letterPadding;
                tempDef.offsetX          = tempRect.origin.x + offsetAdjust;
                tempDef.offsetY          = _fontAscender + tempRect.origin.y - offsetAdjust;
                tempDef.clipBottom     = bottomHeight - (tempDef.height + tempRect.origin.y + offsetAdjust);

                if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
                {
                    _currentPageOrigY += _commonLineHeight;
                    _currentPageOrigX = 0;
                    if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight)
                    {
                        unsigned char *data = nullptr;
                        if(pixelFormat == Texture2D::PixelFormat::AI88)
                        {
                            data = _currentPageData + CacheTextureWidth * (int)startY * 2;
                        }
                        else
                        {
                            data = _currentPageData + CacheTextureWidth * (int)startY;
                        }
                        _atlasTextures[_currentPage]->updateWithData(data, 0, startY, 
                            CacheTextureWidth, CacheTextureHeight - startY);

                        startY = 0.0f;

                        _currentPageOrigY = 0;
                        memset(_currentPageData, 0, _currentPageDataSize);
                        _currentPage++;
                        auto tex = new (std::nothrow) Texture2D;
                        if (_antialiasEnabled)
                        {
                            tex->setAntiAliasTexParameters();
                        } 
                        else
                        {
                            tex->setAliasTexParameters();
                        }
                        tex->initWithData(_currentPageData, _currentPageDataSize, 
                            pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
                        addTexture(tex,_currentPage);
                        tex->release();
                    }  
                }
                fontTTf->renderCharAt(_currentPageData,_currentPageOrigX,_currentPageOrigY,bitmap,bitmapWidth,bitmapHeight);

                tempDef.U                = _currentPageOrigX;
                tempDef.V                = _currentPageOrigY;
                tempDef.textureID        = _currentPage;
                _currentPageOrigX        += tempDef.width + 1;
                // take from pixels to points
                tempDef.width  =    tempDef.width  / scaleFactor;
                tempDef.height =    tempDef.height / scaleFactor;      
                tempDef.U      =    tempDef.U      / scaleFactor;
                tempDef.V      =    tempDef.V      / scaleFactor;
            }
            else{
                if(tempDef.xAdvance)
                    tempDef.validDefinition = true;
                else
                    tempDef.validDefinition = false;

                tempDef.letteCharUTF16   = utf16String[i];
                tempDef.width            = 0;
                tempDef.height           = 0;
                tempDef.U                = 0;
                tempDef.V                = 0;
                tempDef.offsetX          = 0;
                tempDef.offsetY          = 0;
                tempDef.textureID        = 0;
                tempDef.clipBottom = 0;
                _currentPageOrigX += 1;
            }

            _fontLetterDefinitions[tempDef.letteCharUTF16] = tempDef;
        }       
    }

    if(existNewLetter)
    {
        if (_rendererRecreate)
        {
            _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, 
                pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
        } 
        else
        {
            unsigned char *data = nullptr;
            if(pixelFormat == Texture2D::PixelFormat::AI88)
            {
                data = _currentPageData + CacheTextureWidth * (int)startY * 2;
            }
            else
            {
                data = _currentPageData + CacheTextureWidth * (int)startY;
            }
            _atlasTextures[_currentPage]->updateWithData(data, 0, startY, 
                CacheTextureWidth, _currentPageOrigY - startY + _commonLineHeight);
        }
    }
    return true;
}