Advertisement
Guest User

<3

a guest
Sep 1st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL.h>
  3.  
  4. // Screen dimension constants
  5. const int SCREEN_WIDTH = 640;
  6. const int SCREEN_HEIGHT = 480;
  7.  
  8. enum KeySurface {
  9.     KEY_SURFACE_DEFAULT,
  10.     KEY_SURFACE_UP,
  11.     KEY_SURFACE_DOWN,
  12.     KEY_SURFACE_LEFT,
  13.     KEY_SURFACE_RIGHT,
  14.     KEY_SURFACE_TOTAL
  15. };
  16.  
  17. bool init(SDL_Window **, SDL_Surface **);
  18. bool loadMedia(SDL_Surface **);
  19. SDL_Surface* loadSurface(std::string);
  20. bool close(SDL_Window **, SDL_Surface **);
  21.  
  22. int main(int argc, char* args[]) {
  23.     SDL_Window* window = NULL;
  24.     SDL_Surface* surface = NULL;
  25.     SDL_Surface** image = new SDL_Surface*[KEY_SURFACE_TOTAL];
  26.     std::cout << "WHILE LOOP\n";
  27.    
  28.     if (!init(&window, &surface))
  29.         std::cout << "Error during init: " << SDL_GetError() << std::endl;
  30.     else {
  31.         if (!loadMedia(image))
  32.             std::cout << "Error during loadMedia: " << SDL_GetError() << std::endl;
  33.         else {
  34.             bool quit = false;
  35.             SDL_Event e;
  36.             SDL_Surface *current = image[KEY_SURFACE_DEFAULT];
  37.            
  38.             while (!quit) {
  39.                 while((SDL_PollEvent(&e)) != 0) {
  40.                     if (e.type == SDL_QUIT)
  41.                         quit = true;
  42.                     else if (e.type == SDL_KEYDOWN) {
  43.                         switch (e.key.keysym.sym) {
  44.                             case SDLK_UP:
  45.                                 current = image[KEY_SURFACE_UP];
  46.                                 SDL_BlitSurface(current, NULL, surface, NULL);
  47.                                 SDL_UpdateWindowSurface(window);
  48.                                 break;
  49.                             case SDLK_DOWN:
  50.                                 current = image[KEY_SURFACE_DOWN];
  51.                                 SDL_BlitSurface(current, NULL, surface, NULL);
  52.                                 SDL_UpdateWindowSurface(window);
  53.                                 break;
  54.                             case SDLK_LEFT:
  55.                                 current = image[KEY_SURFACE_LEFT];
  56.                                 SDL_BlitSurface(current, NULL, surface, NULL);
  57.                                 SDL_UpdateWindowSurface(window);
  58.                                 break;
  59.                             case SDLK_RIGHT:
  60.                                 current = image[KEY_SURFACE_RIGHT];
  61.                                 SDL_BlitSurface(current, NULL, surface, NULL);
  62.                                 SDL_UpdateWindowSurface(window);
  63.                                 break;
  64.                             default:
  65.                                 current = image[KEY_SURFACE_DEFAULT];
  66.                                 SDL_BlitSurface(current, NULL, surface, NULL);
  67.                                 SDL_UpdateWindowSurface(window);
  68.                                 break;
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75.    
  76. //    close(&window, image);
  77.     delete [] image;
  78.     return 0;
  79. }
  80.  
  81. bool init(SDL_Window **window, SDL_Surface **surface) {
  82.     bool state = true;
  83.    
  84.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  85.         state = false;
  86.     else {
  87.         *window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  88.        
  89.         if (*window == NULL)
  90.             state = false;
  91.         else
  92.             *surface = SDL_GetWindowSurface(*window);
  93.     }
  94.    
  95.     return state;
  96. }
  97.  
  98. SDL_Surface* loadSurface(std::string name) {
  99.     SDL_Surface* surface;
  100.    
  101.     if ((surface = SDL_LoadBMP(name.c_str())) == NULL)
  102.         std::cout << "Error while loading surface: " << SDL_GetError() << std::endl;
  103.     else
  104.         return surface;
  105. }
  106.  
  107. bool loadMedia(SDL_Surface **image){//[KEY_SURFACE_TOTAL]) {
  108.     bool state = true;
  109.     SDL_Surface **temp = image;
  110.    
  111.     *image = loadSurface("default.bmp");
  112.     if (image++ == NULL)
  113.         state = false;
  114.    
  115.     *image = loadSurface("up.bmp");
  116.     if (image++ == NULL)
  117.         state = false;
  118.    
  119.     *image = loadSurface("down.bmp");
  120.     if (image++ == NULL)
  121.         state = false;
  122.    
  123.     *image = loadSurface("left.bmp");
  124.     if (image++ == NULL)
  125.         state = false;
  126.    
  127.     *image = loadSurface("right.bmp");
  128.     if (image++ == NULL)
  129.         state = false;
  130.    
  131.     image = temp;
  132.     return state;
  133. }
  134.  
  135. bool close(SDL_Window **window, SDL_Surface **surface) {
  136.     SDL_FreeSurface(*surface);  *surface = NULL;
  137.     SDL_DestroyWindow(*window); *window = NULL;
  138.     SDL_Quit();
  139.     return true;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement