Пример #1
0
/**
* \fn int initSWR()
* \brief Initialisation de la fenêtre.
*
*La fonction initSWR initialise la SDL, la SDL_Image et les pointeurs
*globalWindow ainsi que globalRenderer. Elle créé la fenêtre et le renderer associé.
*Elle retourne 1 si tout c'est bien passé, -1 sinon.
*
* \returns int, indicateur si la fonction a bien fonctionnée.
* \returns les pointeurs globalWindow et globalRenderer
*/
int initSWR()
{
	/* Initialisation simple */
	if (SDL_VideoInit(NULL) < 0)
	{
		fprintf(logFile, "initSWR : FAILURE, initialisation de la SDL (%s)\n\n", SDL_GetError());
		return -1;
	}
	/* Création de la fenêtre */
	globalWindow = creerFenetre(1080, 600, "KaamWorms");
	if (globalWindow == NULL)
	{
		fprintf(logFile, "initSWR : FAILURE, creerFenetre.\n\n");
		return -1;
	}
	{
		SDL_Surface* surfaceIcone = loadImage(ICONE);
		if (surfaceIcone != NULL)
			SDL_SetWindowIcon(globalWindow, surfaceIcone);
		SDL_FreeSurface(surfaceIcone);
	};
	/* Création du renderer */
	globalRenderer = SDL_CreateRenderer(globalWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (globalRenderer == NULL)//gestion des erreurs
	{
		fprintf(logFile, "initSWR : FAILURE, erreur lors de la creation du renderer : %s\n\n", SDL_GetError());
		cleanUp(NULL, NULL);
		return -1;
	}
	/*Initialisation SDL_Image*/
	if (IMG_Init(IMG_INIT_PNG) < 0)
	{
		fprintf(logFile, "initSWR : FAILURE, initialisation de IMG : %s.\n\n", IMG_GetError());
		cleanUp(NULL, NULL);
		return -1;
	}
	if (Mix_Init(MIX_INIT_MP3) && Mix_Init(MIX_INIT_FLAC))
	{
		if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
		{
			fprintf(logFile, "initSWR : FAILURE, initialisation de Mix_Init : %s.\n\n", Mix_GetError());
		}
	}
	fprintf(logFile, "initSWR : SUCCESS.\n\n");
	return 1;
}
Пример #2
0
int password (Mix_Chunk* gSound, short* musique, Mix_Music* gMusic, short* onoff)
{
    SDL_Window* fenetrepass = NULL;
    char texte[PASSMAX] = {0};
    size_t passlen = 0, l;
    SDL_bool done = SDL_FALSE;
    SDL_Rect rect;
    SDL_Event ev;
    SDL_Surface* ValiderTTF = NULL;
    SDL_Rect ValiderRect;
    SDL_Color ValiderColor = {0, 0, 0, 0};
    int ValiderTTFW = 0;
    int ValiderTTFH = 0;
    SDL_Surface* AnnulerTTF = NULL;
    SDL_Rect AnnulerRect;
    SDL_Color AnnulerColor = {0, 0, 0, 0};
    int AnnulerTTFW = 0;
    int AnnulerTTFH = 0;
    TTF_Font* contfuFont = NULL;
    TTF_Font* helvFont = NULL;
    SDL_Surface* surface = NULL;
    helvFont = chargerPolice (helvFont, HELVFONT, 35);
    contfuFont = chargerPolice (contfuFont, CONTFUFONT, 50);
    SDL_Surface* surfaceTexte = NULL;
    SDL_Color color2 = {255, 255, 255, 0};
    SDL_Color color = {0, 0, 0, 0};
    fenetrepass = creerFenetre (fenetrepass, TITREJEU, LARGEUR_FENETREPASS, HAUTEUR_FENETREPASS);
    SDL_StartTextInput();

    while (!done)
    {
        SDL_WaitEvent (&ev);

        switch (ev.type)
        {
            case SDL_QUIT : // Ne marche pas, je sais pas pourquoi
                printf ("Good bye\n");
                done = SDL_TRUE;
                break;

            case SDL_KEYDOWN :
                if (ev.key.keysym.sym == SDLK_BACKSPACE && passlen > 0)
                {
                    texte[passlen - 1] = 0;
                    passlen--;
                    puts (texte);
                }
                else if (ev.key.keysym.sym == SDLK_ESCAPE)
                {
                    done = SDL_TRUE;
                }
                else if (ev.key.keysym.sym == SDLK_RETURN)
                {
                    printf ("testvalider\n");
                    valider (texte, gSound, musique, gMusic, onoff, &done);
                }

                break;

            case SDL_TEXTINPUT :
                l = strlen (ev.text.text);
                strncpy (texte + passlen, ev.text.text, PASSMAX - 1 - passlen);
                passlen += l;

                if (passlen > PASSMAX - 1)
                {
                    passlen = PASSMAX - 1;
                }

                puts (texte);
                break;

            case SDL_MOUSEBUTTONDOWN:
                playsound (3, gSound);

                if (clickMenu (helvFont, "Valider", ValiderTTFW, ValiderTTFH, ev, ValiderRect) )
                {
                    printf ("testvalider\n");
                    valider (texte, gSound, musique, gMusic, onoff, &done);
                    ev.type = SDL_KEYDOWN;
                    ev.key.keysym.sym = SDLK_1;
                    SDL_PushEvent (&ev);
                }

                if (clickMenu (helvFont, "Annuler", AnnulerTTFW, AnnulerTTFH, ev, AnnulerRect) )
                {
                    done = SDL_TRUE;
                }

                break;

            case SDL_MOUSEMOTION:
                ValiderColor.r = hoverMenu (helvFont, "Valider", ValiderTTFW, ValiderTTFH, ev, ValiderRect);
                AnnulerColor.r = hoverMenu (helvFont, "Annuler", AnnulerTTFW, AnnulerTTFH, ev, AnnulerRect);
                break;
        }

        surface = SDL_GetWindowSurface (fenetrepass);
        SDL_FillRect (surface, NULL, SDL_MapRGB (surface->format, 204, 72, 63) );
        surfaceTexte = creerTexte (surfaceTexte, METHODE_BELLE, contfuFont, "Mot de passe", color2);
        rect.x = (LARGEUR_FENETREPASS / 2) - (surfaceTexte->w / 2);
        rect.y = HAUTEUR_FENETREPASS / 8;
        SDL_BlitSurface (surfaceTexte, NULL, surface, &rect);
        SDL_FreeSurface (surfaceTexte);
        ValiderTTF = creerTexte (ValiderTTF, METHODE_RAPIDE, helvFont, "Valider", ValiderColor);
        ValiderRect.x = (LARGEUR_FENETREPASS / 4) - (ValiderTTF->w / 2);
        ValiderRect.y = (3 * HAUTEUR_FENETREPASS) / 4;
        SDL_BlitSurface (ValiderTTF, NULL, surface, &ValiderRect);
        SDL_FreeSurface (ValiderTTF);
        AnnulerTTF = creerTexte (AnnulerTTF, METHODE_RAPIDE, helvFont, "Annuler", AnnulerColor);
        AnnulerRect.x = (3 * LARGEUR_FENETREPASS / 4) - (AnnulerTTF->w / 2);
        AnnulerRect.y = (3 * HAUTEUR_FENETREPASS) / 4;
        SDL_BlitSurface (AnnulerTTF, NULL, surface, &AnnulerRect);
        SDL_FreeSurface (AnnulerTTF);
        SDL_UpdateWindowSurface (fenetrepass);

        if (passlen > 0)
        {
            surfaceTexte = creerTexte (surfaceTexte, METHODE_BELLE, helvFont, texte, color);
            rect.x = (LARGEUR_FENETREPASS / 2) - (surfaceTexte->w / 2);
            rect.y = HAUTEUR_FENETREPASS / 2;
            SDL_BlitSurface (surfaceTexte, NULL, surface, &rect);
            SDL_FreeSurface (surfaceTexte);
            SDL_UpdateWindowSurface (fenetrepass);
        }
    }

    SDL_StopTextInput();
    libererPolice (helvFont);
    libererPolice (contfuFont);
    SDL_DestroyWindow (fenetrepass);
    return 0;
}
Пример #3
0
int main(int argc,char** argv)
{
  t_surface *surface=NULL;
  int cpt = 0;
  int timestart;
  char buf[50];

#ifdef T2D
  t_point2d *p1 = definirPoint2d(10,50), *p2 = definirPoint2d(100,240), *p3 = definirPoint2d(50,300);
  t_triangle2d *t1 = definirTriangle2d(p1, p2, p3);
#endif
#ifdef T3D
  t_point3d *p10 = definirPoint3d(100,50,80), *p20 = definirPoint3d(100,240,100), *p30 = definirPoint3d(50,300,100);
  t_triangle3d *t10 = definirTriangle3d(p10, p20, p30);
  t_point3d *v10 = definirVecteur3d(1,1,1);
#endif
#ifdef O3D

  t_point3d *origine = definirPoint3d(0,0,0), *vecteur;
    
  t_objet3d *camorigine=camera();

  t_objet3d *soleil=sphere(80,12,12,ROUGEC,ROUGEF);

  t_objet3d *saturne=sphere(30,10,10,MARRON1,MARRON2);
  t_point3d *centreSaturne=definirPoint3d(0,0,0);
  //translationObjet3d(saturne,centreSaturne);

  t_objet3d *uranus=sphere(30,10,10,BLEUC,BLEUC);
  t_point3d *centreUranus=definirPoint3d(0,0,0);
  //translationObjet3d(uranus,centreUranus);

  t_objet3d *terre=sphere(15,8,8,BLEUC,BLEUF);
  t_point3d *centreTerre=definirPoint3d(0,0,0);
  //translationObjet3d(terre,centreTerre);

  t_objet3d *lune=sphere(6,6,6,GRISC,GRISF);
  t_point3d *centreLune=definirPoint3d(0,0,0);
  //translationObjet3d(lune,centreLune);

  t_objet3d *anneauS=tore(40,3,3,16);

  //translationObjet3d(anneauS,centreSaturne);

  t_objet3d *anneauU=tore(40,2,3,16);

  //translationObjet3d(anneauU,centreUranus);


  t_scene3d *scene = definirScene3d(camorigine);

  t_point3d* centreSoleil=origine;

  t_scene3d *scsol = ajouter_relation(scene,soleil);
  t_scene3d *scsat = ajouter_relation(scsol,saturne);
  t_scene3d *scanneau1 = ajouter_relation(scsat, anneauS);
  t_scene3d *scura = ajouter_relation(scsol,uranus);
  t_scene3d *scanneauU=ajouter_relation(scura,anneauU);
  t_scene3d *scterre=ajouter_relation(scsol,terre);
  t_scene3d *sclune=ajouter_relation(scterre,lune);


  vecteur=definirPoint3d(150,0,0);
  translationScene3d(scterre,vecteur);
  free(vecteur);

  vecteur=definirPoint3d(250,0,0);
  translationScene3d(scsat,vecteur);
  free(vecteur);

  vecteur=definirPoint3d(320,0,0);
  translationScene3d(scura,vecteur);
  free(vecteur);

  vecteur=definirPoint3d(40,0,0);
  translationScene3d(sclune,vecteur);
  free(vecteur);


  rotationScene3d(scanneauU,centreUranus,0,0,90);
  





  t_bool quit=false;
  SDL_Event event; //variable event
  float dx=0;
  float dy=0;
  float dz=0;
  float drx=0;
  float dry=0;
  float drz=0;

#endif

  int i=0;

  surface=creerFenetre(RX,RY);
  timestart = SDL_GetTicks();


  while(i<50*10000 && !quit)
    {
      effacerFenetre(surface, 0);

#ifdef T2D
      /*
       * etape 1 : ce n'est qu'un exemple... il faut desactiver le define T2D pour la suite
       */
      remplirTriangle2d(surface, t1, echelle_de_couleur(30*i));
      SDL_Delay(500);

#endif
#ifdef T3D
      /*
       * etape 2 : ecrire le corps des fonctions de lib_mat.c et lib_3d.c
       * - commencer par definirTriangle3d et rotationTriangle3d
       * - tester en activant le define T3D
       * - terminer avec les autres fonctions
       */

      translationTriangle3d(t10,v10);

      rotationTriangle3d(t10, p10, 0, 10, 0); // rotation d'axe Y
	

      remplirTriangle3d(surface, t10, echelle_de_couleur(30*i));
      SDL_Delay(25);
	
#endif
#ifdef O3D
      /*
       * etape 3 : ecrire le corps des fonctions de lib_objet3d.c
       * - commencer par la definition d'un objet simple (parallelepipede) et l'affichage,
       * - continuer par les transformations
       * - finir par le tri des faces d'un objet et la composition des objets
       */


      // Outil SDL Benoit !

      //rotationObjet3d(soleil,origine,0,1,0);
      //dessinerObjet3d(surface,soleil);
      //printf("coucou\n");
      dessinerScene3d(surface,scene);

      SDL_PollEvent(&event); //récupère la dernière entré système(souris, clavier etc...

      
      switch(event.type){
      case SDL_KEYDOWN:
	switch(event.key.keysym.sym){    
	case SDLK_KP9:
	  dy++;
	  break;
	case SDLK_KP3:
	  dy--;
	  break;
	case SDLK_KP6:
	  dx--;
	  break;
	case SDLK_KP4:
	  dx++;
	  break;
	case SDLK_KP8:
	  dz++;
	  break;
	case SDLK_KP2:
	  dz--;
	  break;
	case SDLK_a:
	  drz++;
	  break;
	case SDLK_e:
	  drz--;
	  break;
	case SDLK_q:
	  dry--;
	  break;
	case SDLK_d:
	  dry++;
	  break;
	case SDLK_z:
	  drx++;
	  break;
	case SDLK_s:
	  drx--;
	  break;      
	case SDLK_SPACE:
	  dx=0;
	  dy=0;
	  dz=0;
	  drx=0;
	  dry=0;
	  drz=0;
	  break;
	case SDLK_ESCAPE:
	  quit=true;
	}
	break;
      }    
      
      t_point3d *translation=definirPoint3d(dx,dy,dz);
      translationScene3d(scene->pt_fils,translation);      
      free(translation);

      if(drx<0)
	{    
	  rotationScene3d(scene,origine,-1,0,0);
	}
      if(dry<0)
	{
	  rotationScene3d(scene,origine,0,-1,0);
	}
      if(drz<0)
	{
	  rotationScene3d(scene,origine,0,0,-1);
	}
      if(drx>0)
	{
	  rotationScene3d(scene,origine,1,0,0);
	}
      if(dry>0)
	{
	  rotationScene3d(scene,origine,0,1,0);
	}
      if(drz>0)
	{
	  rotationScene3d(scene,origine,0,0,1);
	  }
      
      multiplicationVecteur3d(centreSaturne,origine,scsat->descendant);
      multiplicationVecteur3d(centreUranus,origine,scura->descendant);
      multiplicationVecteur3d(centreTerre,origine,scterre->descendant);
      multiplicationVecteur3d(centreLune,origine,sclune->descendant);
      
      
      rotationScene3d(scsol,origine,0,0.2,0);
      rotationScene3d(scsat,origine,0,0.5,0);
      rotationScene3d(scterre,origine,0,1,0);
      rotationScene3d(sclune,centreLune,0,-3,0);
      rotationScene3d(scanneauU,origine,5,0,0);
      rotationScene3d(scura,origine,0,0.3,0);
      
      /*
      rotationScene3d(sclune,origine,0,-3,0);      
      rotationScene3d(scterre,centreTerre,0,1,0);
      rotationScene3d(scsat,centreSaturne,0,0.5,0);
      rotationScene3d(scura,centreUranus,0,0.3,0);
      */
      
      

      
      SDL_Delay(25);
#endif

      majEcran(surface);

      i += 1;

      cpt++;
      sprintf(buf,"%d FPS",(int)(cpt*1000.0/(SDL_GetTicks()-timestart)));
      SDL_WM_SetCaption(buf,NULL);
    }
  SDL_Quit();
  return 0;
}