Problem declaring struct in allegro

This is a discussion on Problem declaring struct in allegro within the Game Programming forums, part of the General Programming Boards category; heres my code: //#define DEBUGMODE //include this if your debugging #include <stdio.h> #include <allegro.h> #include "test.h" #define left 1 #define ...

  1. #1
    bobish
    Guest

    Problem declaring struct in allegro

    heres my code:

    //#define DEBUGMODE //include this if your debugging

    #include <stdio.h>
    #include <allegro.h>
    #include "test.h"

    #define left 1
    #define right 2
    #define up 3
    #define down 4
    #define UpandLeft 5
    #define DownandLeft 6
    #define UpandRight 7
    #define DownandRight 8
    #define space 9
    #define ericw 43 // width of eric.bmp
    #define erich 61 // hight of eric.bmp

    typedef struct bullet // this is one bullet with its xpos, ypos, and the direction its moving in
    {
    int bulletx;
    int bullety;
    int direction;
    };

    PALETTE Pal;
    BITMAP *ImageBuffer;
    BITMAP *Ammobuffer; // stores the the immage of the amunition


    int main(void)
    {
    BITMAP *dblbuffer;

    int x=100;
    int y=100;
    int shotx=0;
    int shoty=0;
    int lastkeypressed=0;

    bullet bullets[6]; // this stores information on all the bullets in the air

    allegro_init();

    install_keyboard();
    install_timer(); /* needed for `rest' function */

    set_color_depth(32);
    if (set_gfx_mode(GFX_SAFE, 800, 600, 0, 0) < 0)
    {
    printf("%s\n", allegro_error);
    exit(1);
    }

    clear(screen);

    // Pay attention!!! We HAVE to allocate memory for our memory bitmap
    dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
    clear(dblbuffer);

    ImageBuffer = load_bmp("Eric.bmp", Pal);
    Ammobuffer = load_bmp("rock.bmp", Pal);


    while(1)// main loop
    {
    /////////////////// input
    if (key[KEY_LEFT])
    {
    x--;
    lastkeypressed=left;
    }

    if (key[KEY_RIGHT])
    {
    x++;
    lastkeypressed=right;
    }

    if (key[KEY_UP])
    {
    y--;
    lastkeypressed=up;
    }

    if (key[KEY_DOWN])
    {
    y++;
    lastkeypressed=down;
    }

    if (key[KEY_LEFT] && key[KEY_UP])
    {
    lastkeypressed=UpandLeft;
    }

    if (key[KEY_LEFT] && key[KEY_DOWN])
    {
    lastkeypressed=DownandLeft;
    }

    if (key[KEY_RIGHT] && key[KEY_UP])
    {
    lastkeypressed=UpandRight;
    }

    if (key[KEY_RIGHT] && key[KEY_DOWN])
    {
    lastkeypressed=DownandRight;
    }

    if (key[KEY_SPACE])
    {
    lastkeypressed=space;
    //fire(lastkeypressed, Ammobuffer, x, y+50, dblbuffer);
    }

    //////////////////output


    if (lastkeypressed=right)
    {
    rectfill(dblbuffer, x - 1 , y ,x + ericw - 1, y + erich, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=left)
    {
    rectfill(dblbuffer, x + 1 , y ,x + ericw + 1, y + erich, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=up)
    {
    rectfill(dblbuffer, x , y + 1,x + ericw , y + erich + 1, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=down)
    {
    rectfill(dblbuffer, x , y - 1,x + ericw ,y + erich - 1, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=UpandLeft)
    {
    rectfill(dblbuffer, x + 1 ,y + 1,x + ericw + 1, y + erich + 1, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=DownandLeft)
    {
    rectfill(dblbuffer, x + 1 ,y - 1,x + ericw + 1, y + erich - 1, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=UpandRight)
    {
    rectfill(dblbuffer, x - 1 ,y + 1,x + ericw - 1, y + erich + 1, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=DownandRight)
    {
    rectfill(dblbuffer, x - 1 ,y - 1,x + ericw - 1, y + erich - 1, makecol(000,000,000)); /* erase from last place */
    }

    if (lastkeypressed=space)
    {
    draw_sprite(dblbuffer, Ammobuffer, shotx, shoty);
    }

    //circlefill(dblbuffer, x , y, 50, makecol(255,255,255)); /* redraw at new place */
    draw_sprite(dblbuffer, ImageBuffer, x, y); //draw bitmap on dblbuffer
    draw_sprite(dblbuffer, ImageBuffer, x, y);

    vsync();
    blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // copy from double buffer to screen


    if (key[KEY_ESC])//clean up and quit
    {
    // Now, remember to FREE the memory you previously allocated
    destroy_bitmap(dblbuffer);
    destroy_bitmap(ImageBuffer);
    destroy_bitmap(Ammobuffer);
    allegro_exit();
    exit(1);
    }
    }
    return 0;
    }
    END_OF_MAIN();

    and im getting these error messages:
    --------------------Configuration: allegrotest - Win32 Debug--------------------
    Compiling...
    test.c
    C:\programming stuff\allegrotest\test.c(41) : error C2065: 'bullet' : undeclared identifier
    C:\programming stuff\allegrotest\test.c(41) : error C2146: syntax error : missing ';' before identifier 'bullets'
    C:\programming stuff\allegrotest\test.c(41) : error C2065: 'bullets' : undeclared identifier
    C:\programming stuff\allegrotest\test.c(41) : error C2109: subscript requires array or pointer type
    Error executing cl.exe.

    allegrotest.exe - 4 error(s), 0 warning(s)

    bullet bullets[6]; // this stores information on all the this is line 41

    the struct worked fine when i excluded the rest of the code also it won't let me use an array like this [] i have to set how long it is there any way to make an array of structs as long as you want.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Well, to accomplish it in the least amount of code, I'd do:

    struct bullet{
    int bulletx;
    int bullety;
    int direction;
    }bullets[6];

    The whole typedef thing has always seemed a tad strange to me. Thankfully, you really haven't needed it for a few years.

    struct xyz {
    ...
    }whatever1;

    xyz whatever2[6]; // Should work just fine.

    If you get my meaning.

    Hope that helps. I didn't look thru your whole code, just the structure stuff. (It would help in the future if you only post relevent code instead of the whole thing.)

    -Justin
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  3. #3
    bobish
    Guest
    thanks yet again

  4. #4
    Sayeh
    Guest
    > The whole typedef thing has always seemed a tad
    > strange to me. Thankfully, you really haven't needed
    > it for a few years.


    'typedef' = "Type Define"

    It never went out of style, it is valid today as always as a necessary part of the language. If you aren't using it, you are writing buggy or, at the least, non-portable code. No exceptions, period.

    'typedef' is poorly understood by most because few are left today who even understand what's going on. I will try once again, since I seem to be the main champion and guardian of why how and what as regards structures...

    do this instead:

    Code:
    typedef struct                                   /* <- define type */
       { 
       int bulletx; 
       int bullety; 
       int direction; 
       }bullet;                                          /* <- declare type */
    
    bullet bullets[6];                               /* declare variable of type 'bullet' */
    If you just use 'struct', without the typedef, you are _NOT_ creating a variable and you are _NOT_ creating a type, you are defining a template. An amorphous thing to allow the compiler to calculate offsets during link. People fuzz the difference between declaration, definition, and type declaration and variable declaration.

    People tend to think that because they can confuse a compiler's syntax engine and get code to compile without errors or warnings that the code is correct-- not necessarily so. A compiler is not infallable.

    In development the 'end' does NOT justify the means. both are equally important and harmonious.

    It always pays to do it right. Even if it seems tedious. If you use 'typedef' properly, it means you don't have to litter the 'struct' keyword throughout your code. Which happens to be a symptom of the compiler being 'not sure' what this is that you're referring to, so you have to keep reminding it the item in question is a 'struct'.

    Do it right means the compiler handles error checking more accurately. It means portable, easier to maintain code. Why wouldn't you do it?

    > Well, to accomplish it in the least amount of code, I'd do:

    and justifying cutting corners to cut quantity of code is just plain lazy. In 98% of the cases, the amount of code you write has nothing whatsoever to do with how fast your application executes.

    ---

    To answer your question-- how to make an array of structs any length you like, use malloc(). Using the above typedef, we can do this:

    Code:
    bullet *listP;                            /* declare vars */
    int   n;
    
    n = 5;                                      /* initialize vars */
    listP = 0L;
    
    listP = (bullet*)malloc(10 * sizeof(bullet));     /* allocate an array of 10 bullets */
    if(listP)
       {
       listP[n].bulletx = 5;
       listP[n].bullety = 1;
       . . .
       };
    Since it's a single dimensioned array, it's easy to malloc() it and use it like any other array. Just remember to free() it when done.

    enjoy.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    In c it's more of a style issue, I prefer typedefs. Alot of the bsd socket structures are
    not typedef'ed

    Doing something like

    Code:
    struct S {
        int a;
    };
    S s;
    Is incorrect in c, it is correct in c++. In c you
    must do struct S s;

    Code:
    struct {
         int a;
    } s;
    And I'm certainly created a variable. But a variable must have a type. But this type has no name attached to it. s's type is by convention of type anonymous struct. If you do
    Code:
    typedef struct {
         int a;
    } S;
    You are "attaching" 'S' to that anonymous struct.

    The difference between "what is", "what is the value of", and "what is it called by" can be confusing. Here is a quote from Alice and Wonderland:

    "The name of the song is called 'Haddocks' Eyes"
    "Oh, that's the name of the song, is it?" Alice said
    "No, you don't understand" the King said
    "That's what the name is called. The name really is 'The Aged Aged Man' ..."

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    I still use typedef, but not with structs because I use too much C++ anymore, and it was a C++ struct (class) I was demonstrating. In this, C++ makes more sense to me. I don't see how it is really cutting corners as it doesn't make less sense to define a class simply as "class" or "struct" in this case followed by the class name ("bullet" in the example)... however, I should have pointed out where I was coming from with that.

    A straight C compiler would complain like crazy at my code, I know.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  7. #7
    Sayeh
    Guest
    It is not that typedef's aren't used in C++, or aren't necessary-- they are (atleast for portability forward/backwards and cross-platform). C++ is not a different language, it was a set of extensions onto the C language itself.

    Of course, the main purpose of C++ was, as you indirectly point out, to _abstract_ the rules. Bjarne didn't like the strict discipline of C, having to do things like variable scope, typedefs, and specifics-- he preferred abstraction.

    One of the reasons I know so much about development, programming, and computers is because I don't forget the rules. I don't stop doing something that makes sense because I no longer have to. Everything has a reason, and one of the problems today with the deplorable code being generated is because few people understand the whys and wherefores-- so assumptions are made and syntactic or logical problems are created.

    Sorry to be anal about this, it's just that it's served me well.

    And another thing-- if you use a 'typedef' statement, again it means you don't have to litter your code with the 'struct' keyword everywhere-- which ends up being a lot of extra typing.

    'nuf said.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    And another thing-- if you use a 'typedef' statement, again it means you don't have to litter your code with the 'struct' keyword everywhere-- which ends up being a lot of extra typing.
    Which was, to my understanding, the real purpose of typedefing a struct was it not? I mean, typing out the struct keyword everywhere was the alternative until the abstraction of C++.

    I guess when it comes to structures, I just fail to see the benefit of not assuming that we are creating a type. I respect your defence of using typedef, but I don't like to waste my code either.. so here's the question: what use would there be in not defining a structure as a type? Then maybe I'll be able to understand why we should differentiate. Hope that makes sense.. Just the combinational mathematics coming out in me.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with struct?
    By cangel in forum C Programming
    Replies: 8
    Last Post: 09-28-2008, 12:35 AM
  2. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21