0 Replies - 87 Views - Last Post: 11 July 2008 - 08:14 AM

#1 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

SDL: Load an optimised image to a surface

Posted 11 July 2008 - 08:14 AM

Description: Pass the file path to the function :) | If you have SDL_Image stuff to load other file types, replace SDL_LoadBMP with IMG_Load and #include <SDL/SDL_image.h> (example shown in the code)A function to load and optimise an image.
#include <SDL/SDL.h>

/* Function to load and return an optimised image
 * using the SDL graphics libraries
 * author: Danny Battison
 * contact: [email protected]
 */

SDL_Surface *Load_Optimal (char *filename)
{
    SDL_Surface *loaded  = NULL; /* The surface to load the basic image */
    SDL_Surface *optimal = NULL; /* The optimised surface */
    loaded = SDL_LoadBMP (filename); /* Load the basic image */
    if (loaded != NULL) /* This is why NULL is important, now we can check if it was loaded successfully */
    {
        optimal = SDL_DisplayFormat (loaded); /* Load an optimised version of the basic surface */
        SDL_FreeSurface (loaded); /* Delete the basic one, it's no longer needed */
    }
    return optimal; /* Return the optimised image */
}

/***********ALTERNATIVE, IF YOU USE SDL_IMAGE.H *************/
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

SDL_Surface *Load_Optimal (char *filename)
{
    SDL_Surface *loaded  = NULL; /* The surface to load the basic image */
    SDL_Surface *optimal = NULL; /* The optimised surface */
    loaded = IMG_Load (filename); /* Load the basic image */
    if (loaded != NULL) /* This is why NULL is important, now we can check if it was loaded successfully */
    {
        optimal = SDL_DisplayFormat (loaded); /* Load an optimised version of the basic surface */
        SDL_FreeSurface (loaded); /* Delete the basic one, it's no longer needed */
    }
    return optimal; /* Return the optimised image */
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1