Beispiel #1
0
        void TextureAtlasHandler::LoadAtlasFromFile(const String& Name, const String& Group)
        {
            /// @todo Update after we have refactored the resource system if needed.
            Resource::DataStreamPtr AtlasStream = Resource::ResourceManager::GetSingletonPtr()->OpenAssetStream(Name,Group);
            AtlasStream->SetStreamPosition(0);
            XML::Document AtlasDoc;
            AtlasDoc.Load( *AtlasStream.Get() );

            XML::Node RootNode = AtlasDoc.GetChild("Atlases");
            if( !RootNode.Empty() )
            {
                for( XML::NodeIterator AtlasIt = RootNode.begin() ; AtlasIt != RootNode.end() ; ++AtlasIt )
                {
                    // Parse the Atlas
                    TextureAtlas* NewAtlas = new TextureAtlas( (*AtlasIt) );
                    // Verify we don't already have one of the same name
                    AtlasIterator AtIt = this->Atlases.find( NewAtlas->GetName() );
                    if( AtIt != Atlases.end() )
                    {
                        MEZZ_EXCEPTION(ExceptionBase::II_DUPLICATE_IDENTITY_EXCEPTION,"Texture Atlas with the name \"" + NewAtlas->GetName() + "\" already exists.");
                    }
                    // Add the unique Atlas
                    this->Atlases[NewAtlas->GetName()] = NewAtlas;
                }
            }else{
                MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Mezzanine Texture Atlas file \"" + Name + "\"does not contain expected \"Atlases\" root node.  File is not valid and cannot be parsed.");
            }
        }
 TextureAtlasAnimation::TextureAtlasAnimation(TextureAtlas &atlas, const string &path, float fps, bool looping, bool reverse)
 :
 atlas(atlas),
 fps(fps),
 looping(looping)
 {
     if (reverse)
     {
         auto tmp = atlas.getAnimationSprites(path);
         
         for (int i = tmp.size() - 1; i >= 0; i--)
         {
             sprites.push_back(tmp[i]);
         }
     }
     else
     {
         sprites = atlas.getAnimationSprites(path);
     }
     
     if (sprites.empty())
     {
         throw runtime_error("INVALID ANIMATION");
     }
 }
Beispiel #3
0
TextureAtlas* SDL::loadAtlas( string imagefile, string datafile )
{
	TextureAtlas * ta = new TextureAtlas();
	ta->tex = loadTexture( imagefile );
	ta->loadData( datafile );
	texAtlases[ imagefile ] = ta;
	return ta;
}
TextureAtlas * TextureAtlas::createWithTexture(Texture2D *texture, ssize_t capacity)
{
    TextureAtlas * textureAtlas = new (std::nothrow) TextureAtlas();
    if (textureAtlas && textureAtlas->initWithTexture(texture, capacity))
    {
        textureAtlas->autorelease();
        return textureAtlas;
    }
    CC_SAFE_DELETE(textureAtlas);
    return nullptr;
}
TextureAtlas * TextureAtlas::create(const std::string& file, ssize_t capacity)
{
    TextureAtlas * textureAtlas = new (std::nothrow) TextureAtlas();
    if(textureAtlas && textureAtlas->initWithFile(file, capacity))
    {
        textureAtlas->autorelease();
        return textureAtlas;
    }
    CC_SAFE_DELETE(textureAtlas);
    return nullptr;
}
TextureAtlas * TextureAtlas::createWithTexture(Texture2D *texture, int capacity)
{
    TextureAtlas * textureAtlas = new TextureAtlas();
    if (textureAtlas && textureAtlas->initWithTexture(texture, capacity))
    {
        textureAtlas->autorelease();
        return textureAtlas;
    }
    CC_SAFE_DELETE(textureAtlas);
    return NULL;
}
TextureAtlas * TextureAtlas::create(const char* file, int capacity)
{
    TextureAtlas * textureAtlas = new TextureAtlas();
    if(textureAtlas && textureAtlas->initWithFile(file, capacity))
    {
        textureAtlas->autorelease();
        return textureAtlas;
    }
    CC_SAFE_DELETE(textureAtlas);
    return NULL;
}
Beispiel #8
0
void _AtlasPage_createTexture (AtlasPage* self, const char* path) {
	Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(path);
	TextureAtlas* textureAtlas = TextureAtlas::createWithTexture(texture, 4);
	textureAtlas->retain();
	self->rendererObject = textureAtlas;
    // Using getContentSize to make it supports the strategy of loading resources in cocos2d-x.
	// self->width = texture->getPixelsWide();
	// self->height = texture->getPixelsHigh();
    self->width = texture->getContentSize().width;
    self->height = texture->getContentSize().height;
}
Beispiel #9
0
    std::shared_ptr<TextureAtlas> Project::loadTextureAtlas(std::string filename) {
        std::string texture = filename.substr(0, filename.size()-4) + ".png";

        auto iter = textureAtlasRef.find(filename);
        if (iter != textureAtlasRef.end()){
            if (!iter->second.expired()){
                return iter->second.lock();
            }
        }
        TextureAtlas* textureAtlas = new TextureAtlas();
        textureAtlas->load(filename, texture);
        auto ref = std::shared_ptr<TextureAtlas>{textureAtlas};
        textureAtlasRef[filename] = weak_ptr<TextureAtlas>{ref};
        return ref;
    }
 TextureAtlasAnimation::TextureAtlasAnimation(TextureAtlas &atlas, const string &path, float fps, bool looping, int firstFrameIndex, int lastFrameIndex)
 :
 atlas(atlas),
 fps(fps),
 looping(looping)
 {
     auto tmp = atlas.getAnimationSprites(path);
     
     if (((firstFrameIndex < 0) || (firstFrameIndex >= tmp.size()) || ((lastFrameIndex < 0) || (lastFrameIndex >= tmp.size()))))
     {
         throw runtime_error("OUT-OF-RANGE ANIMATION FRAMES");
     }
     
     if (firstFrameIndex < lastFrameIndex)
     {
         for (int i = firstFrameIndex; i <= lastFrameIndex; i++)
         {
             sprites.push_back(tmp[i]);
         }
     }
     else
     {
         for (int i = firstFrameIndex; i >= lastFrameIndex; i--)
         {
             sprites.push_back(tmp[i]);
         }
     }
     
     if (sprites.empty())
     {
         throw runtime_error("INVALID ANIMATION");
     }
 }
Beispiel #11
0
void CCSkeleton::draw () {
	CC_NODE_DRAW_SETUP();

	GL::blendFunc(blendFunc.src, blendFunc.dst);
	Color3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;
	if (premultipliedAlpha) {
		skeleton->r *= skeleton->a;
		skeleton->g *= skeleton->a;
		skeleton->b *= skeleton->a;
	}

	TextureAtlas* textureAtlas = 0;
	V3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		Slot* slot = skeleton->slots[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
		TextureAtlas* regionTextureAtlas = getTextureAtlas(attachment);
		if (regionTextureAtlas != textureAtlas) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
		}
		textureAtlas = regionTextureAtlas;
		if (textureAtlas->getCapacity() == textureAtlas->getTotalQuads() &&
			!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		RegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha);
		textureAtlas->updateQuad(&quad, textureAtlas->getTotalQuads());
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

	if (debugSlots) {
		// Slots.
		DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
		glLineWidth(1);
		Point points[4];
		V3F_C4B_T2F_Quad quad;
		for (int i = 0, n = skeleton->slotCount; i < n; i++) {
			Slot* slot = skeleton->slots[i];
			if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
			RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
			RegionAttachment_updateQuad(attachment, slot, &quad);
			points[0] = Point(quad.bl.vertices.x, quad.bl.vertices.y);
			points[1] = Point(quad.br.vertices.x, quad.br.vertices.y);
			points[2] = Point(quad.tr.vertices.x, quad.tr.vertices.y);
			points[3] = Point(quad.tl.vertices.x, quad.tl.vertices.y);
			DrawPrimitives::drawPoly(points, 4, true);
		}
	}
	if (debugBones) {
		// Bone lengths.
		glLineWidth(2);
		DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			float x = bone->data->length * bone->m00 + bone->worldX;
			float y = bone->data->length * bone->m10 + bone->worldY;
			DrawPrimitives::drawLine(Point(bone->worldX, bone->worldY), Point(x, y));
		}
		// Bone origins.
		DrawPrimitives::setPointSize(4);
		DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			DrawPrimitives::drawPoint(Point(bone->worldX, bone->worldY));
			if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
		}
	}
}
Beispiel #12
0
	void Part::Load(FileNode *fileNode)
	{
		//Entity::Load(fileNode);

		fileNode->Read("id", id);
		fileNode->Read("name", name);

		sprite = NULL;
		SetGraphic(NULL);

		TextureAtlas *textureAtlas = puppet->GetTextureAtlas();
		if (textureAtlas)
		{
			TextureAtlasEntry *textureAtlasEntry = NULL;
			fileNode->Read("atlasEntry", atlasEntry);
			if (atlasEntry != "")
			{
				textureAtlasEntry = textureAtlas->GetEntryByName(atlasEntry);
				if (textureAtlasEntry)
				{
					sprite = new Sprite(textureAtlas->GetImageName());
					SetGraphic(sprite);
					sprite->textureOffset = textureAtlasEntry->GetTextureOffset();
					sprite->textureScale = textureAtlasEntry->GetTextureScale();
					printf("textureOffset: (%f, %f) textureScale: (%f, %f)\n", sprite->textureOffset.x, sprite->textureOffset.y, sprite->textureScale.x, sprite->textureScale.y);
					sprite->width = textureAtlasEntry->GetWidth();
					sprite->height = textureAtlasEntry->GetHeight();
				}
			}
		}

		if (sprite == NULL)
		{
			std::string image;
			fileNode->Read("image", image);

			sprite = new Sprite(image);
			SetGraphic(sprite);
		}
		
		if (sprite != NULL)
		{
			int width=-1, height=-1;
			fileNode->Read("width", width);
			fileNode->Read("height", height);

			if (width != -1)
			{
				sprite->width = width;
			}

			if (height != -1)
			{
				sprite->height = height;
			}

			fileNode->Read("offset", sprite->position);
		}

		int layer = 0;
		fileNode->Read("layer", layer);
		SetLayer(layer);

//		if (sprite)
//		{
//			std::string atlas;
//			fileNode->Read("atlas", atlas);
//			if (atlas != "")
//			{
//				int x, y, width, height, maxWidth=-1, maxHeight=-1;
//				std::istringstream is(atlas);
//				is >> x >> y >> width >> height >> maxWidth >> maxHeight;
//
//				if (maxWidth == -1)
//				maxWidth = width;
//				if (maxHeight == -1)
//				maxHeight = height;
//
//				sprite->textureOffset = Vector2(x/float(maxWidth), y/float(maxHeight));
//				sprite->textureScale = Vector2(width/float(maxWidth), height/float(maxHeight));
//
//				sprite->width = width;
//				sprite->height = height;
//			}
//		}
	}
void Skeleton::onDraw(const Mat4 &transform, uint32_t flags)
{
    getGLProgram()->use();
    getGLProgram()->setUniformsForBuiltins(transform);

    GL::blendFunc(blendFunc.src, blendFunc.dst);
	Color3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;
	if (premultipliedAlpha) {
		skeleton->r *= skeleton->a;
		skeleton->g *= skeleton->a;
		skeleton->b *= skeleton->a;
	}

	int additive = 0;
	TextureAtlas* textureAtlas = 0;
	V3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		spSlot* slot = skeleton->drawOrder[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
		TextureAtlas* regionTextureAtlas = getTextureAtlas(attachment);

		if (slot->data->additiveBlending != additive) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
			additive = !additive;
            GL::blendFunc(blendFunc.src, additive ? GL_ONE : blendFunc.dst);
		} else if (regionTextureAtlas != textureAtlas && textureAtlas) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
		}
		textureAtlas = regionTextureAtlas;
        setFittedBlendingFunc(textureAtlas);

		ssize_t quadCount = textureAtlas->getTotalQuads();
		if (textureAtlas->getCapacity() == quadCount) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
			if (!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		}

		spRegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha);
		textureAtlas->updateQuad(&quad, quadCount);
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

    if(debugBones || debugSlots) {
        Director* director = Director::getInstance();
        CCASSERT(nullptr != director, "Director is null when seting matrix stack");
        director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
        director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);

        if (debugSlots) {
            // Slots.
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
            glLineWidth(1);
            Vec2 points[4];
            V3F_C4B_T2F_Quad tmpQuad;
            for (int i = 0, n = skeleton->slotCount; i < n; i++) {
                spSlot* slot = skeleton->drawOrder[i];
                if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
                spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
                spRegionAttachment_updateQuad(attachment, slot, &tmpQuad);
                points[0] = Vec2(tmpQuad.bl.vertices.x, tmpQuad.bl.vertices.y);
                points[1] = Vec2(tmpQuad.br.vertices.x, tmpQuad.br.vertices.y);
                points[2] = Vec2(tmpQuad.tr.vertices.x, tmpQuad.tr.vertices.y);
                points[3] = Vec2(tmpQuad.tl.vertices.x, tmpQuad.tl.vertices.y);
                DrawPrimitives::drawPoly(points, 4, true);
            }
        }
        if (debugBones) {
            // Bone lengths.
            glLineWidth(2);
            DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                float x = bone->data->length * bone->m00 + bone->worldX;
                float y = bone->data->length * bone->m10 + bone->worldY;
                DrawPrimitives::drawLine(Vec2(bone->worldX, bone->worldY), Vec2(x, y));
            }
            // Bone origins.
            DrawPrimitives::setPointSize(4);
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                DrawPrimitives::drawPoint(Vec2(bone->worldX, bone->worldY));
                if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
            }
        }
        
        director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    }
}
 SpriteBatch::SpriteBatch(TextureAtlas& atlas) :
     SpriteBatch(atlas.getTexture())
 {
     _atlas = &atlas;
 }
Beispiel #15
0
void Skeleton::onDraw ()
{
	CC_NODE_DRAW_SETUP();

    GL::blendFunc(blendFunc.src, blendFunc.dst);
	Color3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;
	if (premultipliedAlpha) {
		skeleton->r *= skeleton->a;
		skeleton->g *= skeleton->a;
		skeleton->b *= skeleton->a;
	}

	int additive = 0;
	TextureAtlas* textureAtlas = 0;
	V3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		spSlot* slot = skeleton->drawOrder[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
		TextureAtlas* regionTextureAtlas = getTextureAtlas(attachment);

		if (slot->data->additiveBlending != additive) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
			additive = !additive;
            GL::blendFunc(blendFunc.src, additive ? GL_ONE : blendFunc.dst);
		} else if (regionTextureAtlas != textureAtlas && textureAtlas) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
		}
		textureAtlas = regionTextureAtlas;
        setFittedBlendingFunc(textureAtlas);

		ssize_t quadCount = textureAtlas->getTotalQuads();
		if (textureAtlas->getCapacity() == quadCount) {
			textureAtlas->drawQuads();
			textureAtlas->removeAllQuads();
			if (!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		}

		spRegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha);
		textureAtlas->updateQuad(&quad, quadCount);
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

    if(debugBones || debugSlots) {
        kmGLPushMatrix();
        kmGLLoadMatrix(&_modelViewTransform);

        if (debugSlots) {
            // Slots.
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
            glLineWidth(1);
            Point points[4];
            V3F_C4B_T2F_Quad tmpQuad;
            for (int i = 0, n = skeleton->slotCount; i < n; i++) {
                spSlot* slot = skeleton->drawOrder[i];
                if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
                spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
                spRegionAttachment_updateQuad(attachment, slot, &tmpQuad);
                points[0] = Point(tmpQuad.bl.vertices.x, tmpQuad.bl.vertices.y);
                points[1] = Point(tmpQuad.br.vertices.x, tmpQuad.br.vertices.y);
                points[2] = Point(tmpQuad.tr.vertices.x, tmpQuad.tr.vertices.y);
                points[3] = Point(tmpQuad.tl.vertices.x, tmpQuad.tl.vertices.y);
                DrawPrimitives::drawPoly(points, 4, true);
            }
        }
        if (debugBones) {
            // Bone lengths.
            glLineWidth(2);
            DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                float x = bone->data->length * bone->m00 + bone->worldX;
                float y = bone->data->length * bone->m10 + bone->worldY;
                DrawPrimitives::drawLine(Point(bone->worldX, bone->worldY), Point(x, y));
            }
            // Bone origins.
            DrawPrimitives::setPointSize(4);
            DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
            for (int i = 0, n = skeleton->boneCount; i < n; i++) {
                spBone *bone = skeleton->bones[i];
                DrawPrimitives::drawPoint(Point(bone->worldX, bone->worldY));
                if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
            }
        }
        
        kmGLPopMatrix();
    }
}