Loading...
Searching...
No Matches
sprite.h File Reference
`#include "arc/graphics/renderer.h"`
`#include "arc/graphics/spritesheet.h"`
`#include "arc/math/point.h"`
`#include "arc/math/rectangle.h"`
`#include "arc/std/array.h"`

Go to the source code of this file.

## Typedefs

typedef struct ARC_Sprite | ARC_Sprite
| a sprite type
typedef enum ARC_Sprite_Axis | ARC_Sprite_Axix

## Enumerations

enum | ARC_Sprite_Axis { ARC_SPRITE_AXIS_NONE = 0x00 , ARC_SPRITE_AXIS_Y = 0x01 , ARC_SPRITE_AXIS_X = 0x02 }

## Functions

void | ARC_Sprite_Create (ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Array *frames)
| creates ARC_Sprite type
void | ARC_Sprite_Destroy (ARC_Sprite *sprite)
| destroys ARC_Sprite type
void | ARC_Sprite_Copy (ARC_Sprite **newSprite, ARC_Sprite *oldSprite)
| copies ARC_Sprite to a new ARC_Sprite
void | ARC_Sprite_SetOpacity (ARC_Sprite *sprite, uint8_t opacity)
| sets ARC_Sprite's opacity
void | ARC_Sprite_Render (ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds)
| renders ARC_Sprite type
void | ARC_Sprite_RenderFlip (ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, enum ARC_Sprite_Axis axis)
| renders ARC_Sprite type with flip
void | ARC_Sprite_RenderRotated (ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, ARC_Point *center, double angle)
| renders ARC_Sprite type with rotation
void | ARC_Sprite_SetFrameIndex (ARC_Sprite *sprite, uint32_t index)
| switches ARC_Sprite's frame to specified index
void | ARC_Sprite_IterateFrame (ARC_Sprite *sprite)
| switches ARC_Sprite's frames to next for animation
uint32_t | ARC_Sprite_GetFrameIndex (ARC_Sprite *sprite)
| gets ARC_Sprite's current frame
ARC_Rect * | ARC_Sprite_GetBounds (ARC_Sprite *sprite)
| returns the current bounds based on the ARC_Sprite's frames
ARC_Array * | ARC_Sprite_GetAllBounds (ARC_Sprite *sprite)
| returns the array of bounds that a sprite has

## Typedef Documentation

## ARC_Sprite

typedef struct ARC_Sprite ARC_Sprite

a sprite type

Note
  the actual type should be define by overriding for a graphics api

Definition at line 19 of file sprite.h.

## ARC_Sprite_Axix

typedef enum ARC_Sprite_Axis ARC_Sprite_Axix

## Enumeration Type Documentation

## ARC_Sprite_Axis

enum ARC_Sprite_Axis

Enumerator
ARC_SPRITE_AXIS_NONE |
ARC_SPRITE_AXIS_Y |
ARC_SPRITE_AXIS_X |

Definition at line 67 of file sprite.h.

{
ARC_SPRITE_AXIS_NONE = 0x00,
ARC_SPRITE_AXIS_Y = 0x01,
ARC_SPRITE_AXIS_X = 0x02,
} ARC_Sprite_Axix;
ARC_SPRITE_AXIS_Y
@ ARC_SPRITE_AXIS_Y
Definition sprite.h:69
ARC_SPRITE_AXIS_NONE
@ ARC_SPRITE_AXIS_NONE
Definition sprite.h:68
ARC_SPRITE_AXIS_X
@ ARC_SPRITE_AXIS_X
Definition sprite.h:70
ARC_Sprite_Axix
enum ARC_Sprite_Axis ARC_Sprite_Axix

## Function Documentation

## ARC_Sprite_Copy()

void ARC_Sprite_Copy | ( | ARC_Sprite ** | newSprite,
|  | ARC_Sprite * | oldSprite )

copies ARC_Sprite to a new ARC_Sprite

Parameters

  newSprite | ARC_Sprite that is being copied to and created
  oldSprite | ARC_Sprite contents that are being copied

Definition at line 24 of file sprite.c.

{
*newSprite = (ARC_Sprite *)malloc(sizeof(ARC_Sprite));
(*newSprite)->spritesheet = oldSprite->spritesheet;
(*newSprite)->frames = oldSprite->frames;
(*newSprite)->frameIndex = (uint32_t *)malloc(sizeof(uint32_t));
*(*newSprite)->frameIndex = *oldSprite->frameIndex;
}
ARC_Sprite
Definition sprite.h:7
ARC_Sprite::spritesheet
ARC_Spritesheet * spritesheet
Definition sprite.h:8
ARC_Sprite::frameIndex
uint32_t * frameIndex
Definition sprite.h:10
ARC_Sprite::frames
ARC_Array * frames
Definition sprite.h:9

References ARC_Sprite::frameIndex, ARC_Sprite::frames, and ARC_Sprite::spritesheet.

## ARC_Sprite_Create()

void ARC_Sprite_Create | ( | ARC_Sprite ** | sprite,
|  | ARC_Spritesheet * | spritesheet,
|  | ARC_Array * | frames )

creates ARC_Sprite type

Parameters

  sprite | ARC_Sprite that is being created
  spritesheet | ARC_Spritesheet that ARC_Sprite will be pulled from
  frames | ARC_Array of bounds of sprite on spritesheet

Definition at line 11 of file sprite.c.

{
*sprite = (ARC_Sprite *)malloc(sizeof(ARC_Sprite));
(*sprite)->spritesheet = spritesheet;
(*sprite)->frames = frames;
(*sprite)->frameIndex = (uint32_t *)malloc(sizeof(uint32_t));
*(*sprite)->frameIndex = 0;
(*sprite)->opacity = 255;
}

References ARC_Sprite::spritesheet.

Referenced by ARC_Sprite_Read().

## ARC_Sprite_Destroy()

void ARC_Sprite_Destroy | ( | ARC_Sprite * | sprite | ) |

destroys ARC_Sprite type

Parameters

  sprite | ARC_Sprite to destroy

Definition at line 20 of file sprite.c.

{
free(sprite);
}

## ARC_Sprite_GetAllBounds()

ARC_Array * ARC_Sprite_GetAllBounds | ( | ARC_Sprite * | sprite | ) |

returns the array of bounds that a sprite has

Parameters

  sprite | ARC_Sprite to get all the bounds from

Definition at line 88 of file sprite.c.

{
return sprite->frames;
}

References ARC_Sprite::frames.

## ARC_Sprite_GetBounds()

ARC_Rect * ARC_Sprite_GetBounds | ( | ARC_Sprite * | sprite | ) |

returns the current bounds based on the ARC_Sprite's frames

Parameters

  sprite | ARC_Sprite to get bounds from

Definition at line 84 of file sprite.c.

{
return (ARC_Rect *)sprite->frames->data + *sprite->frameIndex;
}
ARC_Array::data
void * data
Definition array.h:15
ARC_Rect
Definition rectangle.h:12

References ARC_Array::data, ARC_Sprite::frameIndex, and ARC_Sprite::frames.

## ARC_Sprite_GetFrameIndex()

uint32_t ARC_Sprite_GetFrameIndex | ( | ARC_Sprite * | sprite | ) |

gets ARC_Sprite's current frame

Parameters

  sprite | ARC_Sprite to get frame from

Returns
  index ARC_Sprite's current frame index

Definition at line 80 of file sprite.c.

{
return *sprite->frameIndex;
}

References ARC_Sprite::frameIndex.

## ARC_Sprite_IterateFrame()

void ARC_Sprite_IterateFrame | ( | ARC_Sprite * | sprite | ) |

switches ARC_Sprite's frames to next for animation

Parameters

  sprite | ARC_Sprite that is having its frame updated

Definition at line 72 of file sprite.c.

{
++*sprite->frameIndex;
if(*sprite->frameIndex == sprite->frames->size){
*sprite->frameIndex = 0;
}
}
ARC_Array::size
uint32_t size
Definition array.h:14

References ARC_Sprite::frameIndex, ARC_Sprite::frames, and ARC_Array::size.

## ARC_Sprite_Render()

void ARC_Sprite_Render | ( | ARC_Sprite * | sprite,
|  | ARC_Renderer * | renderer,
|  | ARC_Rect * | renderBounds )

renders ARC_Sprite type

Parameters

  sprite | ARC_Sprite that will be rendered
  renderer | ARC_Renderer that is handling rendering
  renderBounds | area of renderer that ARC_Sprite will be rendered to

Definition at line 36 of file sprite.c.

{
//TODO: note, this is set here so not all entities in the sheet get opacity set
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
SDL_RenderCopy((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds);
}
ARC_Sprite::opacity
uint8_t opacity
Definition sprite.h:12
ARC_Spritesheet::texture
SDL_Texture * texture
Definition spritesheet.h:8

References ARC_Array::data, ARC_Sprite::frameIndex, ARC_Sprite::frames, ARC_Sprite::opacity, ARC_Sprite::spritesheet, and ARC_Spritesheet::texture.

## ARC_Sprite_RenderFlip()

void ARC_Sprite_RenderFlip | ( | ARC_Sprite * | sprite,
|  | ARC_Renderer * | renderer,
|  | ARC_Rect * | renderBounds,
|  | enum ARC_Sprite_Axis | axis )

renders ARC_Sprite type with flip

Parameters

  sprite | ARC_Sprite that will be rendered
  renderer | ARC_Renderer that is handling rendering
  renderBounds | area of renderer that ARC_Sprite will be rendered to
  axis | axis to flip sprite

Definition at line 42 of file sprite.c.

{
SDL_RendererFlip flip = SDL_FLIP_NONE;
if(axis & ARC_SPRITE_AXIS_X){
flip |= SDL_FLIP_HORIZONTAL;
}
if(axis & ARC_SPRITE_AXIS_Y){
flip |= SDL_FLIP_VERTICAL;
}
//TODO: note, this is set here so not all entities in the sheet get opacity set
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
SDL_RenderCopyEx((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds, 0.0, NULL, flip);
}

References ARC_SPRITE_AXIS_X, ARC_SPRITE_AXIS_Y, ARC_Array::data, ARC_Sprite::frameIndex, ARC_Sprite::frames, ARC_Sprite::opacity, ARC_Sprite::spritesheet, and ARC_Spritesheet::texture.

## ARC_Sprite_RenderRotated()

void ARC_Sprite_RenderRotated | ( | ARC_Sprite * | sprite,
|  | ARC_Renderer * | renderer,
|  | ARC_Rect * | renderBounds,
|  | ARC_Point * | center,
|  | double | angle )

renders ARC_Sprite type with rotation

Parameters

  sprite | ARC_Sprite that will be rendered
  renderer | ARC_Renderer that is handling rendering
  renderBounds | area of renderer that ARC_Sprite will be rendered to
  angle | angle to rotate ARC_Sprite
  center | point to rotate ARC_Sprite around

Definition at line 57 of file sprite.c.

{
//TODO: note, this is set here so not all entities in the sheet get opacity set
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
SDL_RenderCopyEx((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds, angle, (SDL_Point *)center, SDL_FLIP_NONE);
}

References ARC_Array::data, ARC_Sprite::frameIndex, ARC_Sprite::frames, ARC_Sprite::opacity, ARC_Sprite::spritesheet, and ARC_Spritesheet::texture.

## ARC_Sprite_SetFrameIndex()

void ARC_Sprite_SetFrameIndex | ( | ARC_Sprite * | sprite,
|  | uint32_t | index )

switches ARC_Sprite's frame to specified index

Parameters

  sprite | ARC_Sprite that is having its frame set
  index | uint32_t to set ARC_Sprite's frame index to

Definition at line 63 of file sprite.c.

{
if(sprite->frames->size <= index){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("in ARC_Sprite_SetFrameIndex(sprite, %d); index out of bounds", index);
return;
}
*sprite->frameIndex = index;
}
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES
#define ARC_DEBUG_LOG_ERROR_WITH_VARIABLES(STR,...)
Definition errno.h:40
ARC_ERRNO_DATA
#define ARC_ERRNO_DATA
Definition errno.h:7

References ARC_DEBUG_LOG_ERROR_WITH_VARIABLES, arc_errno, ARC_ERRNO_DATA, ARC_Sprite::frameIndex, ARC_Sprite::frames, and ARC_Array::size.

## ARC_Sprite_SetOpacity()

void ARC_Sprite_SetOpacity | ( | ARC_Sprite * | sprite,
|  | uint8_t | opacity )

sets ARC_Sprite's opacity

Parameters

  sprite | ARC_Sprite that is changing opacity
  opacity | new opacity for ARC_Sprite

Note
  this is temp because opacity probably should be a value bigger than 255

Definition at line 32 of file sprite.c.

{
sprite->opacity = opacity;
}

References ARC_Sprite::opacity.