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
| #include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
SDL_Window *fenetre = NULL ;
SDL_Surface *surface_fenetre = NULL ;
SDL_Renderer *moteur_rendu = NULL ;
SDL_Event evenement ;
int largeur_fenetre = 784 ;
int hauteur_fenetre = 560 ;
int touche_caractere ;
int bouton_souris ;
int roulette_souris ;
int xs_souris ;
int ys_souris ;
bool traitement = false ;
//=============================================================================================================================
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
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;
}
return 0 ;
}
//=============================================================================================================================
int affiche_palette( SDL_Renderer *moteur_rendu, int xsg, int ysg, int largeur, int hauteur)
{
int couleur, larg, haut, i, j, ibv, jbh, dv, dh ;
int y ;
Uint8 r,v,b, rr, vv, bb ;
larg = largeur/6 ;
haut = hauteur/2 ;
for (ibv=0 ; ibv < 6 ; ibv++)
{
for (i=0 ; i < 256 ; i++)
{ dv = (larg*i)/255 ;
switch(ibv)
{ case 0 : r = 0xFF ; v = i ; b = 0x00 ; break ;
case 1 : r = 255-i ; v = 0xFF ; b = 0x00 ; break ;
case 2 : r = 0x00 ; v = 0xFF ; b = i ; break ;
case 3 : r = 0x00 ; v = 255-i ; b = 0xFF ; break ;
case 4 : r = i ; v = 0x00 ; b = 0xFF ; break ;
case 5 : r = 0xFF ; v = 0x00 ; b = 255-i ; break ;
}
// for (y = 0 ; y < hauteur ; y++) pixelRGBA(moteur_rendu, xsg + ibv*larg + dv, ysg + y, r, v, b, 0xFF) ;
for(jbh=0 ; jbh < 2 ; jbh++)
{
for (j=0 ; j < 256 ; j++)
{ dh = (haut*j)/255 ;
switch(jbh)
{ case 0 : rr = (r* j )/255 ; vv = (v* j )/255 ; bb = (b* j )/255 ; break ;
// rr = r- r *(255-j)/255 ; vv = v- v *(255-j)/255 ; bb = b- b *(255-j)/255 ; break ;
case 1 : rr = r+(255-r)*j/255 ; vv = v+(255-v)*j/255 ; bb = b+(255-b)*j/255 ; break ;
}
pixelRGBA(moteur_rendu, xsg + ibv*larg + dv, ysg + jbh*haut + dh, rr, vv, bb, 0xFF) ;
}
}
}
}
return(0) ;
}
//===============================================================================================================================
int SDL_main(int argc, char* argv[])
{
Uint8 r,v,b, r1,v1,b1 ;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) ;
atexit(SDL_Quit) ;
fenetre = SDL_CreateWindow( "Test SDL_Window / SDL_Surface"
, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED
, largeur_fenetre, hauteur_fenetre
, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN
) ;
surface_fenetre = SDL_GetWindowSurface(fenetre) ;
moteur_rendu = SDL_CreateRenderer( fenetre
, -1
, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
) ;
affiche_palette(moteur_rendu, 150,150, 500, 300) ;
SDL_RenderPresent(moteur_rendu) ;
touche_caractere = -1 ;
traitement = false ;
while (touche_caractere != SDLK_ESCAPE)
{
while (SDL_PollEvent(&evenement))
{
SDL_GetMouseState(&xs_souris, &ys_souris) ;
switch(evenement.type)
{ case SDL_QUIT:
touche_caractere = SDLK_ESCAPE ;
break ;
case SDL_MOUSEBUTTONDOWN :
bouton_souris = evenement.button.button ;
traitement = true ;
break ;
case SDL_KEYDOWN :
if (evenement.key.keysym.sym == SDLK_ESCAPE) touche_caractere = SDLK_ESCAPE ;
break ;
}
if (traitement == true) break ;
} // => fin du while (SDL_PollEvent(&edgr_comm.evenement))
if (traitement == true)
{
traitement = false ;
//=================================================================
Uint8 *pixel = (Uint32*)surface_fenetre->pixels ;
SDL_LockSurface(surface_fenetre) ; // on verrouille la surface (conseillé dans la doc)
int dpl = ys_souris*surface_fenetre->w + xs_souris ;
r = pixel[dpl+0] ;
v = pixel[dpl+1] ;
b = pixel[dpl+2] ;
Uint32 point = getpixel(surface_fenetre, xs_souris, ys_souris) ;
SDL_GetRGB(point, surface_fenetre->format, &r1, &v1, &b1) ;
SDL_UnlockSurface(surface_fenetre) ;
printf(" Rouge,Vert,Bleu direct : %d(0x%02x) , %d(0x%02x) , %d(0x%02x)\n", r, r, v, v, b, b) ;
printf(" Rouge,Vert,Bleu SDL_GetRGB : %d(0x%02x) , %d(0x%02x) , %d(0x%02x)\n", r1, r1, v1, v1, b1, b1) ;
//=================================================================
SDL_RenderPresent(moteur_rendu) ;
SDL_Delay((Uint32) 20) ;
}
} // fin du while (edgr_comm.touche_caractere != SDLK_ESCAPE)
SDL_DestroyRenderer(moteur_rendu) ;
SDL_DestroyWindow (fenetre ) ;
SDL_Quit() ;
exit(EXIT_SUCCESS) ;
} |
Partager