Public Group

# directx bitmap manager

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Man, I've posted so many questions since I've joined GD I feel like I'm abusing the system! lol I'm writing my first game engine as a 2d sprite-based RPG-type game in C++ using DirectX (DirectDraw). My graphic format is just straight bmp for simplicity. I can handle fixed-size bmps easily, (same-size tiles and chars like in ye olde Finale Fantasie) but I want a more versatile gfx system. My question for you, dear readers, is where can I find a resource on building a bmp manager for variable-size bmps? For instance, in street fighter, Zangief might be 100px tall x 40 px wide while standing, but shrink to 80px x 60px while dashing forward. Does anyone know of any useful links, or how to set up the backbuffer to accomodate this, if I should hold each picture in a separate bmp or combine them all into one sprite sheet or the approximate airspeed of an unladen swallow?

##### Share on other sites
I'm still a beginner, but do you think this question might get more answers in the tile-based forum?

I've got all the abstracts worked out, but I'm still a little fuzzy on the implementation!

##### Share on other sites
put your bitmaps onto 256x256 sized off-screen surfaces (usually during a load screen). use transparentcy (like a violet background #FFFF00FF). the backbuffer should always be the same size as the primary buffer (the screen size, maybe 640x480). blit the offscreen surfaces to the backbuffer as needed but not the whole surface, just the area with the sprite you want.

##### Share on other sites
Should the 256x256 bmp contain all the sprites for that area?

Should that bmp contain my tileset for that map?

How could I manage variably-sized sprites? For instance, one frame might be 100x80 where the next could be 90x90 for a different pose of the same object.

What should go onto that 256x256 bmp? Just one character, all characters in that map?

Sorry if I don't get what you're saying, but I understand the various buffers and color keys. I'm just having trouble encapsulating all that into a class.

I really do appreciate your help, though. :-D

##### Share on other sites
Should the 256x256 bmp contain all the sprites for that area?
no.

Should that bmp contain my tileset for that map?
one (or more if they all wont fit on one) of the offscreen surfaces should contain the map tileset. if the background is static (like in a 2D fighting game) you might want a background offscreen surface which would be the same size as the background bitmap.

How could I manage variably-sized sprites? For instance, one frame might be 100x80 where the next could be 90x90 for a different pose of the same object.
its all about the from and to rectangles when doing your blitting. the from rect may be (32,32,32+90,32+90) and the to could be (70,300). so the actual size of the surface is not an issue when it comes to sprite sizes (as long as the surface is big enough to fit the sprite).

What should go onto that 256x256 bmp? Just one character, all characters in that map?
just one character, unless you can fit all of the characters.

Sorry if I don't get what you're saying, but I understand the various buffers and color keys. I'm just having trouble encapsulating all that into a class.

the class should be an offscreen surface class not a bitmap class. the only time you use bitmaps is when you load them from a file and put them onto the offscreen surfaces.

in the render function (where you draw one frame) you would first clear the backbuffer or blit the background surface if you have one. after that you would blit the various offscreen surfaces. like if you have a character on a surface at (32,32) and the sprite is 90x90 than you would blit that area (32,32,32+90,32+90) from the surface onto the backbuffer at where ever the character is located on the screen.

##### Share on other sites
I see what you mean, but does that mean I would have to load a bitmap into my offscreen buffer every time I want to blit a different character?

i.e. in one render pass I would load the tileset, blit the background, load another bitmap and blit one character, load another bitmap and blit another character, re-load the tileset and blit overhead objects (trees and such), etc...isn't all that bitmap loading from HDD a lot of overhead?

To clarify, I'm actually making an RPG, I just don't like sprites that are all the same size for no reason.

##### Share on other sites
A bit more effecient way would be to create a manager which dynamically allocates memory and returns a UINT or some number to identify what it has just created. When you need to render something, just call a Render function and give the UINT it returned when created the file.

This way, during loadup you might do something like
Manager.CreateTile("something.bmp, size, &SomeUintToHoldValue);

The manager would take care of allocating space (dynamically allocating variables, trees anything of your choice Just make sure that it can be accessed by a UINT or you can extract the info using it)

Now once thats called, the Uint would have a refereance to the tile.

When you're rendering, Just do something like

Game.BeginRendering()
Manager.DrawTile(UINT, position)
manager.DrawTile(AnotherUINT, position)
Game.EndRendering()

of course you could include conditionaly statements to set which is rendered etc. But this is just an example.

Hope thats clear enough.

EDIT: If you're wondering, what I usually do is have a variable in the manager to hold how many bitmaps there are. I also have an array of a set number of spaces (take 50 for example). Whenever I add a new Tile, I check to see that if the Number of tiles MOD 50 is equal to zero, then I need to reallocate for another 50 spaces. This way, the manager isn't bogged down with reallocating for everysingle thing I add. You can ofcourse make that number smaller if you don't need so much.

Then, Once Ive put the info in the array ArrayOfData[m_numofbitmaps], I just set the variable sent as a parameter to m_numofBitmaps and then ++ the m_numbitmaps variable and its ready to take more.

Somethign roughly like this:
HRESULT TileManager::AddTile(char *chFile, UINT *nTileID){	Tileformat Tile	Tile.Load(chFile) //do whatever you need to do to create the tile information		//Add it to the manager	// allocate 10 new memory slots for materials if necessary	if ( (m_nNumTiles%10) == 0 ) 	{		int n = (m_nNumTiles+10)*sizeof(TileFormat);		m_pTiles= (TileFormat*)realloc(m_pTiles, n);		if (!m_pTiles)		{			Log("Out of Memory when allocating for more models");			return MD_FAIL;		}	}	//Put it into the index and return the ID	memcpy(&m_pTiles[m_nNumTiles], &Tile, sizeof(TileFormat));	if (The thing aint created properly)	{		Log("couldn't copy created model to index");		return FAIL;	}	*nTileID = m_nNumTiles;	m_nNumTiles++;	return OK;}

##### Share on other sites
you do not load any files in the render function. files should be loaded once, in the load function. so you would have many offscreen surfaces with the bitmaps already loaded onto them. and you just use the offscreen surfaces in the render function.

offscreen surfaces:
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/ddraw7/directdraw7/ddover_53jr.asp

##### Share on other sites
Thanks, Mistro, that's a little more along the lines I was thinking of, what with the dynamic allocation and all. Rating plus 1!

Do you have any source code I could peek at? Or, perhaps, could you tell me how you set up your sprites? Do you have each frame in its own file, or do you identify it with a RECT on a sprite sheet?

I'm just trying to be as efficient as possible in my use of surfaces, because my sprites have differently sized frames, so if I cram them into one sprite sheet it won't use that space very efficiently.

@twkr:
When do you load those bitmaps, when you load the map of the current area? If so, how many offscreen_plain surfaces do you use? Let's say one map contained 3 NPCs whereas another contained 12. Would you create 12 surfaces for sprite sheets and just load 3 on the smaller map?

I'll rate you up too, for your trouble. Although, I don't know if I'll have much effect on anyone's rating. :-P

Sorry if I'm being a bother, but I'm trying to make my code more OOP and more dynamic, so I'm probably just making my implementation harder than it has to be. lol

##### Share on other sites
Well actually that example up there is what I was using a while back for a model manager. I just changed it around a bit.

A good book which I recomend would "beggining game programming" by jonathan s harbour (not to be confused with begginging C++ game programming)

This book actually goes through creating a "manager" for your applications. It is mostly C style but you can easily make it into classes and such.

It starts of with Blitting, The goes to DX bitmaps and surfaces, how to draw animated sprites, then goes on to the things you were talking about like tiled sprites and how to extract information from it.

It also covers sound, input and an intro to 3d programming up to loading and rendering models (albeit a bit inefficiently)

As for holding sprite data, I remember using an array of LPDirect3dsurface9's to hold the data when needed (ie loading the main part of the game so your charecter would be loaded via the manager) Then just hold a ticker to animate it each frame by itterating through the array each frame and rendering that portion.

If you follow the manager method above and get a handle via a uint for each sprite, then you just need an arary of UINTs. Each frame, you just itterate through each index. You might want to create a sort of system where it dosn't itterate one index per frame but rather one index per n milliseconds or whatever.

I however highly recomend that book since it teaches you all the basics of creating sprite based games, teaches you to make a Pong type game in 2d. Then goes on to 3d and introduces you to that which a good place to start if you're interested in moving on to 3d (which I'm guessing you are).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634073
• Total Posts
3015343
×