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
|
#include "pixels_manip.h"
/**
Classe SurfTextBlended
*/
SurfTextBlended::SurfTextBlended() : _nb_elements(0), _pixels(NULL) { }
/* On libère le tableau en fonction de ce qu'il a alloué */
SurfTextBlended::~SurfTextBlended()
{
if(_nb_elements > 1)
delete[] _pixels;
else
delete _pixels;
}
/* Alloue nb_e pixels */
void SurfTextBlended::initialiser(unsigned int nb_e)
{
_nb_elements = nb_e;
_pixels = new PixelTextBlended[nb_e];
}
/*
Initialise la liste des pixels de la classe et met ts les pixels de la
surface envoyé à l'alpha alphaDepart (utile pour les dégradés)
*/
void SurfTextBlended::tarrage(SDL_Surface* src, Uint8 alphaDepart)
{
if(src) {
SDL_LockSurface(src);
unsigned int x, y = {0};
Uint32 pixel;
Uint8 r, v, b, a;
float q = 0;
/* On compte les pixels ayant un alpha > 0 */
for(x=0; x<src->w; x++) {
for(y=0; y<src->h; y++) {
pixel=getpixel(src,x,y);
SDL_GetRGBA(pixel, src->format, &r, &v, &b, &a);
if(a > 0) { _nb_elements++; }
}
}
_pixels = new PixelTextBlended[_nb_elements];
/* On récupère les pixels ayant alpha > 0 et on met l'alpha de la surface
entière = 0 */
unsigned int listage = 0;
for(x=0; x<src->w; x++) {
for(y=0; y<src->h; y++) {
pixel=getpixel(src,x,y);
SDL_GetRGBA(pixel, src->format, &r, &v, &b, &a);
if(a > 0) {
q = (double) (100 * a)/255; // % de alpha
_pixels[listage].initialiser(x, y, r, v, b, a, q);
pixel=SDL_MapRGBA(src->format, r, v, b, alphaDepart);
putpixel(src, x, y, pixel);
listage++;
}
}
}
SDL_UnlockSurface(src);
}
}
/* change les alphas des pixels de la surface proportionnellement à ce qu'il
était au départ */
void SurfTextBlended::changerAlpha(SDL_Surface* src, Uint8 alphaDelta)
{
if(src) {
SDL_LockSurface(src);
Uint32 pixel;
Uint8 r, v, b;
unsigned int x, y = {0};
Uint8 alphaFinal;
double q;
for(int i=0; i<_nb_elements; i++)
{
x = _pixels[i].getX();
y = _pixels[i].getY();
r = _pixels[i].getR();
v = _pixels[i].getV();
b = _pixels[i].getB();
q = _pixels[i].getQ();
alphaFinal = (Uint8) ((alphaDelta * q)/100);
if(alphaFinal > 255) alphaFinal = 255; // pr la sécurité
pixel=SDL_MapRGBA(src->format, r, v, b, alphaFinal);
putpixel(src, x, y, pixel);
}
SDL_UnlockSurface(src);
}
}
/**
Classe PixelTextBlended
*/
PixelTextBlended::PixelTextBlended() : _x(0), _y(0), _R(0), _V(0), _B(0), _A(0),
_quotient(0) { }
void PixelTextBlended::initialiser(unsigned int x, unsigned y, Uint8 r, Uint8 v,
Uint8 b, Uint8 a, double q)
{
_x = x;
_y = y;
_R = r;
_V = v;
_B = b;
_A = a;
_quotient = q;
}
/* Accesseurs */
unsigned int PixelTextBlended::getX() { return _x; }
unsigned int PixelTextBlended::getY() { return _y; }
Uint8 PixelTextBlended::getR() { return _R; }
Uint8 PixelTextBlended::getV() { return _V; }
Uint8 PixelTextBlended::getB() { return _B; }
double PixelTextBlended::getQ() { return _quotient; }
/*
* Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!
*/
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
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; /* shouldn't happen, but avoids warnings */
}
}
/*
* Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!
*/
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
} |
Partager