user7549179
user7549179

Reputation:

SDL fails to display image?

My code doesn't display anything. All I get is a window with no image.

#include <iostream>
#include <stdio.h>
#include <SDL2/SDL.h>

using namespace std;

SDL_Window *gWindow=NULL;
SDL_Surface *gScreenSurface=NULL;
SDL_Surface *gHelloWorld=NULL;

const int SCREEN_WIDTH=640, SCREEN_HEIGHT=480;
bool init(){
    bool success = true;

    if(SDL_Init (SDL_INIT_VIDEO) < 0 ) {
        printf("SDL could not initialize! SDL_Error : %s \n", SDL_GetError() );
        success=false;
    }
    else{
        gWindow = SDL_CreateWindow ( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL ){
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
            success=false;
        }
        else {
            gScreenSurface = SDL_GetWindowSurface (gWindow);
        }
    }
    return success;
}

bool loadMedia(){
    bool success=true;

    gHelloWorld = SDL_LoadBMP ( "hello_world.bmp" );
    if (gHelloWorld == NULL ){
        printf( "Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError() );
        success=false;
    }
    return success;
}

void close(){
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld=NULL;

    SDL_DestroyWindow( gWindow );
    gWindow=NULL;

    SDL_Quit();
}

int main(int argc, char* args[]){
    if(!init()){
        printf( "failed to initialize!\n" );
    }
    else {
        if( !loadMedia() ) {
            printf ("failed to laod media! \n");
        }
        else {
            SDL_BlitSurface( gHelloWorld, NULL, SDL_GetWindowSurface(gWindow), NULL );
            SDL_UpdateWindowSurface ( gWindow );
            SDL_Delay (2000);
        }
    }
    close();

I expect it to show me a bmp image which is in the path specified here in the loadBMP() function but all I get is an empty transparent window.

I am using KDE Konsole, if that has to do something with this.

Upvotes: 0

Views: 410

Answers (1)

genpfault
genpfault

Reputation: 52084

KDE eh? Plasma composites by default; try disabling compositing or add a proper event-handling loop so your process has a chance to handle repaint events.

Upvotes: 2

Related Questions