IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SDL Discussion :

Dessiner des images SDL dans des processus fork()


Sujet :

SDL

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2014
    Messages
    96
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2014
    Messages : 96
    Points : 51
    Points
    51
    Par défaut Dessiner des images SDL dans des processus fork()
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    typedef struct {
      int road1; //Number of cars on the secondary road
      int road2; //Number of cars on the primary road
    } CarNumber;
    CarNumber *carNumber;
     
     
    /* variables used by several process */
    pid_t pid;
    int mutex = 1;
    int fire = 0;
    int color = 0;
    int end = 0;
     
     
    /* Graphical user interface initialisation */
    SDL_Surface *ecran, *imageFond, *voiture1, *voiture2, *feurouge, *feuvert;
    SDL_Rect positionimgFond, positionVoiture1, positionVoiture2, positionFeurouge, positionFeurouge2;
     
    SDL_Event event;
    int quit = 0;
     
    void initGUI () {
        SDL_Init(SDL_INIT_VIDEO);
        ecran = SDL_SetVideoMode(500, 500, 32, SDL_HWSURFACE);
        imageFond = SDL_LoadBMP("images/imagefond.bmp");
     
        positionimgFond.x = 0;
        positionimgFond.y = 0;
     
        SDL_BlitSurface(imageFond, NULL, ecran, &positionimgFond);
        SDL_Flip(ecran);
    }
     
    void car(int roadNumber, int id) {
        while (quit == 0) {
     
     
        voiture1 = SDL_LoadBMP("images/bleu.bmp");
        voiture2 = SDL_LoadBMP("images/rouge.bmp");
     
        SDL_SetColorKey(voiture1, SDL_SRCCOLORKEY, SDL_MapRGB(voiture1->format, 255, 255, 255));
        SDL_SetColorKey(voiture2, SDL_SRCCOLORKEY, SDL_MapRGB(voiture2->format, 255, 255, 255));
     
        positionVoiture1.x = 100;
        positionVoiture1.y = 210;
     
        positionVoiture2.x = 100;
        positionVoiture2.y = 300;
     
     
        SDL_BlitSurface(voiture1, NULL, ecran, &positionVoiture1);
        SDL_BlitSurface(voiture2, NULL, ecran, &positionVoiture2);
     
        if (positionVoiture1.x == 0 || positionVoiture2.x == 0) {
                positionVoiture1.x = 200;
                positionVoiture2.x = 200;
        }
     
        positionVoiture1.x--;
        positionVoiture2.x--;
     
        P(mutex);
        printf("\tVOITURE : arrivée de la voiture %d sur la voie %d\n", id, roadNumber);
        if (roadNumber == 1)***
            carNumber->road1++;
        else
            carNumber->road2++;
        V(mutex);
        printf("\tVOITURE : la voiture %d attend le feu\n", id);
        if (roadNumber == 1)
          printf("\tVOITURE : il y a %d voitures en attente\n", carNumber->road1);
        else
          printf("\tVOITURE : il y a %d voitures en attente\n", carNumber->road2);
        P(fire);
        printf("\tVOITURE : la voiture %d est passée\n", id);
     
        SDL_Flip(ecran);
     
        }
    }
     
    int main (int nArg, char *args[]) {
        while (quit == 0) {
        initGUI();
     
     if (paramA != 0) {
            int i;
            for (i = 1; i <= paramN; i++) {
                switch (pid = fork()) {
                    case -1: perror("Creation de processus");
                        return 3;
                    case 0:
                        /**********************************/
                        /* process child i : Car i */
                        srandom (pid);
                        car(random () % 2 + 1, i);
                        return 0;
                }
            }
        }
    Bonjour, je vous explique mon soucis j'essaye de représenter graphiquement mes processus voitures afin qu'elle se déplace, avoir un apercu graphique de ce qu'il se passe. C'est une simulation d'un carrefour pour info.
    Merci de votre aide

  2. #2
    Expert éminent sénior

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 196
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 196
    Points : 17 165
    Points
    17 165
    Par défaut
    C'est globalement impossible.

    Il ne doit y avoir qu'un seul thread manipulant la SDL.

    Par contre, rien n'empêche que tu aies un vecteurtableau ou une liste de voitures, chacune modifiée par son propre thread, que tu parcours dans le thread "graphique".
    Ce sera beaucoup plus facile que d'avoir des variables voiture1, voiture2.

    Ce thread se trouve en général être le thread principal.
    La représentation graphique devrait être indépendante de la partie données (les voitures en elle-même).


    Au passage, dans une balise [code], les espaces sont conservés, ce n'est pas la peine de mettre des étoiles.

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2014
    Messages
    96
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2014
    Messages : 96
    Points : 51
    Points
    51
    Par défaut
    Vous pourriez me donner un exemple d'utilisation avec SDL de vecteurs s' il vous plaît

  4. #4
    Expert éminent sénior

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 196
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 196
    Points : 17 165
    Points
    17 165
    Par défaut
    Désolé, j'ai parlé de vecteur parce que je travaille actuellement en C++. Il fallait lire tableau/liste.

    Ca ressemblerait à ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    struct voiture{
       ...
       SDL_Surface *image;
       SDL_Rect *position
    };
     
    void dessiner_voiture(SDL_Surface *ecran, voiture const* v) {
        SDL_BlitSurface(v.image, NULL, ecran, v.position);
    }
     
    int main() {
     
        struct voiture voitures[NB_VOITURES] = /*je ne sais pas quoi, c'est toi qui sait*/
     
        SDL_init(...);
        while (ce n'est pas fini) {
            /*gerer evenement (ce qui inclue noter que c'est fini dans certaines conditions)*/
     
            for (int i = 0; i < NB_VOITURES; ++i) {
                dessiner_voiture(ecran, voitures[i]);
            }
        }
    }
    C'est partiellement du pseudocode, parce que je n'ai pas envie de refouiller la doc de la SDL.
    Inspire toi aussi des exemples que tu trouveras sur leur site, et de nos cours SDL.

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2014
    Messages
    96
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2014
    Messages : 96
    Points : 51
    Points
    51
    Par défaut
    Merci à vous premièrement pour votre aide .
    Secondement, nous avons un peu avancé avec mon binôme qui lui est sur mac et qui a aussi publié sur le forum sous le pseudo koreissimouhamad. Moi je suis sur Linux et la fenêtre SDL se lance mais rien ne s affiche dedans elle se ferme tout de suite.

    Voici le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
     /* Shared variable */
    typedef struct {
      int road1; //Number of cars on the secondary road
      int road2; //Number of cars on the primary road
    } CarNumber;
    CarNumber *carNumber;
     
    /* variables used by several process */
    pid_t pid;
    int mutex = 1;
    int fire = 0;
    int end = 0; 
    int progTime;
     
    Car *tab1, *tab2; // Waiting files that help us to schedule process
     
    typedef struct GUI {
        SDL_Surface *screen, *background, *redFire, *greenFire;
        SDL_Rect *posBackground, *posFire1, *posFire2;
    } *GUI;
     
    /**********************************************************************
     * Function that draw a car on the screen
     **********************************************************************/
    void drawCar (GUI gui, Car const c) {
        SDL_BlitSurface(c -> image, NULL, gui -> screen, c -> position);
    }
     
    void initGui (GUI gui) {
        SDL_Init(SDL_INIT_VIDEO);
        gui -> screen = SDL_SetVideoMode(500, 500, 32, SDL_HWSURFACE);
        gui -> background = SDL_LoadBMP("images/imagefond.bmp");
        gui -> redFire = SDL_LoadBMP("images/feurouge.bmp");
        gui -> greenFire = SDL_LoadBMP("images/feuvert.bmp");
        SDL_SetColorKey(gui -> redFire, SDL_SRCCOLORKEY, SDL_MapRGB(gui -> redFire->format, 255, 255, 255));
        SDL_SetColorKey(gui -> greenFire, SDL_SRCCOLORKEY, SDL_MapRGB(gui -> greenFire->format, 255, 255, 255));
        gui -> posBackground -> x = 0;
        gui -> posBackground -> y = 0;
     
        gui -> posFire1 -> x = 0;
        gui -> posFire1 -> y = 180;
     
        gui -> posFire2 -> x = 0;
        gui -> posFire2 -> y = 280;
    }
     
    void drawGui (GUI gui, int nbCars1, int nbCars2, int fire1, int fire2) {
        int i;
        SDL_BlitSurface(gui -> background, NULL, gui -> screen, gui -> posBackground); 
        if (fire1 == 0) SDL_BlitSurface(gui -> redFire, NULL, gui -> screen, gui -> posFire1); 
        else if (fire1 == 1) SDL_BlitSurface(gui -> greenFire, NULL, gui -> screen, gui -> posFire1); 
        if (fire2 == 0) SDL_BlitSurface(gui -> redFire, NULL, gui -> screen, gui -> posFire2); 
        else if (fire2 == 1) SDL_BlitSurface(gui -> greenFire, NULL, gui -> screen, gui -> posFire2); 
        for (i = 0; i < nbCars1; i++) {
            drawCar(gui, tab1[i]);
        }
        for (i = 0; i < nbCars2; i++) {
            drawCar(gui, tab2[i]);
        }
        SDL_Flip(gui -> screen); 
    }
     
    /**********************************************************************
     * cars' process
     **********************************************************************/
    void car(int roadNumber, int id) {
        Car c = malloc (sizeof (Car*));
        c -> id = id;
        c -> position -> x = 0; c -> position -> y = 0;
        c -> image = SDL_LoadBMP("images/bleu.bmp");
        SDL_SetColorKey(c -> image, SDL_SRCCOLORKEY, SDL_MapRGB(c -> image ->format, 255, 255, 255));
        P(mutex);
        printf("\tVOITURE : arrivée de la voiture %d sur la voie %d\n", id, roadNumber);
        if (roadNumber == 1) {
            carNumber->road1++;
            tab1[carNumber->road1-1] = c;
        }
        else {
            carNumber->road2++;
            tab2[carNumber->road2-1] = c;
        }
        printf("\tVOITURE : la voiture %d attend le feu\n", id);
        if (roadNumber == 1)
          printf("\tVOITURE : il y a %d voitures en attente\n", carNumber->road1);
        else
          printf("\tVOITURE : il y a %d voitures en attente\n", carNumber->road2);
        V(mutex);
        P(fire);
    }
     
    /**********************************************************************
     * fires' process
     **********************************************************************/
    void fires(int roadNumber, int t) {
      while (end != 1) {
        int k;
        printf("CARREFOUR : Le feu %d passe au rouge\n", roadNumber);
        sleep(t);
        V(fire);
        printf("CARREFOUR : Le feu %d passe au vert\n", roadNumber);
        sortTable (tab1, carNumber->road1);
        sortTable (tab2, carNumber->road2);
        if (roadNumber == 1){
            printf("CARREFOUR : On libère %d voitures\n", carNumber->road1);
            for (k = 0; k < carNumber->road1; k++)
                printf("\tVOITURE : la voiture %d est passée\n", tab1[k] -> id);
        }
        else {
            printf("CARREFOUR : On libère %d voitures\n", carNumber->road2);
            for (k = 0; k < carNumber->road2; k++)
                printf("\tVOITURE : la voiture %d est passée\n", tab2[k] -> id);
        }
        P(mutex);
        if (roadNumber == 1){
          carNumber->road1 = 0;
        }
        else {
          carNumber->road2 = 0;
        }
        V(mutex);
        sleep(t);
      }
    }    
     
    /**********************************************************************
     * programm's time
     **********************************************************************/
    void programmTime () {
        GUI gui;
        initGui (gui);
        while (end == 0) {
            drawGui (gui, carNumber->road1, carNumber->road2, 1, 0);
            progTime++;
            printf("Programm's time%d\n", progTime);
            sleep (1);
        }
    }
     
    /**********************************************************************
     * Main function
     **********************************************************************/
     
    int main (int nArg, char **args) {
     	char *filename = NULL;
     	int paramA = 0, paramN = CARSNUMBER, paramT;
     	int automatic = 0;
     
     	//Entry verification
     	if (entryVerification (nArg, args, &paramA, &paramN, &paramT, &filename) == -1 || nArg > 7) {
        	printUsage ();
        	return 1;
        }
     
        //Case of a configuration file
        if (filename != NULL) {
        	int n;
        	char **args2 = str_split (loadParam (filename, &n), " ");
        	if (entryVerification (n, args2, &paramA, &paramN, &paramT, &filename) == -1) {
    	    	printUsage ();
    	    	return 1;
    	    }
        	//printf("n = %d, command = %s, chaine = %s, p1 = %d, %d, %d\n", n, args2[2], loadParam (filename, &n), paramA, paramN, paramT);
        }
     
        int key, kMutex, kFire;
        pid_t pidFire1, pidFire2, pidCar, p;
     
        key = ftok("/etc/passwd",0);
        carNumber = (CarNumber*)shmalloc(key,sizeof(CarNumber));
        tab1 = (Car*)shmalloc(key+1,sizeof(Car)*100);
        tab2 = (Car*)shmalloc(key+2,sizeof(Car)*100);
        if (!carNumber || !tab1 || !tab2) {  
            fprintf(stderr, "erreur de creation de memoire partagee\n");
            return 1;
        }
     
        /* Shared area initialisation */
        carNumber->road1 = 0;      /* There is no car on the secondary road at beginning */
        carNumber->road2 = 0;      /* There is no car on the primary road at beginning */
     
        /* semaphore mutex's creation */
        kMutex = key+1;
        mutex = mutalloc(kMutex);
        if (mutex == -1) {
            fprintf(stderr, "erreur de creation de semaphore\n");
            shmfree(key);
            return 2;
        }
     
        /* semaphore fire's creation */
        kFire = key+2;
        fire = semalloc(kFire,0);
        if (fire == -1) {
            fprintf(stderr, "erreur de creation de semaphore\n");
            shmfree(key);
            mutfree(kMutex);
            return 2;
        }
     
        //Programm's time
        switch (pid=fork()) {
            case -1: perror("Creation de processus");
                return 3;
            case 0: 
                /************************************/
                /* process child 1 : Programm's time */
                programmTime ();
                return 0;
        }
     
        //Secondary road's process
        switch (pid=fork()) {
            case -1: perror("Creation de processus");
                return 3;
            case 0: 
                /************************************/
                /* process child 2 : Secondary road */
                fires(1, paramT);
                return 0;
        }
        pidFire1 = pid;
     
        //Primary road's process
        switch (pid=fork()) {
            case -1: perror("Creation de processus");
                return 3;
            case 0: 
                /************************************/
                /* process child 3 : Primary road */
                fires(2, 2 * paramT);
                return 0;
        }
        pidFire2 = pid;
     
        /************************************/
        /* Cars' process */
     
        //Automatic mode
        if (paramA != 0) {
            int i;
            for (i = 1; i <= paramN; i++) {
                switch (pid = fork()) {
                    case -1: perror("Creation de processus");
                        return 3;
                    case 0: 
                        /**********************************/
                        /* process child i : Car i */
                        srandom (pid);
                        car(random () % 2 + 1, i);
                        return 0;
                }
            }
        }
        else { // Interactive mode
            int i = 1;
            char c; 
            switch (p = fork()) { 
                case -1: perror("Creation de processus");
                    return 3;
                case 0:    
                    while (1) {
                      c = mygetchar(); 
                      if (c == 'q') {
                        return 0;
                      }
                      if (c == 'k'){//If k touch is typed, a car is generated on the secondary road
                         switch (pid=fork()) {
                            case -1: perror("Creation de processus");
                                return 3;
                            case 0: 
                                /**********************************/
                                /* pprocess child i : Car i */
                                car(1, i);
                                return 0;
                        }
                      } 
                      else if (c == 'l') { //If l touch is typed, a car is generated o, the primary road
                        switch (pid=fork()) {
                            case -1: perror("Creation de processus");
                                return 3;
                            case 0: 
                                /**********************************/
                                /* process child i : Car i */
                                car(2, i);
                                return 0;
                        }
                      }
                      i++;
                      bzero(&c, sizeof(c));
                    }
                    return 0;
            }
        }
        end = 1;
     
        /******************/
        /* Father process */
     
        /* attente du Fire pour sortir : permet de tuer les fils en meme temps que le pere sur un ^C */
        waitpid(pid,0,0);
        puts("PERE : fin du pere");
     
        /* recuperation IPC */
        printf("PERE : destruction memoire partagee et semaphores: %d\n", shmfree(key) +  mutfree(mutex) + semfree(fire));
     
     	return  0;

  6. #6
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 084
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 084
    Points : 7 998
    Points
    7 998
    Par défaut
    Tu as utiliser des fonction SDL sans vérifier leur retour. Forcement si une ne fonctionne pas... tu ne sauras pas d'ou vient le problème. Donc rajoute le test du retour des fonction pour voir ou ça coince.

Discussions similaires

  1. Réponses: 10
    Dernier message: 19/07/2014, 14h34
  2. Réponses: 1
    Dernier message: 27/04/2012, 16h41
  3. Réponses: 5
    Dernier message: 09/09/2011, 23h07
  4. [Débutant] Affichage des composantes connexes dans des images differentes
    Par hardman dans le forum Images
    Réponses: 2
    Dernier message: 18/08/2009, 13h31
  5. [Image] image map dans des images dynamique?
    Par xtaze dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 19/05/2006, 17h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo