Loading...
Searching...
No Matches
sprite.c
Go to the documentation of this file.
#include "arc/graphics/sprite.h"
#include "sprite.h"
#include "spritesheet.h"
#include "renderer.h"
#include "arc/math/point.h"
#include "arc/math/rectangle.h"
#include "arc/std/errno.h"
#include <stdlib.h>
void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Array *frames){
*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;
}
void ARC_Sprite_Destroy(ARC_Sprite *sprite){
free(sprite);
}
void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite){
*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;
}
void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, uint8_t opacity){
sprite->opacity = opacity;
}
void ARC_Sprite_Render(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds){
//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);
}
void ARC_Sprite_RenderFlip(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, enum ARC_Sprite_Axis axis){
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);
}
void ARC_Sprite_RenderRotated(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, ARC_Point *center, double angle){
//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);
}
void ARC_Sprite_SetFrameIndex(ARC_Sprite *sprite, uint32_t index){
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;
}
void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){
++*sprite->frameIndex;
if(*sprite->frameIndex == sprite->frames->size){
*sprite->frameIndex = 0;
}
}
uint32_t ARC_Sprite_GetFrameIndex(ARC_Sprite *sprite){
return *sprite->frameIndex;
}
ARC_Rect *ARC_Sprite_GetBounds(ARC_Sprite *sprite){
return (ARC_Rect *)sprite->frames->data + *sprite->frameIndex;
}
ARC_Array *ARC_Sprite_GetAllBounds(ARC_Sprite *sprite){
return sprite->frames;
}
errno.h
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
ARC_Renderer
struct ARC_RendererType ARC_Renderer
Definition renderer.h:16
sprite.h
ARC_Sprite_Render
void ARC_Sprite_Render(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds)
renders ARC_Sprite type
Definition sprite.c:36
ARC_Sprite_Create
void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Array *frames)
creates ARC_Sprite type
Definition sprite.c:11
ARC_Sprite_Destroy
void ARC_Sprite_Destroy(ARC_Sprite *sprite)
destroys ARC_Sprite type
Definition sprite.c:20
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
Definition sprite.c:57
ARC_Sprite_Axis
ARC_Sprite_Axis
Definition sprite.h:67
ARC_SPRITE_AXIS_Y
@ ARC_SPRITE_AXIS_Y
Definition sprite.h:69
ARC_SPRITE_AXIS_X
@ ARC_SPRITE_AXIS_X
Definition sprite.h:70
ARC_Sprite_GetBounds
ARC_Rect * ARC_Sprite_GetBounds(ARC_Sprite *sprite)
returns the current bounds based on the ARC_Sprite's frames
Definition sprite.c:84
ARC_Sprite_Copy
void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite)
copies ARC_Sprite to a new ARC_Sprite
Definition sprite.c:24
ARC_Sprite_IterateFrame
void ARC_Sprite_IterateFrame(ARC_Sprite *sprite)
switches ARC_Sprite's frames to next for animation
Definition sprite.c:72
rectangle.h
renderer.h
ARC_Sprite_GetFrameIndex
uint32_t ARC_Sprite_GetFrameIndex(ARC_Sprite *sprite)
gets ARC_Sprite's current frame
Definition sprite.c:80
ARC_Sprite_SetOpacity
void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, uint8_t opacity)
sets ARC_Sprite's opacity
Definition sprite.c:32
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
Definition sprite.c:42
ARC_Sprite_GetAllBounds
ARC_Array * ARC_Sprite_GetAllBounds(ARC_Sprite *sprite)
returns the array of bounds that a sprite has
Definition sprite.c:88
ARC_Sprite_SetFrameIndex
void ARC_Sprite_SetFrameIndex(ARC_Sprite *sprite, uint32_t index)
switches ARC_Sprite's frame to specified index
Definition sprite.c:63
sprite.h
spritesheet.h
point.h
ARC_Array
a type that holds an array of data and its size
Definition array.h:13
ARC_Array::size
uint32_t size
Definition array.h:14
ARC_Array::data
void * data
Definition array.h:15
ARC_Point
Definition point.h:10
ARC_Rect
Definition rectangle.h:12
ARC_Sprite
Definition sprite.h:7
ARC_Sprite::spritesheet
ARC_Spritesheet * spritesheet
Definition sprite.h:8
ARC_Sprite::opacity
uint8_t opacity
Definition sprite.h:12
ARC_Sprite::frameIndex
uint32_t * frameIndex
Definition sprite.h:10
ARC_Sprite::frames
ARC_Array * frames
Definition sprite.h:9
ARC_Spritesheet
Definition spritesheet.h:7
ARC_Spritesheet::texture
SDL_Texture * texture
Definition spritesheet.h:8