Jump to content
  • Advertisement
Sign in to follow this  

My tiles in memory

This topic is 3848 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;
    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);

        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) 

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

        if(currFrame > range+offset)

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

        lastTicks = SDL_GetTicks();

//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(); 
                tiles[index].image = NULL; 
        for(int i = n;i-n < TilesToLoadAtOnce && i < tiles.size();i++){ 
                SDL_Surface* tmpSurface = SDL_LoadBMP(tiles.name.c_str());  

                    cerr << "Errore nell'apertura img " << tiles.name;
                    return false;

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


        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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!