Archived

This topic is now archived and is closed to further replies.

Lord Maz

A really simple texture bar.. NOT :-)

Recommended Posts

Lord Maz    110
I'm sitting here and trying to iron out the last few bugs in my texture bar for the editor to the simple 2d game I'm making, and I was wondering just for the fun of it if I've done it much more complicated than it had to be.. I'm trying to make a texture bar with 23 slots that loads up a .bmp texture and cuts it up to pieces, placing each texture in a slot. When right arrow is pressed, all the textures are pushed to the right and if there's space over at the left side, the overlapsing textures to the right should come to the left instead.. ugh, hope you understand how i mean The whole thing needs to be dynamic, the texture bar might become smaller and the number of textures are increased all the time.. so.. How would you solve this little problem? I've tried 3 completely different solutions so far, the one right now (the best one so far) is to go through all the textures in the bar and using a check value where it should be placed, for example: I have a bar with 3 slots, and I use 2 textures. The first texture is in the first slot (currtile = 0). This would be simple, the checkvalue (check) is the position of the first texture + the value of the loop (i) that goes through all the slots in the bar and checks if anything should be placed: Currtile + i = check. Since there's two textures and three slots and the first texture is in the first slot, there can't be any overlapsing textures. So simply place the texture number (which equals i) in the right place of the struct: G.EditorBars[check][0] = i; (Never mind the [0], it's going to be used later on ) So, in this example I end up with this structure: 0,1,- - which is exactly what I want Later on I use the number*the tile widht to render stuff as it should be. This is easy, but when it comes to textures overlapsing it gets hard. When there's more textures than slots it's even harder, and that's what I'm working on right now.. Here's the source if anyone is interested:
    
	RECT srcTB, destTB;

	int wHNR, rFM;
	int check;
	int realCTile = G.currtile; // just a little helper, starts at 0

	int realTileCols = EDITOR_TILECOLS - 1; // another helper, starts at 0

															
	wHNR = NUM_TEX_A - EDITOR_TILECOLS;		// wHNR = we Have No Room: Used when there's more textures then slots

	if (wHNR < 1)								// in the bar. ('bout the name: sorry, too tired to come up with something better ;)

		wHNR = 0;

	rFM = realCTile	//rFM = room For More!!!


	for (int i = 0; i <= realTileCols; i++)
	{
		G.EditorBars[i][0] = 0;
	}
		
	for (i = 0; i < NUM_TEX_A; i++)
	{
		check = realCTile + i;

		if (check > realTileCols) //if overlapse

		{
			if (rFM > 0 && wHNR < 1) //if there's space over for the texture at the beginning

			{
				G.EditorBars[realCTile - rFM][0] = i; // put the texture as far to the left as possible

				rFM--;
			}
			
			else if (wHNR > 0)
			{
				wHNR--;
				continue;
			}

			else 
			{
				continue;
			}
		}
		
		else
		{
			G.EditorBars[check][0] = i;
		}

	}	
	
	for (i = 0; i <= realTileCols; i++)
	{
		srcTB.left = G.EditorBars[i][0] * TILE_SIZE;
		srcTB.top = 0;
		srcTB.right = srcTB.left + TILE_SIZE;
		srcTB.bottom = srcTB.top + TILE_SIZE;

		destTB.left = TILE_SIZE + (i * TILE_SIZE);
		destTB.top = SCREEN_HEIGHT - POFFSET - TILE_SIZE * 2;
		destTB.right = destTB.left + TILE_SIZE;
		destTB.bottom = destTB.top + TILE_SIZE;


		if  (!(
			srcTB.left		< 0								||
			srcTB.right		> (NUM_TEX_A * TILE_SIZE)		|| 
			srcTB.top		< 0								|| 
			srcTB.bottom	> TILE_SIZE
			))

			G.lpDDSBack->Blt(&destTB, G.lpDDSTex2, &srcTB, DDBLT_KEYSRC | DDBLT_WAIT, NULL);
	}
    
I know it's not perfectly clear, if you want an explaination just ask. It have a few bugs right now: it works properly only if there's more textures then slots and it doesn't work perfect then either If you think my coding sucks, you are probably right because so far I've learned only from books. btw there's no real meaning in this post, I'll soon get it working anyway.. it's "just for the fun of it" Edited by - Lord Maz on October 17, 2001 4:30:01 PM

Share this post


Link to post
Share on other sites
Advanced Bug    122
Here is my solution:

  

int numSlots;
int numTiles;

int currentTile = 0; // Current tile


void DrawEmptySlot(int slot)
{
// Code to draw an empty slot

}

void DrawSlot(int slot, int tile)
{
// Code to draw a slot with a tile

}

int ScrollingSequenceLength()
{
if(numSlots > numTiles)
return numSlots;
else
return numTiles;
}

void DrawTiles()
{
for(int slot = 0; slot < numSlots; slot++)
{
int tile = (slot + currentTile) % ScrollingSequenceLength();
if(tile < numTiles)
DrawSlot(slot, tile);
else
DrawEmptySlot(slot);
}
}

void ScrollTiles()
{
currentTile++;
if(currentTile >= ScrollingSequenceLength())
currentTile = 0;
}

Share this post


Link to post
Share on other sites