Ejemplo n.º 1
0
	// Inserts new chromosome after the last chromosome currently in the group
	int GaChromosomeGroup::Add(GaChromosomeStorage* chromosome)
	{
		// is chromosome already a member of this group
		if( _membershipFlag && chromosome->GetFlags().IsFlagSetAll( _membershipFlag ) )
			// if membership flag is used, single chromosome cannot inserted in the same group multiple times 
			return -1;

		_hasShuffleBackup = false;

		// group is full?
		if( _count == _array.GetSize() )
		{
			GA_ASSERT( Common::Exceptions::GaInvalidOperationException, _sizable, "This chromosome group is full.", "Population" );

			// increase size of the group so it can accommodate new chromosome
			IncreaseSize();
		}

		// insert chromosome and mark it as a member
		int pos = _count++;
		_chromosomes[ pos ] = chromosome;
		chromosome->GetFlags().SetFlags( _membershipFlag );

		return pos;
	}
Ejemplo n.º 2
0
Object2D* Bullet2DFactory::GetObject()
{
	for(Bullet2D* bullet = begin; bullet != end; ++bullet)
	{
		if(!bullet->active)
		{
			return bullet;
		}
	}

	IncreaseSize();
	
	return begin + GetCapacity();
}
Ejemplo n.º 3
0
	Block * BlockPool::GetBlock(){
		bool hasAvailableBlock = true;

		if (freeBlockStack.empty() == true){
			// Increase the pool size by 4MiB
			hasAvailableBlock = IncreaseSize(4 * 1024 * 1024, true);
		}

		if (hasAvailableBlock){
			Block *tmpBlock = freeBlockStack.top();
			freeBlockStack.pop();
			usedBlockAmount++;
			return tmpBlock;
		}
		else return nullptr;
	}
Ejemplo n.º 4
0
bool CLegacyAtlasAlloc::Allocate()
{
	atlasSize.x = 32;
	atlasSize.y = 32;

	std::vector<SAtlasEntry*> memtextures;
	for (std::map<std::string, SAtlasEntry>::iterator it = entries.begin(); it != entries.end(); ++it) {
		memtextures.push_back(&it->second);
	}
	sort(memtextures.begin(), memtextures.end(), CLegacyAtlasAlloc::CompareTex);

	bool success = true;
	int2 max;
	int2 cur;
	std::list<int2> nextSub;
	std::list<int2> thisSub;
	bool recalc = false;
	for (int a = 0; a < static_cast<int>(memtextures.size()); ++a) {
		SAtlasEntry* curtex = memtextures[a];

		bool done = false;
		while (!done) {
			if (thisSub.empty()) {
				if (nextSub.empty()) {
					cur.y = max.y;
					max.y += curtex->size.y + TEXMARGIN;
					if (max.y > atlasSize.y) {
						if (IncreaseSize()) {
 							nextSub.clear();
							thisSub.clear();
							cur.y = max.y = cur.x = 0;
							recalc = true;
							break;
						} else {
							success = false;
							break;
						}
					}
					thisSub.push_back(int2(0, cur.y));
				} else {
					thisSub = nextSub;
					nextSub.clear();
				}
			}

			if (thisSub.front().x + curtex->size.x > atlasSize.x) {
				thisSub.clear();
				continue;
			}
			if (thisSub.front().y + curtex->size.y > max.y) {
				thisSub.pop_front();
				continue;
			}

			// ok found space for us
			curtex->texCoords.x1 = thisSub.front().x;
			curtex->texCoords.y1 = thisSub.front().y;
			curtex->texCoords.x2 = thisSub.front().x + curtex->size.x - 1;
			curtex->texCoords.y2 = thisSub.front().y + curtex->size.y - 1;

			cur.x = thisSub.front().x + curtex->size.x + TEXMARGIN;
			max.x = std::max(max.x,cur.x);

			done = true;

			if (thisSub.front().y + curtex->size.y + TEXMARGIN < max.y) {
				nextSub.push_back(int2(thisSub.front().x + TEXMARGIN, thisSub.front().y + curtex->size.y + TEXMARGIN));
			}

			thisSub.front().x += curtex->size.x + TEXMARGIN;
			while (thisSub.size()>1 && thisSub.front().x >= (++thisSub.begin())->x) {
				(++thisSub.begin())->x = thisSub.front().x;
				thisSub.erase(thisSub.begin());
			}
		}

		if (recalc) {
			// reset all existing texcoords
			for (std::vector<SAtlasEntry*>::iterator it = memtextures.begin(); it != memtextures.end(); ++it) {
				(*it)->texCoords = float4();
			}
			recalc = false;
			a = -1;
			continue;
		}
	}

	if (npot) {
		atlasSize = max;
	}

	return success;
}
Ejemplo n.º 5
0
bool CTextureAtlas::Finalize()
{
	sort(memtextures.begin(), memtextures.end(), CTextureAtlas::CompareTex);

	bool success = true;
	int cury=0;
	int maxy=0;
	int curx=0;
	std::list<int2> nextSub;
	std::list<int2> thisSub;
	bool recalc=false;
	for(int a=0;a<memtextures.size();++a){
		MemTex *curtex = memtextures[a];

		bool done=false;
		while(!done){
			if(thisSub.empty()){
				if(nextSub.empty()){
					cury=maxy;
					maxy+=curtex->ysize;
					if(maxy>ysize){
						if(IncreaseSize())
						{
 							nextSub.clear();
							thisSub.clear();
							cury=maxy=curx=0;
							recalc=true;
							break;
						}
						else
						{
							success = false;
							break;
						}
					}
					thisSub.push_back(int2(0,cury));
				} else {
					thisSub=nextSub;
					nextSub.clear();
				}
			}
			if(thisSub.front().x+curtex->xsize>xsize){
				thisSub.clear();
				continue;
			}
			if(thisSub.front().y+curtex->ysize>maxy){
				thisSub.pop_front();
				continue;
			}
			//ok found space for us
			curtex->xpos=thisSub.front().x;
			curtex->ypos=thisSub.front().y;

			done=true;

			if(thisSub.front().y+curtex->ysize+TEXMARGIN<maxy){
				nextSub.push_back(int2(thisSub.front().x+TEXMARGIN,thisSub.front().y+curtex->ysize+TEXMARGIN));
			}

			thisSub.front().x+=curtex->xsize+TEXMARGIN;
			while(thisSub.size()>1 && thisSub.front().x >= (++thisSub.begin())->x){
				(++thisSub.begin())->x=thisSub.front().x;
				thisSub.erase(thisSub.begin());
			}

		}
		if(recalc)
		{
			recalc=false;
			a=-1;
			continue;
		}
	}

	CreateTexture();
	for(int i=0; i<memtextures.size(); i++)
	{
		AtlasedTexture tex;
		//adjust texture coordinates by half a pixel to avoid filtering artifacts
		float halfx = 1/((float)xsize*2);
		float halfy = 1/((float)ysize*2);
		tex.xstart = memtextures[i]->xpos/(float)xsize + halfx;
		tex.xend = (memtextures[i]->xpos+memtextures[i]->xsize)/(float)xsize - halfx;
		tex.ystart = memtextures[i]->ypos/(float)ysize + halfy;
		tex.yend = (memtextures[i]->ypos+memtextures[i]->ysize)/(float)ysize - halfy;
		tex.ixstart = memtextures[i]->xpos;
		tex.iystart = memtextures[i]->ypos;
		for(int n=0; n<memtextures[i]->names.size(); n++)
			textures[memtextures[i]->names[n]] = tex;

		usedPixels += memtextures[i]->xpos*memtextures[i]->ypos;
		delete [] (char*)memtextures[i]->data;
		delete memtextures[i];
	}
	memtextures.clear();

	return success;
}
Ejemplo n.º 6
0
bool CTextureAtlas::Finalize()
{
	sort(memtextures.begin(), memtextures.end(), CTextureAtlas::CompareTex);

	bool success = true;
	int maxx = 0;
	int curx = 0;
	int maxy = 0;
	int cury = 0;
	std::list<int2> nextSub;
	std::list<int2> thisSub;
	bool recalc = false;
	for (int a = 0; a < static_cast<int>(memtextures.size()); ++a) {
		MemTex* curtex = memtextures[a];

		bool done = false;
		while (!done) {
			if (thisSub.empty()) {
				if (nextSub.empty()) {
					maxx = std::max(maxx, curx);
					cury = maxy;
					maxy += curtex->ysize + TEXMARGIN;
					if(maxy > ysize) {
						if(IncreaseSize()) {
 							nextSub.clear();
							thisSub.clear();
							cury = maxy = curx = 0;
							recalc = true;
							break;
						} else {
							success = false;
							break;
						}
					}
					thisSub.push_back(int2(0, cury));
				} else {
					thisSub = nextSub;
					nextSub.clear();
				}
			}

			if (thisSub.front().x + curtex->xsize > xsize) {
				thisSub.clear();
				continue;
			}
			if (thisSub.front().y + curtex->ysize > maxy) {
				thisSub.pop_front();
				continue;
			}

			//ok found space for us
			curtex->xpos = thisSub.front().x;
			curtex->ypos = thisSub.front().y;

			done = true;

			if (thisSub.front().y + curtex->ysize + TEXMARGIN < maxy) {
				nextSub.push_back(int2(thisSub.front().x + TEXMARGIN, thisSub.front().y + curtex->ysize + TEXMARGIN));
			}

			thisSub.front().x += curtex->xsize + TEXMARGIN;
			while (thisSub.size()>1 && thisSub.front().x >= (++thisSub.begin())->x) {
				(++thisSub.begin())->x = thisSub.front().x;
				thisSub.erase(thisSub.begin());
			}

		}
		if (recalc) {
			recalc = false;
			a = -1;
			continue;
		}
	}

	if (globalRendering->supportNPOTs && !debug) {
		maxx = std::max(maxx,curx);
		//xsize = maxx;
		ysize = maxy;
	}

	CreateTexture();
	for(std::vector<MemTex*>::iterator it = memtextures.begin(); it != memtextures.end(); ++it) {
		AtlasedTexture tex;
		//adjust texture coordinates by half a pixel (in opengl pixel centers are centeriods)
		tex.xstart =                    ((*it)->xpos + 0.5f) / (float)(xsize);
		tex.xend   = (((*it)->xpos + (*it)->xsize-1) + 0.5f) / (float)(xsize);
		tex.ystart =                    ((*it)->ypos + 0.5f) / (float)(ysize);
		tex.yend   = (((*it)->ypos + (*it)->ysize-1) + 0.5f) / (float)(ysize);
		tex.ixstart = (*it)->xpos;
		tex.iystart = (*it)->ypos;
		for (size_t n = 0; n < (*it)->names.size(); ++n) {
			textures[(*it)->names[n]] = tex;
		}

		usedPixels += (*it)->xpos * (*it)->ypos;
		delete[] (char*)(*it)->data;
		delete (*it);
	}
	memtextures.clear();

	return success;
}