Atrinik Client  4.0
Data Structures | Macros | Typedefs | Functions | Variables
sprite.c File Reference
#include <global.h>
#include <toolkit/string.h>
#include <toolkit/colorspace.h>

Go to the source code of this file.

Data Structures

struct  sprite_cache
 

Macros

#define GLOW_GRID_PIXEL_NONE   0
 
#define GLOW_GRID_PIXEL_VISIBLE   1
 
#define GLOW_GRID_PIXEL_GLOW   2
 
#define GLOW_GRID_PIXEL_OUTLINE   3
 
#define FREE_TMP_SURFACE()
 

Typedefs

typedef struct sprite_cache sprite_cache_t
 

Functions

void sprite_init_system (void)
 
sprite_structsprite_load_file (char *fname, uint32_t flags)
 
sprite_structsprite_tryload_file (char *fname, uint32_t flag, SDL_RWops *rwop)
 
void sprite_free_sprite (sprite_struct *sprite)
 
static sprite_cache_tsprite_cache_find (const char *name)
 
static sprite_cache_tsprite_cache_create (const char *name)
 
static void sprite_cache_add (sprite_cache_t *cache)
 
static void sprite_cache_remove (sprite_cache_t *cache)
 
static void sprite_cache_free (sprite_cache_t *cache)
 
void sprite_cache_free_all (void)
 
void sprite_cache_gc (void)
 
static SDL_Surface * sprite_effect_red (SDL_Surface *surface)
 
static SDL_Surface * sprite_effect_gray (SDL_Surface *surface)
 
static SDL_Surface * sprite_effect_fow (SDL_Surface *surface)
 
static SDL_Surface * sprite_effect_glow (SDL_Surface *surface, const SDL_Color *color, double speed, double state)
 
static SDL_Surface * sprite_effects_create (SDL_Surface *surface, const sprite_effects_t *effects)
 
void surface_show (SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
 
void surface_show_fill (SDL_Surface *surface, int x, int y, SDL_Rect *srcsize, SDL_Surface *src, SDL_Rect *box)
 
void surface_show_effects (SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src, const sprite_effects_t *effects)
 
Uint32 getpixel (SDL_Surface *surface, int x, int y)
 
void putpixel (SDL_Surface *surface, int x, int y, Uint32 pixel)
 
static bool surface_border_get_left (SDL_Surface *surface, int *pos, uint32_t color)
 
static bool surface_border_get_right (SDL_Surface *surface, int *pos, uint32_t color)
 
static bool surface_border_get_top (SDL_Surface *surface, int *pos, uint32_t color)
 
static bool surface_border_get_bottom (SDL_Surface *surface, int *pos, uint32_t color)
 
int surface_borders_get (SDL_Surface *surface, int *top, int *bottom, int *left, int *right, uint32_t color)
 
void surface_pan (SDL_Surface *surface, SDL_Rect *box)
 
void draw_frame (SDL_Surface *surface, int x, int y, int w, int h)
 
void border_create (SDL_Surface *surface, int x, int y, int w, int h, int color, int size)
 
void border_create_line (SDL_Surface *surface, int x, int y, int w, int h, uint32_t color)
 
void border_create_sdl_color (SDL_Surface *surface, SDL_Rect *coords, int thickness, SDL_Color *color)
 
void border_create_color (SDL_Surface *surface, SDL_Rect *coords, int thickness, const char *color_notation)
 
void border_create_texture (SDL_Surface *surface, SDL_Rect *coords, int thickness, SDL_Surface *texture)
 
void rectangle_create (SDL_Surface *surface, int x, int y, int w, int h, const char *color_notation)
 
void surface_set_alpha (SDL_Surface *surface, uint8_t alpha)
 
int polygon_check_coords (double x, double y, double corners_x[], double corners_y[], int corners_num)
 

Variables

SDL_Surface * FormatHolder
 
static int dark_alpha [DARK_LEVELS]
 
static sprite_cache_tsprites_cache = NULL
 

Detailed Description

Sprite related functions.

Definition in file sprite.c.

Macro Definition Documentation

#define FREE_TMP_SURFACE ( )
Value:
do { \
if (tmp != NULL) { \
SDL_FreeSurface(tmp); \
} \
tmp = surface; \
} while (0)

Typedef Documentation

typedef struct sprite_cache sprite_cache_t

Structure used to cache sprite surfaces that have had special effects rendered on them.

Function Documentation

void border_create ( SDL_Surface *  surface,
int  x,
int  y,
int  w,
int  h,
int  color,
int  size 
)

Create a border around the specified coordinates.

Parameters
surfaceSurface to use.
xX start of the border.
yY start of the border.
wMaximum border width.
hMaximum border height.
colorColor to use for the border.
sizeBorder's size.

Definition at line 1226 of file sprite.c.

void border_create_color ( SDL_Surface *  surface,
SDL_Rect *  coords,
int  thickness,
const char *  color_notation 
)

Render a border of the specified color and thickness.

Parameters
surfaceSurface to render on.
coordsCoordinates to render at.
thicknessBorder thickness.
color_notationBorder color, eg, "ff0000".

Definition at line 1359 of file sprite.c.

void border_create_line ( SDL_Surface *  surface,
int  x,
int  y,
int  w,
int  h,
uint32_t  color 
)

Render a line (essentially a rectangle) of the specified width/height and color.

Parameters
surfaceSurface to render on.
xStarting X coordinate.
yStarting Y coordinate.
wWidth of the line.
hHeight of the line.
colorColor of the line.

Definition at line 1277 of file sprite.c.

void border_create_sdl_color ( SDL_Surface *  surface,
SDL_Rect *  coords,
int  thickness,
SDL_Color *  color 
)

Render a border of the specified SDL color and thickness.

Parameters
surfaceSurface to render on.
coordsCoordinates to render at.
thicknessBorder thickness.
colorBorder color.

Definition at line 1306 of file sprite.c.

void border_create_texture ( SDL_Surface *  surface,
SDL_Rect *  coords,
int  thickness,
SDL_Surface *  texture 
)

Render a border using the specified texture.

The texture should be tile-able.

Parameters
surfaceSurface to render on.
coordsCoordinates to render at.
thicknessBorder thickness.
textureBorder texture.

Definition at line 1388 of file sprite.c.

void draw_frame ( SDL_Surface *  surface,
int  x,
int  y,
int  w,
int  h 
)

Draw a border frame.

Parameters
surfaceSurface to draw on.
xX position.
yY position.
wWidth of the frame.
hHeight of the frame.

Definition at line 1185 of file sprite.c.

Uint32 getpixel ( SDL_Surface *  surface,
int  x,
int  y 
)

Get pixel value from an SDL surface at the specified X/Y position.

Parameters
surfaceSDL surface to get the pixel from.
xX position.
yY position.
Returns
The pixel.

Definition at line 910 of file sprite.c.

int polygon_check_coords ( double  x,
double  y,
double  corners_x[],
double  corners_y[],
int  corners_num 
)

Checks whether the given coordinates are within the specified polygon.

The arrays corners_x/corners_y should contain every single corner point of the polygon that you want to test.

Parameters
xX coordinate.
yY coordinate.
corners_xArray of X corner coordinates.
corners_yArray of Y corner coordinates.
corners_numNumber of corner coordinate entries.
Returns
1 if the coordinates are in the polygon, 0 otherwise.

Definition at line 1523 of file sprite.c.

void putpixel ( SDL_Surface *  surface,
int  x,
int  y,
Uint32  pixel 
)

Put a pixel value to the specified X/Y position on an SDL surface.

Parameters
surfaceThe surface.
xX position.
yY position.
pixelPixel to put.

Definition at line 950 of file sprite.c.

void rectangle_create ( SDL_Surface *  surface,
int  x,
int  y,
int  w,
int  h,
const char *  color_notation 
)

Create a rectangle of the specified size and color.

Parameters
surfaceSurface to render on.
xX coordinate to render at.
yY coordinate to render at.
wWidth of the rectangle.
hHeight of the rectangle.
color_notationColor of the rectangle, eg, "ff0000".

Definition at line 1443 of file sprite.c.

static void sprite_cache_add ( sprite_cache_t cache)
static

Add a sprite cache entry to the sprite cache.

Parameters
cacheCache entry to add.

Definition at line 226 of file sprite.c.

static sprite_cache_t* sprite_cache_create ( const char *  name)
static

Create a new sprite cache entry.

Parameters
nameName of the cache entry.
Returns
Created sprite entry.

Definition at line 209 of file sprite.c.

static sprite_cache_t* sprite_cache_find ( const char *  name)
static

Find a sprite in the sprite cache.

Parameters
nameName of the sprite to find.
Returns
Sprite if found, NULL otherwise.

Definition at line 186 of file sprite.c.

static void sprite_cache_free ( sprite_cache_t cache)
static

Free the specified sprite cache entry.

Parameters
cache

Definition at line 250 of file sprite.c.

void sprite_cache_free_all ( void  )

Free all the sprite cache entries.

Definition at line 262 of file sprite.c.

void sprite_cache_gc ( void  )

Free unused sprite cache entries.

Definition at line 274 of file sprite.c.

static void sprite_cache_remove ( sprite_cache_t cache)
static

Remove a sprite entry from the sprite cache.

Parameters
cacheCache entry to remove.

Definition at line 239 of file sprite.c.

static SDL_Surface* sprite_effect_fow ( SDL_Surface *  surface)
static

Creates somewhat gray version of the specified sprite surface.

Used for the fog of war effect.

Parameters
surfaceSurface.
Returns
New surface.

Definition at line 381 of file sprite.c.

static SDL_Surface* sprite_effect_glow ( SDL_Surface *  surface,
const SDL_Color *  color,
double  speed,
double  state 
)
static

Creates a glow effect for the specified sprite surface.

Parameters
surfaceSurface.
colorGlow color.
speedAnimation speed of the glow.
stateCurrent animation state of the glow.
Returns
New surface.

Definition at line 421 of file sprite.c.

static SDL_Surface* sprite_effect_gray ( SDL_Surface *  surface)
static

Creates a gray version of the specified sprite surface.

Used for the invisible effect.

Parameters
surfaceSurface.
Returns
New surface.

Definition at line 347 of file sprite.c.

static SDL_Surface* sprite_effect_red ( SDL_Surface *  surface)
static

Creates a red version of the specified sprite surface.

Used for the infravision effect.

Parameters
surfaceSurface.
Returns
New surface.

Definition at line 312 of file sprite.c.

static SDL_Surface* sprite_effects_create ( SDL_Surface *  surface,
const sprite_effects_t effects 
)
static

Create a new sprite surface based on 'surface', applying the specified 'effects'.

Parameters
surfaceSurface to use as the base.
effectsEffects to apply.
Returns
New surface, NULL on failure.

Definition at line 609 of file sprite.c.

void sprite_free_sprite ( sprite_struct sprite)

Free a sprite.

Parameters
spriteSprite to free.

Definition at line 164 of file sprite.c.

void sprite_init_system ( void  )

Initialize the sprite system.

Definition at line 62 of file sprite.c.

sprite_struct* sprite_load_file ( char *  fname,
uint32_t  flags 
)

Load sprite file.

Parameters
fnameSprite filename.
flagsFlags for the sprite.
Returns
NULL if failed, the sprite otherwise.

Definition at line 86 of file sprite.c.

sprite_struct* sprite_tryload_file ( char *  fname,
uint32_t  flag,
SDL_RWops *  rwop 
)

Try to load a sprite image file.

Parameters
fnameSprite filename
flagFlags
rwopPointer to memory for the image
Returns
The sprite if success, NULL otherwise

Definition at line 110 of file sprite.c.

static bool surface_border_get_bottom ( SDL_Surface *  surface,
int *  pos,
uint32_t  color 
)
static

Calculate the bottom border in the surface - this is the position of the first pixel from the bottom that that does not match 'color'.

Parameters
surfaceSurface.
[out]posWhere to store the position.
colorColor to check for.
Returns
True if the border was found, false otherwise.

Definition at line 1082 of file sprite.c.

static bool surface_border_get_left ( SDL_Surface *  surface,
int *  pos,
uint32_t  color 
)
static

Calculate the left border in the surface - this is the position of the first pixel from the left that that does not match 'color'.

Parameters
surfaceSurface.
[out]posWhere to store the position.
colorColor to check for.
Returns
True if the border was found, false otherwise.

Definition at line 998 of file sprite.c.

static bool surface_border_get_right ( SDL_Surface *  surface,
int *  pos,
uint32_t  color 
)
static

Calculate the right border in the surface - this is the position of the first pixel from the right that that does not match 'color'.

Parameters
surfaceSurface.
[out]posWhere to store the position.
colorColor to check for.
Returns
True if the border was found, false otherwise.

Definition at line 1026 of file sprite.c.

static bool surface_border_get_top ( SDL_Surface *  surface,
int *  pos,
uint32_t  color 
)
static

Calculate the top border in the surface - this is the position of the first pixel from the top that that does not match 'color'.

Parameters
surfaceSurface.
[out]posWhere to store the position.
colorColor to check for.
Returns
True if the border was found, false otherwise.

Definition at line 1054 of file sprite.c.

int surface_borders_get ( SDL_Surface *  surface,
int *  top,
int *  bottom,
int *  left,
int *  right,
uint32_t  color 
)

Get borders from SDL_surface. The borders indicate the first pixel from the border's side that does not match 'color'.

Parameters
surfaceSurface to get borders from.
[out]topWhere to store the top border.
[out]bottomWhere to store the bottom border.
[out]leftWhere to store the left border.
[out]rightWhere to store the right border.
colorColor to check for.
Returns
1 if the borders were found, 0 otherwise (image is all filled with 'color' color).

Definition at line 1117 of file sprite.c.

void surface_pan ( SDL_Surface *  surface,
SDL_Rect *  box 
)

Pans the surface.

Parameters
surfaceSurface.
boxCoordinates.

Definition at line 1151 of file sprite.c.

void surface_set_alpha ( SDL_Surface *  surface,
uint8_t  alpha 
)

Changes alpha value of the specified surface.

If the surface is per-pixel alpha, changes every pixel on the surface to match the specified alpha value.

Parameters
surfaceSurface to change alpha value of.
alphaAlpha value to set.

Definition at line 1476 of file sprite.c.

void surface_show ( SDL_Surface *  surface,
int  x,
int  y,
SDL_Rect *  srcrect,
SDL_Surface *  src 
)

Render the specified surface.

Parameters
surfaceSurface on which to render.
xX rendering position.
yY rendering position.
srcrectLimit which parts of the source surface to render. Can be NULL.
srcSource surface to render.

Definition at line 761 of file sprite.c.

void surface_show_effects ( SDL_Surface *  surface,
int  x,
int  y,
SDL_Rect *  srcrect,
SDL_Surface *  src,
const sprite_effects_t effects 
)

Render a surface, applying the specified effects.

Parameters
surfaceSurface on which to render.
xX rendering position.
yY rendering position.
srcrectLimit which parts of the source surface to render. Can be NULL.
srcSource surface to render.
effectsEffects to apply.

Definition at line 830 of file sprite.c.

void surface_show_fill ( SDL_Surface *  surface,
int  x,
int  y,
SDL_Rect *  srcsize,
SDL_Surface *  src,
SDL_Rect *  box 
)

Render the specified surface until the specified 'box' is completely filled.

Used for rendering tile-able textures.

Parameters
surfaceSurface on which to render.
xX rendering position.
yY rendering position.
srcrectLimit which parts of the source surface to render. Can be NULL.
srcSource surface to render.
boxSpecifies maximum width and height to render.

Definition at line 792 of file sprite.c.

Variable Documentation

int dark_alpha[DARK_LEVELS]
static
Initial value:
= {
0, 44, 80, 117, 153, 190, 226
}

Darkness alpha values.

Definition at line 49 of file sprite.c.

SDL_Surface* FormatHolder

Format holder for red_scale(), fow_scale() and grey_scale() functions.

Definition at line 46 of file sprite.c.

sprite_cache_t* sprites_cache = NULL
static

The sprite cache hash table.

Definition at line 56 of file sprite.c.