// load a 32-bit RGBA TGA file into sprite 's', and add colors to the palette // as needed so that it can be shown exactly as found in the file. // returns nonzero on failure. char CEGASprit::LoadTGASprite( const std::string &filename, CSprite &sprite ) { byte *image, *base; int x,y; Uint16 w,h; unsigned char r,g,b,a; int c; Uint8 *pixel, *maskpx; // Look in local location than in global, if tga was not found! if (!LoadTGA(filename, &image, w, h)) return 1; base = image; sprite.setSize(w, h); sprite.createSurface( g_pVideoDriver->mp_VideoEngine->getBlitSurface()->flags, g_pGfxEngine->Palette.m_Palette ); SDL_Surface *sfc = sprite.getSDLSurface(); SDL_Surface *msksfc = sprite.getSDLMaskSurface(); if(SDL_MUSTLOCK(sfc)) SDL_LockSurface(sfc); if(SDL_MUSTLOCK(msksfc)) SDL_LockSurface(msksfc); pixel = (Uint8*) sfc->pixels; maskpx = (Uint8*) msksfc->pixels; for(y=h-1;y>=0;y--) { for(x=0;x<w;x++) { b = *image++; g = *image++; r = *image++; a = *image++; if (a & 128) { c = g_pGfxEngine->Palette.getcolor(r, g, b); if (c==-1) c = g_pGfxEngine->Palette.addcolor(r, g, b); if (c==-1) return 1; pixel[y*w + x] = c; maskpx[y*w + x] = 15; } else maskpx[y*w + x] = 0; } } if(SDL_MUSTLOCK(msksfc)) SDL_UnlockSurface(msksfc); if(SDL_MUSTLOCK(sfc)) SDL_UnlockSurface(sfc); sprite.m_bboxX1=0; sprite.m_bboxY1=0; sprite.m_bboxX2=sprite.getWidth(); sprite.m_bboxY2=sprite.getHeight(); delete [] base; return 0; }
void CSprite::copy( CSprite &Destination, SDL_Color *Palette ) { Destination.m_bboxX1 = m_bboxX1; Destination.m_bboxY1 = m_bboxY1; Destination.m_bboxX2 = m_bboxX2; Destination.m_bboxY2 = m_bboxY2; Destination.setSize(m_xsize, m_ysize); Destination.createSurface( mpSurface->flags, Palette ); SDL_FillRect(Destination.getSDLSurface(), NULL, COLORKEY); SDL_BlitSurface( mpSurface.get(), NULL, Destination.getSDLSurface(), NULL); }
void CEGASprit::CreateYellowSpriteofTile( CTilemap &tilemap, Uint16 tile, CSprite& sprite ) { SDL_Rect tile_rect; tile_rect.x = 16*(tile%13); tile_rect.y = 16*(tile/13); tile_rect.w = tile_rect.h= 16; sprite.setSize(tile_rect.w, tile_rect.h); sprite.createSurface( g_pVideoDriver->mp_VideoEngine->getBlitSurface()->flags, g_pGfxEngine->Palette.m_Palette ); sprite.optimizeSurface(); SDL_Surface *src_sfc = sprite.getSDLSurface(); SDL_BlitSurface(tilemap.getSDLSurface(), &tile_rect, src_sfc, NULL); if(SDL_MUSTLOCK(src_sfc)) SDL_LockSurface(src_sfc); if(src_sfc->format->BitsPerPixel == 8) return; // The first pixel is usually the transparent one on items. Use it! Uint8* pixel = (Uint8*)src_sfc->pixels; Uint32 transparent_colour; Uint32 colour; Uint8 r,g,b,a; memcpy(&transparent_colour, pixel, src_sfc->format->BytesPerPixel); for(Uint8 x=0 ; x<16 ; x++) { for(Uint8 y=0 ; y<16 ; y++) { memcpy(&colour, pixel, src_sfc->format->BytesPerPixel); SDL_GetRGBA( colour, src_sfc->format, &r, &g, &b, &a ); if( colour == transparent_colour ) a = 0; if( r!=0 && g!=0 && b!=0 && a!=0) r = g = 255; colour = SDL_MapRGBA( src_sfc->format, r, g, b, a ); memcpy( pixel, &colour ,src_sfc->format->BytesPerPixel); pixel += src_sfc->format->BytesPerPixel; } } if(SDL_MUSTLOCK(src_sfc)) SDL_UnlockSurface(src_sfc); }