A RPG I'm messing with made in Raylib.
1#include "../include/animated_texture.h"
2#include <stdlib.h>
3
4AnimatedTexture animated_texture_create(
5 const char *file_path, int number_of_frames, float speed
6) {
7 Texture2D texture = LoadTexture(file_path);
8
9 Rectangle current_frame = { 0, 0, texture.width / (float)number_of_frames, texture.height };
10
11 return (AnimatedTexture){
12 .number_of_frames = number_of_frames,
13 .current_frame = current_frame,
14 .duration_left = 1.0f,
15 .speed = speed,
16 .texture = texture
17 };
18}
19
20int animated_texture_delete(AnimatedTexture animated_texture) {
21 UnloadTexture(animated_texture.texture);
22
23 return EXIT_SUCCESS;
24}
25
26void animated_texture_update(AnimatedTexture *animated_texture) {
27 float delta_time = GetFrameTime();
28 animated_texture->duration_left -= delta_time * animated_texture->speed;
29
30 if (animated_texture->duration_left > 0.0)
31 return;
32
33 animated_texture->duration_left = 1.0f;
34
35 Rectangle next_frame = {
36 ((int)animated_texture->current_frame.x
37 + (int)animated_texture->current_frame.width)
38 % animated_texture->texture.width,
39 0, animated_texture->current_frame.width,
40 animated_texture->current_frame.height
41 };
42
43 animated_texture->current_frame = next_frame;
44}
45
46void animated_texture_draw(
47 AnimatedTexture *animated_texture, Vector2 position, Color tint
48) {
49 Rectangle dest = { position.x, position.y, animated_texture->current_frame.width, animated_texture->current_frame.height };
50
51 DrawTexturePro(
52 animated_texture->texture, animated_texture->current_frame, dest, (Vector2){ 0, 0 }, 0.0f, tint
53 );
54}