Jump to content
  • Advertisement
Sign in to follow this  
programering

Loading/Saving class data

This topic is 4975 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

I just wanted to post this to help you who don't know how to do it. For example:

#define DataFromBuffer(data,buff) memcpy(buff,(byte*)&data,sizeof(data); buff += sizeof(data);

#define DataToBuffer(data,buff) memcpy((byte*)&dat,buff,sizeof(data); buff += sizeof(data);

class CObject {
public:
    CObject(short type, int x, int y)
    {
        indexObjDef = type;
        pObjDef = GetObjectDef(type);
        xpos = x;
        ypos = y;
    }
    CObject(byte &*data) { LoadData(data); }
    void LoadData(byte &*buffer)
    {
        DataFromBuffer(indexObjDef,buffer);
        pObjDef = GetObjectDef(indexObjDef);
        DataFromBuffer(xpos,buffer);
        DataFromBuffer(ypos,buffer);
        DataFromBuffer(indexCurBmp,buffer);
        pCurBmp = pObjDef->anim->bitmaps + indexCurBmp;
        DataFromBuffer(indexCurSrcRect,buffer);
        pCurSrcRect = pObjDef->anim->srcRects + indexCurSrcRect;
    }
    void SaveData(byte &*buffer)
    {
        DataToBuffer(indexObjDef,buffer);
        DataToBuffer(xpos,buffer);
        DataToBuffer(ypos,buffer);
        DataToBuffer(indexCurBmp,buffer);
        DataToBuffer(indexCurSrcRect,buffer);
    }
protected:
    short indexObjDef;
    ObjectDef *pObjDef;
    int xpos;
    int ypos;
    short indexCurBmp;
    SDL_Surface *pCurBmp;
    short indexCurSrcRect;
    SDL_Rect *pCurSrcRect;
};

std::vector<CObject *> vecObjects;
//short numObjects;

void LoadObjects(char *filename)
{
    FILE *pFile = fopen(filename,"rb");
    fseek(pFile,SEEK_END,0);
    long size = ftell(pFile);
    fseek(pFile,SEEK_SET,0);
    byte *buffer = new byte[size];
    fread(pFile,buffer,size);
    short amount;
    DataFromBuffer(amount,buffer);
    vecObjects(amount);
    byte *temp = buffer;
    for (int i = 0; i < amount; i++)
    {
        vecObjects.push_back(new CObject(temp));
    }
    free(buffer);
}


byte *GetFileBuffer(char *filename, long *size)
{
    FILE *pFile = fopen(filename,"rb");
    fseek(pFile,SEEK_END,0);
    *size = ftell(pFile);
    fseek(pFile,SEEK_SET,0);
    byte *buffer = new byte[*size];
    fread(pFile,buffer,size);
    fclose(pFile);
    return buffer;
}


void LoadObjects(char *filename)
{
    long size;
    byte *buffer = GetFileBuffer(filename,&size);
    short amount;
    DataFromBuffer(amount,buffer);
    vecObjects(amount);
    for (int i = 0; i < amount; i++)
    {
        vecObjects.push_back(new CObject(buffer));
    }
    free(buffer);
}    


void WriteFileBuffer(char *filename,byte *buffer,long size)
{
    FILE *pFile = fopen(filename,"wb");
    fwrite(pFile,buffer,size);
    fclose(pFile);
}

void SaveObjects(char *filename)
{
    short amount = vecObjects.size();
    long size = amount + sizeof CObject;
    byte *buffer = new byte[size];
    byte *temp = buffer;
    DataToBuffer(amount,temp);
    for (int i = 0; i < amount; i++)
    {
        vecObjects.at(i)->SaveData(temp);
    }
    WriteFileBuffer(filename,buffer,temp-buffer);
}

or without the constructor loading:

#define DataFromBuffer(data,buff) memcpy(buff,(byte*)&data,sizeof(data); buff += sizeof(data);

#define DataToBuffer(data,buff) memcpy((byte*)&dat,buff,sizeof(data); buff += sizeof(data);

class CObject {
public:
    CObject(short type, int x, int y)
    {
        indexObjDef = type;
        pObjDef = GetObjectDef(type);
        xpos = x;
        ypos = y;
    }
    byte *LoadData(byte *buffer)
    {
        DataFromBuffer(indexObjDef,buffer);
        pObjDef = GetObjectDef(indexObjDef);
        DataFromBuffer(xpos,buffer);
        DataFromBuffer(ypos,buffer);
        DataFromBuffer(indexCurBmp,buffer);
        pCurBmp = pObjDef->anim->bitmaps + indexCurBmp;
        DataFromBuffer(indexCurSrcRect,buffer);
        pCurSrcRect = pObjDef->anim->srcRects + indexCurSrcRect;
        return buffer;
    }
    byte *SaveData(byte *buffer)
    {
        DataToBuffer(indexObjDef,buffer);
        DataToBuffer(xpos,buffer);
        DataToBuffer(ypos,buffer);
        DataToBuffer(indexCurBmp,buffer);
        DataToBuffer(indexCurSrcRect,buffer);
        return buffer;
    }
protected:
    short indexObjDef;
    ObjectDef *pObjDef;
    int xpos;
    int ypos;
    short indexCurBmp;
    SDL_Surface *pCurBmp;
    short indexCurSrcRect;
    SDL_Rect *pCurSrcRect;
};

std::vector<CObject *> vecObjects;
//short numObjects;

void LoadObjects(char *filename)
{
    FILE *pFile = fopen(filename,"rb");
    fseek(pFile,SEEK_END,0);
    long size = ftell(pFile);
    fseek(pFile,SEEK_SET,0);
    byte *buffer = new byte[size];
    fread(pFile,buffer,size);
    short amount;
    DataFromBuffer(amount,buffer);
    vecObjects(amount);
    byte *temp = buffer;
    for (int i = 0; i < amount; i++)
    {
        CObject *obj = new CObject(0,0,0);
        temp = obj->LoadData(temp);
        vecObjects.push_back(obj);
    }
    free(buffer);
    fclose(pFile);
}


void SaveObjects(char *filename)
{
    FILE *pFile = fopen(filename,"wb");
    short amount = vecObjects.size();
    long size = amount + sizeof CObject;
    byte *buffer = new byte[size];
    byte *temp = buffer;
    DataToBuffer(amount,temp);
    for (int i = 0; i < amount; i++)
    {
        temp = vecObjects.at(i)->SaveData(temp);
    }
    fwrite(pFile,buffer,temp-buffer);
    fclose(pFile);
}

but the best way to do it is probably direct file read/writing:

class CObject {
public:
    CObject(short type, int x, int y)
    {
        indexObjDef = type;
        pObjDef = GetObjectDef(type);
        xpos = x;
        ypos = y;
    }
    CObject(FILE *src)
    {
        LoadData(src);
    }
    void LoadData(File *src)
    {
        fread(src,indexObjDef,sizeof(indexObjDef));
        pObjDef = GetObjectDef(indexObjDef);
        fread(src,xpos,sizeof(xpos));
        fread(src,ypos,sizeof(ypos));
        fread(src,indexCurBmp,sizeof(indexCurBmp));
        pCurBmp = pObjDef->anim->bitmaps + indexCurBmp;
        fread(src,indexCurSrcRect,sizeof(indexCurSrcRect));
        pCurSrcRect = pObjDef->anim->srcRects + indexCurSrcRect;
    }
    void SaveData(FILE *dst)
    {  // Note. one must write them in the same order as loading
        fwrite(dst,indexObjDef,sizeof(indexObjDef));
        fwrite(dst,xpos,sizeof(xpos));
        fwrite(dst,ypos,sizeof(ypos));
        fwrite(dst,indexCurBmp,sizeof(indexCurBmp));
        fwrite(dst,indexCurSrcRect,sizeof(indexCurSrcRect));
    }
protected:
    short indexObjDef;
    ObjectDef *pObjDef;
    int xpos;
    int ypos;
    short indexCurBmp;
    SDL_Surface *pCurBmp;
    short indexCurSrcRect;
    SDL_Rect *pCurSrcRect;
};

std::vector<CObject *> vecObjects;
//short numObjects;

void LoadObjects(char *filename)
{
    FILE *pFile = fopen(filename,"rb");
    short amount;
    fread(pFile,amount,sizeof(amount));
    vecObjects(amount);
    for (int i = 0; i < amount; i++)
    {
//      CObject *obj = new CObject(0,0,0);
//      obj->LoadData(pFile);
//      vecObjects.push_back(obj);
        // or with the loading constructor:
        vecObjects.push_back(new CObject(pFile));
    }
    fclose(pFile);
}


void SaveObjects(char *filename)
{
    FILE *pFile = fopen(filename,"wb");
    short amount = vecObjects.size();
    fwrite(pFile,amount,sizeof(amount));
    for (int i = 0; i < amount; i++)
    {
         vecObjects.at(i)->SaveData(pFile);
    }
    fclose(pFile);
}

I just wonder if fread() and fwrite() moves the file pointer after calling/writing/reading?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by programering
#define DataFromBuffer(data,buff) memcpy(buff,(byte*)&data,sizeof(data); buff += sizeof(data);

#define DataToBuffer(data,buff) memcpy((byte*)&dat,buff,sizeof(data); buff += sizeof(data);


In addition to the question about whether you've thought about using streams you appear to be missing closing parentheses on the two memcpy calls here. Also, why are you using #defines instead of templated functions?

Enigma

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!