Advertisement
thecplusplusguy

SDL tutorial 19

Jun 27th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. #include <SDL/SDL.h>
  4. #include <iostream>
  5. #include <SDL/SDL_ttf.h>
  6. #include <SDL/SDL_mixer.h>
  7. #include "ball.h"
  8. #include "paddle.h"
  9.  
  10. int showmenu(SDL_Surface* screen, TTF_Font* font)
  11. {
  12.     Uint32 time;
  13.     int x, y;
  14.     const int NUMMENU = 2;
  15.     const char* labels[NUMMENU] = {"Continue","Exit"};
  16.     SDL_Surface* menus[NUMMENU];
  17.     bool selected[NUMMENU] = {0,0};
  18.     SDL_Color color[2] = {{255,255,255},{255,0,0}};
  19.  
  20.     menus[0] = TTF_RenderText_Solid(font,labels[0],color[0]);
  21.     menus[1] = TTF_RenderText_Solid(font,labels[1],color[0]);
  22.     SDL_Rect pos[NUMMENU];
  23.     pos[0].x = screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
  24.     pos[0].y = screen->clip_rect.h/2 - menus[0]->clip_rect.h;
  25.     pos[1].x = screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
  26.     pos[1].y = screen->clip_rect.h/2 + menus[0]->clip_rect.h;
  27. //  SDL_Surface* alpha = SDL_CreateRGBSurface(SDL_SWSURFACE,screen->w,screen->h,32,screen->format->Rmask,screen->format->Gmask,screen->format->Bmask,screen->format->Amask);    // alternative 1
  28.     SDL_Surface* alpha = SDL_LoadBMP("background.bmp"); // alternative 2
  29. //  SDL_FillRect(alpha,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00));   // alternative 1
  30.     SDL_SetAlpha(alpha,SDL_SRCALPHA,129);
  31.     SDL_BlitSurface(alpha,NULL,screen,NULL);
  32.     SDL_FreeSurface(alpha);
  33.  
  34.     SDL_Event event;
  35.     while(1)
  36.     {
  37.         time = SDL_GetTicks();
  38.         while(SDL_PollEvent(&event))
  39.         {
  40.             switch(event.type)
  41.             {
  42.                 case SDL_QUIT:
  43.                     SDL_FreeSurface(menus[0]);
  44.                     SDL_FreeSurface(menus[1]);
  45.                     return 1;
  46.                 case SDL_MOUSEMOTION:
  47.                     x = event.motion.x;
  48.                     y = event.motion.y;
  49.                     for(int i = 0; i < NUMMENU; i += 1) {
  50.                         if(x>=pos[i].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h)
  51.                         {
  52.                             if(!selected[i])
  53.                             {
  54.                                 selected[i] = 1;
  55.                                 SDL_FreeSurface(menus[i]);
  56.                                 menus[i] = TTF_RenderText_Solid(font,labels[i],color[1]);
  57.                             }
  58.                         }
  59.                         else
  60.                         {
  61.                             if(selected[i])
  62.                             {
  63.                                 selected[i] = 0;
  64.                                 SDL_FreeSurface(menus[i]);
  65.                                 menus[i] = TTF_RenderText_Solid(font,labels[i],color[0]);
  66.                             }
  67.                         }
  68.                     }
  69.                     break;
  70.                 case SDL_MOUSEBUTTONDOWN:
  71.                     x = event.button.x;
  72.                     y = event.button.y;
  73.                     for(int i = 0; i < NUMMENU; i += 1) {
  74.                         if(x>=pos[i].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h)
  75.                         {
  76.                             SDL_FreeSurface(menus[0]);
  77.                             SDL_FreeSurface(menus[1]);
  78.                             return i;
  79.                         }
  80.                     }
  81.                     break;
  82.                 case SDL_KEYDOWN:
  83.                     if(event.key.keysym.sym == SDLK_ESCAPE)
  84.                     {
  85.                         SDL_FreeSurface(menus[0]);
  86.                         SDL_FreeSurface(menus[1]);
  87.                         return 0;
  88.                     }
  89.             }
  90.         }
  91.         for(int i = 0; i < NUMMENU; i += 1) {
  92.             SDL_BlitSurface(menus[i],NULL,screen,&pos[i]);
  93.         }
  94.         SDL_Flip(screen);
  95.         if(1000/30 > (SDL_GetTicks()-time))
  96.             SDL_Delay(1000/30 - (SDL_GetTicks()-time));
  97.     }
  98. }
  99.  
  100.  
  101. SDL_Surface *load_image(const char *c, Uint32 colorkey = 0)
  102. {
  103.     SDL_Surface *tmp = SDL_LoadBMP(c);
  104.     if(colorkey != 0)
  105.     {
  106.         SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, colorkey);
  107.     }
  108.     return tmp;
  109. }
  110.  
  111. int main()
  112. {
  113.     SDL_Surface *screen, *icon;
  114.     const int width = 640;
  115.     const int height = 480;
  116.     const int FPS = 30;
  117.     screen = SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
  118.     icon = load_image("icon.bmp");
  119.     SDL_WM_SetIcon(icon, NULL);
  120.     SDL_WM_SetCaption("Pong Game", NULL);
  121.     TTF_Font *font;
  122.     TTF_Init();
  123.     Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
  124.     Mix_Music *music;
  125.     Mix_Chunk *effect,*effect2;
  126.     music = Mix_LoadMUS("tempmusic.wav");
  127.     effect = Mix_LoadWAV("tempsound1.wav");
  128.     effect2 = Mix_LoadWAV("tempsound2.wav");
  129.     Mix_PlayMusic(music,-1);
  130.     font = TTF_OpenFont("Test.ttf",30);
  131.     SDL_Color color = {0,0,0};
  132.     SDL_Event event;
  133.     Uint32 start;
  134.     bool running = true;
  135.     bool arr[4] = {0,0,0,0};
  136.     paddle player1(load_image("paddle.bmp"),0,225,10,50,3);
  137.     paddle player2(load_image("paddle.bmp"),width-10,255,10,50,3);
  138.     ball ball1(load_image("ball.bmp",SDL_MapRGB(screen->format,0x00,0xff,0xff)),320,240,20,20,3,3);
  139.     int i = showmenu(screen, font);
  140.     if(i==1)
  141.         running = false;
  142.     while(running)
  143.     {
  144.         start = SDL_GetTicks();
  145.         //handle events
  146.         while(SDL_PollEvent(&event))
  147.         {
  148.             switch(event.type)
  149.             {
  150.                 case SDL_QUIT:
  151.                     running = false;
  152.                     break;
  153.                 case SDL_KEYDOWN:
  154.                     switch(event.key.keysym.sym)
  155.                     {
  156.                         case SDLK_UP:
  157.                             arr[0] = 1;
  158.                             break;
  159.                         case SDLK_DOWN:
  160.                             arr[1] = 1;
  161.                             break;
  162.  
  163.                         case SDLK_w:
  164.                             arr[2] = 1;
  165.                             break;
  166.                         case SDLK_s:
  167.                             arr[3] = 1;
  168.                             break;
  169.                         case SDLK_ESCAPE:
  170.                             int i = showmenu(screen, font);
  171.                             if(i==1)
  172.                                 running = false;
  173.                             break;
  174.                     }
  175.                     break;
  176.                 case SDL_KEYUP:
  177.                     switch(event.key.keysym.sym)
  178.                     {
  179.                         case SDLK_UP:
  180.                             arr[0] = 0;
  181.                             break;
  182.                         case SDLK_DOWN:
  183.                             arr[1] = 0;
  184.                             break;
  185.  
  186.                         case SDLK_w:
  187.                             arr[2] = 0;
  188.                             break;
  189.                         case SDLK_s:
  190.                             arr[3] = 0;
  191.                             break;
  192.                     }
  193.                     break;
  194.             }
  195.         }
  196.         //logic
  197.         if(arr[0])
  198.             player2.moveUp();
  199.         else if(arr[1])
  200.             player2.moveDown();
  201.         if(arr[2])
  202.             player1.moveUp();
  203.         else if(arr[3])
  204.             player1.moveDown();
  205.         ball1.move(player1.getRect(), player2.getRect(), effect);
  206.         switch(ball1.isOut())
  207.         {
  208.             case 1:
  209.                 player2.incpoint();
  210. //              player1.setBack(0,225,10,50,3);
  211. //              player2.setBack(width-10,255,10,50,3);
  212.                 ball1.setBack(320,240,20,20,3,3,effect2);
  213.                 break;
  214.             case 2:
  215.                 player1.incpoint();
  216. //              player1.setBack(0,225,10,50,3);
  217. //              player2.setBack(width-10,255,10,50,3);
  218.                 ball1.setBack(320,240,20,20,3,3,effect2);
  219.                 break;
  220.         }
  221.        
  222.  
  223.         //render
  224.         SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0xff,0xff,0xff));
  225.         player1.show();
  226.         player2.show();
  227.         ball1.show();
  228.  
  229.         char c[5];
  230.         SDL_Rect tmp = {10,0};
  231.         sprintf(c, "%d", player1.getPoints());
  232.         SDL_Surface *text = TTF_RenderText_Solid(font,c,color);
  233.         SDL_BlitSurface(text,NULL,screen,&tmp);
  234.  
  235.         tmp.x = width-40;
  236.         sprintf(c, "%d", player2.getPoints());
  237.         text = TTF_RenderText_Solid(font,c,color);
  238.         SDL_BlitSurface(text,NULL,screen,&tmp);
  239.         SDL_FreeSurface(text);
  240.  
  241.         SDL_Flip(screen);
  242.         //regulate FPS
  243.         if(1000/FPS > (SDL_GetTicks()-start))
  244.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  245.     }
  246.     // deinitialization
  247.     SDL_FreeSurface(icon);
  248.     Mix_FreeMusic(music);
  249.     Mix_FreeChunk(effect);
  250.     Mix_FreeChunk(effect2);
  251.     Mix_CloseAudio();
  252.     TTF_CloseFont(font);
  253.     TTF_Quit();
  254.     SDL_Quit();
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement