libtcod - Movement function not responding to input

212 Views Asked by At

I'm using libtcod and c and my movement function does not respond to input, here is how the function looks when being called

    TCOD_key_t key;
    move_entity(player.x, player.y, key);

and here is the actualy source code

void move_entity(int x, int y, TCOD_key_t key){
    TCOD_sys_check_for_event(TCOD_EVENT_KEY_PRESS, &key, NULL);
    switch(key.vk){
        case TCODK_UP : y--; break;
        case TCODK_DOWN : y++; break;
        case TCODK_RIGHT : x++; break;
        case TCODK_LEFT : x--; break;
        default:break;
    }
}

Curiously enough when the code from inside of move_entity is copied into the main function the program responds, here is the main loop as it stands

#include "libtcod.h"
#include "move.h"
#include "entity.h"

int main(){
struct Entity player = {40, 25, '@', 100, TCOD_COLOR_LIME};
struct Entity enemy = {20, 35, '&', 50, TCOD_COLOR_RED};
TCOD_console_set_custom_font("terminal12x12_gs_ro.png", TCOD_FONT_LAYOUT_ASCII_INROW, 16, 16);
TCOD_console_init_root(80, 50, "Placeholder title", false, TCOD_RENDERER_SDL);

while(!TCOD_console_is_window_closed()){
    TCOD_key_t key;
    move_entity(player.x, player.y, key);
    TCOD_console_clear(NULL);
    TCOD_console_print(NULL, 1, 1, "Player Health:%d", player.health);
    TCOD_console_print(NULL, 1, 2, "Enemy Health:%d", enemy.health);
    entity_render(player.x, player.y, player.ch, player.forcol);
    entity_render(enemy.x, enemy.y, enemy.ch, enemy.forcol);
    TCOD_console_flush(NULL);
}
return 0;

}

I'm sure it's just something silly I've overlooked but it's really thrown me for a spin and I appreciate the help :)

*edit selalerer's advice here is my edited code

The code as it is called in the main loop

 move_entity(&player.x, &player.y);

And the code in the function

 void move_entity(int *x, int *y){
     TCOD_key_t key;
     TCOD_sys_wait_for_event(TCOD_EVENT_KEY_PRESS, &key, NULL, false);
     switch(key.vk){
         case TCODK_UP : *y--; break;
         case TCODK_DOWN : *y++; break;
         case TCODK_RIGHT : *x++; break;
         case TCODK_LEFT : *x--; break;
         default:break;
      }
}
2

There are 2 best solutions below

0
On BEST ANSWER
void test(int *x1, int *x2) {
    printf("before modif x1 = %d\n", *x1);
    printf("before modif x2 = %d\n", *x2);

    *x1++;   // equivalent to *(x1 + 1) 
    (*x2)++; // equivalent to *x2 += 1;
}

int main() {
    int x1 = 1;
    int x2 = 1;

    test(&x1, &x2);
    printf("after modif x1 = %d\n", x1);
    printf("after modif x2 = %d\n", x2);

    return 0;
}

See the above C example in order to understand what happens, you didn't express the priorities in the right order, you have to dereference first before adding 1 else you are dereferencing the memory space located after your variable which has no effects.

Output:

before modif x1 = 1
before modif x2 = 1
after modif x1 = 1
after modif x2 = 2
1
On

It seems you pass player.x and player.y by value to the move_entity() function. Any change made to x and y in this function is local to the function and won't affect player.x and player.y.

You should change the move_entity() function to receive pointers to int and send the addresses of player.x and player.y to it so it will be able to change them.