Image not visible (even though it was loaded correctly...)

Good Morning/Afternoon/Evening all,

There was a slight problem in my program output today, and it’s really annoying. In case the title isn’t saying much, When I run my program it fails to display the image. I’ve done some simple checks, I.e checking if it’s in the right file path, making sure the image is the right extension file, etc. All the obvious, yet I’ve failed to do one of the simplest tasks in SDL2. I’m using the latest version (if that helps). Also, I’m writing in C though it shouldn’t matter even if in C++.

The code is as follows::

#include "space.h"

int main( int argc, char** argv ) {

    Character player ;
    static int posX = SDL_WINDOWPOS_UNDEFINED, posY = SDL_WINDOWPOS_UNDEFINED ;
    const int WIDTH = 640, HEIGHT = 480 ;

    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) ;

    IMG_Init( IMG_INIT_PNG | IMG_INIT_JPG ) ;

    SDL_Window* window = SDL_CreateWindow( "SPACE INVADERS", posX, posY, WIDTH, HEIGHT, 0 ) ;

    SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED ) ;
    load_file( renderer, "images/space_ship.png", space_text ) ;

    player.x = 0 ;
    player.y = 0 ;
    player.w = 16 * 2 ;
    player.h = 16 * 2 ;

    bool running = true ;
    while( running ) {
        SDL_Event e ;
        if( SDL_PollEvent( &e ) ) {
            if( e.type == SDL_QUIT ||
                e.key.keysym.sym == SDLK_ESCAPE )
                running = false ;
        }

        render( renderer, &player ) ;
    }

    SDL_DestroyRenderer( renderer ) ;
    SDL_DestroyWindow( window ) ;

    SDL_DestroyTexture( space_text ) ;
    SDL_DestroyTexture( bullet_text ) ;
    SDL_DestroyTexture( enemy_text ) ;

    SDL_Quit( ) ;
    IMG_Quit() ;

    return 0 ;
}

void load_file( SDL_Renderer* renderer, const char* filename, SDL_Texture* texture ) {
    SDL_Surface *surface = IMG_Load( filename ) ;
    if( !surface ) {
        printf( "%s could not be found!\n", filename ) ;
        exit( -1 ) ;
    }
    texture = SDL_CreateTextureFromSurface( renderer, surface ) ;
    SDL_FreeSurface( surface ) ;
}

void render( SDL_Renderer* renderer, Character* player ) {

    SDL_RenderClear( renderer ) ;
    SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0x00 ) ;

    SDL_Rect src = { 0, 0, player->w, player->h } ;
    SDL_Rect dst = { ( int ) player->x, ( int ) player->y, player->w, player->h } ;
    SDL_RenderCopy( renderer, space_text, &src, &dst ) ;

    SDL_RenderPresent( renderer ) ;
}

You’re assigning the address of the newly created texture object to a local variable inside the load_file-function.

Right, yeah, I realised that last night. Just to be sure, the local variable was the surface right ?

No, it’s the “texture”-pointer. You probably want to define load_file as
void load_file( SDL_Renderer* renderer, const char* filename, SDL_Texture* &texture)
where the “texture” parameter is reference to a pointer instead of just a pointer.

Another option is to have load_file return the texture address:
SDL_Texture* load_file(SDL_Renderer* renderer, const char* filename)