0

An adjustment in my code is causing a strange occurrence which is causing segfaults. After running it through gdb, I found that a call to SDL_QueryTexture wasn't assigning the SDL_Texture's width and height values to the int pointers passed to the method. I called SDL_GetError() and printed it, and it says: "Failed loading SHCORE.DLL: The specified module could not be found." While doing searching, I heard this may have to do something with older versions of Windows. I have Windows 7, but the code was working from before, so I doubt Windows is the problem here, but the code. The code I think is causing the problem (which includes the SDL_QueryTexture call) is below:

struct TextTexture {

private:
    SDL_Texture* texture;
    SDL_Rect destinationrect;
    int* w;
    int* h;

void loadText(string s) {
        SDL_Color color = {255,255,255};
        SDL_Surface* textsurface = TTF_RenderText_Solid(font, s.c_str(), color);
        if(textsurface == NULL) {
            cout << "Could not rendertext onto surface" << endl;
        }
        texture = SDL_CreateTextureFromSurface(r,textsurface);
        if(texture == NULL) {
            cout << "Could not make texture" << SDL_GetError() << endl;
        }
        SDL_QueryTexture(texture, NULL, NULL, w, h);
        cout << SDL_GetError() << endl;
        SDL_FreeSurface(textsurface);
        textsurface = NULL;



    }

    void textRender(int x, int y) {

        destinationrect = {x,y,*w,*h};
        if (SDL_RenderCopy(r,texture,NULL,&destinationrect) < 0) {
            cout << "Rendercopy error" << SDL_GetError() << endl;
        }


    }
};

1 Answers1

0

The problem with your code is that you are simply passing the int variables by value and not by pointer. SDL was written in C, and due to this it does not have pass by reference unlike C++. So, to allow SDL to input values into your variables you need to pass the pointers to those variables

You would change this line of code:

SDL_QueryTexture(texture, NULL, NULL, w, h);

To this:

SDL_QueryTexture(texture, NULL, NULL, &w, &h);

The ampersands return the addresses of the variables w and h.

If you would like to learn more about pointers in C++ here's a link:

how does the ampersand(&) sign work in c++?

James Balajan
  • 301
  • 4
  • 13