Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_ttf.h>
- #include <iostream>
- #include <string>
- #include "map.h"
- #include "game.h"
- #include "playBoard.h"
- #include "songBox.h"
- #include <stdlib.h>
- #include <time.h>
- //#include <SDL_audio.h>
- #include "sdlClass.h"
- #include <SDL_mixer.h>
- #include <fstream>
- #include <sstream>
- #include "tile.h"
- #include "file.h"
- const int SCREEN_WIDTH = 1000;
- const int SCREEN_HEIGHT = 600;
- using namespace std;
- Mix_Music *music;
- Mix_Chunk *chunk;
- TTF_Font *police = NULL;
- sdlClass::sdlClass()
- {
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
- cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << endl;SDL_Quit();exit(1);
- }
- window = SDL_CreateWindow("Image", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
- if (window == NULL) {
- cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << endl; SDL_Quit(); exit(1);
- }
- if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0 )
- {
- cout<<"error audio"<<Mix_GetError()<<endl;
- }
- renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
- if(TTF_Init() == -1)
- {
- cout<<"Erreur d'initialisation de TTF_Init :"<<endl<<TTF_GetError()<<endl;
- exit(1);
- }
- }
- sdlClass::~sdlClass()
- {
- Mix_FreeChunk(chunk);
- Mix_FreeMusic(music);
- Mix_CloseAudio();
- Mix_Quit();
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- TTF_CloseFont(police);
- TTF_Quit();
- SDL_Quit();
- }
- void sdlClass::affBords()
- {
- playBoard a;
- SDL_Surface *side1 = SDL_LoadBMP("./data/skin/side.bmp"); // Bordures terrain
- if (side1 == NULL)
- {
- cout<<"err"<<endl;
- }
- SDL_Rect dest1 = {a.getPoscol1() - 37, 0, 0, 0};
- SDL_BlitSurface(side1,NULL,SDL_GetWindowSurface(window),&dest1);
- SDL_Rect dest2 = {a.getPoscol4() + a.getLargeurcol(), 0, 0, 0};
- SDL_BlitSurface(side1,NULL,SDL_GetWindowSurface(window),&dest2);
- }
- string IntToString2(int a)
- {
- ostringstream temp;
- temp<<a;
- return temp.str();
- }
- int lengthY(int nbMap)
- {
- int lenInitial = 133;
- if(nbMap > 3)
- lenInitial = lenInitial - int(lenInitial / nbMap);
- return lenInitial;
- }
- int vide(int nbMap)
- {
- int videInitial = 50;
- if(nbMap > 3)
- videInitial = videInitial - int(videInitial / 3);
- return videInitial;
- }
- void posY(int nbMap, int tab[])
- {
- int posInitial = 50;
- cout<<nbMap<<endl;
- int longueur = lengthY(nbMap);
- cout<<longueur<<endl;
- tab[0] = posInitial;
- cout<<tab[0]<<endl;
- cout<<"fonction vide"<<vide(nbMap)<<endl;
- for(int i = 1; i < nbMap; i++)
- {
- cout<<"dans la boucle"<<endl;
- tab[i] = posInitial + longueur + vide(nbMap);
- posInitial = tab[i];
- cout<<"longueur : "<<longueur<<endl;
- cout<<"vide : "<<vide(nbMap)<<endl;
- cout<<tab[i]<<endl;
- }
- }
- void initTabeaux(songBox& a, map m[], SDL_Surface *sprPtr[], SDL_Rect pos[], SDL_Rect position[])
- {
- int i = 0;
- int j = 0;
- int k = 0;
- while(i < a.getNbMaps())
- {
- m[i] = map(a.getTabMaps()[i]);
- m[i].affmap();
- i = i + 1;
- }
- while(j < a.getNbMaps())
- {
- sprPtr[j] = SDL_LoadBMP(m[j].getWP().c_str());
- cout<<"afichage de map "<<m[j].getWP().c_str()<<endl;
- j = j + 1;
- }
- while(k < a.getNbMaps())
- {
- pos[k].x = 450;
- pos[k].y = position[k].y + 40;
- cout<<position[k].y<<endl<<endl<<"hazudyazcbqsjcbaqjshailhza"<<endl;
- pos[k].w = 200;
- pos[k].h = 100;
- //= {500, position[k].y - 20, 100, 100};
- k = k + 1;
- }
- }
- /*void tableauTexte(SDL_Surface *texte[], songBox& a, SDL_Color couleurNoire)
- {
- int i;
- i = 0;
- while(i < a.getNbMaps())
- {
- string niveau = "NIVEAU" + IntToString2(i+1);
- cout<<niveau<<endl;
- texte[i] = TTF_RenderText_Blended(police, niveau.c_str(), couleurNoire);
- i = i + 1;
- }
- cout<<"Dans fonction d'ecriture de texte"<<endl;
- }*/
- void sdlClass::affMenu()
- {
- songBox a;
- int nbMap = a.getNbMaps();
- SDL_Texture **text = new SDL_Texture*[nbMap];
- SDL_Rect position[nbMap];
- SDL_Surface **texte = new SDL_Surface*[nbMap];
- SDL_Surface **sprPtr = new SDL_Surface*[nbMap];
- SDL_Texture **affichage = new SDL_Texture*[nbMap];
- //BACKGROUND
- SDL_Surface *bckg = SDL_LoadBMP("./data/skin/background.bmp");
- SDL_Texture *bg = SDL_CreateTextureFromSurface(renderer, bckg);
- SDL_Rect srcBG = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
- SDL_Rect destBG = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
- SDL_RenderCopy(renderer, bg, NULL, &destBG);
- SDL_Rect pos[nbMap];
- map m[nbMap];
- int tab[nbMap];
- police = TTF_OpenFont("./include/font/font.ttf", 40);
- SDL_Color couleurNoire = {255, 255, 255};
- int i = 0;
- int j = 0;
- int z = 0;
- //SDL_Rect test = {200, 200, 0, 0};
- posY(nbMap,tab);
- initTabeaux(a, m, sprPtr, pos, position);
- // tableauTexte(texte, a, couleurNoire);
- for(int l = 0; l < a.getNbMaps(); l++)
- {
- cout<<"contenu tableau"<<endl;
- cout<<tab[i]<<endl;
- }
- if( renderer )
- {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 120);
- SDL_RenderClear(renderer);
- SDL_Delay(1500);
- while(j < nbMap)
- {
- if(sprPtr[j] != NULL)
- {
- text[j] = SDL_CreateTextureFromSurface(renderer, sprPtr[j]);
- if (text[j] != NULL)
- {
- position[j].x = 400;
- position[j].y = tab[j];
- position[j].w = 500;
- position[j].h = lengthY(nbMap);
- cout<<"contenu position"<<position[j].x<<" "<<position[j].y<<" "<<position[j].w<<" "<<position[j].h<<endl;
- SDL_RenderCopy(renderer, text[j], NULL, &position[j]);
- /* POUR LE LOGO
- SDL_Rect pos = {100,0,116,116};
- SDL_RenderCopy(renderer,pTexture,NULL,&pos); //copie du sprite grace au sdl_renderer
- SDL_DestroyTexture(pTexture); //libération de la mémoire associé à la texture */
- }
- else
- cout<<"Echec creation de texture"<<endl;
- }
- j += 1;
- cout<<" je suis laaaa"<<endl;
- }
- initTabeaux(a, m, sprPtr, pos, position);
- while(z < nbMap)
- {
- string niveau = "NIVEAU" + IntToString2(z+1);
- cout<<niveau<<endl;
- texte[z] = TTF_RenderText_Blended(police, niveau.c_str(), couleurNoire);
- affichage[z] = SDL_CreateTextureFromSurface(renderer, texte[z]);
- SDL_RenderCopy(renderer, affichage[z], NULL, &pos[z]);
- // SDL_RenderClear(renderer);
- // SDL_Delay(1000);
- z = z + 1;
- }
- }
- SDL_RenderPresent(renderer);
- SDL_Delay(5000);
- }
- void sdlClass::affFond(const string WPath)
- {
- playBoard a;
- SDL_Surface *wp = SDL_LoadBMP(WPath.c_str());
- if (wp == NULL)
- {
- cout<<"erreur"<<endl;
- }
- int x = a.getPoscol4() + a.getLargeurcol() + 13;
- SDL_Rect srcwp = {0, 0, a.getPoscol1() - 13, SCREEN_HEIGHT};
- SDL_Rect srcwp2 = {x , 0,SCREEN_WIDTH - x , SCREEN_HEIGHT};
- SDL_Rect destwp1 = {0,0,0,0};
- SDL_Rect destwp2 = {x + 17 ,0,0,0};
- SDL_SetSurfaceAlphaMod(wp , 240);
- SDL_BlitSurface(wp,&srcwp,SDL_GetWindowSurface(window),&destwp1);
- SDL_BlitSurface(wp,&srcwp2,SDL_GetWindowSurface(window),&destwp2);
- }
- void lanceAudio(map map1)
- {
- cout<<map1.getWAV()<<endl;
- music = Mix_LoadMUS(map1.getWAV().c_str());
- chunk = Mix_LoadWAV("./data/maps/maptest/clap.wav");
- Mix_PlayMusic(music, -1);
- SDL_Delay(1000);
- if(Mix_PlayMusic(music, -1)==-1) {
- cout<<"La musique ne joue pas"<<endl<<Mix_GetError()<<endl;
- }
- //Mix_PlayChannel(-1, chunk, 2);
- }
- void sdlClass::affTiles() // Fontion temp
- {
- cout<<"test4"<<endl;
- playBoard p;
- if (!(tileFile.estVide())) {
- SDL_Surface *tileSkin = SDL_LoadBMP(tileFile.premierDeLaFile().getTileTex().c_str());
- unsigned int i = 0;
- cout<<"test4b"<<endl;
- Cellule * temp = new Cellule;
- cout<<"test4c"<<endl;
- temp = tileFile.l.prem;
- cout<<"test4d"<<endl;
- cout<<tileFile.nbElements()<<endl;
- while(i < tileFile.nbElements())
- {
- cout<<tileFile.nbElements()<<endl;
- cout<<"test4e"<<endl;
- SDL_Rect srct = {0, 0, 70, 70};
- SDL_Rect destt = {p.getPoscolN(temp->info.getCol()), temp->info.getTilePos(),0,0};
- SDL_BlitSurface(tileSkin,&srct,SDL_GetWindowSurface(window),&destt);
- temp = temp->suivant;
- cout<<"test4f"<<endl;
- cout<<tileFile.nbElements()<<endl;
- i = i + 1;
- }
- delete temp;
- cout<<"test4g"<<endl;
- }
- }
- void sdlClass::AjTileTabFile()
- {
- cout<<"test2"<<endl;
- tile t;
- tileFile.enfiler(t);
- cout<<"test2b"<<endl;
- }
- void sdlClass::updateFile(map map1)
- {
- unsigned int i = 0;
- Cellule * temp = new Cellule;
- temp = tileFile.l.prem;
- while (i < tileFile.nbElements())
- {
- temp->info.setTilePos(temp->info.getTilePos() + 200 );
- i = i + 1;
- }
- delete temp;
- }
- void sdlClass::sdlAffTerr(map map1)
- {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 128);
- SDL_RenderClear(renderer);
- cout<<"test1"<<endl;
- affBords();
- affFond(map1.getWP());
- cout<<"test1b"<<endl;
- cout<<tileFile.nbElements()<<endl;
- AjTileTabFile();
- cout<<tileFile.nbElements()<<endl;
- cout<<"test3"<<endl;
- affTiles();
- // updateFile(map1);
- SDL_UpdateWindowSurface(window);
- SDL_Delay(1000);
- }
- void sdlClass::sdlBoucle (map map1) {
- //sdlAffTerr(map1);
- SDL_Event events;
- bool quit = false;
- lanceAudio(map1);
- Uint32 t = SDL_GetTicks(), nt;
- while (!quit) {
- nt = SDL_GetTicks();
- if (nt-t>500) {
- t = nt;
- }
- while (SDL_PollEvent(&events)) {
- if (events.type == SDL_QUIT) quit = true;
- else if (events.type == SDL_KEYDOWN) {
- switch (events.key.keysym.scancode) {
- case SDL_SCANCODE_ESCAPE:
- case SDL_SCANCODE_Q:
- quit = true;
- break;
- default: break;
- }
- }
- }
- affMenu();
- SDL_Delay(250);
- SDL_RenderPresent(renderer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement