Exemplo n.º 1
0
//drawing
void AnimatedSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	if(m_sprites.size() == 0)
		return;
	states.transform *= getTransform();
	int currentFrame = getCurrentFrame();
	sf::Sprite& currentSprite = const_cast<sf::Sprite&>(getFrameSprite(currentFrame)); //this should always work
	//set the color
	//you're going to be using multiply 99% of the time
	switch(m_spriteBlendMode)
	{
	case sf::BlendAlpha:
		currentSprite.setColor(sf::Color(
			m_color.r*m_color.a + getFrameColor(currentFrame).r*(1-m_color.a), //red
			m_color.g*m_color.a + getFrameColor(currentFrame).r*(1-m_color.a), //green
			m_color.b*m_color.a + getFrameColor(currentFrame).r*(1-m_color.a), //blue
			m_color.a + getFrameColor(currentFrame).a)); //alpha
		break;
	case sf::BlendAdd:
		currentSprite.setColor(m_color + getFrameColor(currentFrame));
		break;
	case sf::BlendMultiply:
		currentSprite.setColor(m_color * getFrameColor(currentFrame));
		break;
	case sf::BlendNone:
		currentSprite.setColor(getFrameColor(currentFrame));
		break;
	}
	target.draw(currentSprite, states);
}
Exemplo n.º 2
0
animFrame_* getAnimFrame(unsigned char *image8bpp, int wi, int fx, int fy, int wf, int hf, int time, int collision)
{
    int i, j;
    int nbSprW, nbSprH;
    int lastSprW, lastSprH;
    int ws, hs;
    animFrame_* result;
    frameSprite_** frameSprites;
    frameSprite_* frameSprite;
    box_* box;
    circle_* circle;

    nbSprW = (wf + 3) / 4;
    nbSprH = (hf + 3) / 4;
    lastSprW = wf & 3;
    if (lastSprW == 0) lastSprW = 4;
    lastSprH = hf & 3;
    if (lastSprH == 0) lastSprH = 4;

    // allocate result
    result = malloc(sizeof(animFrame_));
    result->numSprite = nbSprW * nbSprH;
    // allocate frameSprite array
    frameSprites = malloc(result->numSprite * sizeof(frameSprite_*));
    result->frameSprites = frameSprites;
	result->w = wf;
	result->h = hf;
	result->tc = collision;
    result->timer = time;

    // handle collision structure
    switch(collision)
    {
        case COLLISION_NONE:
            result->numCollision = 0;
            result->collisions = NULL;
        break;

        case COLLISION_BOX:
            result->numCollision = 1;
            // allocate collision array
            result->collisions = malloc(1 * sizeof(void*));
            // allocate collision structure
            box = malloc(sizeof(box_));
            // use 75% the size of the frame for the collision
            box->x = (wf * 8) / 2;
            box->y = (hf * 8) / 2;
            box->w = ((wf * 8) * 3) / 4;
            box->h = ((hf * 8) * 3) / 4;
            result->collisions[0] = box;
        break;

        case COLLISION_CIRCLE:
            result->numCollision = 1;
            // allocate collision array
            result->collisions = malloc(1 * sizeof(void*));
            // allocate collision structure
            circle = malloc(sizeof(circle_));
            // use 75% the size of the frame for the collision
            circle->x = (wf * 8) / 2;
            circle->y = (hf * 8) / 2;
            circle->ray = ((wf * 8) * 3) / 4;
            result->collisions[0] = circle;
        break;
    }

    for(j = 0; j < nbSprH; j++)
    {
        if (j == (nbSprH - 1)) hs = lastSprH;
        else hs = 4;

        for(i = 0; i < nbSprW; i++)
        {
            if (i == (nbSprW - 1)) ws = lastSprW;
            else ws = 4;

            frameSprite = getFrameSprite(image8bpp, wi, (fx * wf) + (i * 4), (fy * hf) + (j * 4), ws, hs);
            if (frameSprite == NULL) return NULL;

            // set x and y offset
            frameSprite->x = i * 32;
            frameSprite->y = j * 32;
            *frameSprites++ = frameSprite;
        }
    }

    return result;
}