[SDL] je comprends pas bien une fonction...

je comprends pas bien une fonction... [SDL] - C - Programmation

Marsh Posté le 30-10-2003 à 10:19:13    

youp,  
 
 
quelqu'un pourrait m'éclairer sur ce pitit bout de code ?
 

Code :
  1. 1. void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
  2. 2. {
  3. 3.   Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  4. 4.   Uint32 *bufp;
  5. 5.   bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
  6. 6.   *bufp = color;
  7. 7. }


 
 
je ne comprends pas la ligne 5.
je devine qu'on donne les coordonnées d'un point à bufp... mais je n'en suis meme pas sur :sweat:  
et je comprends pas comment ca marche :(
 
 
 
merci de votre aide :jap:  
 
 
je suis neuneu ! n'est-ce pas ?


---------------
oui oui
Reply

Marsh Posté le 30-10-2003 à 10:19:13   

Reply

Marsh Posté le 30-10-2003 à 10:33:48    

oui. mais on peut pas deviner sans connaitre la classe screen. c'est par là qu'il faut chercher

Reply

Marsh Posté le 30-10-2003 à 10:57:33    

ben screen est de type SDL_Surface [:spamafote]  
 
 
je mets tout le code au cas ou...
 
source de l'exemple : http://cone3d.gamedev.net/cgi-bin/ [...] xsdl/index
 
le résultat :  
 
http://cone3d.gamedev.net/cone3d/gfxsdl/tut1.jpg
 
 

Code :
  1. #include <SDL/SDL.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B);
  5. void Slock(SDL_Surface *screen);
  6. void Sulock(SDL_Surface *screen);
  7. void DrawScene(SDL_Surface *screen);
  8. int main(int argc, char **argv) {
  9.     if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0) {
  10.         printf("initialisation caca! Erreur : %s\n", SDL_GetError());
  11.         return 1;
  12.     }
  13.     Uint32 init = SDL_WasInit(SDL_INIT_AUDIO);
  14.     Uint8 sound;
  15.     if(init & SDL_INIT_AUDIO)
  16.         sound = 1;
  17.     else
  18.         sound = 0;
  19.     atexit(SDL_Quit);
  20.    
  21.     SDL_Surface * screen;
  22.     screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
  23.     if(screen == NULL) {
  24.         printf("pas réussi à mettre 640x480x32. Erreur : %s\n", SDL_GetError());
  25.         return 1;
  26.     }
  27.     int done = 0;
  28.    
  29.     while(done == 0)
  30.     {
  31.         SDL_Event event;
  32.         while(SDL_PollEvent(&event))
  33.         {
  34.             if(event.type == SDL_QUIT)
  35.                 done = 1;
  36.             if(event.type == SDL_KEYDOWN) {
  37.                 if(event.key.keysym.sym = SDLK_ESCAPE) {
  38.                     done = 1;
  39.                 }
  40.             }
  41.            
  42.             DrawScene(screen);
  43.         }
  44.     }
  45.    
  46.     return 0;
  47. }
  48. void DrawScene(SDL_Surface *screen) {
  49.     int x;
  50.     int y;
  51.     Slock(screen);
  52.     for(x=0;x<640;x++) {
  53.         for(y=0;y<480;y++) {
  54.             DrawPixel(screen,x,y,y/2,y/2,x/3);
  55.         }
  56.     }
  57.     Sulock(screen);
  58.     SDL_Flip(screen);   // met ce qui est dans le buffer sur l'écran
  59. }
  60. void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) {
  61.     Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  62.     switch (screen->format->BytesPerPixel) {
  63.         case 1: {// si 8 bpp
  64.             Uint8 *bufp;
  65.             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
  66.             *bufp = color;
  67.         }
  68.         break;
  69.         case 2: {// 15 ou 16 bpp
  70.             Uint16 *bufp;
  71.             bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
  72.             *bufp = color;
  73.         }
  74.         break;
  75.         case 3: { // slow 24 bpp mode
  76.             Uint8 *bufp;
  77.             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x*3;
  78.             if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
  79.                 bufp[0] = color;
  80.                 bufp[1] = color >> 8;
  81.                 bufp[2] = color >> 16;
  82.             }
  83.             else {
  84.                 bufp[2] = color;
  85.                 bufp[1] = color >> 8;
  86.                 bufp[0] = color >> 16;
  87.             }
  88.         }
  89.         break;
  90.         case 4: {
  91.             Uint32 *bufp;
  92.             bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 +x;
  93.             *bufp = color;
  94.         }
  95.         break;
  96.     }
  97. }
  98. void Slock(SDL_Surface *screen) {   // vérifiage de savoir si on doit lock screen
  99.     if(SDL_MUSTLOCK(screen))
  100.         if(SDL_LockSurface(screen)<0)
  101.             return;
  102. }
  103. void Sulock(SDL_Surface *screen) {
  104.     if(SDL_MUSTLOCK(screen))
  105.         SDL_UnlockSurface(screen);
  106. }


Message édité par art_dupond le 30-10-2003 à 11:00:02

---------------
oui oui
Reply

Marsh Posté le 30-10-2003 à 11:05:40    

Ca ressemble à la "formule" générale pour trouver un pixel(x,y) dans un buffer vidéo :

Code :
  1. mon_pixel = buffer[ y * SCREEN_WIDTH + x ];


screen->pitch/4 doit permettre de récupérer la largeur de ton buffer.
 
Après recherche :
"pitch: Pitch refers to the width of the surface in bytes."
 
Comme tu effectues des accès à coup de 4 octets (Uint32), on divise par 4.


---------------
FAQ fclc++ - FAQ C++ - C++ FAQ Lite
Reply

Marsh Posté le 30-10-2003 à 11:06:21    

bah les lignes 5 et 6 permettent de changer la couleur du pixel de coordonnées (x,y) avec la couleur formée par R,G et B
(Uint32 *)screen->pixels ça permet d'acceder à l'adresse du premier pixel
y*screen->pitch/4 + x en plus, ça te permet de décaler l'adresse jusqu'à celle du pixel-qui-va-bien
 
edit: grilled  [:benou_grilled]


Message édité par Moktar1er le 30-10-2003 à 11:06:47
Reply

Marsh Posté le 30-10-2003 à 11:14:20    

Pas assez rapide petit scarabée !;)


---------------
FAQ fclc++ - FAQ C++ - C++ FAQ Lite
Reply

Marsh Posté le 30-10-2003 à 11:20:49    

ah ok... tout s'éclaire...  
 
je croyais que screen->pixels donnait l'adresse du pixel courant => moi pas comprendre...
 
puis je comprenais pas trop la définition de pitch, mais là s'est devenu limpide d'un coup (enfin grace a vous :p)
 
 
merci tous les deux :jap:


Message édité par art_dupond le 30-10-2003 à 11:21:26

---------------
oui oui
Reply

Marsh Posté le 30-10-2003 à 11:25:28    

enfin, grâce à google surtout... Y'a une tonne de tuts sur la SDL...


---------------
FAQ fclc++ - FAQ C++ - C++ FAQ Lite
Reply

Marsh Posté le 30-10-2003 à 11:57:38    

yop mais comme j'ai dit, j'ai mal compris le screen->pixels, du coup je voyais pas trop ce que faisait le reste...  
 
je suis neuneu après tout :p
 
 
 
 :hello:


Message édité par art_dupond le 30-10-2003 à 11:57:46

---------------
oui oui
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed