/*
 * Cloud.cpp
 *
 *  Created on: 2009-07-10
 *      Author: wasyl
 */

#include "Cloud.h"

#include "../prep/misc/Random.h"
#include "../prep/res/Resources.h"
#include "../misc/Theme.h"

Cloud::Cloud(bool initial)
{
	Resources::load(mBmp, "media/cloud.png");
	mPos = Vec(randomFloat(-60.0, 860.0), randomFloat(-20.0, 580.0));
	mSpeed = randomFloat(15.0, 30.0);
	mFlips = 0;
	if (randomInt(0,1))
		mFlips |= ALLEGRO_FLIP_HORIZONTAL;
	if (randomInt(0,1))
		mFlips |= ALLEGRO_FLIP_VERTICAL;
	mBehind = Theme::active().behind();
}

Cloud::~Cloud()
{
}

void Cloud::update(double dt)
{
	mPos.x -= mSpeed * dt;
	if (mPos.x < -al_get_bitmap_width(mBmp))
	{
		mPos.x = 800.0;
		mPos.y = randomFloat(-20.0, 580.0);
	}
}

void Cloud::render()
{
	unsigned char r, g, b, a;
	al_unmap_rgba(Theme::active().cloudColor(), &r, &g, &b, &a);
	if (mBehind)
		a = (unsigned char)(a*0.7);

	al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba(r,g,b,a));
	al_draw_bitmap(mBmp, mPos.x, mPos.y, 0);
	al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba(255,255,255,255));
}

