Adding Sprite Animation (untested {no sprite sheet}) [running]

This commit is contained in:
n0ffie 2025-02-07 00:03:37 +01:00
parent 23a40216de
commit b7ddc3f292
6 changed files with 160 additions and 33 deletions

View file

@ -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)

View file

@ -0,0 +1,38 @@
#ifndef ANIMATION_SPRITE_H
#define ANIMATION_SPRITE_H
#include <raylib.h>
#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

View file

@ -1,42 +1,47 @@
#ifndef RLYSON_HPP
#define RLYSON_HPP
#ifdef __cplusplus
extern "C" {
#endif
#include <raylib.h>
/*
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 <animation/sprite.h>
// other animation types
#endif
#ifdef __cplusplus
}
#endif
#endif // RLYSON_HPP

View file

@ -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();

View file

@ -0,0 +1,58 @@
#include <animation/sprite.h>
#include <stdlib.h>
#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
);
}

View file

@ -1,10 +1,15 @@
#include <rlyson.h>
#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);
}
origin.y += textSize.y / 2;
DrawCircleV(Vector2Add(origin, pos), 10, DARKGREEN);
SetTextLineSpacing(lineSpacing);
DrawTextPro(font, text, pos, origin, rotation, fontSize, spacing, tint);
}