0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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
  • \$\begingroup\$ If you find an answer good, than you must accept the best one. Read more about accepting answers on the help center \$\endgroup\$ – H. Pauwelyn Feb 24 '16 at 16:12
3
\$\begingroup\$

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"

It's because C# implement 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.

You could 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.

(Source and more information about literals: MSDN)


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"
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
-1
\$\begingroup\$

From the answers on these links it says not to use IMG_LoadTexture, and IMG_Load instead.

Here is a great tutorial on how to load a .png with SDL2: http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index2.php

Also this post suggests to use *IMG_Load instead of IMG_LoadTexture for loading images:SDL Function for Loading PNGs

\$\endgroup\$
  • \$\begingroup\$ Using IMG_Load also fails to load the .png image to a surface, so does SDL_LoadBMP when attempting to load a .bmp version of the image. I'm half certain that the problem lies in the program actually finding the image in the folder, but I can't see the reason. \$\endgroup\$ – Xeez Dec 30 '15 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.