timesynk/engine/sdl/sdl_extra.c

94 lines
2.6 KiB
C

/****** sdl_extra.c
sdl_extra.c/.h contain extra functions, structs, etc. that expand basic SDL functionality, such as pixel manipulation or surface scaling.
******/
#include "interface.h"
#include "sdl_extra.h"
SDL_Surface *SDL_ScaleSurface(SDL_Surface *surface, float scale_x, float scale_y) {
long x;
long y;
long output_x;
long output_y;
long new_width = (long)((float)surface->w * scale_x+0.5f);
long new_height = (long)((float)surface->h * scale_y+0.5f);
SDL_Surface *new_surface = SDL_CreateRGBSurface(surface->flags, new_width, new_height, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
for (y = 0;y < surface->h;y++) {
for (x = 0;x < surface->w;x++) {
for (output_y = 0;output_y < scale_y; ++output_y) {
for (output_x = 0;output_x < scale_x; ++output_x) {
putpixel(new_surface, (Sint32)(scale_x*x) + output_x, (Sint32)(scale_y*y)+output_y, getpixel(surface, x, y));
}
}
}
}
return new_surface;
}
Uint32 getpixel(SDL_Surface *surface, int x, int y) {
if (y >= 0 && x >= 0 && x <= surface->w && y <= surface->h) {
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;
break;
case 2:
return *(Uint16 *)p;
break;
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;
break;
case 4:
return *(Uint32 *)p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
return 0;
}
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
if (y >= 0 && x >= 0 && x <= surface->w && y <= surface->h) {
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;
}
}
}