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 :

probleme de fonction pixel perfect sdl


Sujet :

SDL

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    26
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 26
    Par défaut probleme de fonction pixel perfect sdl
    bonjour a tous alors voila j'ai voulu faire une fonction de collision perfect pixel en sdl l'idée : je regarde grace au coordonnées des images et a leur tailles si il y a une collision grossiere si les deux surfaces se superpose , puis si c'est le cas je teste si les pixels ne sont pas transparent et si c'est bon je renvoi collision. hors ça marche a par en dessous ou meme si les surfaces se touche avec des pixels transparents il me dit collision se qui est faux , je vous poste donc mon code pour les images prennez deux images png qui on un fond transparent pour tester .
    merci par avance .
    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
     
    #include <SDL/SDL.h>
    #include <SDL/SDL_image.h>
    #include <iostream>
     
    using namespace std;
     
     
     
    Uint32 getPixel(SDL_Surface *surface, int x, int y)
    {
        int bpp = surface->format->BytesPerPixel;
         /* Ici p est l'adresse du pixel que l'on veut connaitre */
         Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
     
         switch(bpp) {
         case 1:
             return *p;
     
         case 2:
             return *(Uint16 *)p;
     
         case 3:
             if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
                 return p[0] << 16 | p[1] << 8 | p[2];
             else
                 return p[0] | p[1] << 8 | p[2] << 16;
     
         case 4:
             return *(Uint32 *)p;
     
         default:
             return 0; /* Ne devrait pas arriver, mais évite les erreurs */
         }
     }
     
     
     
    int main(int argc, char *argv[])
    {
        SDL_Surface *ecran = NULL, *surf1 = NULL, *surf2=NULL;
        SDL_Rect pos1, pos2;
        SDL_Event event;
        int continuer = 1, collisionx=0, collisiony, collision=0, aa1,aa2, bb1,bb2,gg1,gg2,rr1,rr2;
        Uint32 pixel1, pixel2;
        Uint8 r1,g1,b1,a1,r2,g2,b2,a2;
     
        SDL_Init(SDL_INIT_VIDEO);
     
        ecran = SDL_SetVideoMode(1200, 900, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); /* Double Buffering */
        SDL_WM_SetCaption("pixel perfect", NULL);
     
        surf1 = IMG_Load("boule2.png");
        surf2 = IMG_Load("boule.png");
     
     
     
        pos1.x = ecran->w / 2 - surf1->w / 2;
        pos1.y = ecran->h / 2 - surf1->h / 2;
        pos2.x=300;
        pos2.y=300;
     
        SDL_EnableKeyRepeat(1, 1); /* Activation de la répétition des touches */
     
        while (continuer)
        {
            SDL_WaitEvent(&event);
            switch(event.type)
            {
                case SDL_QUIT:
                    continuer = 0;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_UP:
                            pos1.y--;
                            break;
                        case SDLK_ESCAPE:
                            continuer=0;
                            break;
                        case SDLK_DOWN:
                            pos1.y++;
                            break;
                        case SDLK_RIGHT:
                            pos1.x++;
                            break;
                        case SDLK_LEFT:
                            pos1.x--;
                            break;
                    }
                    break;
            }
     
    //////////////////////calcul de la longueur en x /////////////////////
     
            collisionx=0;
            collisiony=0;
            collision=0;
     
          if((pos1.x - pos2.x) > 0)
          {
              if((pos1.x - pos2.x) < surf2->w)
              {
                  collisionx = (surf2->w - (pos1.x - pos2.x));
              }
          }
          else if((pos2.x - pos1.x) > 0)
          {
              if((pos2.x - pos1.x) < surf1->w)
              {
                  collisionx = (surf1->w - (pos2.x - pos1.x));
              }
          }
          else
          {
              collisionx= 1;
          }
     
    //////////////////////calcul de la longueur en y /////////////////////
     
          if(collisionx !=0)
          {
     
          if((pos1.y - pos2.y) > 0)
          {
              if((pos1.y - pos2.y) < surf2->h)
              {
                  collisiony = ( surf2->h -(pos1.y - pos2.y));
              }
          }
          else if((pos2.y - pos1.y) > 0)
          {
              if((pos2.y - pos1.y) < surf1->h)
              {
                  collisiony = ( surf1->h - (pos2.y - pos1.y));
              }
          }
          else
          {
              collisiony= 1;
          }
          }
     
    if(collisiony !=0)
    {
        for(int i=0; i<= collisionx ;i++)
     
        {
            for(int j=0; j <= collisiony ;j++)
     
            {
                if((pos1.x - pos2.x) > 0)
                {
                    if((pos1.y - pos2.y) > 0)
                   {
     
                    pixel1 = getPixel(surf1,i,j);
                    pixel2 = getPixel(surf2,((surf2->w - collisionx)+i),((surf2->h- collisiony)+j));
     
                    SDL_GetRGBA(pixel1, surf1->format, &r1, &g1, &b1, &a1);
                    SDL_GetRGBA(pixel2, surf2->format, &r2, &g2, &b2, &a2);
                    aa1 = (int)a1;
                    aa2 = (int)a2;
                    rr1 = (int)r1;
                    rr2 = (int)r2;
                    gg2 = (int)g1;
                    gg1 = (int)g2;
                    bb1 = (int)b1;
                    bb2 = (int)b2;
     
                    if(aa1 !=0 && aa2 !=0)
                        {
                            cout << aa1 <<" : "<< aa2  << " "<< rr1 <<" : "<< rr2 << " " << gg1 <<" : "<< gg2 << " " << bb1 <<" : "<< bb2 <<endl;
                            collision=1;
                        }
                   }
                   else if((pos1.y - pos2.y) <0)
                   {
     
                    pixel1=getPixel(surf1,(i),((surf1->h - collisiony) + j));
                    pixel2=getPixel(surf2,((surf2->w - collisionx)+i),(j));
     
                    SDL_GetRGBA(pixel1, surf1->format, &r1, &g1, &b1, &a1);
                    SDL_GetRGBA(pixel2, surf2->format, &r2, &g2, &b2, &a2);
                    aa1 = (int)a1;
                    aa2 = (int)a2;
                    rr1 = (int)r1;
                    rr2 = (int)r2;
                    gg2 = (int)g1;
                    gg1 = (int)g2;
                    bb1 = (int)b1;
                    bb2 = (int)b2;
     
                    if(aa1 !=0 && aa2 !=0)
                        {
                            cout << aa1 <<" : "<< aa2  << " "<< rr1 <<" : "<< rr2 << " " << gg1 <<" : "<< gg2 << " " << bb1 <<" : "<< bb2 <<endl;
                            collision=1;
                        }
                   }
     
                }
                else if((pos1.x - pos2.x) < 0)
                {
                    if((pos1.y - pos2.y) > 0)
                   {
                    pixel1 = getPixel(surf1,((surf1->w - collisionx) + i),j);
                    pixel2 = getPixel(surf2,i,((surf2->h-collisiony)+j));
     
                    SDL_GetRGBA(pixel1, surf1->format, &r1, &g1, &b1, &a1);
                    SDL_GetRGBA(pixel2, surf2->format, &r2, &g2, &b2, &a2);
                    aa1 = (int)a1;
                    aa2 = (int)a2;
                    rr1 = (int)r1;
                    rr2 = (int)r2;
                    gg2 = (int)g1;
                    gg1 = (int)g2;
                    bb1 = (int)b1;
                    bb2 = (int)b2;
     
                    if(aa1 !=0 && aa2 !=0)
                        {
                              cout << aa1 <<" : "<< aa2  << " "<< rr1 <<" : "<< rr2 << " " << gg1 <<" : "<< gg2 << " " << bb1 <<" : "<< bb2 <<endl;
                            collision=1;
                        }
                   }
                   else if((pos1.y - pos2.y) < 0)
                   {
     
                    pixel1=getPixel(surf1,((surf1->w - collisionx)+i),((surf1->h - collisiony)+j));
                    pixel2=getPixel(surf2,i,j);
     
                    SDL_GetRGBA(pixel1, surf1->format, &r1, &g1, &b1, &a1);
                    SDL_GetRGBA(pixel2, surf2->format, &r2, &g2, &b2, &a2);
                    aa1 = (int)a1;
                    aa2 = (int)a2;
                    rr1 = (int)r1;
                    rr2 = (int)r2;
                    gg2 = (int)g1;
                    gg1 = (int)g2;
                    bb1 = (int)b1;
                    bb2 = (int)b2;
     
                    if(aa1 !=0 && aa2 !=0)
                        {
                            collision=1;
                            cout << aa1 <<" : "<< aa2  << " "<< rr1 <<" : "<< rr2 << " " << gg1 <<" : "<< gg2 << " " << bb1 <<" : "<< bb2 <<endl;
                        }
                   }
     
                }
            }
        }
     
    }
     
    //if(collision == 1) cout << "collision !!!" << endl;
     
            SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
            SDL_BlitSurface(surf1, NULL, ecran, &pos1);
            SDL_BlitSurface(surf2, NULL, ecran, &pos2);
            SDL_Flip(ecran);
        }
     
        SDL_FreeSurface(surf1);
        SDL_Quit();
     
        return EXIT_SUCCESS;
    }
    ps : voici mon projet code blocks en archive ci-dessous !
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. probleme de fonction javascript
    Par cach dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 06/07/2005, 10h34
  2. Réponses: 17
    Dernier message: 24/03/2005, 12h24
  3. [langage] problème avec fonction read
    Par domidum54 dans le forum Langage
    Réponses: 2
    Dernier message: 30/03/2004, 20h42
  4. [VB6]Problème de fonction
    Par mustang-ffw02 dans le forum VB 6 et antérieur
    Réponses: 13
    Dernier message: 27/03/2004, 15h09
  5. Probleme de fonction
    Par yenna dans le forum ASP
    Réponses: 6
    Dernier message: 01/03/2004, 14h10

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