SDL 2: Render image to screen?

So, I am trying to render a gif to the screen. However, the gif does not show up on the screen. All that I see is an empty white window. How can I fix this problem? My code is below. All necessary includes are also in their own header file. I have defined my init and exit functions externally, with each in their own file-could that have something to do with the problem, if doing so is even necessary?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "game.h"

extern void init(SDL_Window*, SDL_Renderer*);
extern void exit(SDL_Window*, SDL_Renderer*);

int main( int argc, char* argv[])
{
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;
    init(window, renderer);

    SDL_Texture* texture = IMG_LoadTexture(renderer, "male_base-test-anim.gif");
    SDL_Rect base;
    base.x = 0;
    base.y = 0;
    base.w = 948;
    base.h = 335;

    bool quit = false;

    SDL_Event e;

    while( !quit )
    {
        //Handle events on queue
        while( SDL_PollEvent( &e ) != 0 )
		{
			//User requests quit
			if( e.type == SDL_QUIT )
			{
				quit = true;
			}
        }

        SDL_RenderClear(renderer); //clears screen
        SDL_RenderCopy(renderer, texture, NULL, &base); //fetches image
        SDL_RenderPresent(renderer); //puts image on screen
    }



    SDL_DestroyTexture(texture);
    texture = NULL;
    exit(window, renderer);
	return 0;
}
Have you checked so that IMG_LoadTexture does not return a null pointer?
Thank you for the suggestion. IMG_LoadTexture did indeed return a null pointer. The picture was in the same directory, so it has to do with the renderer. If I have init and exit defined in different files, it would seem I that I need to pass window and renderer by reference instead of by value so that the value of render changes. Then, the functions can change the memory address in main. The issue is how to do this.

Could I do something like extern void init(SDL_Window*&, SDL_Renderer*&); so that the init and exit functions have the ability to change the value of window and renderer in main? It seems like window and renderer are changed in their respective functions, but since they do not have access to window and renderer in main, they do not change their values in main, so the null pointer still remains.
Yes, that should work. It's not strictly necessary to use extern when defining functions because they have external linkage by default, but it doesn't hurt either. Function declarations are often put in header files. If the init function was defined in a file named init.cpp I would have put the declaration of init in a header file named init.h that can be included from main.cpp (like you do with game.h).
Last edited on
Great. It worked. I just needed to remember that you only pass the variables by value when the function is called. They only become references when they go inside the function after becoming function parameters. Thanks for the info about the functions, too. It's those little things about the language that are so important to know.
Topic archived. No new replies allowed.