Add animation support

This commit is contained in:
mibi88 2024-08-01 16:47:53 +02:00
parent 0f887ea419
commit a8294d3542
11 changed files with 89 additions and 4 deletions

View file

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

After

Width:  |  Height:  |  Size: 380 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 208 B

After

Width:  |  Height:  |  Size: 324 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 B

After

Width:  |  Height:  |  Size: 134 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 107 B

After

Width:  |  Height:  |  Size: 126 B

31
src/animation.c Normal file
View file

@ -0,0 +1,31 @@
#include <gint/display.h>
#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;
}
}

27
src/animation.h Normal file
View file

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

View file

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

View file

@ -5,6 +5,7 @@
#include <gint/display.h>
#include <stdint.h>
#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

View file

@ -28,13 +28,15 @@
#include <stdlib.h>
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 ?

View file

@ -7,6 +7,7 @@
#include "npc.h"
#include <gint/display.h>
#include <gint/keyboard.h>
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) {