[SDL2]-Need Help-Image Load Work, But Is Incorrect Implementation


I battled all damn day with PNG image loading function.
I got something that works now but the implementation is incorrect.

Can someone look at my source code below and say how to properly implement these changes?

Game’s source is 100% cross-platform(Linux/Windows®).
Game’s source is clean.
Game is stable.

This is where the new image loading function is:

Thank you in advance!


Can you go into more detail about whats happening.
Also is there any reason you you use C Code rather than C++ for SDL ?
Game is called CPlusPlus-LettersFall but seems to be very C like.


The new image loading code works, but it’s not ideal.
I am having trouble reading individual lines of file names in a regular TXT file.

I’ll post your response in the original forum post, please continue the discussion there.

Also, yes the game’s source is mostly C code with some small C++ parts like C++ Classes.


If your allowing yourself to use C++ then you should use it to simplify the issue.

You have a lot going on in the LoadSpritesAndInitialize and its difficult to understand what your doing or trying to achieve. Hopefully this might help you.

Here is a quick example of what I mean.
It may not be the faster than C but it will save you a headache.

This example reads the content of a file into a stringstream.
Using the getline function we extract each line of characters into a string.

#include <fstream>
#include <sstream>

void getDataFromFile(const char* fileName, std::stringstream& ss) {
    std::ifstream file(fileName);
    if(file.is_open()) {
        ss << file.rdbuf(); //read the file into the stringstream

int main() {
    std::stringstream ss;
    getDataFromFile("../textures.txt", ss);

    std::string data;
    while (std::getline(ss, data)) {
        // Do whatever with string
        // Example
        // texture = loadTexture(renderer, data.c_str());
        std::cout << data << '\n';
    return 0;

Thanks for the code sample…implementing today…



Got a little issue now:

tempSurface = IMG_Load(imageFilePath); // ERROR: Can’t convert string to ‘const char*’???

Any help would be appreciated, thanks!


bool Visuals::LoadSpritesAndInitialize(void)
char filePath[256];
SDL_Surface *tempSurface = NULL;
Uint32 textureFormat;
int textureAccess;
int textureWidth;
int textureHeight;
std::string imageFilePath;
int imageIndex = 0;

for (int index = 0; index < NumberOfSprites; index++)
    Sprites[index].Texture = NULL;

std::stringstream ss;
GetDataFromFile("data/visuals/LoadImageData.txt", ss);

std::string data;
while (imageIndex > -1)
    std::getline(ss, data);
    std::cout << data << '\n';
    imageIndex = atoi( data.c_str() );

    std::getline(ss, data);
    std::cout << data << '\n';
    imageFilePath =  data.c_str();

    if (imageIndex > -1)
        printf( "Image Load File: %s\n", imageFilePath );

        tempSurface = IMG_Load(imageFilePath); // ERROR: Can't convert string to 'const char*'???

        if (!tempSurface)
            printf( "Image loading failed: %s\n", IMG_GetError() );
            CoreFailure = true;

        Sprites[imageIndex].Texture = SDL_CreateTextureFromSurface(Renderer, tempSurface);

        Sprites[imageIndex].ScreenX = 320;
        Sprites[imageIndex].ScreenY = 240;
        Sprites[imageIndex].ScaleX = 1.0;
        Sprites[imageIndex].ScaleY = 1.0;
        Sprites[imageIndex].RotationDegree = 0;
        Sprites[imageIndex].RedHue = 255;
        Sprites[imageIndex].GreenHue = 255;
        Sprites[imageIndex].BlueHue = 255;
        Sprites[imageIndex].Transparency = 255;
        Sprites[imageIndex].Smooth = false;
        Sprites[imageIndex].FlipX = false;
        Sprites[imageIndex].FlipY = false;

        Sprites[imageIndex].AnimationTimer = 0;

        SDL_QueryTexture(Sprites[imageIndex].Texture, &textureFormat, &textureAccess, &textureWidth, &textureHeight);
        Sprites[imageIndex].TextureWidthOriginal = textureWidth;
        Sprites[imageIndex].TextureHeightOriginal = textureHeight;


if ( LoadInterfaceIntoMemory() == false)
    CoreFailure = true;

if ( PreloadStaffTextsIntoMemory() == false)
    CoreFailure = true;

printf("Loaded all sprite images into memory.\n");


Hi Again!

I solved the above issue with below change:

tempSurface = IMG_Load( data.c_str() );

Have a new issue:
It builds and runs 100% on Windows 10
it builds but does not run on Linux?

Getting error messages in Linux terminal when running
about not finding the image files?

Hope someone can help me, thanks!


Hello again,

Bit more info needed here as to the file and folder layout.

Just check with one texture using the following.
May need to change texture folder location.

Got it working last night and fell asleep.
Moving forward…