Sign in to follow this  

My tiles in memory

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here there is the simple idea that i'm devving for load the tiles of my sprites in memory. I pick the idea from this article. http://www.gamedev.net/reference/articles/article847.asp Here the code.

//frame class
class Sframe {
    SDL_Surface* image;
    int width,height,pause;
    int red,green,blue;//color key
    string name;
    friend class Ssprite;
    public:
    ~Sframe();
    Sframe(const string&,int,int,int,int,int,int);
};
//in the costructor of Sframe i init image to NULL,
//sprite class (i clean the code from from everything not about cache tiles
class Ssprite {
    SDL_Rect pos;

    vector<Sframe> tiles;
    list<int> cache;

    SDL_Surface* screen;

    int offset,range,currFrame;
    int lastTicks; //for animation

    bool CacheTiles(int);

    public:
        Ssprite(const char*,SDL_Surface*);
        void Draw();
        void SetAnimation(int,int,int);

};

//draw fuction (thanks to borntocode)
void Ssprite::Draw(){

//if tiles[currFrame].image == NULL the tile isn't loaded
    if(tiles[currFrame].image == NULL) 
        CacheTiles(currFrame);

    if(lastTicks + tiles[currFrame].pause < SDL_GetTicks()){

        currFrame++;
        if(currFrame > range+offset)
            currFrame=offset;

        if(tiles[currFrame].image == NULL) 
            CacheTiles(currFrame);


        lastTicks = SDL_GetTicks();
    }
    SDL_BlitSurface(tiles[currFrame].image,0,screen,&pos);

}
//And there is the cache fuction
bool Ssprite::CacheTiles(int n){
        if(cache.size() > MAXTILESLOADED){  
            int index;
            for(int i = 0;i < 150;i++){ 
                index = cache.back(); 
                SDL_FreeSurface(tiles[index].image); 
                tiles[index].image = NULL; 
                cache.pop_back(); 
            }
        }
        for(int i = n;i-n < TilesToLoadAtOnce && i < tiles.size();i++){ 
                SDL_Surface* tmpSurface = SDL_LoadBMP(tiles[i].name.c_str());  

                if(!tmpSurface){
                    cerr << "Errore nell'apertura img " << tiles[i].name;
                    return false;
                }

                tiles[i].image = SDL_DisplayFormat(tmpSurface); 
                SDL_SetColorKey(tiles[i].image,SDL_SRCCOLORKEY |    SDL_RLEACCEL,SDL_MapRGB(screen->format,tiles[i].red,tiles[i].green,tiles[i].blue));
                SDL_FreeSurface(tmpSurface);//libero la memoria della surface temporanea

                cache.push_front(i);

            }
        return true;

}
The vector Tiles is init by the costructor of Ssprite by parsing a textFile setting all SDL_Surface* image to NULL. What do you think about?Is it good? [Edited by - garulf on May 26, 2008 3:37:59 PM]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this