# Cannot load png images to textures with SDL2 in Visual Studio 2015

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:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
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. – HolyBlackCat Dec 30 '15 at 9:44
• If you find an answer good, than you must accept the best one. Read more about accepting answers on the help center – H. Pauwelyn Feb 24 '16 at 16:12

You could use one of this two ways to place a backslash in a string:

@"Sprites\Tiles_png\11.png"
"Sprites\\Tiles_png\\11.png"


This must be done because C# implements backslashes. So, next character literals \n, \r, \t, \" and \\ are respectively a new line, character return line feed, vertical tab, qoutation mark and one backslash.

It's also possible to escape them by using a string literals like the @-char before opening the first quotation mark, like I'd for the first string.

Here you have the definition of literals:

A literal is a source code representation of a value.

A third option is to use forward slashes and then you haven't that problem anymore with the implementation of the backslashes. But because Microsoft use by default backslashes for file paths, I prefer to use one of the first two options.

"Sprites/Tiles_png/11.png"


I've found out my stupid mistake. The file path recognizes "/" and not "\".