help with casts

Started by
7 comments, last by fastcall22 15 years, 11 months ago
I am getting the following error: "--------------------Configuration: test - Win32 Debug-------------------- Compiling... main.cpp C:\test\main.cpp(190) : error C2664: 'LoadMap' : cannot convert parameter 2 from 'int [12][18]' to 'int *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. Here is line 190:
LoadMap(map_id, g_levels[k].map);
So how do I get it to compile? Also, here is the map array:
int map[WORLDSIZE_Y][WORLDSIZE_X];
I see that I have an int* and an array int[][] so how do I get this to compile?
Advertisement
LoadMap should have the following signature:

LoadMap(int id, int map[][WORLDSIZE_X]);
Quote:Original post by fpsgamer
LoadMap should have the following signature:

LoadMap(int id, int map[][WORLDSIZE_X]);


Ok now it compiles but it immeddiately crashes

Here's load map for reference:

void LoadMap(const char* name, int destination[][WORLDSIZE_X]){	FILE* map_data = fopen(name, "r");			if (!map_data)	{		WriteToLogFile("Error: Unable to open map file.");	}		for (int m = 0; m < WORLDSIZE_X; m++)	{		for (int n = 0; n < WORLDSIZE_Y; n++)		{			fscanf(map_data, "%d", &destination[n * WORLDSIZE_X + m]);		}	}	fclose(map_data);}

Change this:

fscanf(map_data, "%d", &destination[n * WORLDSIZE_X + m]);

to this:

fscanf(map_data, "%d", &destination[n][m]);
Ok now it doesn't crash but the tiles are not in the correct spots.

Ok in the previous post there's a pic of the output and also of the map file. Note that the blue is tile 0 and the green is tile 1.
Hmmm

I noticed that it renders 25 of the blue tiles and there's 25 of them in the actual map00.txt file

so it's detecting them but putting them in the wrong place in the array
how though?

I'm fairly sure that my DrawTiles function is working properly, here it is for reference though:

void DrawTiles(){		// used to retrieve current tile	int current_tile;		// so we will know where to draw the tile	SDL_Rect src, dest;	// each tile is 32x32	src.w = 32;	src.h = 32;	for (int i = 0; i < WORLDSIZE_X; i++)	{		for (int j = 0; j < WORLDSIZE_Y; j++)		{			// retrive current tile			current_tile = g_levels[g_current_level].map[j];						// determine where to render it onscreen			src.x = current_tile * 32;			src.y = 0;			dest.x = OFFSET_X + i*32;			dest.y = OFFSET_Y + j*32;			// draw tile			SDL_BlitSurface(g_tileset,&src,g_screen,&dest);		}	}}
You have it transposed; as in, you've mixed up your rows for columns, when you are loading your map.

Change:
for (int m = 0; m < WORLDSIZE_X; m++)  // for each column{	for (int n = 0; n < WORLDSIZE_Y; n++) // for each row in column	{		fscanf(map_data, "%d", &destination[n][m]);	}}


to:

for (int n = 0; n < WORLDSIZE_Y; n++) // for each row...{	for (int m = 0; m < WORLDSIZE_X; m++) // for each column in row...	{		fscanf(map_data, "%d", &destination[n][m]);	}}

This topic is closed to new replies.

Advertisement