Advertisement
Rapptz

SDLhello.cpp

Nov 5th, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include "SDL.h"
  2. #include <string>
  3.  
  4. const int SCREEN_WIDTH = 640;
  5. const int SCREEN_HEIGHT = 480;
  6. const int SCREEN_BPP = 32;
  7.  
  8. SDL_Surface* message = NULL;
  9. SDL_Surface* screen = NULL;
  10. SDL_Surface* background = NULL;
  11. SDL_Surface* load_image(std::string filename) {
  12.     SDL_Surface* loadedImage = NULL;
  13.     SDL_Surface* optimizedImage = NULL;
  14.     loadedImage = SDL_LoadBMP(filename.c_str());
  15.     if(loadedImage != NULL) {
  16.     optimizedImage = SDL_DisplayFormat(loadedImage);
  17.         SDL_FreeSurface(loadedImage);
  18.     }
  19.     return optimizedImage;
  20. }
  21.  
  22. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  23.     SDL_Rect offset;
  24.     offset.x = x;
  25.     offset.y = y;
  26.     SDL_BlitSurface(source,NULL,destination,&offset);
  27. }
  28. int main( int argc, char* args[] ) {
  29.     if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
  30.         return 1;
  31.     screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
  32.     SDL_WM_SetCaption("Hello World",NULL);
  33.     message = load_image("hello.bmp");
  34.     background = load_image("background.bmp");
  35.     apply_surface(0,0,background,screen);
  36.     apply_surface(320,0,background,screen);
  37.     apply_surface(0,240,background,screen);
  38.     apply_surface(320,240,background,screen);
  39.     apply_surface(180,140,message,screen);
  40.     if(SDL_Flip(screen) == -1)
  41.         return 1;
  42.     SDL_Delay(5000);
  43.     SDL_FreeSurface(message);
  44.     SDL_FreeSurface(background);
  45.     SDL_Quit();
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement