Kaktusen

Kaktus C++ SDL game

May 15th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2.  
  3. using namespace std;
  4.  
  5. //variabler
  6. bool quit = false;
  7. SDL_Event event;
  8. Uint8 *key;
  9. int playerX = 0;
  10. int playerY = 0;
  11. bool menuQuit = false;
  12.  
  13.  
  14. SDL_Surface* background = NULL;
  15. SDL_Surface* screen = NULL;
  16. SDL_Surface* player = NULL;
  17.  
  18. SDL_Surface* menuBackground = NULL;
  19. SDL_Surface* menuStart = NULL;
  20. SDL_Surface* menuExit = NULL;
  21.  
  22. SDL_Surface* arrow = NULL;
  23.  
  24.  
  25. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  26.     SDL_Rect offset;
  27.  
  28.     offset.x = x;
  29.     offset.y = y;
  30.  
  31.     SDL_BlitSurface(source, NULL, destination, &offset);
  32. }
  33.  
  34. void paint() {
  35.     SDL_BlitSurface(background, NULL, screen, NULL);
  36.     apply_surface(playerX, playerY, player, screen);
  37. }
  38.  
  39. void clean() {
  40.     SDL_FreeSurface(background);
  41.     SDL_FreeSurface(player);
  42.     SDL_FreeSurface(menuBackground);
  43.     SDL_FreeSurface(menuStart);
  44.     SDL_FreeSurface(menuExit);
  45.     SDL_FreeSurface(arrow);
  46. }
  47.  
  48. void menu() {
  49.  
  50.  
  51.     bool up = false;
  52.  
  53.     int arrowY = 125;
  54.  
  55.     menuBackground = SDL_LoadBMP("image/menu/background.bmp");
  56.     menuStart = SDL_LoadBMP("image/menu/start.bmp");
  57.     menuExit = SDL_LoadBMP("image/menu/exit.bmp");
  58.     arrow = SDL_LoadBMP("image/menu/arrow.bmp");
  59.  
  60.     while(menuQuit == false) {
  61.         if( SDL_PollEvent (&event)) {
  62.  
  63.                 apply_surface(0,0,menuBackground, screen);
  64.                 apply_surface(200,100,menuStart, screen);
  65.                 apply_surface(200,180,menuExit, screen);
  66.                 apply_surface(165, arrowY, arrow, screen);
  67.  
  68.                 if(event.type == SDL_KEYDOWN) {
  69.  
  70.                     switch(event.key.keysym.sym) {
  71.                         case SDLK_UP:
  72.                             if(up == true) {
  73.                                 arrowY-=80;
  74.                                 up = false;
  75.                             }
  76.                             break;
  77.  
  78.                         case SDLK_DOWN:
  79.                             if(up == false) {
  80.                                 arrowY+=80;
  81.                                 up = true;
  82.                             }
  83.                             break;
  84.  
  85.                         case SDLK_SPACE:
  86.                             if(up == false) {
  87.                                 menuQuit = true;
  88.                             } else if(up == true) {
  89.                                 quit = true;
  90.                                 menuQuit = true;
  91.                             }
  92.                     }
  93.                 }
  94.  
  95.                 else if(event.type == SDL_QUIT) {
  96.                     quit = true;
  97.                 }
  98.             }
  99.  
  100.             SDL_Flip(screen);
  101.         }
  102.  
  103. }
  104.  
  105. void keyInput() {
  106.     if( SDL_PollEvent (&event)) {
  107.  
  108.             if(event.type == SDL_KEYDOWN) {
  109.  
  110.                 switch(event.key.keysym.sym) {
  111.  
  112.                     case SDLK_DOWN:
  113.                         playerY+=1;
  114.                         break;
  115.  
  116.                     case SDLK_UP:
  117.                         playerY-=1;
  118.                         break;
  119.  
  120.                     case SDLK_RIGHT:
  121.                         playerX+=1;
  122.                         break;
  123.  
  124.                     case SDLK_LEFT:
  125.                         playerY-=1;
  126.                         break;
  127.  
  128.  
  129.                     //exit
  130.                     case SDLK_ESCAPE:
  131.                         quit = true;
  132.                         break;
  133.                 }
  134.             }
  135.  
  136.             else if(event.type == SDL_QUIT) {
  137.                 quit = true;
  138.             }
  139.         }
  140. }
  141.  
  142.  
  143. int main(int argc, char* args[]) {
  144.  
  145.     SDL_Init(SDL_INIT_EVERYTHING);
  146.     SDL_WM_SetCaption("Kaktus SDL Project", NULL);
  147.  
  148.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  149.     background = SDL_LoadBMP("image/background.bmp");
  150.     player = SDL_LoadBMP("image/player.bmp");
  151.  
  152.     menu();
  153.  
  154.     while(quit == false) {
  155.         paint();
  156.  
  157.         keyInput();
  158.  
  159.         SDL_Flip(screen);
  160.     }
  161.  
  162.     clean();
  163.  
  164.     SDL_Quit();
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment