Need assistance with beginner collision code

I have taken quite a few hours to investigate this myself. I am very new to C++ and have only found trouble when trying to fix this. Perhaps someone can help.

I use allegro with Dev-C++.

This code was a tutorial from the internet, part 8 of 9.
http://www.loomsoft.net/resources/alltut/alltut_lesson8.htm

After spending those hours looking for a solution I finally gave in and simply copy/pasted the code from the site. It gave me the same error.

My errors are the following:
a function-definition is not allowed here before '{' token
expected `,' or `;' before '{' token
expected `}' at end of input

And here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <allegro.h>
#include <stdlib.h>

volatile long speed_counter = 0; //integer to store value of the speed counter
void increment_speed_counter() //function to increment the speed counter
{
     speed_counter++;
     }
END_OF_FUNCTION(increment_speed_counter);

int main(int argc, char *argv[])
{
    allegro_init();
    install_keyboard();
    install_timer();
    
    LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter);
    install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));
    
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT, 640,480,0,0);
    
    BITMAP *buffer = create_bitmap(640,480); //create a buffer for smooth animation
    if(buffer == NULL)
    {
              set_gfx_mode(GFX_TEXT,0,0,0,0);
              allegro_message("Could not create buffer!");
              exit(EXIT_FAILURE);
              }
BITMAP *image1 = load_bitmap("C:/Dev-Cpp/Teh Stuph/AL8/image1.bmp", NULL);
if(image1 == NULL)
{
          set_gfx_mode(GFX_TEXT,0,0,0,0); //sets screen mode for allegro messages
          allegro_message("Could not load image1.bmp");
          exit(EXIT_FAILURE);
          }
BITMAP *image2 = load_bitmap ("C:/Dev-Cpp/Teh Stuph/AL8/image2.bmp", NULL);
if(image2 == NULL)
{
          set_gfx_mode(GFX_TEXT,0,0,0,0);
          allegro_message("Could not load image2.bmp");
          exit(EXIT_FAILURE);
          }
int image1_x_position = 0;
int image1_y_position = 0;

int image2_x_position = 100;
int image2_y_position = 100;

int image1_bb_left = image1_x_position;
int image1_bb_top = image1_y_position;
int image1_bb_right = (image1_bb_left + image1->w);
int image1_bb_bottom = (image1_bb_top + image1->h);

int image2_bb_left = image2_x_position;
int image2_bb_top = image2_y_position;
int image2_bb_right = (image2_bb_left + image2->w);
int image2_bb_bottom = (image2_bb_top + image2->h);

int show_bbox = FALSE;
int collision = FALSE;

while(!key[KEY_ESC])
{
                    while(speed_counter > 0)
                    {
                    if(key[KEY_LEFT])
                    image1_x_position --;
                    if(key[KEY_RIGHT])
                    image1_x_position ++;
                    if(key[KEY_DOWN])
                    image1_y_position ++;
                    if(key[KEY_UP])
                    image1_y_position --;
                    
                    if(key[KEY_A])
                    image2_x_position --;
                    if(key[KEY_D])
                    image2_x_position ++;
                    if(key[KEY_S])
                    image2_y_position --;
                    if(key[KEY_W])
                    image2_y_position ++;
                    
                    if(key[KEY_SPACE])
                    show_bbox = TRUE;
                    else if(!key[KEY_SPACE])
                    show_bbox = FALSE;
                    
                    image1_bb_left = image1_x_position;
                    image1_bb_top = image1_y_position;
                    image1_bb_right = (image1_bb_left + image1->w);
                    image1_bb_bottom = (image1_bb_top + image1->h);
                    
                    image2_bb_left = image1_x_position;
                    image2_bb_top = image1_y_position;
                    image2_bb_right = (image2_bb_left + image2->w);
                    image2_bb_bottom = (image2_bb_top + image2->h);
                    
                    collision = TRUE; //assume there is a collision
                    if(image1_bb_bottom < image2_bb_top)
                    {
                                        collision = FALSE;
                                        }
                    if(image1_bb_top > image2_bb_bottom)
                    {
                                     collision = FALSE;
                                     }
                    if(image1_bb_right < image2_bb_left)
                    {
                                       collision = FALSE;
                                       }
                    if(image1_bb_left < image2_bb_right)
                    {
                                      collision = FALSE;
                                      }
                    speed_counter --;
                    }

draw_sprite(buffer, image1, image1_x_position, image1_y_position);
draw_sprite(buffer, image2, image2_x_position, image2_y_position);

if(show_bbox == TRUE)
{
line(buffer, image1_bb_left, image1_bb_top, image1_bb_right, image1_bb_top, makecol(255,0,0));
line(buffer, image1_bb_left, image1_bb_bottom, image1_bb_right, image1_bb_bottom, makecol(255,0,0));
line(buffer, image1_bb_left, image1_bb_top, image1_bb_left, image1_bb_bottom, makecol(255,0,0));
line(buffer, image1_bb_right, image1_bb_top, image1_bb_right, image1_bb_bottom, makecol(255,0,0));

line(buffer, image2_bb_left, image2_bb_top, image2_bb_right, image2_bb_top, makecol(255,0,0));
line(buffer, image2_bb_left, image2_bb_bottom, image2_bb_right, image2_bb_bottom, makecol(255,0,0));
line(buffer, image2_bb_left, image2_bb_top, image2_bb_left, image2_bb_bottom, makecol(255,0,0));
line(buffer, image2_bb_right, image2_bb_top, image2_bb_right, image2_bb_bottom, makecol(255,0,0));
}

if(collision == TRUE)
{
             textprintf_ex(buffer, font, 0,0, makecol(255,255,255), -1, "Collision!");
             }

blit(buffer, screen, 0,0,0,0,640,480); //blit the buffer
clear(buffer); //clear the buffer

destroy_bitmap(buffer);
destroy_bitmap(image1);
destroy_bitmap(image2);

return 0;
}
END_OF_MAIN()
Last edited on
I'd recommend that you start C++ without Allegro, first... but I don't know anything about Allegro, so I can't help you with this specifically.
I have done quite a few things without allegro, my interests just push me into it. It's so tempting to try this idea. Two objects that can collide, I know it's simple but I couldn't resist. I will take your advice and work on C++ mainly at first.
I believe the loop on line 64 while(!key[KEY_ESC]) is missing an ending brace somewhere...I can't tell for sure because the indentation got kind of weird at the end.
I've been working on the possible solution of a missing brace, no luck yet. Thank for your input though Firedraco, hopefully I'll get it soon.
I have some Collission Detection code at home that doesn't use Allegro. Would you like me to post it after work?
Might it have to do with END_OF_MAIN() [assuming that is a function in allegro] being called after the main executes? or that increment_speed_counter() never appears to be called? maybe I'm just blind.
firedraco is right, put it in notepad++, use folding, the while loop has no closing brace. he is again also correct that it is unclear where that loop is supposed to end, otherwise i could pinpoint it for you.
Zaita, I would appreciate that! Please do.

Mal, I'm going to try the idea with notepad++. I actually wasn't aware of that until you said something, so thank you.

Omega, I was thinking along the same lines and have been trying each idea I can think of. I'm probably overlooking something simple.

Thank you all for aiding me in finding a solution.
Last edited on
No worries. Have left myself a message to post it tonight when I get home.

Z.
Topic archived. No new replies allowed.