From a86462fdb215d2f24844b188b8b87ef03c709fd4 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Thu, 1 Aug 2024 17:37:42 +0200 Subject: [PATCH 1/2] npc : Functional npc_stack --- src/game.c | 10 ++++------ src/game.h | 3 ++- src/npc.c | 10 +++++----- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/game.c b/src/game.c index 744223d..276764a 100644 --- a/src/game.c +++ b/src/game.c @@ -175,16 +175,14 @@ void game_get_inputs(Game *game) { if(keydown(KEY_F1)) { NPC *mynpc = npc_create(); if(mynpc) { - mynpc->curx = player->x; - mynpc->cury = player->y; - mynpc->x = 0; - mynpc->y = 0; + mynpc->curx = (player->x << PRECISION)/PXSIZE; + mynpc->cury = (player->y << PRECISION)/PXSIZE; + mynpc->x = player->x; + mynpc->y = player->x; mynpc->hasPath = 0; mynpc->face = 0; mynpc->paused = 0; mynpc->has_dialog = 0; - dprint(0, 50, 0, "succes"); - dupdate(); } } diff --git a/src/game.h b/src/game.h index 7a20c1c..ac815f2 100644 --- a/src/game.h +++ b/src/game.h @@ -70,7 +70,8 @@ typedef struct { } Sign; typedef struct { - /* current coordinates of the NPC */ + /* current coordinates of the NPC + * In 24:8 fixed point */ uint32_t curx; uint32_t cury; diff --git a/src/npc.c b/src/npc.c index 32dbc53..7adf076 100644 --- a/src/npc.c +++ b/src/npc.c @@ -60,9 +60,9 @@ int npc_append_path(uint16_t x, uint16_t y, NPC *npc) { npc->ypath = realloc(npc->ypath, npc->path_length * 2 + 2); if(npc->xpath == NULL || npc->ypath == NULL) return 1; + npc->xpath[npc->path_length] = x - npc->x; + npc->ypath[npc->path_length] = y - npc->y; npc->path_length++; - npc->xpath[npc->path_length - 1] = x - npc->x; - npc->ypath[npc->path_length - 1] = y - npc->y; return 0; } @@ -297,12 +297,12 @@ void npc_draw(Game *game) { Player *pl = &game->player; uint32_t u; - for(u = 0; u < game->map_level->nbNPC; u++) { - npc_draw_single(&game->map_level->npcs[u], pl); - } for(u = 0; u < npc_count; u++) { npc_draw_single(&npc_stack[u], pl); } + for(u = 0; u < game->map_level->nbNPC; u++) { + npc_draw_single(&game->map_level->npcs[u], pl); + } } void npc_reload(GUNUSED Game *game) { npc_count = 0; } From 5cc9216155878bbc808559c4650e6e1519a93301 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Thu, 1 Aug 2024 17:48:10 +0200 Subject: [PATCH 2/2] Fix warnings and clang-format --- src/animation.c | 12 ++++++------ src/game.c | 4 ++-- src/game.h | 2 +- src/main.c | 39 ++++++++++++++++++++++++--------------- src/map.c | 4 ++-- src/npc.c | 3 ++- src/player.c | 6 +++--- 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/animation.c b/src/animation.c index ecff79d..3644934 100644 --- a/src/animation.c +++ b/src/animation.c @@ -1,7 +1,7 @@ -#include - #include "animation.h" +#include + void animation_new(Animation *animation, bopti_image_t *image, unsigned char len, unsigned short frame_ms) { animation->image = image; @@ -9,21 +9,21 @@ void animation_new(Animation *animation, bopti_image_t *image, animation->len = len; animation->frame_ms = frame_ms; animation->current_ms = 0; - animation->width = image->width/len; + 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, + 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){ + while(animation->current_ms > animation->frame_ms) { animation->frame++; - if(animation->frame >= animation->len){ + if(animation->frame >= animation->len) { animation->frame = animation->wrap_dest; } animation->current_ms -= animation->frame_ms; diff --git a/src/game.c b/src/game.c index 2bc82a6..934c503 100644 --- a/src/game.c +++ b/src/game.c @@ -175,8 +175,8 @@ void game_get_inputs(Game *game) { if(keydown(KEY_F1)) { NPC *mynpc = npc_create(); if(mynpc) { - mynpc->curx = (player->x << PRECISION)/PXSIZE; - mynpc->cury = (player->y << PRECISION)/PXSIZE; + mynpc->curx = (player->x << PRECISION) / PXSIZE; + mynpc->cury = (player->y << PRECISION) / PXSIZE; mynpc->x = player->x; mynpc->y = player->x; mynpc->hasPath = 0; diff --git a/src/game.h b/src/game.h index f9e8b19..6adb1a4 100644 --- a/src/game.h +++ b/src/game.h @@ -1,11 +1,11 @@ #ifndef GAME_H #define GAME_H +#include "animation.h" #include "events.h" #include #include -#include "animation.h" /* The direction where the player is going to. */ typedef enum { D_UP, D_DOWN, D_LEFT, D_RIGHT } Direction; diff --git a/src/main.c b/src/main.c index b9dce89..2934faf 100644 --- a/src/main.c +++ b/src/main.c @@ -34,22 +34,31 @@ extern bopti_image_t tiny_npc_male; 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, - {}}, - {{}, {}, 0}, - false, - false, - false, - 0, +Game game = {NULL, + {12 * PXSIZE, + 36 * PXSIZE, + 0, + 0, + 100, + SPEED, + false, + 0, + false, + false, + true, + {}}, + {{}, {}, 0}, + false, + false, + false, + 0, - /* debug variables*/ - false, - false, - false, - {}, - 100}; + /* debug variables*/ + false, + false, + false, + {}, + 100}; /* screen capture management code. TODO: Clean this up! */ diff --git a/src/map.c b/src/map.c index 3734d8f..f4b2fc7 100644 --- a/src/map.c +++ b/src/map.c @@ -180,8 +180,8 @@ void map_render_by_layer(Game *game, int layer) { for(x = 0; x < dw; x++) { /* I get the tile number if his position is inside the map. Then * I draw it. */ - if(tx + x >= 0 && tx + x < map_level->w && ty + y >= 0 && - ty + y < map_level->h) { + if((tx + x >= 0) && (tx + x < (int)map_level->w) && (ty + y >= 0) && + (ty + y < (int)map_level->h)) { /* index of the current tile */ int currentIndex = (y + ty) * map_level->w + tx + x; /* we get the ID of the tile in the current drawable layers diff --git a/src/npc.c b/src/npc.c index 4966c4b..f308b3e 100644 --- a/src/npc.c +++ b/src/npc.c @@ -292,7 +292,8 @@ void npc_draw_single(NPC *npc, Game *game) { int16_t delY = ((npc->cury * PXSIZE) >> PRECISION) - (int16_t)pl->y; game->npc_animation.image = npc_sprites[npc->face]; unsigned char frame = game->npc_animation.frame; - if(npc->paused || !npc->hasPath) game->npc_animation.frame = 0; + if(npc->paused || !npc->hasPath) + game->npc_animation.frame = 0; animation_draw(&game->npc_animation, pl->px - P_WIDTH / 2 + delX, pl->py - P_HEIGHT / 2 + delY); game->npc_animation.frame = frame; diff --git a/src/player.c b/src/player.c index 7da7998..53aab0c 100644 --- a/src/player.c +++ b/src/player.c @@ -47,11 +47,11 @@ void player_draw(Game *game) { Player *player = &game->player; clearevents(); if(!keydown(KEY_LEFT) && !keydown(KEY_RIGHT) && !keydown(KEY_UP) && - !keydown(KEY_DOWN)){ + !keydown(KEY_DOWN)) { game->player.animation.frame = 0; } - player->animation.image = player->is_male ? &player_male_img : - &player_female_img; + 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); }