worked on rewrite of sprite

This commit is contained in:
herbglitch 2025-03-23 00:45:39 -06:00
parent 1b2e2cb7f1
commit 0918b6f225
4 changed files with 125 additions and 113 deletions

View file

@ -3,8 +3,8 @@
#include <SDL3_image/SDL_image.h>
#include <stdio.h>
#include "renderer.h"
#include "sprite.h"
#include "spritesheet.h"
#include "arc/graphics/sprite.h"
#include "arc/graphics/spritesheet.h"
#include "arc/std/string.h"
#include "arc/std/parser/helpers.h"
#include "arc/std/errno.h"

View file

@ -1,20 +1,26 @@
#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>
#include <SDL3/SDL.h>
void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Array *frames){
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;
(*sprite)->frameIndex = 0;
(*sprite)->angle = 0.0;
(*sprite)->origin = (ARC_FPoint){ 0.0, 0.0 };
//called this way to also set the alpha mod
ARC_Sprite_SetOpacity(*sprite, 1.0f);
(*sprite)->axis = ARC_SPRITE_AXIS_NONE;
}
void ARC_Sprite_Destroy(ARC_Sprite *sprite){
@ -24,73 +30,78 @@ void ARC_Sprite_Destroy(ARC_Sprite *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;
(*newSprite)->frames = oldSprite->frames;
(*newSprite)->frameIndex = oldSprite->frameIndex;
(*newSprite)->angle = oldSprite->angle;
(*newSprite)->origin = oldSprite->origin;
(*newSprite)->opacity = oldSprite->opacity;
}
void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, uint8_t opacity){
sprite->opacity = opacity;
void ARC_Sprite_Render(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_FRect renderBounds){
SDL_RenderTexture((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_FRect *)(sprite->frames.data + sprite->frameIndex), (SDL_FRect *)&renderBounds);
}
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);
ARC_FRect sourceRect = ARC_Rect_CastToFRect(*(ARC_Rect *)(sprite->frames->data + *sprite->frameIndex));
ARC_FRect destinationRect = ARC_Rect_CastToFRect(*renderBounds);
SDL_RenderTexture((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_FRect *)&sourceRect, (SDL_FRect *)&destinationRect);
}
void ARC_Sprite_RenderAt(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_FPoint point, double scale){
ARC_FRect sourceRect = *(ARC_FRect *)(sprite->frames.data + sprite->frameIndex);
//TODO: check that this works
ARC_FRect destinationRect = {
(point.x - sprite->origin.x) * scale,
(point.y - sprite->origin.y) * scale,
sourceRect.w * scale,
sourceRect.h * scale
};
void ARC_Sprite_RenderFlip(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, enum ARC_Sprite_Axis axis){
//TODO: probably want to optomize this
SDL_FlipMode flip = SDL_FLIP_NONE;
if(axis & ARC_SPRITE_AXIS_X){
if(sprite->axis & ARC_SPRITE_AXIS_X){
flip |= SDL_FLIP_HORIZONTAL;
}
if(axis & ARC_SPRITE_AXIS_Y){
if(sprite->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);
ARC_FRect sourceRect = ARC_Rect_CastToFRect(*(ARC_Rect *)(sprite->frames->data + *sprite->frameIndex));
ARC_FRect destinationRect = ARC_Rect_CastToFRect(*renderBounds);
SDL_RenderTextureRotated((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_FRect *)&sourceRect, (SDL_FRect *)&destinationRect, 0.0, NULL, flip);
SDL_RenderTextureRotated((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_FRect *)&sourceRect, (SDL_FRect *)&destinationRect, sprite->angle, (SDL_FPoint *)&(sprite->origin), 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);
ARC_FRect sourceRect = ARC_Rect_CastToFRect(*(ARC_Rect *)(sprite->frames->data + *sprite->frameIndex));
ARC_FRect destinationRect = ARC_Rect_CastToFRect(*renderBounds);
SDL_RenderTextureRotated((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_FRect *)&sourceRect, (SDL_FRect *)&destinationRect, angle, NULL, SDL_FLIP_NONE);
void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){
sprite->frameIndex++;
if(sprite->frameIndex == sprite->frames.size){
sprite->frameIndex = 0;
}
}
void ARC_Sprite_SetFrameIndex(ARC_Sprite *sprite, uint32_t index){
if(sprite->frames->size <= 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;
sprite->frameIndex = index;
}
void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){
++*sprite->frameIndex;
void ARC_Sprite_SetAngle(ARC_Sprite *sprite, double angle){
sprite->angle = angle;
}
if(*sprite->frameIndex == sprite->frames->size){
*sprite->frameIndex = 0;
}
void ARC_Sprite_SetOrigin(ARC_Sprite *sprite, ARC_FPoint origin){
sprite->origin = origin;
}
void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, float opacity){
sprite->opacity = opacity;
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
}
uint32_t ARC_Sprite_GetFrameIndex(ARC_Sprite *sprite){
return *sprite->frameIndex;
return sprite->frameIndex;
}
ARC_Rect *ARC_Sprite_GetBounds(ARC_Sprite *sprite){
return (ARC_Rect *)sprite->frames->data + *sprite->frameIndex;
ARC_FRect *ARC_Sprite_GetBounds(ARC_Sprite *sprite){
return (ARC_FRect *)sprite->frames.data + sprite->frameIndex;
}
ARC_Array *ARC_Sprite_GetAllBounds(ARC_Sprite *sprite){
ARC_Array ARC_Sprite_GetAllBounds(ARC_Sprite *sprite){
return sprite->frames;
}

View file

@ -1,14 +0,0 @@
#ifndef ARC_SDL_SPRITE_H_
#define ARC_SDL_SPRITE_H_
#include "arc/graphics/sprite.h"
struct ARC_Sprite {
ARC_Spritesheet *spritesheet;
ARC_Array *frames;
uint32_t *frameIndex;
//TODO: temp
uint8_t opacity;
};
#endif // !ARC_SDL_SPRITE_H_