0
votes

So, I've been searching relentlessly in my code for this tiny little bug, and I cannot find it at all. I've looked at references, passes, types, arguments, and every other constructor. My code returns a boolean true when asking if texture == NULL. I've checked everything possibly related to this texture. I've never had this problem before, and it makes zero sense to me. Here's the class cSprite constructor that takes the renderer and loads a texture with it:

    cSprite::cSprite(SDL_Renderer* passed_renderer, std::string passed_filepath, //...//) {
renderer = passed_renderer;
if (renderer == NULL) {
    *errorcheck = true;
}

texture = NULL;
texture = IMG_LoadTexture(renderer, passed_filepath.c_str());
if (texture == NULL) {
    *errorcheck = true;
}

Any one have a clue what I did wrong? Here's the passing of the renderer in a while loop function of another class (which contains both headers of the other classes cSprite and cSetup):

    std::string CurrentFilePath;
    CurrentFilePath = "mainbox.png";
    cSpriteObj = new cSprite(cSetupObj->GetRenderer(),
                             CurrentFilePath, //...//);

For clarity's sake, here's the renderer's construction in cSetup:

    renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
    quit = true;
}

And the GetRenderer() function in that same class:

    SDL_Renderer* cSetup::GetRenderer() {
return renderer;

}

I get no errors at all. I run it, and a white window appears for a moment, and disappears, since I have the while loop make it close if I recieve an error. I know there's no error messages to tell me if something went wrong, but when I did the error checks, I ran the code after each one I implemented. The window only froze. However, once I set up an error checker for texture, THAT'S when it closed itself. Can someone help? I have no idea what's wrong with the code. It has the exact same settings and headers as some other working SDL2 projects I have.

EDIT: I just rearranged the code instead. I'm not 100% sure what the issue was before, but I think I did something wrong equating the renderers. The functions now just take the passed renderer and use it directly.

1
Do you print something EXACTLY were the error occurs? What's the content of SDL_GetError()? And how do you create/pass errorcheck? - olevegard
"errorcheck" is a pointer to Boolean "quit". If it's true, the while loop breaks. - user3658679
What I mean is that you should add a cout where you seterrorcheck to true. So, for instance, here : if (texture == NULL) { *errorcheck = true; } You should add soemthing like std::cout << "Error! " << SDL_GetError() << std::endl; inside the if statement - olevegard

1 Answers

0
votes

Make sure you have IMG_Init() somewhere in your code with the right parameters for the kind of texture formats you are using. You can also call IMG_GetError() to see if texture loading itself is working fine. And as @olevegard has said, add std::cout to where ever there is a conditional for any kind of error with a description of what went wrong. or create a simple logger class and output to that.