Don't like ads? PRO users don't see any ads ;-)

SDL tutorial 18

By: thecplusplusguy on Jun 22nd, 2012  |  syntax: C++  |  size: 6.06 KB  |  hits: 627  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.  
  28.         SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00));
  29.  
  30.         SDL_Event event;
  31.         while(1)
  32.         {
  33.                 time = SDL_GetTicks();
  34.                 while(SDL_PollEvent(&event))
  35.                 {
  36.                         switch(event.type)
  37.                         {
  38.                                 case SDL_QUIT:
  39.                                         SDL_FreeSurface(menus[0]);
  40.                                         SDL_FreeSurface(menus[1]);
  41.                                         return 1;
  42.                                 case SDL_MOUSEMOTION:
  43.                                         x = event.motion.x;
  44.                                         y = event.motion.y;
  45.                                         for(int i = 0; i < NUMMENU; i += 1) {
  46.                                                 if(x>=pos[i].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h)
  47.                                                 {
  48.                                                         if(!selected[i])
  49.                                                         {
  50.                                                                 selected[i] = 1;
  51.                                                                 SDL_FreeSurface(menus[i]);
  52.                                                                 menus[i] = TTF_RenderText_Solid(font,labels[i],color[1]);
  53.                                                         }
  54.                                                 }
  55.                                                 else
  56.                                                 {
  57.                                                         if(selected[i])
  58.                                                         {
  59.                                                                 selected[i] = 0;
  60.                                                                 SDL_FreeSurface(menus[i]);
  61.                                                                 menus[i] = TTF_RenderText_Solid(font,labels[i],color[0]);
  62.                                                         }
  63.                                                 }
  64.                                         }
  65.                                         break;
  66.                                 case SDL_MOUSEBUTTONDOWN:
  67.                                         x = event.button.x;
  68.                                         y = event.button.y;
  69.                                         for(int i = 0; i < NUMMENU; i += 1) {
  70.                                                 if(x>=pos[i].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h)
  71.                                                 {
  72.                                                         SDL_FreeSurface(menus[0]);
  73.                                                         SDL_FreeSurface(menus[1]);
  74.                                                         return i;
  75.                                                 }
  76.                                         }
  77.                                         break;
  78.                                 case SDL_KEYDOWN:
  79.                                         if(event.key.keysym.sym == SDLK_ESCAPE)
  80.                                         {
  81.                                                 SDL_FreeSurface(menus[0]);
  82.                                                 SDL_FreeSurface(menus[1]);
  83.                                                 return 0;
  84.                                         }
  85.                         }
  86.                 }
  87.                 for(int i = 0; i < NUMMENU; i += 1) {
  88.                         SDL_BlitSurface(menus[i],NULL,screen,&pos[i]);
  89.                 }
  90.                 SDL_Flip(screen);
  91.                 if(1000/30 > (SDL_GetTicks()-time))
  92.                         SDL_Delay(1000/30 - (SDL_GetTicks()-time));
  93.         }
  94. }
  95.  
  96.  
  97. SDL_Surface *load_image(const char *c, Uint32 colorkey = 0)
  98. {
  99.         SDL_Surface *tmp = SDL_LoadBMP(c);
  100.         if(colorkey != 0)
  101.         {
  102.                 SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, colorkey);
  103.         }
  104.         return tmp;
  105. }
  106.  
  107. int main()
  108. {
  109.         SDL_Surface *screen, *icon;
  110.         const int width = 640;
  111.         const int height = 480;
  112.         const int FPS = 30;
  113.         screen = SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
  114.         icon = load_image("icon.bmp");
  115.         SDL_WM_SetIcon(icon, NULL);
  116.         SDL_WM_SetCaption("Pong Game", NULL);
  117.         TTF_Font *font;
  118.         TTF_Init();
  119.         Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
  120.         Mix_Music *music;
  121.         Mix_Chunk *effect,*effect2;
  122.         music = Mix_LoadMUS("tempmusic.wav");
  123.         effect = Mix_LoadWAV("tempsound1.wav");
  124.         effect2 = Mix_LoadWAV("tempsound2.wav");
  125.         Mix_PlayMusic(music,-1);
  126.         font = TTF_OpenFont("Test.ttf",30);
  127.         SDL_Color color = {0,0,0};
  128.         SDL_Event event;
  129.         Uint32 start;
  130.         bool running = true;
  131.         bool arr[4] = {0,0,0,0};
  132.         paddle player1(load_image("paddle.bmp"),0,225,10,50,3);
  133.         paddle player2(load_image("paddle.bmp"),width-10,255,10,50,3);
  134.         ball ball1(load_image("ball.bmp",SDL_MapRGB(screen->format,0x00,0xff,0xff)),320,240,20,20,3,3);
  135.         int i = showmenu(screen, font);
  136.         if(i==1)
  137.                 running = false;
  138.         while(running)
  139.         {
  140.                 start = SDL_GetTicks();
  141.                 //handle events
  142.                 while(SDL_PollEvent(&event))
  143.                 {
  144.                         switch(event.type)
  145.                         {
  146.                                 case SDL_QUIT:
  147.                                         running = false;
  148.                                         break;
  149.                                 case SDL_KEYDOWN:
  150.                                         switch(event.key.keysym.sym)
  151.                                         {
  152.                                                 case SDLK_UP:
  153.                                                         arr[0] = 1;
  154.                                                         break;
  155.                                                 case SDLK_DOWN:
  156.                                                         arr[1] = 1;
  157.                                                         break;
  158.  
  159.                                                 case SDLK_w:
  160.                                                         arr[2] = 1;
  161.                                                         break;
  162.                                                 case SDLK_s:
  163.                                                         arr[3] = 1;
  164.                                                         break;
  165.                                                 case SDLK_ESCAPE:
  166.                                                         int i = showmenu(screen, font);
  167.                                                         if(i==1)
  168.                                                                 running = false;
  169.                                                         break;
  170.                                         }
  171.                                         break;
  172.                                 case SDL_KEYUP:
  173.                                         switch(event.key.keysym.sym)
  174.                                         {
  175.                                                 case SDLK_UP:
  176.                                                         arr[0] = 0;
  177.                                                         break;
  178.                                                 case SDLK_DOWN:
  179.                                                         arr[1] = 0;
  180.                                                         break;
  181.  
  182.                                                 case SDLK_w:
  183.                                                         arr[2] = 0;
  184.                                                         break;
  185.                                                 case SDLK_s:
  186.                                                         arr[3] = 0;
  187.                                                         break;
  188.                                         }
  189.                                         break;
  190.                         }
  191.                 }
  192.                 //logic
  193.                 if(arr[0])
  194.                         player2.moveUp();
  195.                 else if(arr[1])
  196.                         player2.moveDown();
  197.                 if(arr[2])
  198.                         player1.moveUp();
  199.                 else if(arr[3])
  200.                         player1.moveDown();
  201.                 ball1.move(player1.getRect(), player2.getRect(), effect);
  202.                 switch(ball1.isOut())
  203.                 {
  204.                         case 1:
  205.                                 player2.incpoint();
  206. //                              player1.setBack(0,225,10,50,3);
  207. //                              player2.setBack(width-10,255,10,50,3);
  208.                                 ball1.setBack(320,240,20,20,3,3,effect2);
  209.                                 break;
  210.                         case 2:
  211.                                 player1.incpoint();
  212. //                              player1.setBack(0,225,10,50,3);
  213. //                              player2.setBack(width-10,255,10,50,3);
  214.                                 ball1.setBack(320,240,20,20,3,3,effect2);
  215.                                 break;
  216.                 }
  217.                
  218.  
  219.                 //render
  220.                 SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0xff,0xff,0xff));
  221.                 player1.show();
  222.                 player2.show();
  223.                 ball1.show();
  224.  
  225.                 char c[5];
  226.                 SDL_Rect tmp = {10,0};
  227.                 sprintf(c, "%d", player1.getPoints());
  228.                 SDL_Surface *text = TTF_RenderText_Solid(font,c,color);
  229.                 SDL_BlitSurface(text,NULL,screen,&tmp);
  230.  
  231.                 tmp.x = width-40;
  232.                 sprintf(c, "%d", player2.getPoints());
  233.                 text = TTF_RenderText_Solid(font,c,color);
  234.                 SDL_BlitSurface(text,NULL,screen,&tmp);
  235.                 SDL_FreeSurface(text);
  236.  
  237.                 SDL_Flip(screen);
  238.                 //regulate FPS
  239.                 if(1000/FPS > (SDL_GetTicks()-start))
  240.                         SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  241.         }
  242.         // deinitialization
  243.         SDL_FreeSurface(icon);
  244.         Mix_FreeMusic(music);
  245.         Mix_FreeChunk(effect);
  246.         Mix_FreeChunk(effect2);
  247.         Mix_CloseAudio();
  248.         TTF_CloseFont(font);
  249.         TTF_Quit();
  250.         SDL_Quit();
  251. }