I'm learning SDL2 with Visual Studio 2015 and I'm trying to draw a 16x16 level with a .map file and 32x32 .png images for tiles (that's 256 tiles) on a 512x512 window. I have both SDL2 and SDL2 Image set up.
I'm attempting to turn the .png image into a texture using IMG_LoadTexture, but it never succeeds.
Here's the code for the structure for a single tile:
struct Tile
{
int tileType; //a range from 1-12 that determines what kind of tile it is.
SDL_Texture *texture = NULL; //The texture for the tile that comes from a .png file
SDL_Rect texture_rect; //Rectangle for rendering the texture with SDL_RenderCopy
};
I successfully read the tileType from a .map file and use it in a function to give a tile in an array of tiles the right texture. Here is the code for the function :
void get_textures(Tile a[16][16])
{
for (int i = 0; i < 16; i++)
{
for (int k = 0; k < 16; k++)
{
cout << a[i][k].tileType << ", ";
switch (a[i][k].tileType)
{
case 1:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\1.png");
break;
case 2:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\2.png");
break;
case 3:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\3.png");
break;
case 4:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\4.png");
break;
case 5:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\5.png");
break;
case 6:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\6.png");
break;
case 7:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\7.png");
break;
case 8:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\8.png");
break;
case 9:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\9.png");
break;
case 10:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\10.png");
break;
case 11:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\11.png");
break;
case 12:
a[i][k].texture = IMG_LoadTexture(renderer, "Sprites\Tiles_png\12.png");
break;
}
if (a[i][k].texture == NULL)
{
cout << "Failed to load texture from file." << endl;
}
a[i][k].texture_rect.x = k*TILE_WIDTH; //defined as 32
a[i][k].texture_rect.y = i*TILE_HEIGHT; //defines as 32
a[i][k].texture_rect.h = 32;
a[i][k].texture_rect.w = 32;
}
}
}
Every time this runs I get a tileType and a "Failed to load texture from file." for every one of the 256 tiles. I have re-checked the file paths multiple times, made sure the "Sprites" folder is in the same folder as the project's .vcxproj file and that the folders and images are included in the project in Visual Studio and it still doesn't work (I only get a black SDL window).
Any help would be appreciated.
Sprites
folder must be not in the project directory, but in a directory that contains your.exe
. \$\endgroup\$ – HolyBlackCat Dec 30 '15 at 9:44