Exemplo n.º 1
0
void dijkstra( long int n, long int nSrc, double *sources, double *D, double *P, const mxArray *G ) {
  // dealing with sparse array
  double *Gpr = mxGetPr(G);
  int *Gir = mxGetIr(G);
  int *Gjc = mxGetJc(G);
  
  // allocate memory for single source results (automatically recycled)
  double *D1 = (double *) mxCalloc( n , sizeof( double ));
  double *P1 = (double *) mxCalloc( n , sizeof( double ));
  
  // loop over sources
  long int s, i, j;
  for( i=0; i<nSrc; i++ ) {
    
    // run the dijkstra1 code for single source (0 indexed)
    s = (long int) *( sources + i ) - 1;
    if (s<0 || s > n-1) mexErrMsgTxt( "Source node(s) out of bound" );
    dijkstra1( n, s, D1, P1, Gpr, Gir, Gjc );
    
    // store results
    for( j=0; j<n; j++ ) {
      *( D + j*nSrc + i ) = *( D1 + j );
      *( P + j*nSrc + i ) = *( P1 + j );
    }
  }
}
Exemplo n.º 2
0
/**
 * \brief      Main pour lancer le jeu
 * \details    Gestion des evenements (clavier) de l'utilisateur pour lancer le jeu
 */
int main(int argc, char *argv[])
{
    score=0;

    // Variables pour ecriture avec TTF
    TTF_Font *police, *police2, *police3 = NULL;
    SDL_Color couleurBlanche = {255, 255, 255};
    SDL_Color couleurScore = {0, 0, 0};
    SDL_Surface *texteP1, *texteP2, *texteP3, *texteScore, *texteScore2 = NULL;

    TTF_Init();

    if(TTF_Init() == -1)
    {
        fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
        exit(EXIT_FAILURE);
    }

    /* Chargement des polices */
    police = TTF_OpenFont("ARCHRISTY.ttf", 65);
    police2 = TTF_OpenFont("ARCHRISTY.ttf", 35);
    police3 = TTF_OpenFont("ARCHRISTY.ttf", 26);
    /* Écriture du texte dans la SDL_Surface texte en mode Blended (optimal) */
    texteP1 = TTF_RenderText_Blended(police2, "HOW PLAY TO SQUIRREL ACORN : ", couleurBlanche);
    texteP2 = TTF_RenderText_Blended(police3, "PRESS 1 : NORMAL MODE", couleurBlanche);
    texteP3 = TTF_RenderText_Blended(police3, "PRESS 2 : NO CONSTRAINTS MODE (without obstacles)", couleurBlanche);
    // Init des positions des cases et du personnage
    initilisationPositions();

    SDL_Init(SDL_INIT_VIDEO);

    ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, TAILLE_SPRITE, SDL_HWSURFACE); // Ouverture de la fenêtre
    if (ecran == NULL)
    {
        fprintf(stderr, "Impossible de charger le mode video : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    SDL_WM_SetCaption("2D GAME | SQUIRREL ACORN", NULL);

    // Autres variables pour les positions des ecritures
    SDL_Rect positionT;
    SDL_Rect positionT2;
    SDL_Rect positionT3;
    SDL_Rect positionT4;
    SDL_Rect positionT5;
    SDL_Rect positionT6;
    SDL_Rect positionScore;
    SDL_Rect positionScore2;
    positionT.x = 70;
    positionT.y = 185;
    positionT2.x = 45;
    positionT2.y = 265;
    positionT3.x = 27;
    positionT3.y = 365;
    positionT4.x = 70;
    positionT4.y = 215;
    positionT5.x = 170;
    positionT5.y = 275;
    positionT6.x = 20;
    positionT6.y = 330;
    positionScore.x = 75;
    positionScore.y = 265;
    positionScore2.x = 150;
    positionScore2.y = 345;

    // Affichage du texte
    SDL_BlitSurface(texteP1, NULL, ecran, &positionT4); /* Blit du texte */
    SDL_BlitSurface(texteP2, NULL, ecran, &positionT5); /* Blit du texte */
    SDL_BlitSurface(texteP3, NULL, ecran, &positionT6); /* Blit du texte */

    // Initialisation du tableau avec la position des ressources
    int tabR[4];
    tabR[0]=14;
    tabR[1]=138;
    tabR[2]=242;
    tabR[3]=359;

    SDL_Flip(ecran);

    //Gestion des evèvements ( inputs )
    while (exec)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                exec=false;
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    versHaut();
                    break;
                case SDLK_DOWN:
                    versBas();
                    break;
                case SDLK_RIGHT:
                    versDroite();
                    break;
                case SDLK_LEFT:
                    versGauche();
                    break;
                case SDLK_1:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra1(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                case SDLK_KP1:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra1(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                case SDLK_2:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra2(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                case SDLK_KP2:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra2(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                    case SDLK_ESCAPE:
                        // Pour quitter le jeu
                        exit(0);
                    break;
            }
            break;
        }
    }

    // Liberation de la surface
    SDL_FreeSurface(sprites);

    TTF_CloseFont(police);
    TTF_Quit();

    SDL_Quit();


    return EXIT_SUCCESS; // Fermeture du programme
}