Advertisement

Map loading / Map Editor

Started by November 05, 2000 01:36 PM
0 comments, last by PsYchOtroW 24 years, 2 months ago
Hi, i have one prob with my map editor. This is the loading function: datei=fopen(path,"r"); if(datei==0) // Wenn die Datei nicht existiert { cout << "Error !"; return(0); } fread(&spalten,sizeof(int),1,datei); fread(&zeilen,sizeof(int),1,datei); plan=new int[zeilen*spalten]; for(int i=0;i),sizeof(int),1,datei); } And then the save function: int* OutPlan; FILE* datei; datei=fopen(path,"w+"); fwrite(&spalten,sizeof(int),1,datei); fwrite(&zeilen,sizeof(int),1,datei); //OutPlan=new int[zeilen*spalten]; for(int i=0;i<(zeilen*spalten);i++) { fwrite(&(plan),sizeof(int),zeilen*spalten,datei); } return 0; delete[]plan; delete[]OutPlan; Now i want to edit an position; int editpos( int x, int y, int wert ) { spalten = cFringe::GetMaxX( ); plan[ (y * spalten) + x ] = wert; return( 1 ); } But there is one prob. When the "int wert" is over 1 digit. like: wert = 1001; then it doesn`t works. but when it`s like this char ''a''; no prob. Why? Can someone help me? Thanx Lutz Hören </i>
psYchOtroW
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////// These values are STATIC within the Map Module...//static int* Plan;static int  NewSize, spalten, zeilen;bool ReadMap( char* path ){  FILE* datei=fopen(path,"r");   bool result = false;  // Do we have an open file...?  if (datei)   {    // 4-bytes for width/height...?    fread(&spalten,sizeof(int),1,datei);     fread(&zeilen,sizeof(int),1,datei);        // New Size to Create...    NewSize = zeilen * spalten;    Plan=new int[NewSize];         //Read the Entire file...    fread(&Plan,NewSize,1,datei);      result = true;      }  else cout << "Error !";    return result;}bool WriteMap( char* path ){  FILE* datei = fopen(path,"w+");   bool result = false;    // Do we have an open file...?  if (datei)  {    // Write the width/height values...    fwrite(&spalten,sizeof(int),1,datei);     fwrite(&zeilen,sizeof(int),1,datei);        // Write the entire contents to file...    fwrite(&Plan,NewSize,1,datei);     result = true;  }  else cout << "Error !";    return result;}int editpos( int x, int y, int wert ){  Plan[ (y * spalten) + x ] = wert;  return( 1 );  // Why return anything...?}        


I don't know if this will help but I noticed that you were doing alot of redundent writes. Your filesize was probably (2 * (spalten * zeilen)) + 8 bytes in length. This comes from:

for(int i=0;i<(zeilen*spalten);i++) {  fwrite(&(plan),sizeof(int),zeilen*spalten,datei); }  


If this doesn't do it, it will at least improve load/save times, but you might then want to try to explicitly declare the values as unsigned long ints to force 4-byte lengths (or unsigned short to force two-bytes). Maybe the compiler was trying to hard to optimize?

Regards,
Jumpster

Edited by - Jumpster on November 5, 2000 6:25:44 PM
Regards,JumpsterSemper Fi

This topic is closed to new replies.

Advertisement