GP32 Its All Upside Down And Back To Front?!?!


Certified Guru
Feb 16, 2004

Working on adding the loading and saving into Gigas, finally figured out how to save and load which i was chuffed about, but now i notice that anything which was in array seems to be jumbled, as in it was written back to front.

for example, the map was stored like this

map[x][y] (x and y are both the same number, always)

but when i load it from the routine, i have to switch the x and y around as it seems to of loaded it backwards or something, this isnt a problem as both X and Y are always the same so i can just flip it around like so...

//Array to hold the current map layer in
unsigned short temp_map[map_settings.MAPW][map_settings.MAPH];

//i think maps have to be even shaped for this to work

//Load the map from the SMC
char filename[8];
int i;
for (i = 0; i < LAYERS; i++ ) {
sprintf((char*)filename, "M%dL%d.DAT", map_number, i);
GpFileOpen(filename, OPEN_R, &h_file );
        GpFileRead(h_file, temp_map, sizeof(temp_map),NULL);
GpFileClose( h_file );

  int x,y;
  for(x = 0; x < map_settings.MAPW; x++)
  for(y = 0; y < map_settings.MAPH; y++)
  map[x][y] = temp_map[y][x];


That all works, because like i said - both y and x go upto the same value.

However, im having problems loading arrays where the two values dont match, for example,

//Now load the entitys
//Emulate the .DAT file for entitys
short temp_entitys[100][16]; //was 100 16

//Load the entity information from SMC into that dat file
sprintf((char*)filename, "M%E.DAT", map_number);
GpFileOpen(filename, OPEN_R, &h_file );
        GpFileRead(h_file, temp_entitys, sizeof(temp_entitys),NULL);
GpFileClose( h_file );

//Apply the settings to the entitys
for(i = 0; i < (ENTITY_COUNT - 1); i++)

  //Load the info
   ENTITYS.x = temp_entitys[0];
   ENTITYS.y = temp_entitys[1];
   ENTITYS.enabled = temp_entitys[2]; = temp_entitys[3];
   ENTITYS.animation = temp_entitys[4];
   ENTITYS.attack_animation = temp_entitys[5];
   ENTITYS.state = temp_entitys[6]; = temp_entitys[7];
   ENTITYS.STAT_BASE_HP = temp_entitys[8];
   ENTITYS.STAT_BASE_MP = temp_entitys[9];
   ENTITYS.STAT_BASE_ATK = temp_entitys[10];
   ENTITYS.STAT_BASE_DEF = temp_entitys[11];
   ENTITYS.STAT_EXP = temp_entitys[12];
   ENTITYS.STAT_EXP_GRAPH = temp_entitys[13];
   ENTITYS.speed = temp_entitys[14];

} //end of entity loop

The problem here is, the array i wrote into the file was a short[100][16]. but because each [] doesn't store the same amount, i cannot just reverse it to put everything back in its correct place, any suggestions on how i can do this?
	//save the entitys
	GpFileCreate("M1E.DAT", NOT_IF_EXIST, &h_file );
	GpFileOpen("M1E.DAT", OPEN_W, &h_file );
	GpFileClose( h_file );

the Save function is nice and simple, the problem is the data always seems to get written/read back to front, as it has two arrays [100][16],

if it was [100][100] i could just do [x][y] = [y][x] but as both array has a different length i cannot figure out how to flip it back to its normal position :p i figure this must be a common problem and therefore has already been solved by somebody, somewhere.
Load into something like temp_arr[1600], then use a for loop to put it into the array.

short temp_arr[1600];
int x, y, onarr = 0;
/* File loading code (into temp_arr) */
for(x = 1; x < 16; x++) for(y = 1; y < 100; y++) {temp_entities[x-1][y-1] = temp_arr[onarr]; onarr++;}

If that still reverses, just change temp_entities[x-1][y-1] to temp_entities[y-1][x-1]. Theres prolly a better way, but that works :D
no luck with that either, to begin with i just tried to load it like that - but it just came out all '255's'

so i decided to make it convert the data into the one [1600] array then save it, then when it loads, to load back that [1600] array and peice of back together like you mentioned.

it generates a 200kb file for some reason - where as saving the arrays before only used 4kb :p had a little play and tried to fix it, but couldn't really see any problem other than your x and y's are back to front

it goes [100][16], but i fixed that and still no avail, even tried loading it backwards, so like:

data[x][y] = big_data_string[(1600 - 1) - onarr]

nothing seems to work, which is annoying as it should be such a simple task - i'll talk to you more when your on MSN mate and see if we can fix it.
Another idea: Someone told me that sizeof() didn't work well on multidementional arrays. What about trying: sizeof(short) * 1600 ?