textprintf_ex format modifiers
xmltorrent

I'm having trouble remembering how to handle the %s format modifier of textprintf_ex.

Basically, I just want to display information about the data structure on the screen so I know what I'm dealing with.

First off, here's the data structure I have. It contains four other data structures as its members.

typedef struct PLAYER{
    char *name;
    WEAPON *current_weapon;
    ARMOR *current_armor;
    ACCESSORY *current_accessory;
    SPRITE *sprite;
}PLAYER;

Now here's the function that initializes an instance (not correct terminology but it's how I know it as) of the PLAYER structure:

1void init_player(PLAYER *player){
2 player = malloc(sizeof(PLAYER));
3 player->current_weapon = malloc(sizeof(WEAPON));
4 player->current_armor = malloc(sizeof(ARMOR));
5 player->current_accessory = malloc(sizeof(ACCESSORY));
6 player->sprite = malloc(sizeof(SPRITE));
7
8 player->name = "Greg";
9 player->sprite->x = 0;
10 player->sprite->y = 0;
11 player->sprite->width = 0;
12 player->sprite->height = 0;
13 player->sprite->xspeed = 0;
14 player->sprite->yspeed = 0;
15 player->sprite->xcount = 0;
16 player->sprite->ycount = 0;
17 player->sprite->xdelay = 0;
18 player->sprite->ydelay = 0;
19 player->sprite->framecount = 0;
20 player->sprite->framedelay = 0;
21 player->sprite->curframe = 0;
22 player->sprite->maxframe = 0;
23 player->sprite->animdir = 1;
24 player->sprite->alive = 1;
25 player->current_weapon->name = "Wooden Sword";
26 player->current_weapon->desc = "A simple wooden sword";
27 player->current_weapon->icon = '\0';
28 player->current_armor->name = "Leather Vest";
29 player->current_armor->desc = "A basic vest that looks worn";
30 player->current_armor->icon = '\0';
31 player->current_accessory->name = "Bandana";
32 player->current_accessory->desc = "A white bandana worn by all young boys";
33 player->current_accessory->icon = '\0';
34}

Now here's the function that displays the information on the player. I was just getting here doing one line at a time for errors so that's why it's as small as it is:

void print_player_information(PLAYER *player){
    acquire_bitmap(buffer);
    textprintf_ex(buffer, font, 0, 0, WHITE, -1, "Player Information");
    textprintf_ex(buffer, font, 0, 10, WHITE, -1, "Name: %s", &player->name);
    release_bitmap(buffer);
}

The code doesn't generate errors when compiled... and the program even runs. But instead of the name, I get a bunch of random characters. How do I fix this?

Matthew Dalrymple

Shouldn't it just be

textprintf_ex(buffer, font, 0, 10, WHITE, -1, "Name: %s", player->name);

?

xmltorrent

No because the %s modifier looks for an address.

Kitty Cat

char* is already an address. You're passing an address to an address. Beyond that, you're allocating a local 'player' pointer, but never giving that pointer back for the rest of the program.

xmltorrent

Suggestion as to what I should do?

Matthew Leverton
PLAYER *init_player(void){
  // ...

  return player;
}

player = init_player();

Kris Asick

Remove the & symbol from your textprintf_ex() statement and you'll be fine.

I exclusively use the variety of printf statements available in Allegro and C/C++. I don't actually know how to use cout or Allegro's textout functions. ::)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Thread #590123. Printed from Allegro.cc