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 5bacf68..3af47fb 100644 Binary files a/assets-cg/player_female.png and b/assets-cg/player_female.png differ diff --git a/assets-cg/player_male.png b/assets-cg/player_male.png index 830590a..101a987 100644 Binary files a/assets-cg/player_male.png and b/assets-cg/player_male.png differ diff --git a/assets-fx/player_female.png b/assets-fx/player_female.png index 786f9b9..299ce7a 100644 Binary files a/assets-fx/player_female.png and b/assets-fx/player_female.png differ diff --git a/assets-fx/player_male.png b/assets-fx/player_male.png index 7c1da00..98c74ae 100644 Binary files a/assets-fx/player_male.png and b/assets-fx/player_male.png differ diff --git a/src/animation.c b/src/animation.c new file mode 100644 index 0000000..ecff79d --- /dev/null +++ b/src/animation.c @@ -0,0 +1,31 @@ +#include + +#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) {