



#include <allegro_flare/bins/bitmap_bin.h>








BitmapBin::BitmapBin(std::string directory)
	: Bin<ALLEGRO_BITMAP *>(directory)
{}




BitmapBin::~BitmapBin()
{
	clear();
}




ALLEGRO_BITMAP *BitmapBin::load_data(std::string identifier)
{
	return al_load_bitmap(identifier.c_str());
}




void BitmapBin::destroy_data(ALLEGRO_BITMAP *bmp)
{
	al_destroy_bitmap(bmp);
}




/*
#include <algorithm>
#include <sstream>




static bool bitmap_record_comp(const BitmapBin::BitmapRecord *b1, const BitmapBin::BitmapRecord *b2)
{
	return b1->identifier.compare(b2->identifier) < 0;
}



BitmapBin::BitmapBin()
{
	directory = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
	al_append_path_component(directory, "data");
	al_append_path_component(directory, "bitmaps");
}



BitmapBin::BitmapRecord *BitmapBin::get_record(std::string identifier)
{
	int first, last, mid, comp;

	if (record.empty()) return NULL;

	first = 0;
	last = record.size()-1;

	while (first <= last)
	{
		mid = (first + last) / 2;  // compute mid point.
		comp = record[mid]->identifier.compare(identifier);

		if (comp < 0) first = mid + 1;  // repeat search in top half.
		else if (comp > 0) last = mid - 1; // repeat search in bottom half.
		else return record.at(mid);     // found it. return position
	}

	return NULL;
}



ALLEGRO_BITMAP *BitmapBin::get(std::string identifier)
{
	BitmapBin::BitmapRecord *r = get_record(identifier);
	if (r) return r->data;
	return NULL;
}



bool BitmapBin::load(std::string identifier, std::string filename)
{
	ALLEGRO_PATH *path = NULL;
	BitmapBin::BitmapRecord *r = get_record(identifier);
	if (r) return false;

	path = al_clone_path(directory);
	al_set_path_filename(path, filename.c_str());

	r = new BitmapBin::BitmapRecord;
	r->identifier = identifier;
	r->file_path = path;
	r->data = al_load_bitmap(al_path_cstr(r->file_path, ALLEGRO_NATIVE_PATH_SEP));

	record.push_back(r);
	std::sort(record.begin(), record.end(), bitmap_record_comp);

	if (!r->data) return false;
	return true;
}



std::string BitmapBin::get_str()
{
	std::stringstream result;
	result << "BitmapBin dump" << std::endl;
	for (unsigned i=0; i<record.size(); i++)
		result << i << ": " << record[i]->identifier;
	return result.str();
}


*/