From b7ddc3f2924631e871037db0fa436a11768cddfe Mon Sep 17 00:00:00 2001 From: n0ffie Date: Fri, 7 Feb 2025 00:03:37 +0100 Subject: [PATCH] Adding Sprite Animation (untested {no sprite sheet}) [running] --- alyson/CMakeLists.txt | 1 + alyson/includes/animation/sprite.h | 38 ++++++++++++++++++++ alyson/includes/rlyson.h | 57 +++++++++++++++-------------- alyson/src/alyson.cpp | 9 ++++- alyson/src/animation/sprite.c | 58 ++++++++++++++++++++++++++++++ alyson/src/rlyson.c | 30 ++++++++++++---- 6 files changed, 160 insertions(+), 33 deletions(-) create mode 100644 alyson/includes/animation/sprite.h create mode 100644 alyson/src/animation/sprite.c diff --git a/alyson/CMakeLists.txt b/alyson/CMakeLists.txt index 63affd7..a41b2c1 100644 --- a/alyson/CMakeLists.txt +++ b/alyson/CMakeLists.txt @@ -21,6 +21,7 @@ target_compile_definitions(alyson PRIVATE ASSETS_PATH="${ASSETS_PATH}") # Add alyson files find_files(alyson_src src cpp hpp cxx hxx c h) + target_sources(alyson PRIVATE ${alyson_src}) # set(ALYSON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/alyson/include) diff --git a/alyson/includes/animation/sprite.h b/alyson/includes/animation/sprite.h new file mode 100644 index 0000000..63e1629 --- /dev/null +++ b/alyson/includes/animation/sprite.h @@ -0,0 +1,38 @@ +#ifndef ANIMATION_SPRITE_H +#define ANIMATION_SPRITE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + Texture2D texture; + Vector2 dimentions; + + Rectangle sourceRect; + Vector2 size; + float rotation; + float scale; + + int frameCount; + int currentFrame; + + float speed; + float time; +} sprite_animation_t, *SpriteAnimation; + +SpriteAnimation CreateSpriteAnimation(Texture2D texture, Vector2 sprite_texture_dimentions, Vector2 sprite_size, float rotation, float scale, int frameCount); + +void UpdateSpriteAnimation(SpriteAnimation spriteAnimation, float dt); + +void DrawSpriteAnimation(SpriteAnimation spriteAnimation, Vector2 position); + +void DestroySpriteAnimation(SpriteAnimation spriteAnimation); + +#ifdef __cplusplus +} +#endif + +#endif // ANIMATION_SPRITE_H \ No newline at end of file diff --git a/alyson/includes/rlyson.h b/alyson/includes/rlyson.h index edcb2c7..8056210 100644 --- a/alyson/includes/rlyson.h +++ b/alyson/includes/rlyson.h @@ -1,42 +1,47 @@ #ifndef RLYSON_HPP #define RLYSON_HPP +#ifdef __cplusplus +extern "C" { +#endif + + #include -/* - the following functions are not part of the raylib API, - but are used by alyson and can be used by other projects using alyson +/* + the following functions are not part of the raylib API, + but are used by alyson and can be used by other projects using alyson */ typedef enum { - TEXT_ORIENTATION_LEFT, - TEXT_ORIENTATION_RIGHT, - TEXT_ORIENTATION_CENTER + TEXT_ORIENTATION_LEFT, + TEXT_ORIENTATION_RIGHT, + TEXT_ORIENTATION_CENTER } text_orientation_t; -/* - * Draws a text with all possible parameters - * @function DrawTextFull - * @param font Font to use - * @param text Text to draw - * @param origin Position to draw the text from - * @param orientation Text orientation - * @param fontSize Font size - * @param spacing Spacing between letters - * @param lineSpacing Line spacing - * @param tint Tint of the text - */ void DrawTextFull( - Font font, - const char *text, - Vector2 origin, - text_orientation_t orientation, - float fontSize, - float spacing, - float lineSpacing, - Color tint + Font font, + const char* text, + Vector2 origin, + text_orientation_t orientation, + float rotation, + float fontSize, + float spacing, + float lineSpacing, + Color tint ); +#ifndef RLAYSON_NO_ANIMATION +#include +// other animation types +#endif + + + +#ifdef __cplusplus +} +#endif + #endif // RLYSON_HPP \ No newline at end of file diff --git a/alyson/src/alyson.cpp b/alyson/src/alyson.cpp index 484b854..2e96760 100644 --- a/alyson/src/alyson.cpp +++ b/alyson/src/alyson.cpp @@ -31,7 +31,14 @@ int main(int argc, char *argv[]) { BeginDrawing(); DrawTexture(logo, GetScreenWidth() / 2 - logo.width / 2, GetScreenHeight() / 2 - logo.height / 2, WHITE); - DrawTextFull(GetFontDefault(), WINDOW_TITLE, { GetScreenWidth() / 2.f, GetScreenHeight() - 60.f }, TEXT_ORIENTATION_CENTER, 40.f, 0.f, 0.f, WHITE); + DrawTextFull( + GetFontDefault(), + WINDOW_TITLE, + { GetScreenWidth() / 2.f, GetScreenHeight() - 60.f }, + TEXT_ORIENTATION_CENTER, 0.f, + 40.f, 5.f, + 0.f, BLACK + ); ClearBackground(RAYWHITE); EndDrawing(); diff --git a/alyson/src/animation/sprite.c b/alyson/src/animation/sprite.c new file mode 100644 index 0000000..8dddabd --- /dev/null +++ b/alyson/src/animation/sprite.c @@ -0,0 +1,58 @@ +#include +#include + +#include "raylib.h" +#include "raymath.h" + +SpriteAnimation CreateSpriteAnimation(Texture2D texture, Vector2 sprite_texture_dimentions, Vector2 sprite_size, float rotation, float scale, int frameCount) { + SpriteAnimation spriteAnimation = malloc(sizeof(sprite_animation_t)); + spriteAnimation->texture = texture; + spriteAnimation->dimentions = sprite_texture_dimentions; + spriteAnimation->sourceRect = (Rectangle){0, 0, sprite_size.x, sprite_size.y}; + spriteAnimation->size = sprite_size; + spriteAnimation->rotation = rotation; + spriteAnimation->scale = scale; + spriteAnimation->frameCount = frameCount; + spriteAnimation->currentFrame = 0; + spriteAnimation->time = 0.f; + spriteAnimation->speed = 1.f; + + return spriteAnimation; +} + +void UpdateSpriteAnimation(SpriteAnimation self, float dt) { + self->time += dt; + if (self->time >= self->speed) { + self->time = 0.f; + self->currentFrame++; + } + + if(self->currentFrame >= self->frameCount) { + self->currentFrame = 0; + } + + self->sourceRect.x = (self->currentFrame * (int)self->size.x) % (int)self->dimentions.x; + self->sourceRect.y = (self->currentFrame * (int)self->size.y) % (int)self->dimentions.y; +} + +void DrawSpriteAnimation(SpriteAnimation spriteAnimation, Vector2 position) { + Vector2 drawpos = Vector2Add(position, Vector2Scale(spriteAnimation->size, 2.f)); + DrawTexturePro( + spriteAnimation->texture, + (Rectangle){ + spriteAnimation->sourceRect.x, + spriteAnimation->sourceRect.y, + spriteAnimation->sourceRect.width, + spriteAnimation->sourceRect.height + }, + (Rectangle){ + position.x, + position.y, + spriteAnimation->size.x * spriteAnimation->scale, + spriteAnimation->size.y * spriteAnimation->scale + }, + position, + spriteAnimation->rotation, + WHITE + ); +} \ No newline at end of file diff --git a/alyson/src/rlyson.c b/alyson/src/rlyson.c index 72843b6..c07b893 100644 --- a/alyson/src/rlyson.c +++ b/alyson/src/rlyson.c @@ -1,10 +1,15 @@ + #include +#include "raylib.h" +#include "raymath.h" + void DrawTextFull( Font font, const char *text, Vector2 origin, text_orientation_t orientation, + float rotation, float fontSize, float spacing, float lineSpacing, @@ -13,16 +18,29 @@ void DrawTextFull( { Vector2 textSize = MeasureTextEx(font, text, fontSize, spacing); + DrawCircleV(origin, 10, RED); + + Vector2 pos = origin; + origin = (Vector2){0,0}; + switch (orientation) { case TEXT_ORIENTATION_LEFT: - origin.x -= textSize.x; - break; - case TEXT_ORIENTATION_RIGHT: origin.x += textSize.x; break; + case TEXT_ORIENTATION_RIGHT: + break; case TEXT_ORIENTATION_CENTER: - origin.x -= textSize.x / 2; + origin.x += (textSize.x / 2); + break; + default: break; } - DrawTextPro(font, text, textSize, origin, 0, spacing, lineSpacing, tint); -} \ No newline at end of file + + origin.y += textSize.y / 2; + + DrawCircleV(Vector2Add(origin, pos), 10, DARKGREEN); + + SetTextLineSpacing(lineSpacing); + + DrawTextPro(font, text, pos, origin, rotation, fontSize, spacing, tint); +}