#include "mxsurface.h" #include "mx_types.h" #ifdef HAVESTRETCH extern "C" { #include #include } #endif namespace mx { int stretchX(int cur_w ,int x, int nw) { float xp = (float)x * (float)cur_w / (float)nw; return (int)xp; } int stretchY(int cur_h, int y, int nh) { float yp = (float)y * (float)cur_h / (float)nh; return (int)yp; } mxSurface::mxSurface(SDL_Surface *surface) { p_buffer = 0; object_surface = surface; noZero(false); } mxSurface::mxSurface(const mxSurface &c) { p_buffer = 0; clean_up(); object_surface = c.object_surface; noZero(false); } mxSurface::~mxSurface() { clean_up(); } mxSurface& mxSurface::operator=(const mxSurface &c) { clean_up(); object_surface = c.object_surface; return *this; } mxSurface& mxSurface::operator=(SDL_Surface *surface) { clean_up(); object_surface = surface; return *this; } SDL_Surface *mxSurface::getSurface() const { return object_surface; } bool mxSurface::createSurface(int w, int h) { static int rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif clean_up(); object_surface = SDL_CreateRGBSurface(SDL_SWSURFACE , w, h, SDL_GetVideoSurface()->format->BitsPerPixel, rmask, gmask, bmask, amask); return true; } bool mxSurface::copySurface(const mxSurface &c, SDL_Rect *source, SDL_Rect *dest) { if(object_surface == 0) return false; SDL_BlitSurface(c.object_surface, source, object_surface, dest); return true; } bool mxSurface::copySurfaceColorKey(const mxSurface &c, SDL_Rect *source, SDL_Rect *dest, SDL_Color color_key) { if(object_surface == 0) return false; SDL_SetColorKey(c.object_surface, SDL_SRCCOLORKEY, SDL_MapRGB(c.object_surface->format, color_key.r, color_key.g, color_key.b)); copySurface(c, source, dest); return true; } void mxSurface::cleanSurface() { clean_up(); } void mxSurface::clean_up() { if(p_buffer != 0) { int i; for(i = 0; i < getSurface()->w; i++) { delete [] p_buffer[i]; } delete [] p_buffer; } if(object_surface && protected_noremove == false) { cout << "removing surface: " << (void*)object_surface << " "; if(object_surface != 0) SDL_FreeSurface(object_surface); object_surface = 0; cout << "removed\n"; } } void mxSurface::Flip() { SDL_Flip(object_surface); } mxSurface::operator SDL_Surface *() { return object_surface; } void mxSurface::noZero(bool zero) { protected_noremove = zero; } mxPoint **mxSurface::buildResizeTable(int nw, int nh, int w, int h, mxPoint **buffer) { int i,z; std::cout << "building resize table\n"; buffer = new mxPoint*[w]; for(i = 0; i < w; i++) { buffer[i] = new mxPoint[ h ]; } for(i = 0; i < w; i++) { for(z = 0; z < h; z++) { mxPoint *ptr = &buffer[i][z]; ptr->x = stretchX(nw, i, w); ptr->y = stretchY(nh, z, h); } } return buffer; } bool mxSurface::copyResizeSurface(const mxSurface &c) { if(p_buffer == 0) { p_buffer = buildResizeTable(c.getSurface()->w, c.getSurface()->h, getSurface()->w, getSurface()->h, p_buffer); } static void *buf1, *buf2; static int i,z; buf1 = lock(c.object_surface, c.object_surface->format->BitsPerPixel); buf2 = lock(object_surface, object_surface->format->BitsPerPixel); for(i = 0; i < object_surface->w; i++) { for(z = 0; z < object_surface->h; z++) { SDL_Color col; mxPoint *cur_pixel = &p_buffer[i][z]; Uint32 color = getpixel(c.object_surface, cur_pixel->x,cur_pixel->y, c.object_surface->format->BitsPerPixel, c.object_surface->pitch, &col); Color cz = color; setpixel(buf2, i, z, SDL_MapRGB(object_surface->format, cz.color.colors[0], cz.color.colors[1], cz.color.colors[2]), object_surface->format->BitsPerPixel, object_surface->pitch); } } unlock(c.object_surface); unlock(object_surface); return true; } bool mxSurface::LoadPNG(const char *file_name) { mxPng p; if(!p.pngOpen(file_name)) return false; SDL_Surface *temp; temp = p.LoadPNG(); p.pngClose(); if(temp == 0) return false; this->operator=(temp); return true; } }