Animated tiles, need some input

Started by
9 comments, last by Nausea 11 years, 3 months ago
Hey,

So I was thinking of how I would go about creating tiles which will be animated. I think I have a pretty good idea, but I just wanted to see what some more well versed people think about it.

My thought is to have a Tileframe class that will just have X/Y coordinate for where from the image the picture will be drawn. The tile class will then have a list (vector) of tileframes that animation will loop over. The map class will then have a list of tiles. The map will handle all the saving and loading of map information to and from a binary file.

Does this sound like a good approach?

For map editing will I have to have one of each available tile hardcoded into the game that a new placed tile will copy it´s information from (animation etc) ?

Would be great to have some input, and maybe some alternative ways. And if this would be bad for performance in any way or if it´s a bad idea to save so much for a pretty simple tile map.

Thanks
Advertisement

One thing to bear in mind is that different animations have different delays between frames - you need some way to specify the delay per frame of different animations loaded in your game. Some animations even have different delays between each frame (this is less common, though, and can be ignored)

One thing to bear in mind is that different animations have different delays between frames - you need some way to specify the delay per frame of different animations loaded in your game. Some animations even have different delays between each frame (this is less common, though, and can be ignored)

Ye this I know already, might not do different speed between the frames. However, I will maybe try to do it just for the learning experience.

I was thinking something like having a way to tell the tile renderer to update a tile (assuming you dont resend all the data every frame), causing it to ask the tile class what it currently looks like (uvs/texture) given the context (position, world, data the tile instance contains)

o3o

I was thinking something like having a way to tell the tile renderer to update a tile (assuming you dont resend all the data every frame), causing it to ask the tile class what it currently looks like (uvs/texture) given the context (position, world, data the tile instance contains)

Could you expand on what you are saying? Not very clear to a noob like me. :I

Well, not much information given.
Your using c++ i see that, any library/engines ? that would greatly narrow our approach towards the problem.

Use one variable for position instead of two

Also Saving X and Y for each image instead of one variable like (int pos) and use it to get image from TileSheet?

EX:
//Tile size 32x32
//Tilesheet size 128x128 class Tile
{
public:
int pos = 6;
};

int TileSheetSize = Image_Tile_Sheet.getSize().x / TILE_SIZE; // This would provide size of tilesheet.x as tiles

draw(Tile_List[My_Tile_To_Draw_From_Vector].pos % Tile_Sheet_Size,
Tile_List[My_Tile_To_Draw_From_Vector].pos / Tile_Sheet_Size)//x, y

Now if you wandering what did this do?

Let me try to explain.

Image was 128x128 we divided by 32 we get 4x4

00 01 02 03 04 05 06 07 08 08 10 11 12 13 14 15

We grab the 06 witch would evaluate to 2 ints (x = 3 or x = 64, y = 2 or y = 32)

Tip for animated tiles

Also considering the animated tiles.
Point all tiles to one pointer / reference / sprite / texture that will hold the animation image, so in process of changing animated tile you just change one tile and all using this data instead of looping true all tiles, looking for specified ones and updating them all.

My thought on your approach


My thought is to have a Tileframe class that will just have X/Y coordinate for where from the image the picture will be drawn. The tile class will then have a list (vector) of tileframes that animation will loop over. The map class will then have a list of tiles. The map will handle all the saving and loading of map information to and from a binary file.

Does this sound like a good approach?

[/quote]

Define good. Most efficient No, Able to work this way YES, Would i do it this way NO.

But have in mind that (Thinking of something on your own, creating on your own) will allow you to develop a sense of (oh this is good, oh this is bad)

When i was starting to program my cousin shown me how to output to console and told me to output 100 times same text onto console.

After i finished that he shown me 'for' loop (hes asshole), doing something hard way is not without it benefits, you will find creative ways of dealing with code even then, and learning to do stuff efficiently comes much on its own.

I mean who wants to write same code 10 times? make a function for it and use it 10 other places to save up to 10x in time - function creation time = profit.

Am pretty sure most of "Experienced" programers would be able to improve your idea / design, but experience gained from you doing it the way you thought off, priceless. Am most sure you will notice flaws and think of better ideas as you progress.

Good luck!

Tip for animated tiles
Also considering the animated tiles.
Point all tiles to one pointer / reference / sprite / texture that will hold the animation image, so in process of changing animated tile you just change one tile and all using this data instead of looping true all tiles, looking for specified ones and updating them all.
How do you mean? I will be loading a big sprite sheet into a asset manager and all the tiles will use this surface. I won't load it for every tile, just once and then point to it. Could you expand on how you mean I will update all by changing one, if I understand you correctly? Won't I have to go through all tiles and update the animated ones on every frame?

Or I what i meant was that the map class will hold what tileset (spritesheet) to use and then all the tiles will use that one.

And I will use SDL btw.

If you wont have performance issues it will be fine to do it the way you intent to.

Didn't do SDL in long time...

What i meant to do is


Declaration
SDL_Surface* water_Tile; // Surface that can load images and will be base that all tiles will point to
class TileBase{
public:
int type;
SDL_Surface** img;
};
Setup
if(Tile_Base.type_of_tile == enu_water_tile)
    Tile_Base.img = &water_Tile; // I know SDL uses allot of pointers, don't remember if this would be valid so you can access what water_Tile holds
//So you could do following when drawing tiles
SDL_BlitSurface( *Tile.img , NULL, Screen , Position);
The game loop
if(1 second passed)
{
water_Tile.update_Next_Frame(); //Here just grab next frame / image for water_Tile
}

water_Tile would be updated once and all tiles therefore would be updated.

NOTE::
This may not be the valid way to use in SDL, am unsure if doing SDL_Surface** and then using memory adress of another SDL_Surface* would provide you results your striving to achieve. Just trying to provide you with concept.

I apologize if the code is in mess, but editing the post totally replaces each newline with space character :(

I apologize if the code is in mess, but editing the post totally replaces each newline with space character sad.png

Ok, well thank you for your input anyway. Looks like I'm not totally lost on the subject after all. Well, guess I will have to test it out and see how it performs if I can manage to get it working.

Again, thank you :)

This topic is closed to new replies.

Advertisement