SDL tile animation?

Started by
0 comments, last by samuraicrow 16 years, 5 months ago
I'm making a 2D tile tank game, and i have problems with my animations the thing is that i load my level from a file, it looks like this: 02 01 01 01 02 04 03 01 00 00 00 00 03 00 00 04 01 01 00 00 01 00 00 00 00 00 00 00 07 08 00 03 02 05 06 00 00 00 00 03 03 04 01 01 00 00 01 01 00 04 04 04 00 00 00 01 and if there is a 7 i load the first frame, my images (tiles) are all in one file, the base tiles and the frames and i separate them by cliping smaller images from the big one. the problem comes when i move my camera moves it apears that the frames folow the camera, this problem has been solved for all the tiles but it hasen't been for the frames becouse i load ONLY the level from a txt file and in a txt file i can only load the FIRST frame, the frames are actualy SDL_rect's
      red_gate_count+=1;
      clips [TILE_GATE_RED] = clips [red_gate_count]; 
      if (red_gate_count>=27)
      {
         red_gate_count=9;
      }
this is the code in the main loop that increases the frame of the animation (red_gate_cunt) does anyone have experience with tile animation???
Advertisement
You might be able to combine some of your code to make a simpler design. Also, you shouldn't need to modify the contents of your map array for animation if you make each element a struct or class containing the base number and frame count of each tile pattern.

The following example code assumes the existence of a blit_rect() routine elsewhere in your code and some other stuff but it should give you an idea of where to start. Also, my example doesn't take into account scrolling but adding that shouldn't be hard.
// tile_anim.hppclass tile_anim{  private:    int first;  // first frame of the tile animation    int frames;  // number of frames in the tile animation    int count;  // frame counter  public:    // Note: These following implementations should probably just be prototypes    // with their implementation in a separate C++ file for static linkage.    void update(int x,int y)    {      // increment counter with range check resetting it to 0      this->count=(this->count+1)%this->frames;      // blit the tile to the screen if visible      blit_rect(x*TILE_WIDTH, y*TILE_HEIGHT, this->first+this->count);    }    tile_anim(int first_rect, int num_frames)    {      first=first_rect;      frames=num_frames;      count=0;  // set the counter to be the first frame of the animation    }};


// main.cpp#include "tile_anim.hpp"tile_anim *red_gate=new tile_anim(9,18);...

This topic is closed to new replies.

Advertisement