Questions About Multiple Elements...

SephirothSephiroth Fayetteville, NC, USA
When declaring an array with multiple elements (say char text[512][10]), is the first number the columns or rows? I need to know because I am adding talking NPCs to my game and I want to be able to add ten per level, each with a message text of up to 512 characters (might increase if needed). Also, can I declare multiple elements with bool, such as "bool gamebits[50]"? I need to put in some bits for the game, and I'd like to use true/false bits if I don't have to type in multiple bool variables. Thanks for the help!

P.S.: Coldshine, Seph2, and those of you that have helped me with this game over the last year or so are welcome to a free copy when I finish it.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

Comments

  • first is the rows, so in ur case (10 NPCs with 512 chars in msg each) it should be
    char bla[10][512];

    and u access the string for charcater #7 with
    printf("%s
    ", bla[7]);

    for an easier example, say want an int array of 2 rows and 3 columns, here it is with initialization:
    [code]
    int myarray[2][3] =
    {
    { 0, 1, 2 },
    { 3, 4, 5 }
    };
    [/code]
    in memory, its layout is like this:
    myarray = 0, 1, 2, 3, 4, 5

    so, you can do:
    int *p = (int*)myarray;
    printf("%d", p[4]); ==> prints out 4



    : When declaring an array with multiple elements (say char text[512][10]), is the first number the columns or rows? I need to know because I am adding talking NPCs to my game and I want to be able to add ten per level, each with a message text of up to 512 characters (might increase if needed). Also, can I declare multiple elements with bool, such as "bool gamebits[50]"? I need to put in some bits for the game, and I'd like to use true/false bits if I don't have to type in multiple bool variables. Thanks for the help!
    :
    : P.S.: Coldshine, Seph2, and those of you that have helped me with this game over the last year or so are welcome to a free copy when I finish it.
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]
    :




  • SephirothSephiroth Fayetteville, NC, USA
    Thanks, pinpong. I've been using matrices since my game began, but I had it backwards in my head for some reason. In fact, I use four matrices in my levels, each being 50x50, but I was accessing them backwards. I guess it worked because they were the same height and width though. Thanks again for the help!

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]


  • : When declaring an array with multiple elements (say char text[512][10]), is the first number the columns or rows? I need to know because I am adding talking NPCs to my game and I want to be able to add ten per level, each with a message text of up to 512 characters (might increase if needed). Also, can I declare multiple elements with bool, such as "bool gamebits[50]"? I need to put in some bits for the game, and I'd like to use true/false bits if I don't have to type in multiple bool variables. Thanks for the help!
    :
    : P.S.: Coldshine, Seph2, and those of you that have helped me with this game over the last year or so are welcome to a free copy when I finish it.
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]
    :

    BOOL is the same as long, so if you don't want to use 32 times as much memory, use this:
    int gamebits[13];
    gamebits[x>>5]>>(x&31)&1; // To get bit x
    gamebits[x>>5]&=~(1<<(x&31)); // To clear bit x
    gamebits[x>>5]|=1<<(x&31); // To set bit x
    gamebits[x>>5]^=1<<(x&31); // To toggle bit x

  • SephirothSephiroth Fayetteville, NC, USA
    : BOOL is the same as long, so if you don't want to use 32 times as much memory, use this:
    : int gamebits[13];
    : gamebits[x>>5]>>(x&31)&1; // To get bit x
    : gamebits[x>>5]&=~(1<<(x&31)); // To clear bit x
    : gamebits[x>>5]|=1<<(x&31); // To set bit x
    : gamebits[x>>5]^=1<<(x&31); // To toggle bit x
    :

    OK I like that idea, but I am self-taught, and I've never messed with those operations (>>, ^>, |=, etc) and am unfamiliar with what they do or how they work. Is there some way I can get some info on them?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories