Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,227 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,996 people online right now. Registration is fast and FREE... Join Now!





SDL: Load an optimised image to a surface

A function to load and optimise an image.

Submitted By: gabehabe
Actions:
Rating:
Views: 533

Language: C++

Last Modified: July 11, 2008
Instructions: 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 (example shown in the code)

Snippet


  1. #include <SDL/SDL.h>
  2.  
  3. /* Function to load and return an optimised image
  4. * using the SDL graphics libraries
  5. * author: Danny Battison
  6. * contact: gabehabe@hotmail.com
  7. */
  8.  
  9. SDL_Surface *Load_Optimal (char *filename)
  10. {
  11.     SDL_Surface *loaded  = NULL; /* The surface to load the basic image */
  12.     SDL_Surface *optimal = NULL; /* The optimised surface */
  13.     loaded = SDL_LoadBMP (filename); /* Load the basic image */
  14.     if (loaded != NULL) /* This is why NULL is important, now we can check if it was loaded successfully */
  15.     {
  16.         optimal = SDL_DisplayFormat (loaded); /* Load an optimised version of the basic surface */
  17.         SDL_FreeSurface (loaded); /* Delete the basic one, it's no longer needed */
  18.     }
  19.     return optimal; /* Return the optimised image */
  20. }
  21.  
  22. /***********ALTERNATIVE, IF YOU USE SDL_IMAGE.H *************/
  23. #include <SDL/SDL.h>
  24. #include <SDL/SDL_image.h>
  25.  
  26. SDL_Surface *Load_Optimal (char *filename)
  27. {
  28.     SDL_Surface *loaded  = NULL; /* The surface to load the basic image */
  29.     SDL_Surface *optimal = NULL; /* The optimised surface */
  30.     loaded = IMG_Load (filename); /* Load the basic image */
  31.     if (loaded != NULL) /* This is why NULL is important, now we can check if it was loaded successfully */
  32.     {
  33.         optimal = SDL_DisplayFormat (loaded); /* Load an optimised version of the basic surface */
  34.         SDL_FreeSurface (loaded); /* Delete the basic one, it's no longer needed */
  35.     }
  36.     return optimal; /* Return the optimised image */
  37. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month