thecplusplusguy

Sidescroller game with map loader function - game,cpp

Aug 12th, 2011
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.33 KB | None | 0 0
  1. #include "game.h"
  2. SDL_Rect baseclass::baseclass::coord;
  3.  
  4. void game::showMessage(const char* c)
  5. {
  6.     SDL_Color color={255,0,0};
  7.     SDL_Surface* text=TTF_RenderText_Solid(font,c,color);
  8.     SDL_Rect pos={screen->w/2 - text->w/2, 100};
  9.     SDL_BlitSurface(text,NULL,screen,&pos);
  10.     SDL_FreeSurface(text);
  11.     SDL_Flip(screen);
  12. }
  13.  
  14.  
  15.  
  16. int game::showmenu(SDL_Surface* screen)
  17. {
  18.     int x,y;
  19.     bool run=true;
  20.     const int NUMMENU=3;
  21.     const char* array[NUMMENU] = {"Continue","Map creator","Exit"};
  22.     SDL_Surface* menu[NUMMENU];
  23.     bool selected[NUMMENU] = {0,0};
  24.     SDL_Color colors[2] = {{255,255,255},{255,0,0}};
  25.  
  26.     for(int i=0;i<NUMMENU;i++)
  27.         menu[i]=TTF_RenderText_Solid(font,array[i],colors[0]);
  28.  
  29.     SDL_Rect pos[NUMMENU];
  30.     for(int i=0;i<NUMMENU;i++)
  31.     {
  32.         pos[i].x=screen->clip_rect.w/2-menu[i]->clip_rect.w/2;
  33.         pos[i].y=screen->clip_rect.h/2+i*(menu[i]->clip_rect.h);
  34.     }
  35.     SDL_Event menuevent;
  36.  
  37.     SDL_Surface* alpha=SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA,screen->w,screen->h,32,screen->format->Rmask,screen->format->Gmask,screen->format->Bmask,screen->format->Amask);
  38.     SDL_FillRect(alpha,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00));
  39.     SDL_SetAlpha(alpha,SDL_SRCALPHA,129);
  40.     SDL_BlitSurface(alpha,NULL,screen,NULL);
  41.     SDL_FreeSurface(alpha);
  42.     while(run)
  43.     {
  44.         SDL_WaitEvent(&menuevent);
  45.             switch(menuevent.type)
  46.             {
  47.                 case SDL_QUIT:
  48.                     for(int i=0;i<NUMMENU;i++)
  49.                         SDL_FreeSurface(menu[i]);
  50.                     return 2;
  51.                 case SDL_MOUSEMOTION:
  52.                     x=menuevent.motion.x;
  53.                     y=menuevent.motion.y;
  54.                     for(int i=0;i<NUMMENU;i++)
  55.                     {
  56.                         if(x>=pos[i].x && x <= (pos[i].x + menu[i]->clip_rect.w) && y>=pos[i].y && y <= (pos[i].y + menu[i]->clip_rect.h))
  57.                         {
  58.                             if(!selected[i])
  59.                             {
  60.                                 SDL_FreeSurface(menu[i]);
  61.                                 menu[i]=TTF_RenderText_Solid(font,array[i],colors[1]);
  62.                                 selected[i]=1;
  63.                             }
  64.                         }else{
  65.                             if(selected[i])
  66.                             {
  67.                                 SDL_FreeSurface(menu[i]);
  68.                                 menu[i]=TTF_RenderText_Solid(font,array[i],colors[0]);
  69.                                 selected[i]=0;
  70.                             }
  71.                         }
  72.                 }
  73.                 break;
  74.                 case SDL_MOUSEBUTTONDOWN:
  75.                     x=menuevent.button.x;
  76.                     y=menuevent.button.y;
  77.                     for(int i=0;i<NUMMENU;i++)
  78.                     {
  79.                         if(x>=pos[i].x && x <= (pos[i].x + menu[i]->clip_rect.w) && y>=pos[i].y && y <= (pos[i].y + menu[i]->clip_rect.h))
  80.                         {
  81.                             for(int j=0;j<NUMMENU;j++)
  82.                                 SDL_FreeSurface(menu[j]);
  83.                             return i;
  84.                         }
  85.                     }
  86.                     break;
  87.                 case SDL_KEYDOWN:
  88.                     if(menuevent.key.keysym.sym==SDLK_ESCAPE)
  89.                     {
  90.                         for(int i=0;i<NUMMENU;i++)
  91.                             SDL_FreeSurface(menu[i]);
  92.                         return 0;
  93.                     }
  94.                     break;
  95.             }
  96.         for(int i=0;i<NUMMENU;i++)
  97.             SDL_BlitSurface(menu[i],NULL,screen,&pos[i]);
  98.         SDL_Flip(screen);
  99.     }
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. game::game()
  107. {
  108.     screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,32,SDL_SWSURFACE);
  109.     blocks=load_image("blocks.bmp");
  110.     background=load_image("endless.bmp");
  111.     baseclass::coord.x=camera.x=0;
  112.     baseclass::coord.y=camera.y=0;
  113.     baseclass::coord.w=camera.w=SCREEN_WIDTH;
  114.     baseclass::coord.h=camera.h=SCREEN_HEIGHT;
  115.     TTF_Init();
  116.     running=1;
  117.     direction[0]=0;
  118.     direction[1]=0;
  119.     player1=new player(load_image("player.bmp"));
  120.     font=TTF_OpenFont("air.ttf",20);
  121.     finish.x=0;
  122.     finish.y=0;
  123.     finish.w=TILE_SIZE;
  124.     finish.h=TILE_SIZE;
  125.    
  126. }
  127.  
  128. game::~game()
  129. {
  130.     SDL_FreeSurface(blocks);
  131.     SDL_FreeSurface(background);
  132.     SDL_Quit();
  133.     delete player1;
  134.     for(int i=0;i<bullets.size();i++)
  135.         delete bullets[i];
  136. }
  137.  
  138. SDL_Surface* game::load_image(const char* c)
  139. {
  140.     SDL_Surface* tmp=SDL_LoadBMP(c);
  141.     SDL_Surface* tmp2=SDL_DisplayFormat(tmp);
  142.     SDL_FreeSurface(tmp);
  143.     SDL_SetColorKey(tmp2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));
  144.     return tmp2;
  145. }
  146.  
  147. void game::loadmap(const char* c)
  148. {
  149.     std::ifstream in(c);
  150.     if(!in.is_open())
  151.     {
  152.         in.open("map.map");
  153.     }
  154.     int width;
  155.     int height;
  156.     in >> width;
  157.     in >> height;
  158.     int current;
  159.     for(int i=0;i<height;i++)
  160.     {
  161.         std::vector<int> vec;
  162.         for(int j=0;j<width;j++)
  163.         {
  164.             if(in.eof())
  165.                 return;
  166.             in >> current;
  167.             if(current>=-1 && current<=8)
  168.             {
  169.                 if(current==-1){
  170.                         enemies.push_back(new enemy(load_image("enemy.bmp"),j*TILE_SIZE,i*TILE_SIZE-5,1,0));
  171.                         vec.push_back(0);
  172.                         continue;
  173.                 }else if(current==3){
  174.                     finish.x=j*TILE_SIZE;
  175.                     finish.y=i*TILE_SIZE;
  176.                     vec.push_back(current);
  177.                 }else{
  178.                 vec.push_back(current);
  179.                 }
  180.             }else{
  181.                 vec.push_back(0);
  182.             }
  183.         }
  184.         map.push_back(vec);
  185.     }
  186.     in.close();
  187.     if(finish.x==0 && finish.y==0)
  188.     {
  189.         finish.x=width*TILE_SIZE-TILE_SIZE;
  190.     }
  191. }
  192.  
  193. void game::showmap()
  194. {
  195.     int start=(baseclass::coord.x-(baseclass::coord.x%TILE_SIZE))/TILE_SIZE;
  196.     int end=(baseclass::coord.x+baseclass::coord.w+(TILE_SIZE-(baseclass::coord.x+baseclass::coord.w)%TILE_SIZE))/TILE_SIZE;
  197.     if(end>map[0].size())
  198.         end=map[0].size();
  199.     if(start<0)
  200.         start=0;
  201.     for(int i=0;i<map.size();i++)
  202.         for(int j=start;j<end;j++)
  203.             if(map[i][j]!=0)
  204.             {
  205.                 if(map[i][j]==-1)
  206.                 {
  207.                         continue;
  208.                 }
  209.                 SDL_Rect blockrect = {(map[i][j]-1)*TILE_SIZE,0,TILE_SIZE,TILE_SIZE};
  210.                 SDL_Rect destrect= {j*TILE_SIZE-baseclass::coord.x,i*TILE_SIZE};
  211.                     SDL_BlitSurface(blocks,&blockrect,screen,&destrect);
  212.             }
  213. }
  214.  
  215. void game::handleEvent()
  216. {
  217.     SDL_Event event;
  218.     while(SDL_PollEvent(&event))
  219.     {
  220.         switch(event.type)
  221.         {
  222.             case SDL_QUIT:
  223.                 running=false;
  224.                 return;
  225.             case SDL_KEYDOWN:
  226.                 switch(event.key.keysym.sym)
  227.                 {
  228.                     case SDLK_LEFT:
  229.                         direction[0]=1;
  230.                         player1->setMoving(1);
  231.                         break;
  232.                     case SDLK_RIGHT:
  233.                         direction[1]=1;
  234.                         player1->setMoving(1);
  235.                         break;
  236.                     case SDLK_SPACE:
  237.                         player1->setJump();
  238.                         break;
  239.                     case SDLK_f:
  240.                         if(player1->getDirection()=='r')
  241.                             bullets.push_back(new bullet(load_image("bullet.bmp"),player1->getRect()->x+player1->getRect()->w,player1->getRect()->y+13,5,0));
  242.                         else
  243.                             bullets.push_back(new bullet(load_image("bullet.bmp"),player1->getRect()->x,player1->getRect()->y+13,-5,0));       
  244.                         break;
  245.                     case SDLK_ESCAPE:
  246.                         int g=showmenu(screen);
  247.                         if(g==1)
  248.                         {
  249. //                          mapcreator mc;
  250. //                          mc.start();
  251.                             showMessage("Not implemented");
  252.                             SDL_Delay(1000);
  253.                         }else
  254.                         if(g==2)
  255.                             running=false;
  256.                         break;
  257.                 }
  258.                 break;
  259.             case SDL_KEYUP:
  260.                 switch(event.key.keysym.sym)
  261.                 {
  262.                     case SDLK_LEFT:
  263.                         direction[0]=0;
  264.                         player1->setMoving(0);
  265.                         break;
  266.                     case SDLK_RIGHT:
  267.                         player1->setMoving(0);
  268.                         direction[1]=0;
  269.                         break;
  270.                 }
  271.                 break;
  272.         }
  273.     }
  274. }
  275.  
  276. void game::start()
  277. {
  278.     std::ifstream in("game.dat");   //open the files with the maps
  279.     char c[20][30];
  280.     int nummaps=0;
  281.     int currentmap=0;
  282.     while(!in.eof() && nummaps<20)
  283.     {
  284.         in.getline(c[nummaps],30);  //load at max 20 maps (use vector for more) std::vector<std::string>  moremaps
  285.         nummaps++;  //number of maps
  286.     }
  287.     nummaps--;  //we go through with one map
  288.     in.close();
  289.     Uint32 start;
  290.     loadmap(c[currentmap]); //load the current map (start with 0)
  291.     while(running)
  292.     {
  293.         start=SDL_GetTicks();
  294.         if(direction[0])
  295.         {
  296.             player1->setDirection('l');
  297.             if(player1->getX()>0)
  298.                 player1->setXvel(-1);
  299.             else{
  300.                 player1->setXvel(0);
  301.                 camera.x--;
  302.                 baseclass::coord.x--;
  303.             }
  304.             if(camera.x<0)
  305.                 camera.x=2000-SCREEN_WIDTH;
  306.         }else if(direction[1])
  307.         {
  308.             player1->setDirection('r');
  309.             if(player1->getX()<80)
  310.                 player1->setXvel(1);
  311.             else{
  312.                 player1->setXvel(0);
  313.                 camera.x++;
  314.                 baseclass::coord.x++;
  315.             }
  316.             if(camera.x>=2000-SCREEN_WIDTH)
  317.                 camera.x=0;
  318.         }else{
  319.             player1->setXvel(0);
  320.         }
  321.         player1->move(map);
  322.  
  323.         SDL_BlitSurface(background,&camera,screen,NULL);
  324.         showmap();
  325.         player1->show(screen);
  326.         for(int i=0;i<enemies.size();i++)
  327.         {
  328.             if(collision(&baseclass::coord,enemies[i]->getRect()))
  329.             {
  330.     //          std::cout << "coll" << std::endl;
  331.                 SDL_Rect tmprect = {enemies[i]->getRect()->x-baseclass::coord.x,enemies[i]->getRect()->y,enemies[i]->getRect()->w,enemies[i]->getRect()->h};
  332.                 if(collision(&tmprect,player1->getRect()))
  333.                 {
  334.         //          std::cout << "collision" << std::endl;
  335.                     if(player1->getRect()->y+player1->getRect()->h>=enemies[i]->getRect()->y && player1->getRect()->y+player1->getRect()->h<=enemies[i]->getRect()->y+10) //we jumped to an enemy
  336.                     {
  337.                         delete enemies[i];
  338.                         enemies.erase(enemies.begin()+i);
  339.                         continue;
  340.                     }else{
  341.                         player1->setHealth(player1->getHealth()-1);
  342.                     }
  343.                 }
  344.                 enemies[i]->move(map);
  345.                 enemies[i]->show(screen);
  346.             }
  347.         }
  348.  
  349.  
  350.  
  351.         //hadle the bullets!
  352.         for(int i=0;i<bullets.size();i++)
  353.             if(bullets[i]->getRect()->x > SDL_GetVideoSurface()->clip_rect.w || bullets[i]->getRect()->x <0)
  354.             {
  355.                 delete bullets[i];
  356.                 bullets.erase(bullets.begin()+i);          
  357.             }
  358.  
  359.         for(int i=0;i<bullets.size();i++)
  360.         {
  361.             bullets[i]->show(screen);
  362.             bullets[i]->move();
  363.         }
  364.  
  365.     int str=(baseclass::coord.x-(baseclass::coord.x%TILE_SIZE))/TILE_SIZE;
  366.     int end=(baseclass::coord.x+baseclass::coord.w+(TILE_SIZE-(baseclass::coord.x+baseclass::coord.w)%TILE_SIZE))/TILE_SIZE;
  367.     bool nc=0;
  368.     if(end>map[0].size())
  369.         end=map[0].size();
  370.     if(start<0)
  371.         start=0;
  372.     for(int i=0;i<map.size();i++)
  373.         for(int j=str;j<end;j++)
  374.         {
  375.             if(map[i][j]==0)
  376.                 continue;
  377.             SDL_Rect destrect= {j*TILE_SIZE-baseclass::coord.x,i*TILE_SIZE,TILE_SIZE,TILE_SIZE};
  378.             for(int g=0;g<bullets.size();g++)
  379.                 if(collision(&destrect,bullets[g]->getRect()))
  380.                 {
  381.                     delete bullets[g];
  382.                     bullets.erase(bullets.begin()+g);
  383.                 }
  384.         }
  385.  
  386.         for(int i=0;i<enemies.size();i++)
  387.             for(int j=0;j<bullets.size();j++)
  388.             {
  389.                 SDL_Rect tmprec={enemies[i]->getRect()->x-baseclass::coord.x,enemies[i]->getRect()->y,40,40};
  390.                 if(collision(&tmprec,bullets[j]->getRect()))
  391.                 {
  392.                     delete enemies[i];
  393.                     delete bullets[j];
  394.                     enemies.erase(enemies.begin()+i);
  395.                     bullets.erase(bullets.begin()+j);
  396.                 }
  397.         }
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.         handleEvent();
  405.         SDL_Flip(screen);
  406.  
  407.  
  408.         if(player1->getHealth()==0 || player1->getRect()->y > screen->h)
  409.         {
  410.             showMessage("Game over");
  411.             running=false;
  412.             SDL_Delay(3000);
  413.         }
  414.         SDL_Rect tmp={baseclass::coord.x+player1->getRect()->x,player1->getRect()->y,TILE_SIZE,TILE_SIZE};
  415.         if((baseclass::coord.x+player1->getRect()->x >= finish.x && finish.y==0) || collision(&tmp,&finish))
  416.         {
  417.             currentmap++;
  418.             if(currentmap<nummaps)  //if there are other maps
  419.             {
  420.                 showMessage("You win, loading next map...");    //show a message on the screen
  421.                 SDL_Delay(3000);    //wait 3 seconds, so you can see
  422.                 map.clear();                        //load the new map and set back every variable to their initialize state;
  423.                 finish.x=finish.y=0;    //set back the finish coordinate to default
  424.                 loadmap(c[currentmap]); //load the map
  425.  
  426.                 for(int i=0;i<enemies.size();i++)
  427.                 {
  428.                     delete enemies[i];                              //delete reserved memory
  429.                     enemies.erase(enemies.begin()+i);   //and delete it from the vector too
  430.                 }
  431.                 for(int i=0;i<bullets.size();i++)
  432.                 {
  433.                     delete bullets[i];
  434.                     bullets.erase(bullets.begin()+i);
  435.                 }
  436.                 coord.x=0;  //set all parameter back
  437.                 coord.y=0;
  438.                 camera.x=0;
  439.                 camera.y=0;
  440.                 delete player1;
  441.                 player1=new player(load_image("player.bmp"));   //if you need another picture for the player
  442.                 direction[0]=0; //we are not moving
  443.                 direction[1]=0;
  444.             }else
  445.             {
  446.                 showMessage("You win, exiting...");
  447.                 SDL_Delay(3000);
  448.                 running=false;
  449.                 break;
  450.             }
  451.         }
  452.         if(1000/30>(SDL_GetTicks()-start))
  453.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  454.     }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment