Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <stdio.h>
  5.  
  6.  
  7. const int SCH=640;
  8. const int SCW=480;
  9. const int BBP=32;
  10. const int FPS=30;
  11. const char* name = "TEHGAEM";
  12.  
  13. // sprite height and width
  14. const int SPH=20;
  15. const int SPW=20;
  16.  
  17. using namespace std;
  18.  
  19. SDL_Event event;
  20.  
  21.  
  22. SDL_Surface *screen=NULL;
  23. SDL_Surface *sprite=NULL;
  24. SDL_Surface *temp = NULL;
  25.  
  26. class Player
  27. {
  28.     private:
  29.     int x,xVel;
  30.     int y,yVel;
  31.     public:
  32.     Player();
  33.     void show();
  34.     void move();
  35.     void handle_input();
  36. };
  37.  
  38.  
  39. Player::Player()
  40. {
  41.     x=0;
  42.     y=0;
  43.     xVel=0;
  44.     yVel=0;
  45. }
  46.  
  47. enum KEYS
  48. {
  49.     UP = SDLK_UP, DOWN= SDLK_DOWN, LEFT = SDLK_LEFT, RIGHT = SDLK_RIGHT
  50. };
  51.  
  52. void apply(int x, int y, SDL_Surface *dest, SDL_Surface *src, SDL_Rect *clip = NULL)
  53. {
  54.     SDL_Rect offset;
  55.     offset.x=x;
  56.     offset.y=y;
  57.     SDL_BlitSurface (src, clip, dest, &offset);
  58. }
  59.  
  60. void Player::show()
  61. {
  62.     apply(x,y, screen, sprite);
  63. }
  64.  
  65. void Player::handle_input()
  66. {
  67.     if (event.type ==SDL_KEYDOWN)
  68.     {
  69.         switch (event.key.keysym.sym)
  70.         {
  71.             case SDLK_UP: yVel -= SPH /2; break;
  72.             case SDLK_DOWN: yVel += SPH /2; break;
  73.             case SDLK_LEFT: xVel -=SPW /2; break;
  74.             case SDLK_RIGHT: xVel +=SPW /2; break;
  75.         }
  76.     }
  77.     else if (event.type == SDL_KEYUP)
  78.     {
  79.         switch(event.key.keysym.sym)
  80.         {
  81.             case SDLK_UP: yVel += SPH /2; break;
  82.             case SDLK_DOWN: yVel -= SPH /2; break;
  83.             case SDLK_LEFT: xVel +=SPW /2; break;
  84.             case SDLK_RIGHT: xVel -=SPW /2; break;
  85.         }
  86.     }
  87.  
  88. }
  89.  
  90. void Player::move()
  91. {
  92.     Uint32 start=SDL_GetTicks();
  93.     x+=xVel;
  94.     if ((x<0) || (x+SPW > SCW))
  95.         {
  96.             x-=1;
  97.         }
  98.     y+=yVel;
  99.     if ((y<0) || (y+SPH > SCH))
  100.         {
  101.             y-=1;
  102.         }
  103.  
  104. }
  105.  
  106.  
  107.  
  108. bool init()
  109. {
  110.     if (SDL_Init(SDL_INIT_EVERYTHING)<0)
  111.     {
  112.         return false;
  113.     }
  114.     screen = SDL_SetVideoMode(SCH,SCW,BBP, SDL_SWSURFACE);
  115.     if (screen == NULL)
  116.     {
  117.         return false;
  118.     }
  119.     SDL_WM_SetCaption(name, NULL);
  120.     return true;
  121. }
  122.  
  123. bool somethings()
  124. {
  125.     temp = SDL_LoadBMP("sprite.bmp");
  126.     if (temp == NULL)
  127.     {
  128.  
  129.         return false;
  130.     }
  131.     sprite = SDL_DisplayFormat (temp);
  132.     SDL_FreeSurface(temp);
  133.     if (sprite ==NULL)
  134.     {
  135.         return false;
  136.     }
  137.  
  138.  
  139.     return true;
  140. }
  141.  
  142. void clean()
  143. {
  144.     SDL_FreeSurface(sprite);
  145.     SDL_Quit();
  146. }
  147.  
  148.  
  149. int console_appinstruct()
  150. {
  151.     printf("Here are the instructions:");
  152.     std::cout << "Use the arrow keys to turn around" << endl;
  153.     std::cout << "Remove this program by deleting SDL3.exe from task manager"<<endl;
  154.     std::cout << "I'll add a quit button soon :)";
  155. }
  156.  
  157.  
  158.  
  159. int main(int argc, char* args[])
  160. {
  161.     console_appinstruct();
  162.     Player P1;
  163.     Uint32 go =SDL_GetTicks();
  164.     bool quit=false;
  165.     if (init() == false)
  166.     {
  167.         return 1;
  168.     }
  169.  
  170.     if (somethings() ==false)
  171.     {
  172.         return 1;
  173.     }
  174.  
  175.     while (quit ==false)
  176.     {
  177.         while (SDL_PollEvent(&event))
  178.         {
  179.             P1.handle_input();
  180.             if (event.type == SDL_KEYDOWN)
  181.             switch (event.key.keysym.sym)
  182.             {
  183.                 case SDL_QUIT:
  184.                 case SDLK_ESCAPE:
  185.                 case SDLK_q:
  186.  
  187.                 quit = true;
  188.                 clean();
  189.                 break;
  190.             }
  191.         }
  192.         SDL_Delay((1000/FPS)-go);
  193.         if (SDL_Flip(screen) ==-1)
  194.         {
  195.             return 1;
  196.         }
  197.         P1.move();
  198.         P1.show();
  199.  
  200.     }
  201.     clean();
  202.     return 0;
  203. }
Add Comment
Please, Sign In to add comment