Skip to content

Instantly share code, notes, and snippets.

@Jragon
Created November 18, 2012 18:17
Show Gist options
  • Save Jragon/4106624 to your computer and use it in GitHub Desktop.
Save Jragon/4106624 to your computer and use it in GitHub Desktop.
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
// screen atributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
// surfaces
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
// function declerations
SDL_Surface *load_image(std::string filename);
bool init();
void apply_surface(Sint16 x, Sint16 y, SDL_Surface* source, SDL_Surface* destination);
void clean_up();
SDL_Surface *load_image(std::string filename){
// where the surface is loaded to
SDL_Surface* loadedImage = NULL;
// optimized image, that will be used
SDL_Surface* optimizedImage = NULL;
// load the image using SDL_Image
loadedImage = IMG_Load(filename.c_str());
// checks the loaded image
if(loadedImage != NULL){
// create the optimized image
optimizedImage = SDL_DisplayFormat(loadedImage);
// free the old image
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface(Sint16 x, Sint16 y, SDL_Surface* source, SDL_Surface* destination){
// make a temp rectangel to hold offsets
SDL_Rect offset;
// offsets of the rectangle
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
bool init(){
// initilize all sdl
if(SDL_Init(SDL_INIT_EVERYTHING))
return false;
// set up the screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
// check the screen
if(screen == NULL)
return false;
SDL_WM_SetCaption("PNG Test", NULL);
return true;
}
void clean_up(){
// free surfaces
SDL_FreeSurface(image);
// quit sdl
SDL_Quit();
}
int main(){
if (init())
return 1;
// load the image
image = load_image("assets/sdl_image/look.png");
// apply surface
apply_surface(0, 0, image, screen);
// update the screen
SDL_Flip(screen);
// wait two seconds
SDL_Delay(2000);
clean_up();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment