passing a string to textprinf_ex
notwist

I want to store a description inside of a map tile structure array and display it on the screen.

so i create this structure type:

struct tile
{
const char *desc;
bool walkable;
char img;
int colorR,colorG,colorB;
unsigned int sapling, tree, rock;
} floor, rock, rocksoil, grass, wall;

define it with:

floor.desc="Stone floor.";
floor.walkable=1;
floor.img=46;
floor.colorR = 100;
floor.colorG = 200;
floor.colorB = 100;

move a little '@' left and right along a single horizontal line

if (key[KEY_4_PAD]&& player.x>0){
player.x-=8;
clear_bitmap(screen);
}


if (key[KEY_6_PAD]&& player.x<472){
player.x+=8;
clear_bitmap(screen);
}

and everything falls apart when i add the line

textprintf_ex(screen, font, 0, 8, makecol(map[player.x].colorR, map[player.x].colorG, map[player.x].colorB), -1, "%s", map[player.x].desc);

DanielH

Is player.x within the parameters of map?
Meaning if map is defined as map[ n ] then player.x should be from 0 to n-1

Is desc defined of map[ player.x ].desc?

what is desc? what type of variable is it. You said string. Is it a C string or a C++ string?

std::string desc or
char desc[] or
char *desc or
...

Without seeing more code, that's all I say about that.

notwist

struct Player
{
char img;
int colorR, colorG, colorB;
int x;
} player;

player.img=64;
player.colorR=140;
player.colorG=140;
player.colorB=140;
player.x=0;

tile map[80];
for(x=0;x<79;x++)
{


i=rand()%8+1;
if(i==1)
map[x]=grass;
else

map[x]=rocksoil;


}

DanielH

Now I see that map is of tile

and tile is of
char *desc

Is player.x defined before you use it?

And when is each tile in map defined?

notwist

player x is defined and set to 0 before it is referenced

this is the main screen drawing loop

for(x=0;x<80;x++)
{
/* multiply by 8 to allow for 8 pixels per character */
xa=x*8;
if (xa == player.x)
{
textprintf_ex(screen, font, xa, 0, makecol(0, 0, 0),-1, "%c", 32);
}
else {

textprintf_ex(screen, font, xa, 0, makecol(map[x].colorR, map[x].colorG, map[x].colorB),-1, "%c", map[x].img);
}

textprintf_ex(screen, font, 0, 8, makecol(map[player.x].colorR, map[player.x].colorG, map[player.x].colorB), -1, "%s", map[player.x].desc);
textprintf_ex(screen, font, 0, 16, makecol(player.colorR, player.colorG, player.colorB), -1, "%d ", player.x/8 );


textprintf_ex(screen, font, player.x, 0, makecol(player.colorR, player.colorG,player.colorB),-1, "%c", player.img);
}

LennyLen
Quote:

and everything falls apart when i add the line

Care to explain what you mean by 'falls apart'?

DanielH

Do this and tell me what happens

if ( map[player.x].desc )
}
    textprintf_ex(screen, font, 0, 8, makecol(map[player.x].colorR, map[player.x].colorG, map[player.x].colorB), -1, "%s", map[player.x].desc);
{
else
{
    textout_ex(screen, font, "null", 0, 8, makecol(map[player.x].colorR, map[player.x].colorG, map[player.x].colorB), -1 );
}

And I should have said it earlier, but use 'code' brackets when displaying code on this website.

[code]
[/code]

notwist

sure, it compiles, and runs

i can iterate the character and it will display the string, however the string doesnt coorelate to the tile the character is standing on

the pointer appears to iterate beyond the array, as it will display strings of desc then it starts to display random symbols then the app freezes

//same thing happens when i replace my map[].desc with the given conditional display

1#include <cstdlib>
2#include <iostream>
3#include <allegro.h>
4#include <string>
5#include <time.h>
6using namespace std;
7 
8 
9 
10int main()
11{
12 int x, y, xa, ya, i,j ,ii, jj;
13 srand(time(NULL));
14 BITMAP *buffer;
15 
16 allegro_init();
17 install_keyboard();
18 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
19/* tile data type and declarations*/
20 struct tile
21 {
22 const char *desc;
23 bool walkable;
24 char img;
25 int colorR,colorG,colorB;
26 unsigned int sapling, tree, rock;
27 } floor, rock, rocksoil, grass, wall;
28
29
30/*player position etc.*/
31 struct Player
32 {
33 char img;
34 int colorR, colorG, colorB;
35 int x;
36 } player;
37
38 player.img=64;
39 player.colorR=140;
40 player.colorG=140;
41 player.colorB=140;
42 player.x=0;
43
44 /* tile definitions */
45 floor.desc="Stone floor.";
46 floor.walkable=1;
47 floor.img=46;
48 floor.colorR = 100;
49 floor.colorG = 200;
50 floor.colorB = 100;
51
52 rock.walkable=0;
53 rock.img=46;
54 rock.colorR = 110;
55 rock.colorG = 110;
56 rock.colorB = 110;
57 rock.desc="Bare rock.";
58
59 rocksoil.walkable=0;
60 rocksoil.img=35;
61 rocksoil.colorR = 120;
62 rocksoil.colorG = 44;
63 rocksoil.colorB = 1;
64 rocksoil.desc="Rocky soil.";
65
66 grass.walkable=0;
67 grass.img=118;
68 grass.colorR = 77;
69 grass.colorG = 179;
70 grass.colorB = 41;
71 grass.desc="Grassy soil.";
72
73 tile map[80];
74 for(x=0;x<79;x++)
75 {
76
77
78 i=rand()%8+1;
79 if(i==1)
80 map[x]=grass;
81 else
82
83 map[x]=rocksoil;
84
85
86 }
87 
88 /*
89x=rand()%70;
90
91 ii=(rand()%9+3)+x;
92
93 for(i=x;i<ii;i++){
94
95 map<i>=rock;
96
97 }
98
99 /* Do this over and over again as long as the player hasn't pressed the
100 excape key */
101 while ( !key[KEY_ESC] )
102 {
103
104
105 clear_keybuf();
106 acquire_screen();
107
108
109 /* movement commands allowing left and right movement*/
110 
111 if (key[KEY_4_PAD]&& player.x>0){
112 player.x-=8;
113 clear_bitmap(screen);
114 }
115 
116
117 if (key[KEY_6_PAD]&& player.x<472){
118 player.x+=8;
119 clear_bitmap(screen);
120 }
121 
122 /* draw the strip of 80 tiles at the top of the screen and the player skipping the tile where the player is located*/
123 for(x=0;x<80;x++)
124 {
125 /* multiply by 8 to allow for 8 pixels per character */
126 xa=x*8;
127 if (xa == player.x)
128 {
129 textprintf_ex(screen, font, xa, 0, makecol(0, 0, 0),-1, "%c", 32);
130 }
131 else {
132
133 textprintf_ex(screen, font, xa, 0, makecol(map[x].colorR, map[x].colorG, map[x].colorB),-1, "%c", map[x].img);
134 }
135
136 if ( map[player.x].desc )
137{
138 textprintf_ex(screen, font, 0, 8, makecol(map[player.x].colorR, map[player.x].colorG, map[player.x].colorB), -1, "%s", map[player.x].desc);
139}
140else
141{
142 textout_ex(screen, font, "null", 0, 8, makecol(map[player.x].colorR, map[player.x].colorG, map[player.x].colorB), -1 );
143}
144 
145 textprintf_ex(screen, font, 0, 16, makecol(player.colorR, player.colorG, player.colorB), -1, "%d ", player.x/8 );
146
147
148 textprintf_ex(screen, font, player.x, 0, makecol(player.colorR, player.colorG,player.colorB),-1, "%c", player.img);
149 }
150
151
152 release_screen();
153
154 rest(50);
155 
156 }
157 return 0;
158
159}
160END_OF_MAIN()

DanielH

EDIT

I know that you can put structure declarations inside the function, but !!!NO!!!

map[ x ] = grass;

What is that?

How about

map[ x ].walkable = grass.walkable;
map[ x ].img = grass.img;
map[ x ].colorR = grass.colorR;
map[ x ].colorG = grass.colorG;
map[ x ].colorB = grass.colorB;
map[ x ].desc = grass.desc;

Or you could do

tile *map[ 80 ];

map[ x ] = &grass;

notwist

if grass is and map[x] are the same data type can't you just assign the values of grass to the array node x?

gnolam

1) Edit your posts to use code tags. They're nigh unreadable without indentation.

2)

Quote:

floor.desc="Stone floor.";

There's your problem.

LennyLen

And in case you don't know what gnolam means, that line should be:

strcpy(floor.desc, "Stone floor");

Thread #589657. Printed from Allegro.cc