[SDL] Couleur de la surface en fonction de sa position Difficulté : 10/100
Publié le 05/02/2008 à 19h 11min par Marco565



Afficher la couleur(R,V,B) en fonction de la position de la sourie sur la surface




Cet exercice requière une maîtrise de SDL.
Vous aurez besoin de la lib SDL_TTF et du SDK de SDL.

Cahier des charges :
afficher la couleur de la position a la quelle se trouve la sourie sur la surface (on utiliseras une image)

grâce a la fonction, qui se trouve dans la doc de SDL, nous allons récupéré la couleur de la positon de la sourie sur l'image.

pour ceux qui aurais la fleme d'aller chercher la fonction dans la doc je vous la donne :

Code : c
  1. Uint32 getPixel(SDL_Surface *surface, int x, int y)
  2. {
  3. int bpp = surface->format->BytesPerPixel;
  4. /* Here p is the address to the pixel we want to retrieve */
  5. Uint8 *p = (Uint8 *)surface->pixels y * surface->pitch x * bpp;
  6.  
  7. switch(bpp) {
  8. case 1:
  9. return *p;
  10.  
  11. case 2:
  12. return *(Uint16 *)p;
  13.  
  14. case 3:
  15. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  16. return p[0] << 16 | p[1] << 8 | p[2];
  17. else
  18. return p[0] | p[1] << 8 | p[2] << 16;
  19.  
  20. case 4:
  21. return *(Uint32 *)p;
  22.  
  23. default:
  24. return 0; /* shouldn't happen, but avoids warnings */
  25. }
  26. }
  27.  



Aide :

la fonction renvoi un Uint32, il faut le separer en 3 Uint8 en fesant

Code : c
  1. Uint32 couleur;
  2. Uint8 r, v, b;
  3. SDL_GetRGB(couleur, ecran->format, &r, &g, &b);




Correction



Vous n'y arrivez pas a trouver, ne vous découragez pas voici la correction:


Code : c
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <SDL/SDL.h>
  4. #include <SDL/SDL_TTF.h>
  5. #include <SDL/SDL_image.h>
  6.  
  7.  
  8.  
  9. Uint32 getPixel(SDL_Surface *surface, int x, int y);
  10.  
  11.  
  12. int main(int argc, char** argv)
  13. {
  14. SDL_Surface *ecran, *image, *texte;
  15. SDL_Rect pos;
  16. TTF_Font *police;
  17. SDL_Color noir = {0,0,0};
  18.  
  19.  
  20.  
  21. SDL_Init(SDL_INIT_VIDEO);
  22. TTF_Init();
  23.  
  24. ecran = SDL_SetVideoMode(480, 320, 32, SDL_HWSURFACE);
  25.  
  26.  
  27.  
  28. image = SDL_LoadBMP("image.bmp");
  29. pos.x = 0;
  30. pos.y = 0;
  31.  
  32. char txt[100];
  33. police = TTF_OpenFont("arial.ttf", 15);
  34.  
  35.  
  36. int continuer = 1;
  37. int mouseX = 0, mouseY = 0;
  38. Uint32 couleur;
  39. Uint8 r = 0, b = 0, g = 0;
  40.  
  41. while(continuer)
  42. {
  43. SDL_Event event;
  44. SDL_WaitEvent(&event);
  45.  
  46. switch(event.type)
  47. {
  48. case SDL_QUIT: continuer = 0; break;
  49.  
  50. case SDL_MOUSEMOTION:
  51. mouseX = event.motion.x;
  52. mouseY = event.motion.y;
  53. break;
  54. }
  55.  
  56.  
  57. couleur = getPixel(image, mouseX, mouseY);
  58. SDL_GetRGB(couleur, ecran->format, &r, &g, &b);
  59.  
  60. sprintf(txt, "r : %d, v : %d, b : %d", (int)r, (int)g, (int)b);
  61.  
  62. texte = TTF_RenderText_Blended(police, txt, noir);
  63.  
  64. SDL_FillRect(ecran, NULL, 0);
  65.  
  66. SDL_BlitSurface(image, NULL, ecran, &pos);
  67. SDL_BlitSurface(texte, NULL, ecran, &pos);
  68.  
  69. SDL_Flip(ecran);
  70.  
  71. }
  72.  
  73.  
  74.  
  75. SDL_FreeSurface(ecran);
  76. SDL_FreeSurface(image);
  77. TTF_CloseFont(police);
  78.  
  79. TTF_Quit();
  80. SDL_Quit();
  81.  
  82.  
  83. return 0;
  84. }
  85.  
  86.  
  87. Uint32 getPixel(SDL_Surface *surface, int x, int y)
  88. {
  89. int bpp = surface->format->BytesPerPixel;
  90. /* Here p is the address to the pixel we want to retrieve */
  91. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  92.  
  93. switch(bpp) {
  94. case 1:
  95. return *p;
  96.  
  97. case 2:
  98. return *(Uint16 *)p;
  99.  
  100. case 3:
  101. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  102. return p[0] << 16 | p[1] << 8 | p[2];
  103. else
  104. return p[0] | p[1] << 8 | p[2] << 16;
  105.  
  106. case 4:
  107. return *(Uint32 *)p;
  108.  
  109. default:
  110. return 0; /* shouldn't happen, but avoids warnings */
  111. }
  112. }