What is wrong with my struct??
Greg Martin

I'm having a little trouble with my code. I'm posting my entire source code below just so I cover myself and alleviate any confusion. I'm using Win2000 with the latest release of DevC++.

1#include <allegro.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <time.h>
5#include "tankFrames.h"
6 
7/* TYPEDEFS */
8typedef BITMAP bmp;
9typedef DATAFILE dat;
10 
11/* ENUMERATIONS */
12enum tankDirection {DIR_NORTH, DIR_SOUTH, DIR_EAST, DIR_WEST};
13 
14/* GLOBAL VARIABLES */
15volatile long speed = 0;
16volatile long animPace = 0;
17dat *tankSprites;
18bmp *imgBackBuffer;
19 
20/* STRUCTS */
21struct tank{
22 tankDirection dir;
23 int x;
24 int y;
25 bmp *stillFrame;
26 bmp *northAnimFrame0;
27 bmp *northAnimFrame1;
28 bmp *northAnimFrame2;
29 bmp *northAnimFrame3;
30 bmp *northAnimFrame4;
31 bmp *northAnimFrame5;
32 bmp *northAnimFrame6;
33 bmp *northAnimFrame7;
34};
35 
36/* INCSPEED FUNCTION DEFINTION */
37void incSpeed(){
38 speed++;
39}
40END_OF_FUNCTION(incSpeed);
41 
42/* INIT FUNCTION DEFINITION */
43void init(){
44 /*
45 * TODO:
46 * Initialize Allegro
47 * Set the graphics mode and color depth
48 * Install the keyboard and timer devices
49 * Lock all memory used by timer routines
50 * Load the necessary datafiles into memory
51 */
52 allegro_init();
53 LOCK_VARIABLE(speed);
54 LOCK_FUNCTION(incSpeed);
55 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
56 set_color_depth(16);
57 install_keyboard();
58 install_timer();
59 install_int_ex(incSpeed, BPS_TO_TIMER(60));
60 tankSprites = load_datafile("tankFrames.dat");
61 imgBackBuffer = create_bitmap(SCREEN_W, SCREEN_H);
62 srand(time('\0'));
63}
64END_OF_FUNCTION(init);
65 
66/* DEINIT FUNCTION DEFINITION */
67void deinit(){
68 /*
69 * TODO:
70 * Clear the key buffer
71 * Destroy all bitmaps
72 * Destroy all MIDIs
73 * Set all variables to null or zero
74 */
75 clear_keybuf();
76 destroy_bitmap(imgBackBuffer);
77 unload_datafile(tankSprites);
78}
79END_OF_FUNCTION(deinit);
80 
81/* TANKANIMNORTH FUNCTION DEFINITION *//*
82void tankAnimNorth(struct tank *temp){
83 while(animPace <= 24){
84 if(animPace < 3)
85 draw_sprite(imgBackBuffer, temp->northAnimFrame0, temp->x, temp->y);
86 else if(animPace >= 3 && animPace < 6)
87 draw_sprite(imgBackBuffer, temp->northAnimFrame1, temp->x, temp->y);
88 else if(animPace >= 6 && animPace < 9)
89 draw_sprite(imgBackBuffer, temp->northAnimFrame2, temp->x, temp->y);
90 else if(animPace >= 9 && animPace < 12)
91 draw_sprite(imgBackBuffer, temp->northAnimFrame3, temp->x, temp->y);
92 else if(animPace >= 12 && animPace < 15)
93 draw_sprite(imgBackBuffer, temp->northAnimFrame4, temp->x, temp->y);
94 else if(animPace >= 15 && animPace < 18)
95 draw_sprite(imgBackBuffer, temp->northAnimFrame5, temp->x, temp->y);
96 else if(animPace >= 18 && animPace < 21)
97 draw_sprite(imgBackBuffer, temp->northAnimFrame6, temp->x, temp->y);
98 else if(animPace >= 21 && animPace < 24)
99 draw_sprite(imgBackBuffer, temp->northAnimFrame7, temp->x, temp->y);
100 }
101}*/
102 
103/* INITTANK FUNCTION DEFINITION */
104void initTank(struct tank *temp, int x, int y){
105 temp->x = x;
106 temp->y = y;
107 temp->stillFrame = tankSprites[tankNorthFrame0].dat;
108 temp->northAnimFrame0 = tankSprites[tankNorthFrame0].dat;
109 temp->northAnimFrame1 = tankSprites[tankNorthFrame1].dat;
110 temp->northAnimFrame2 = tankSprites[tankNorthFrame2].dat;
111 temp->northAnimFrame3 = tankSprites[tankNorthFrame3].dat;
112 temp->northAnimFrame4 = tankSprites[tankNorthFrame4].dat;
113 temp->northAnimFrame5 = tankSprites[tankNorthFrame5].dat;
114 temp->northAnimFrame6 = tankSprites[tankNorthFrame6].dat;
115 temp->northAnimFrame7 = tankSprites[tankNorthFrame7].dat;
116}
117END_OF_FUNCTION(initTank);
118 
119/* MAIN FUNCTION */
120int main(){
121 init();
122
123 struct tank p1;
124
125 initTank(&p1, 50, 50);
126
127 while(!key[KEY_ESC]){
128 while(speed > 0){
129 /*
130 * TODO:
131 * This is the processing loop for all of the game changing data
132 */
133 speed--;
134 }
135
136 /*
137 * TODO:
138 * This is where all of the drawing in the game takes place
139 */
140 blit(imgBackBuffer, screen, 0, 0, 0, 0, imgBackBuffer->w, imgBackBuffer->h);
141 clear_bitmap(imgBackBuffer);
142 }
143
144 deinit();
145 return 0;
146}
147END_OF_MAIN()

My problem is with the struct tank. When the first line of the struct that says tankDirection dir; isn't there or commented out, the program compiles fine. However, if I add the line, the compiler says there's a syntax error before tankDirection and says there is no semicolon at end of struct or union. Why is it doing that??

ReyBrujo

If you are using C, you need to define the variable dir as enum tankDirection instead of just tankDirection.

BAF

Or typedef it:

typedef enum {DIR_NORTH, DIR_SOUTH, DIR_EAST, DIR_WEST} tankDirection;

James Stanley

Or in the declaration use:

typedef enum tankDirection {DIR_NORTH, DIR_SOUTH, DIR_EAST, DIR_WEST} tankDirection;

Do the same for the struct as well. It makes things easier.

EDIT:
Beaten

Thread #587866. Printed from Allegro.cc