83 lines
1.8 KiB
C++
83 lines
1.8 KiB
C++
#include "sdlfuncs.h"
|
|
|
|
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 */
|
|
}
|
|
}
|
|
|
|
void setPixel(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;
|
|
}
|
|
}
|
|
|
|
void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2) {
|
|
Uint32 a,b;
|
|
a = getPixel(surface, x1, y1);
|
|
b = getPixel(surface, x2, y2);
|
|
|
|
setPixel(surface, x1, y1, b);
|
|
setPixel(surface, x2, y2, a);
|
|
}
|
|
|
|
void mirrorSurfaceMiddleX(SDL_Surface *surface) {
|
|
SDL_LockSurface(surface);
|
|
int upto = surface->h / 2;
|
|
|
|
for(int i=0; i<upto; i++) {
|
|
for(int x=0; x<surface->w; x++) {
|
|
swapPixel(surface, x, i, x, surface->h-1-i);
|
|
}
|
|
}
|
|
SDL_UnlockSurface(surface);
|
|
}
|