Making a texture manager

Started by
10 comments, last by kzar 18 years, 5 months ago
Hi, I am making a 2d engine for my game. I am working on getting animation and objects working in a moduler way so that I can build on it. So far I have made animation class's (cSpriteFrame for each single frame, cSpriteBase to store the animation's details and cSprite for each instance of an animation), an object class (cObject just has a cSprite pointer and x,y coords at the moment) and a kind of system class (the engine, I kind of shove everything in it to avoid global variables). Now I was confused as to how to progress, how would the user make a object? He could make a spriteBase, sprite, and then the object manually, or I could make a function to automate it and do all 3 in one but then how could I avoid loading the same spriteBase twice. Should each object really have its own sprite? What happens when I want to have an object that could play a few different animations depending on stuff that happens? Anyway I am getting really confused at this point and I asked my housemate for some advice and he said he uses a teture manager to do this and I have been searching about guides about how to implement one. Anyway I am still a bit stuck. I read that you can use a texture manager to manage animations, particle effects and everything, which sounds great but is making my head hurt trying to figure out how to do it! I have started the texture_manager class which has a std::map to convert the file names of animations to a cSpriteBase pointer so that I can check if an animation has already been loaded before loading it agian. I'm not sure if this is the best way of doing it because that would work alright for making sure cSpriteBase's arn't loaded twice but it doesn't help me when objects have multiple animations or with particle effects or anything. Anyway if someone could explain a bit how to do it, it would be very helpfull. Thanks, David
Advertisement
That's a fine start. Just keep with that and you'll gain the experience that will teach you how to do the bigger, better, more complete version.

Yeah like Telastyn says youre on the right road, just take it one step at a time and keep adding whatever features you need. I use a std::map as well in my texture manager and it works for me.
I guess your right, normaly when I try and figure out a really clever way of doing somthing I just confuse myself and get stuck lol. Thanks

Hello David,



Sprite programming is really fun, because once you get it out of the way you'll be producing games like crazy, because sprites are the foundation of animation in game programming, and games are just animation with rules.



Anyway, I like the idea that your trying to do everything in modules, but their only useful if their properly organized, if you're wrapping every method in a module, it becomes redundent, and adds coding overhead with no benefit. The whole point of modules is to organize your code.



I would converge your spriteframe, and sprite class all together into a class called cSprite, and only keep SpriteBase separated. Have cSprite take care of what frame its on, and what animation to use, and where to find this animation, and have a SpriteBase object in Sprite. Your Object class, commonly known as an Entity class would then have have a pointer to cSprite. So to answer your question the user would then create a Entity object like so</pL

{    Entity Monster_Ogre(x, y);    Monster.cSprite = new cSprite("Ogre", x, y);}// here is a pseudo code implementation of cSprite()cSprite::cSprite(char *AnimationSet, int x, int y){     if ( strcmp(AnimationSet, "Mermaid") == 1)     {         GraphixManager::LoadSpriteSheet("MermaidSS.bmp");          cSpriteBase::LoadAnimationInfo("Mermaid.ani"); // SpriteBase then takes in the animation details     }     if ( strcmp(AnimationSet, "Ogre") == 1)     {         GraphixManager::LoadSpriteSheet("OgreSS.bmp");         cSpriteBase::LoadAnimationInfo("Ogre.ani", animation_info); // SpiteBase then takes in the animation details.     }     // assign member data     position_x = x;     position_y = y;     animation_frame = 0;     // rest of implementation} 


During your game logic, if the Monster_Ogre was moving, you would tell its cSprite to use its animation method with the "Ogre_Walking" animation, by calling its Animation Method cSprite.Animation("Walking"), if the monster was fighting you would call cSprite.Animation("Fighting"), if the monster takes damage you would call cSprite.Animation("Damage"). Because you already loaded the sprite sheet into memory when you created your entity, loading animation is as simple as telling your image pointers where to point to.




void cSprite::Animation(char *NameOfAnimation){    if (IsAnimationDifferent() == true) // see if were doing a different animation    {        SpriteBase::GetAnimationDetails(NameOfAnimation);        Frame = 0; // reset which frame we're on        MaxFrames = SpriteBase::MaxFrames; // redudent, just to illustrate how cSPrite and SpriteBase work    }    AnimateFrame(NameOfAnimation, frame, x, y);    frame++;    if (frame == SpriteBase::MaxFrames)    {       frame = 0; // loop the animation    } };// pseudo implemantation of AnimateFramevoid cSprite::AnimateFrame(char *NameOfAnimation, int frame, int x, int y){    IMAGE *ptr = (IMAGE)SpriteBase.GetImage(NameofAnimation, 5);    Render::TransparentBlit(ptr, x, y); // this can a global object that all entities see, or you can have a pointer to it in your sprite class, the skys the limit.// just keep it organized}


That's one way to do it, you don't need anything else, its that simple, your Entity object will take care of its position and whats happening to it, your Sprite only has to worry about what it's animating, your renderer takes care of actually rasterizing the images, and you only have to take care of your game logic.

Quote:
and games are just animation with rules.


Actually games are just rules. How you choose to present those rules to the player(s) is up to you, and by providing well engineered, modular code you can more easily mix/match/fix/upgrade the presentation of the game. Not so infrequently you won't even present animations, images or anything to the player with multiplayer gaming as prevalent as it is.

Oh, and strcmp returns 0 on a match, not 1.
Quote:Original post by ordered_disorder

Hello David,



Sprite programming is really fun, because once you get it out of the way you'll be producing games like crazy, because sprites are the foundation of animation in game programming, and games are just animation with rules.



Anyway, I like the idea that your trying to do everything in modules, but their only useful if their properly organized, if you're wrapping every method in a module, it becomes redundent, and adds coding overhead with no benefit. The whole point of modules is to organize your code.



I would converge your spriteframe, and sprite class all together into a class called cSprite, and only keep SpriteBase separated. Have cSprite take care of what frame its on, and what animation to use, and where to find this animation, and have a SpriteBase object in Sprite. Your Object class, commonly known as an Entity class would then have have a pointer to cSprite. So to answer your question the user would then create a Entity object like so</pL
*** Source Snippet Removed ***


During your game logic, if the Monster_Ogre was moving, you would tell its cSprite to use its animation method with the "Ogre_Walking" animation, by calling its Animation Method cSprite.Animation("Walking"), if the monster was fighting you would call cSprite.Animation("Fighting"), if the monster takes damage you would call cSprite.Animation("Damage"). Because you already loaded the sprite sheet into memory when you created your entity, loading animation is as simple as telling your image pointers where to point to.




*** Source Snippet Removed ***


That's one way to do it, you don't need anything else, its that simple, your Entity object will take care of its position and whats happening to it, your Sprite only has to worry about what it's animating, your renderer takes care of actually rasterizing the images, and you only have to take care of your game logic.




Cool that sounds good. So each sprite file stores all the possible animations for that sprite and each of the animations has a name.

So I would load a sprite text file up, setup the cSpriteBase with its values + an array of all the possible animations, then add a cSprite using that cSpriteBase and then add an object using that cSprite.

Say when I wanted to add an object that was an ogre though, where would I start? somthing like game_engine.add_object("ogre.txt") ?

I pretty much understand how to do it now, thanks for the replys.
Quote:
Say when I wanted to add an object that was an ogre though, where would I start? somthing like game_engine.add_object("ogre.txt") ?


Yes, that's it, the implementation is up to you. Generally it's very automatous, where you engine is adding and deleting objects based on the type of game your creating, and the parts that are being played.
Is there a way to loop through every single entry in a std::map ?
Quote:Original post by kzar
Is there a way to loop through every single entry in a std::map ?


for_each(...), or the general for(iterator=map.begin();iterator!=map.end();++iterator){

This topic is closed to new replies.

Advertisement