Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.67 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <SDL2/SDL_ttf.h>
  3. #include <iostream>
  4. #include <string>
  5. #include "map.h"
  6. #include "game.h"
  7. #include "playBoard.h"
  8. #include "songBox.h"
  9.  
  10. #include <stdlib.h>
  11. #include <time.h>
  12. //#include <SDL_audio.h>
  13. #include "sdlClass.h"
  14. #include <SDL_mixer.h>
  15. #include <fstream>
  16. #include <sstream>
  17.  
  18. #include "tile.h"
  19. #include "file.h"
  20.  
  21. const int SCREEN_WIDTH = 1000;
  22. const int SCREEN_HEIGHT = 600;
  23.  
  24. using namespace std;
  25.  
  26. Mix_Music *music;
  27. Mix_Chunk *chunk;
  28. TTF_Font *police = NULL;
  29.  
  30. sdlClass::sdlClass()
  31. {
  32.     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  33.         cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << endl;SDL_Quit();exit(1);
  34.     }
  35.  
  36.  
  37.     window = SDL_CreateWindow("Image", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
  38.     if (window == NULL) {
  39.         cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << endl; SDL_Quit(); exit(1);
  40.     }
  41.  
  42.     if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0 )
  43.     {
  44.         cout<<"error audio"<<Mix_GetError()<<endl;
  45.     }
  46.  
  47.     renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
  48.    
  49.     if(TTF_Init() == -1)
  50.     {
  51.         cout<<"Erreur d'initialisation de TTF_Init :"<<endl<<TTF_GetError()<<endl;
  52.         exit(1);
  53.     }
  54. }
  55.  
  56. sdlClass::~sdlClass()
  57. {
  58.     Mix_FreeChunk(chunk);
  59.     Mix_FreeMusic(music);
  60.  
  61.  
  62.     Mix_CloseAudio();
  63.     Mix_Quit();
  64.     SDL_DestroyRenderer(renderer);
  65.     SDL_DestroyWindow(window);
  66.     TTF_CloseFont(police);
  67.     TTF_Quit();
  68.     SDL_Quit();
  69. }
  70.  
  71.  
  72. void sdlClass::affBords()
  73. {
  74.     playBoard a;
  75.     SDL_Surface *side1 = SDL_LoadBMP("./data/skin/side.bmp");    // Bordures terrain
  76.         if (side1 == NULL)
  77.     {
  78.         cout<<"err"<<endl;
  79.     }
  80.     SDL_Rect dest1 = {a.getPoscol1() - 37, 0, 0, 0};
  81.     SDL_BlitSurface(side1,NULL,SDL_GetWindowSurface(window),&dest1);
  82.     SDL_Rect dest2 = {a.getPoscol4() + a.getLargeurcol(), 0, 0, 0};
  83.     SDL_BlitSurface(side1,NULL,SDL_GetWindowSurface(window),&dest2);
  84.  
  85. }
  86. string IntToString2(int a)
  87. {
  88.     ostringstream temp;
  89.     temp<<a;
  90.     return temp.str();
  91. }
  92.  
  93. int lengthY(int nbMap)
  94. {
  95.     int lenInitial = 133;
  96.     if(nbMap > 3)
  97.         lenInitial = lenInitial - int(lenInitial / nbMap);
  98.     return lenInitial;
  99. }
  100.  
  101. int vide(int nbMap)
  102. {
  103.     int videInitial = 50;
  104.     if(nbMap > 3)
  105.         videInitial = videInitial - int(videInitial / 3);
  106.     return videInitial;
  107. }
  108.  
  109. void posY(int nbMap, int tab[])
  110. {
  111.     int posInitial = 50;
  112.     cout<<nbMap<<endl;
  113.     int longueur = lengthY(nbMap);
  114.     cout<<longueur<<endl;
  115.     tab[0] = posInitial;
  116.     cout<<tab[0]<<endl;
  117.     cout<<"fonction vide"<<vide(nbMap)<<endl;
  118.     for(int i = 1; i < nbMap; i++)
  119.     {
  120.         cout<<"dans la boucle"<<endl;
  121.         tab[i] = posInitial + longueur + vide(nbMap);
  122.         posInitial = tab[i];
  123.         cout<<"longueur : "<<longueur<<endl;
  124.         cout<<"vide : "<<vide(nbMap)<<endl;
  125.         cout<<tab[i]<<endl;
  126.     }
  127. }
  128. void initTabeaux(songBox& a, map m[], SDL_Surface *sprPtr[],  SDL_Rect pos[], SDL_Rect position[])
  129. {
  130.     int i = 0;
  131.     int j = 0;
  132.     int k = 0;
  133.    
  134.     while(i < a.getNbMaps())
  135.     {
  136.         m[i] = map(a.getTabMaps()[i]);
  137.         m[i].affmap();
  138.         i = i + 1;
  139.     }
  140.  
  141.     while(j < a.getNbMaps())
  142.     {
  143.         sprPtr[j] = SDL_LoadBMP(m[j].getWP().c_str());
  144.         cout<<"afichage de map "<<m[j].getWP().c_str()<<endl;
  145.         j = j + 1;
  146.     }
  147.     while(k < a.getNbMaps())
  148.     {
  149.         pos[k].x = 450;
  150.         pos[k].y = position[k].y + 40;
  151.         cout<<position[k].y<<endl<<endl<<"hazudyazcbqsjcbaqjshailhza"<<endl;
  152.         pos[k].w = 200;
  153.         pos[k].h = 100;
  154.        
  155.          //= {500, position[k].y - 20, 100, 100};
  156.         k = k + 1;
  157.     }
  158.    
  159. }
  160.  
  161. /*void tableauTexte(SDL_Surface *texte[], songBox& a, SDL_Color couleurNoire)
  162. {
  163.     int i;
  164.     i = 0;
  165.     while(i < a.getNbMaps())
  166.     {
  167.         string niveau = "NIVEAU" + IntToString2(i+1);
  168.         cout<<niveau<<endl;
  169.         texte[i] = TTF_RenderText_Blended(police, niveau.c_str(), couleurNoire);
  170.         i = i + 1;
  171.     }
  172.     cout<<"Dans fonction d'ecriture de texte"<<endl;
  173. }*/
  174.  
  175. void sdlClass::affMenu()
  176. {
  177.     songBox a;
  178.     int nbMap = a.getNbMaps();
  179.     SDL_Texture **text = new SDL_Texture*[nbMap];
  180.     SDL_Rect position[nbMap];
  181.     SDL_Surface **texte = new SDL_Surface*[nbMap];
  182.     SDL_Surface **sprPtr = new SDL_Surface*[nbMap];
  183.     SDL_Texture **affichage = new SDL_Texture*[nbMap];
  184.    
  185.     //BACKGROUND
  186.     SDL_Surface *bckg = SDL_LoadBMP("./data/skin/background.bmp");
  187.     SDL_Texture *bg = SDL_CreateTextureFromSurface(renderer, bckg);
  188.     SDL_Rect srcBG = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
  189.     SDL_Rect destBG = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
  190.     SDL_RenderCopy(renderer, bg, NULL, &destBG);
  191.    
  192.    
  193.     SDL_Rect pos[nbMap];
  194.     map m[nbMap];
  195.     int tab[nbMap];
  196.     police = TTF_OpenFont("./include/font/font.ttf", 40);
  197.     SDL_Color couleurNoire = {255, 255, 255};
  198.     int i = 0;
  199.     int j = 0;
  200.     int z = 0;
  201.    
  202.     //SDL_Rect test = {200, 200, 0, 0};
  203.     posY(nbMap,tab);
  204.     initTabeaux(a, m, sprPtr, pos, position);
  205.     //  tableauTexte(texte, a, couleurNoire);
  206.  
  207.     for(int l = 0; l < a.getNbMaps(); l++)
  208.     {
  209.         cout<<"contenu tableau"<<endl;
  210.         cout<<tab[i]<<endl;
  211.     }
  212.  
  213.     if( renderer )
  214.     {
  215.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 120);
  216.         SDL_RenderClear(renderer);
  217.         SDL_Delay(1500);
  218.  
  219.  
  220.         while(j < nbMap)
  221.         {
  222.             if(sprPtr[j] != NULL)
  223.             {
  224.  
  225.                     text[j] = SDL_CreateTextureFromSurface(renderer, sprPtr[j]);
  226.                     if (text[j] != NULL)
  227.                     {
  228.                         position[j].x =  400;
  229.                         position[j].y = tab[j];
  230.                         position[j].w =  500;
  231.                         position[j].h = lengthY(nbMap);        
  232.                         cout<<"contenu position"<<position[j].x<<"  "<<position[j].y<<"   "<<position[j].w<<" "<<position[j].h<<endl;
  233.                         SDL_RenderCopy(renderer, text[j], NULL, &position[j]);
  234.  
  235.                         /*  POUR LE LOGO
  236.                         SDL_Rect pos = {100,0,116,116};
  237.                         SDL_RenderCopy(renderer,pTexture,NULL,&pos); //copie du sprite grace au sdl_renderer
  238.                         SDL_DestroyTexture(pTexture); //libération de la mémoire associé à la texture */
  239.                     }
  240.                     else
  241.                         cout<<"Echec creation de texture"<<endl;
  242.  
  243.             }
  244.             j += 1;
  245.             cout<<" je suis laaaa"<<endl;
  246.         }
  247.         initTabeaux(a, m, sprPtr, pos, position);
  248.  
  249.         while(z < nbMap)
  250.         {
  251.             string niveau = "NIVEAU" + IntToString2(z+1);
  252.             cout<<niveau<<endl;
  253.             texte[z] = TTF_RenderText_Blended(police, niveau.c_str(), couleurNoire);
  254.             affichage[z] = SDL_CreateTextureFromSurface(renderer, texte[z]);
  255.             SDL_RenderCopy(renderer, affichage[z], NULL, &pos[z]);
  256.         //  SDL_RenderClear(renderer);
  257.         //  SDL_Delay(1000);
  258.             z = z + 1;
  259.         }
  260.     }
  261.     SDL_RenderPresent(renderer);
  262.     SDL_Delay(5000);
  263. }
  264.  
  265. void sdlClass::affFond(const string WPath)
  266. {
  267.     playBoard a;
  268.  
  269.     SDL_Surface *wp = SDL_LoadBMP(WPath.c_str());
  270.     if (wp == NULL)
  271.     {
  272.         cout<<"erreur"<<endl;
  273.     }
  274.     int x = a.getPoscol4() + a.getLargeurcol() + 13;
  275.  
  276.     SDL_Rect srcwp = {0, 0, a.getPoscol1() - 13, SCREEN_HEIGHT};
  277.     SDL_Rect srcwp2 = {x , 0,SCREEN_WIDTH - x , SCREEN_HEIGHT};
  278.     SDL_Rect destwp1 = {0,0,0,0};
  279.     SDL_Rect destwp2 = {x + 17  ,0,0,0};
  280.  
  281.     SDL_SetSurfaceAlphaMod(wp , 240);
  282.  
  283.  
  284.     SDL_BlitSurface(wp,&srcwp,SDL_GetWindowSurface(window),&destwp1);
  285.     SDL_BlitSurface(wp,&srcwp2,SDL_GetWindowSurface(window),&destwp2);
  286.  
  287. }
  288.  
  289. void lanceAudio(map map1)
  290. {
  291.     cout<<map1.getWAV()<<endl;
  292.     music = Mix_LoadMUS(map1.getWAV().c_str());
  293.  
  294.     chunk = Mix_LoadWAV("./data/maps/maptest/clap.wav");
  295.  
  296.     Mix_PlayMusic(music, -1);
  297.  
  298.     SDL_Delay(1000);
  299.     if(Mix_PlayMusic(music, -1)==-1) {
  300.         cout<<"La musique ne joue pas"<<endl<<Mix_GetError()<<endl;
  301.     }
  302.     //Mix_PlayChannel(-1, chunk, 2);
  303.  
  304.  
  305.  
  306. }
  307.  
  308.  
  309.  
  310. void sdlClass::affTiles()  // Fontion temp
  311. {
  312.     cout<<"test4"<<endl;
  313.     playBoard p;
  314.     if (!(tileFile.estVide())) {
  315.     SDL_Surface *tileSkin = SDL_LoadBMP(tileFile.premierDeLaFile().getTileTex().c_str());
  316.     unsigned int i = 0;
  317.     cout<<"test4b"<<endl;
  318.     Cellule * temp = new Cellule;
  319.     cout<<"test4c"<<endl;
  320.     temp = tileFile.l.prem;
  321.     cout<<"test4d"<<endl;
  322.     cout<<tileFile.nbElements()<<endl;
  323.     while(i < tileFile.nbElements())
  324.     {
  325.  
  326.     cout<<tileFile.nbElements()<<endl;
  327.         cout<<"test4e"<<endl;
  328.         SDL_Rect srct = {0, 0, 70, 70};
  329.         SDL_Rect destt = {p.getPoscolN(temp->info.getCol()), temp->info.getTilePos(),0,0};
  330.         SDL_BlitSurface(tileSkin,&srct,SDL_GetWindowSurface(window),&destt);
  331.         temp = temp->suivant;
  332.         cout<<"test4f"<<endl;
  333.  
  334.     cout<<tileFile.nbElements()<<endl;
  335.     i = i + 1;
  336.     }
  337.     delete temp;
  338.     cout<<"test4g"<<endl;
  339. }
  340. }
  341.  
  342. void sdlClass::AjTileTabFile()
  343. {
  344.     cout<<"test2"<<endl;
  345.     tile t;
  346.     tileFile.enfiler(t);
  347.     cout<<"test2b"<<endl;
  348. }
  349.  
  350. void sdlClass::updateFile(map map1)
  351. {
  352.     unsigned int i = 0;
  353.  
  354.     Cellule * temp = new Cellule;
  355.     temp = tileFile.l.prem;
  356.     while (i < tileFile.nbElements())
  357.     {
  358.         temp->info.setTilePos(temp->info.getTilePos() + 200 );
  359.         i = i + 1;
  360.     }
  361.     delete temp;
  362. }
  363.  
  364.  
  365. void sdlClass::sdlAffTerr(map map1)
  366. {
  367.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 128);
  368.     SDL_RenderClear(renderer);
  369.  
  370.     cout<<"test1"<<endl;
  371.     affBords();
  372.     affFond(map1.getWP());
  373.     cout<<"test1b"<<endl;
  374.  
  375.     cout<<tileFile.nbElements()<<endl;
  376.     AjTileTabFile();
  377.  
  378.     cout<<tileFile.nbElements()<<endl;
  379.     cout<<"test3"<<endl;
  380.     affTiles();
  381.   //  updateFile(map1);
  382.  
  383.  
  384.  
  385.     SDL_UpdateWindowSurface(window);
  386.     SDL_Delay(1000);
  387.  
  388.  
  389. }
  390.  
  391. void sdlClass::sdlBoucle (map map1) {
  392.  
  393.     //sdlAffTerr(map1);
  394.  
  395.     SDL_Event events;
  396.     bool quit = false;
  397.  
  398.  
  399.     lanceAudio(map1);
  400.  
  401.     Uint32 t = SDL_GetTicks(), nt;
  402.  
  403.     while (!quit) {
  404.  
  405.         nt = SDL_GetTicks();
  406.         if (nt-t>500) {
  407.             t = nt;
  408.  
  409.         }
  410.  
  411.         while (SDL_PollEvent(&events)) {
  412.             if (events.type == SDL_QUIT) quit = true;
  413.             else if (events.type == SDL_KEYDOWN) {
  414.                 switch (events.key.keysym.scancode) {
  415.                 case SDL_SCANCODE_ESCAPE:
  416.                 case SDL_SCANCODE_Q:
  417.                     quit = true;
  418.                     break;
  419.                 default: break;
  420.                 }
  421.             }
  422.         }
  423.  
  424.         affMenu();
  425.         SDL_Delay(250);
  426.  
  427.        SDL_RenderPresent(renderer);
  428.     }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement