SDL abstraction

Sturm 0 Tallied Votes 155 Views Share

Here are some functions I made for SDL abstraction:

SDL_Surface* getImage(string filename, int r=255, int g=0, int b=198) 
{
    SDL_Surface* loadedImage=0;
    SDL_Surface* optimizedImage=0;
    
    loadedImage=IMG_Load(filename.c_str());
    optimizedImage = SDL_DisplayFormat(loadedImage);
    SDL_FreeSurface(loadedImage);
    
    Uint32 colorkey=SDL_MapRGB(optimizedImage->format, r, g, b);
    SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);

    return optimizedImage;
}

void drawSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, int cX=0, int cY=0, int cW=0, int cH=0)
{
    SDL_Rect offset={x, y};
    SDL_Rect clip={cX, cY, cW, cH};
    SDL_Rect* pClip=&clip;
    
    if(!cX && !cY && !cW && !cH)
	SDL_BlitSurface(source, 0, destination, &offset);
    else
	SDL_BlitSurface(source, pClip, destination, &offset);
}

void drawText(int x, int y, string text, SDL_Surface* destination, TTF_Font* font,  int fR=255, int fG=255, int fB=255, int bR=0, int bG=0, int bB=0)
{
     SDL_Color foregroundColor={fR, fG, fB};
     SDL_Color backgroundColor={bR, bG, bB};

     SDL_Surface* textSurface=TTF_RenderText_Shaded(font, text.c_str(), foregroundColor, backgroundColor);

     apply_surface(x, y,  textSurface, destination);

     SDL_FreeSurface(textSurface);
}
Sturm 270 Veteran Poster

um sorry.. but line 35 should be drawSurface.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.