From a8294d354201b92afdee97f6b599f6d4fec50c75 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:47:53 +0200 Subject: [PATCH] Add animation support --- CMakeLists.txt | 3 ++- assets-cg/player_female.png | Bin 238 -> 380 bytes assets-cg/player_male.png | Bin 208 -> 324 bytes assets-fx/player_female.png | Bin 110 -> 134 bytes assets-fx/player_male.png | Bin 107 -> 126 bytes src/animation.c | 31 +++++++++++++++++++++++++++++++ src/animation.h | 27 +++++++++++++++++++++++++++ src/game.c | 4 ++++ src/game.h | 6 ++++++ src/main.c | 10 +++++++++- src/player.c | 12 ++++++++++-- 11 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/animation.c create mode 100644 src/animation.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e5745f6..e646b91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ set(SOURCES src/dialogs.c src/npc.c src/events.c + src/animation.c # ... ) # Shared assets, fx-9860G-only assets and fx-CG-50-only assets @@ -122,7 +123,7 @@ if("${FXSDK_PLATFORM_LONG}" STREQUAL fx9860G) elseif("${FXSDK_PLATFORM_LONG}" STREQUAL fxCG50) add_executable(myaddin ${SOURCES} ${ASSETS} ${ASSETS_${FXSDK_PLATFORM}} ${ASSETS_${FXSDK_PLATFORM}_${COLORMODE_cg}} ) elseif("${FXSDK_PLATFORM_LONG}" STREQUAL fx9860G_G3A) - add_executable(myaddin ${SOURCES} ${ASSETS} ${ASSETS_${FXSDK_PLATFORM}} ${ASSETS_${FXSDK_PLATFORM}_${COLORMODE_fx}} ) + add_executable(myaddin ${SOURCES} ${ASSETS} ${ASSETS_${FXSDK_PLATFORM}} ${ASSETS_${FXSDK_PLATFORM}_${COLORMODE_fx}} ) endif() target_link_options(myaddin PRIVATE -Wl,-Map=Build_Addin.map -Wl,--print-memory-usage) diff --git a/assets-cg/player_female.png b/assets-cg/player_female.png index 5bacf689da3398e3b34938830983d1a07df3eb3d..3af47fb125b9757fb1fa16162327e8b8224dda4d 100644 GIT binary patch literal 380 zcmV-?0fYXDP)x4ZxJpME|T-ZRgtzFU6buiOjg{(mn}*Na)rk=3;?4O38=TO^Pgy&1~23a z&q=KELmxL9@G2vz-py&5f7*cdn>v__peC1P>%=PG`G~#}li95U01ye>uZbMCiNh7X zY(QKGwr#_D3w>Lm0a-*ce7eH|a`$GonO6BHdI7~)6WgzWBD2b8J!wFHh;AIhas;mM znQg^un5D_bv4u!LV1?zuYi9?IeVX7ZACo(zTqY017n$p2jI~4<)+ObxP9&6VD|{== zz!!#1V6A9&FbwWE_~9Xr0j=uTMBn7MY!D(+_2FAlgeZ6V*Zf(48Nlz#2j3qT*6@Y`z?QOwb#ZTiC9rZZiEzVR9c)T>NOL(BCe2)L$ a|Nj7?F$tzIhux9@0000f5o`C zw+$&QlMT!je92@^?6E?8{|hfr3P`I{73%`ywIo=nq+}rLo9md1_kmRRp8L>4%caK zao*H#Yu;|`|F)g;$facpKNLF-Fdb23SeHKa4&TJsBG<~$v|^pB$6W;`b}Dss{I^~4 zh{5u`dGnEvSKm)zvt5vPP-3p|`K*;??JK(Pa2?xf9Z>)7fLZuTklv%|9oiMLlb$_b z+n{#g($cvOLhoJ%2a9cctfsqVPId2L1KGd4F;Pqm>Ny83(@RcIo&$6ugQu&X%Q~lo FCIA;xQXv2U diff --git a/assets-fx/player_female.png b/assets-fx/player_female.png index 786f9b971812e0cf734ab25169dea4f9b2b0c4f3..299ce7ad90c956959a9da0cbbbb2315453eaae0b 100644 GIT binary patch literal 134 zcmeAS@N?(olHy`uVBq!ia0vp^5bSA2f~qGgix*^Hk@H~l@Dxl4vMNkqezy9@5y$Nwi)FJdDi$uU$}ET}W(ab=di0!S`roOy jF7}@~G$+lE_W*aAqUvvhj`yd51~YiN`njxgN@xNAwlFXi literal 110 zcmeAS@N?(olHy`uVBq!ia0vp^93afW0wnX;%77$;si%u$2*=FOvw>U(40xC>q~G|v z{*9g2^-vc9U!GSAi(PfEehi)V%<4d~QIq4Qw~zKd`o8`H)68Rh?4?gx=Yve}boFyt I=akR{07rZzlK=n! diff --git a/assets-fx/player_male.png b/assets-fx/player_male.png index 7c1da00eb6988b570c732a4e62908d67d942a06f..98c74aef00ee1a19c861bb56ca8597a6db0a89e1 100644 GIT binary patch literal 126 zcmeAS@N?(olHy`uVBq!ia0vp^5bSA2f~q+C5+9780gw)Q)6F(`1br2qXt zf4lQU)roQ~g^3}IYTmN!W(jG>W!a-AE|TA_9&+u@?3XX=3Ih)HEmrZm_N@5F?w?_6 a=d+(@R%M7db7D8pKn71&KbLh*2~7aFJ}R>S literal 107 zcmeAS@N?(olHy`uVBq!ia0vp^93afW0wnX;%77$;k*AAe2*=FWGY2^t3WZsdx7C_t?e@=pi_AHXoj + +#include "animation.h" + +void animation_new(Animation *animation, bopti_image_t *image, + unsigned char len, unsigned short frame_ms) { + animation->image = image; + animation->frame = 0; + animation->len = len; + animation->frame_ms = frame_ms; + animation->current_ms = 0; + animation->width = image->width/len; + animation->height = image->height; + animation->wrap_dest = 0; +} + +void animation_draw(Animation *animation, int x, int y) { + dsubimage(x, y, animation->image, animation->frame*animation->width, 0, + animation->width, animation->height, DIMAGE_NONE); +} + +void animation_update(Animation *animation, unsigned short frame_ms) { + animation->current_ms += frame_ms; + while(animation->current_ms > animation->frame_ms){ + animation->frame++; + if(animation->frame >= animation->len){ + animation->frame = animation->wrap_dest; + } + animation->current_ms -= animation->frame_ms; + } +} diff --git a/src/animation.h b/src/animation.h new file mode 100644 index 0000000..62830f7 --- /dev/null +++ b/src/animation.h @@ -0,0 +1,27 @@ +#ifndef ANIMATION_H +#define ANIMATION_H + +#include "config.h" + +typedef struct { + unsigned char frame; + bopti_image_t *image; + unsigned char len; + unsigned short frame_ms; + int current_ms; + unsigned short width; + unsigned short height; + unsigned short wrap_dest; +} Animation; + +/* TODO: Doc! */ +void animation_new(Animation *animation, bopti_image_t *image, + unsigned char len, unsigned short frame_ms); + +/* TODO: Doc! */ +void animation_draw(Animation *animation, int x, int y); + +/* TODO: Doc! */ +void animation_update(Animation *animation, unsigned short frame_ms); + +#endif diff --git a/src/game.c b/src/game.c index 744223d..df3ab8a 100644 --- a/src/game.c +++ b/src/game.c @@ -211,3 +211,7 @@ void game_get_inputs(Game *game) { #endif // USB_FEATURE } + +void game_update_animations(Game *game, unsigned char ms) { + animation_update(&game->player.animation, ms); +} diff --git a/src/game.h b/src/game.h index 7a20c1c..e52e768 100644 --- a/src/game.h +++ b/src/game.h @@ -5,6 +5,7 @@ #include #include +#include "animation.h" /* The direction where the player is going to. */ typedef enum { D_UP, D_DOWN, D_LEFT, D_RIGHT } Direction; @@ -34,6 +35,8 @@ typedef struct { /* the player is interacting with a NPC */ bool isInteractingWithNPC; bool is_male; + + Animation animation; } Player; typedef struct { @@ -200,4 +203,7 @@ void game_render_indicator(Game *game); */ void game_get_inputs(Game *game); +/* TODO: Doc! */ +void game_update_animations(Game *game, unsigned char ms); + #endif diff --git a/src/main.c b/src/main.c index b5e5dc4..3e3e959 100644 --- a/src/main.c +++ b/src/main.c @@ -28,13 +28,15 @@ #include extern bopti_image_t player_face_img; +extern bopti_image_t player_male_img; extern Map *worldRPG[]; /* Game data (defined in "game.h")*/ Game game = { NULL, - {12 * PXSIZE, 36 * PXSIZE, 0, 0, 100, SPEED, false, 0, false, false, true}, + {12 * PXSIZE, 36 * PXSIZE, 0, 0, 100, SPEED, false, 0, false, false, true, + {}}, {{}, {}, 0}, false, false, @@ -107,6 +109,9 @@ int main(void) { timer_start(timer); game.map_level = worldRPG[0]; + animation_new(&game.player.animation, &player_male_img, 3, 250); + /* Wrapping back to 1 to skip the idle frame. */ + game.player.animation.wrap_dest = 1; events_init_handler(&game.handler); events_bind_variable(&game.handler, (int *)&game.player.life, "life"); events_bind_variable(&game.handler, &game.mana, "mana"); @@ -189,6 +194,9 @@ int main(void) { /* Run the game at max. 50fps */ while(game.frame_duration < 20) sleep(); + /* Calling it here to get the real frame duration (there is a lot of lag + on cg) */ + game_update_animations(&game, (unsigned char)game.frame_duration); /* Reset frame_duration for the next frame */ game.frame_duration = 0; } while(!game.exittoOS); // want to exit ? diff --git a/src/player.c b/src/player.c index dd0f843..7da7998 100644 --- a/src/player.c +++ b/src/player.c @@ -7,6 +7,7 @@ #include "npc.h" #include +#include extern bopti_image_t player_male_img; extern bopti_image_t player_female_img; @@ -44,8 +45,15 @@ extern uint32_t nbNPC; void player_draw(Game *game) { Player *player = &game->player; - dimage(player->px - P_WIDTH / 2, player->py - P_HEIGHT / 2, - player->is_male ? &player_male_img : &player_female_img); + clearevents(); + if(!keydown(KEY_LEFT) && !keydown(KEY_RIGHT) && !keydown(KEY_UP) && + !keydown(KEY_DOWN)){ + game->player.animation.frame = 0; + } + player->animation.image = player->is_male ? &player_male_img : + &player_female_img; + animation_draw(&player->animation, player->px - P_WIDTH / 2, + player->py - P_HEIGHT / 2); } void player_move(Game *game, Direction direction) {