I've been making a game in SDL, in order to minimize the amount of spaghetti-code present, i've been making my own functions to automate many SDL processes, one of which is a function to allocate a picture or font to a texture.
Problem is that after the end of function, the SDL_Texture variable doesn't work anymore.
I've been able to replicate the problem scaling it down to a 'test' program.
Code:
void func(SDL_Texture* pText, SDL_Rect* pRect, SDL_Renderer* rend){
SDL_Surface* loadSurface = IMG_Load("pic.png");
if(!loadSurface)
printf("Error creating surface. - %s", SDL_GetError());
pText = SDL_CreateTextureFromSurface(rend, loadSurface);
if(!pText)
printf("Error creating texture. - %s", SDL_GetError());
SDL_FreeSurface(loadSurface);
}
int main(){
SDL_Renderer* rend;
SDL_Texture* pText;
SDL_Rect pRect;
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0){
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window* win = SDL_CreateWindow("app", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 250, 250, 0);
rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
func(pText, &pRect, rend); // test function
int x, y;
SDL_QueryTexture(pText, NULL, NULL, &x, &y); //Get texture size (program will either crash or print nonsense values.)
printf("C: %d, y: %d\n", x, y);
printf("Hang in there...");
return 0;
}
Compiled with G++, SDL2 dynamic (using .dll and .so)