Ejemplo SDL_image.

Libreria encargada de cargar las imagenes en nustra pantalla, esta libreria es mas simple , solo posee una unica funcion SDL_Surface * IMG_Load(const char *file)

Esta libreria la podes encontrar acá.

* Para esta libreria deves agregar este comando : -lSDL_image

sd2

#include <stdio.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

//Atributos de la pantalla
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *image = NULL;
SDL_Surface *pantalla = NULL;
SDL_Rect rect = {0, 100, 0, 0};

int done = 0;
SDL_Event event;

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image using SDL_image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    pantalla = SDL_SetVideoMode(600, 400, 16, 0);

    //If there was an error in setting up the screen
    if( pantalla == NULL )
    {
        return false;
    }

    SDL_WM_SetCaption("Imagen en SDL!", "Imagen en SDL!");
    //If everything initialized fine
    return true;
}

int main( int argc, char* args[] )
{
    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the image
    image = load_image( "nave.png" );

    //If there was a problem in loading the image
    if( image == NULL )
    {
        return 1;
    }

    SDL_BlitSurface(image, NULL, pantalla, &rect);
    SDL_Flip(pantalla);

    while (! done)
    {
        SDL_WaitEvent(& event);

        if (event.type == SDL_QUIT)
            done = 1;
    }

    return 0;
}

Resultado:

img_sdl

Contestar a esta entrada