From 366bdf7d207361c38762175e3f3cdf9a53735878 Mon Sep 17 00:00:00 2001 From: Rendo Date: Wed, 11 Mar 2026 19:07:58 +0500 Subject: [PATCH] Animation and fps --- assets/sprites/pacman.png | Bin 461 -> 760 bytes src/components.cpp | 15 ++++++++++++++- src/components.h | 8 +++++++- src/main.cpp | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/assets/sprites/pacman.png b/assets/sprites/pacman.png index 63e743875caeadeae390af711de547d014acc72b..25dfc887aaf3c15a07a9459102c5c03b95695117 100644 GIT binary patch literal 760 zcmVPx%vPnciRCt{2n?aIfsnXH1%<7U+r)<0BG9N=4*D)2_Qaz+JW=V_tYIgblH3Tee{No zZ#s}5Sm4XQM{V`4cl8cL-eI*sXfJ?Jx!Oh?-*n6dj3dy9e<|ez+}t@MJ@K4Rk%jwz zReF|bk0XT&^aF|)VU&sI_F~*>YV>KDOCD@5Ufgl zBg(;-8nI=i|IkKW`!E~*_=jr{XZ>r-R0_?Xl)1kM)9}Ai-Oi4#iqMz&s4Z(e=lxUc z!70?*vf3Muiv2%yxjL*}N%!*g8a#m99pPx#1am@3R0s$N2z&@+hyVZqcS%G+RA_$m+jnoV+Xtp8)jeuYolF)e=~f zTMXMjYi|(g0I1E&0+3SA>aSh`%M7$Y4BG<~h+#A&)v@&>XD&445K04CLENH*IoNp) zBT0~m<5(2n*x`txYqbYji6TIp0HmX&`f0%BeO*}~9YFrw6q2;HrJ52VYO`Ve*aW>Q zNhSYUm=NU&TZ0H!LROeYmSmXSau!SlV0b-n^?&Bgr~qzu2#)J^NGzlIRL;1wlNmuuZ)V{00000NkvXXu0mjf Dietoq diff --git a/src/components.cpp b/src/components.cpp index 84addd9..c9d40d7 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -7,7 +7,7 @@ Rectangle Pacman::getTextureRect() const{ Rectangle result; - result.x = 0; + result.x = 16*frame; result.y = facing*16; result.height = 16; result.width = 16; @@ -17,6 +17,7 @@ Pacman::Pacman() { this->texture = LoadTexture("assets/sprites/pacman.png"); this->facing = 0; this->position = {0.,0.}; + this->frame = 0; } Pacman::Pacman(Vector2 position) { this->texture = LoadTexture("assets/sprites/pacman.png"); @@ -53,6 +54,18 @@ void Pacman::tick() { } } +void Pacman::process() { + this->time += GetFrameTime(); + if (this->time >= 1.0/this->fps){ + this->time = 0.; + this->animation_tick(); + } +} + +void Pacman::animation_tick() { + this->frame = (this->frame+1)%frame_count; +} + void Pacman::draw() const { // Drawing DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); diff --git a/src/components.h b/src/components.h index 2b31aca..2db18e3 100644 --- a/src/components.h +++ b/src/components.h @@ -19,16 +19,22 @@ class Entity{ class Pacman : public Entity{ const int speed = 16; + const int frame_count = 7; + const int fps = 24; private: Texture2D texture; int facing; - Rectangle getTextureRect() const; public: + unsigned char frame; + float time; + Pacman(); Pacman(Vector2 position); ~Pacman(); void tick() override; + void process() override; + void animation_tick(); void draw() const override; }; diff --git a/src/main.cpp b/src/main.cpp index fb2b3f9..ba4d5b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ int main() { SetTargetFPS(60); - world = create_world_with(.25); + world = create_world_with(1./10.); world.entities.push_back(new Pacman({SCREEN_WIDTH/2,SCREEN_HEIGHT/2}));