Confusing problem using vector in game

Pages: 12
closed account (o3hC5Di1)
It seems to me like &type is only the address of type.

A pointer contains an address of a variable, so a pointer to a pointer is basically an address pointing to an address of a variable.

It seems to me that the compiler can't go that deep - try:

void explosionhandler::add(explosion_type* a)

No expert here by the way - just trying to think along.

All the best,
NwN
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
void explosionhandler::registerexplosion(int& ttype,int seq,float a,float m,float e)
{
	add(type);
}
void explosionhandler::add(explosion_type* a)
{
	if(a)
	{
		struct explosion_type* nnew = new explosion_type;
		nnew->a=100;
		nnew->m=30;
		nnew->e=10;
		nnew->num_seq=2;
		nnew->type=num_types;
		nnew->next = a;
		*a = *nnew;
	}
	else {
		a = new explosion_type;
		if(a==0)
		{
			throw "In add, new operator failed";
		}
		a->a=100;
		a->m=30;
		a->e=10;
		a->num_seq=2;
		a->type=-1;
		a->next=0;
	}
	num_types++;
}


GDB:
1
2
3
4
5
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000018
[Switching to process 26793]
0x000000010000819a in explosionhandler::registerexplosion (this=0x0, ttype=@0x10000f91c, seq=3, a=120, m=0, e=-0.180000007) at explosionhandler.cpp:31
31		add(type);


so it definitely has something to do with the value passed.

I remember screwing around with linked list and I modified a tutorial program to this:

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <cstdlib>
#include <stdlib.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>

const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int BOUNCER_SIZE = 32;
float startx = 570;
float starty = 200;
int max = 11;
float dx = -10;

struct circle 
{
	float x;
	float y;
	float dy;
	struct circle *c;
} *top;
void deepcopy(struct circle*& a, struct circle*& b);
void add(struct circle*& a);
void pop(struct circle*& a);
void renew(struct circle*& a);
void push(struct circle*& a, struct circle* b);
void move(struct circle*& a);
int count(struct circle* a);

int count(struct circle* a)
{
	int c = 0;
	for(struct circle* b = a;b!=0;b=b->c)
	{
		c++;
	}
	return c;
}
void deepcopy(struct circle*& a, struct circle*& b)
{
	b->x=a->x;
	b->y=a->y;
	b->dy=a->dy;
}
void add(struct circle*& a)
{
	if(a!=0)
	{
	struct circle* nnew = new circle;
	nnew->x=startx;
	nnew->y=starty;
	nnew->dy= (float)((rand() % (2*max)) - max);
	nnew->c = a;
	a = nnew;
	}
	else {
		a = new circle;
		if(a==0)
		{
			throw "In add, new operator failed";
		}
		a->x=startx;
		a->y=starty;
		a->dy=(float)((rand() % (2*max)) - max);
		a->c=0;
	}
}
void pop(struct circle*& a)
{
	struct circle *c = a;
	a=a->c;
	free(c);
}
void renew(struct circle*& a)
{
	struct circle *nnew;
	while(a!=0)
	{
		if(!((a->x<=0)||(a->x>=SCREEN_W)||(a->y<=0)||(a->y>=SCREEN_H)))
		{
			struct circle *b = new circle;
			deepcopy(a,b);
			push(nnew,b);
		}
		pop(a);
	}
	a = nnew;
}
void push(struct circle*& a, struct circle* b)
{
	b->c=a;
	a = b;
}
void move(struct circle*& a)
{
	struct circle *b = a;
	while(b!=0)
	{
		b->x+=dx;
		b->y+=b->dy;
		b=b->c;
	}
}
int main(int argc, char **argv)
{
   top = 0;
   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_EVENT_QUEUE *event_queue = NULL;
   ALLEGRO_TIMER *timer = NULL;
   ALLEGRO_BITMAP *bouncer = NULL;
   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
   float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
   bool redraw = true;
 
   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }
 
   if(!al_install_mouse()) {
      fprintf(stderr, "failed to initialize the mouse!\n");
      return -2;
   }
 
   timer = al_create_timer(1.0 / FPS);
   if(!timer) {
      fprintf(stderr, "failed to create timer!\n");
      return -3;
   }
 
   display = al_create_display(SCREEN_W, SCREEN_H);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      al_destroy_timer(timer);
      return -4;
   }
 
   bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
   if(!bouncer) {
      fprintf(stderr, "failed to create bouncer bitmap!\n");
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -5;
   }
   if(!al_init_primitives_addon())
   {
	   fprintf(stderr,"primitives didn't load\n");
	   return -6;
   }
   al_set_target_bitmap(bouncer);
   al_clear_to_color(al_map_rgb(255, 0, 255));
   al_draw_circle((float)BOUNCER_SIZE/2,(float)BOUNCER_SIZE/2,(float)BOUNCER_SIZE/3,al_map_rgb(255,255,255),3);
   al_set_target_bitmap(al_get_backbuffer(display));
   event_queue = al_create_event_queue();
   if(!event_queue) {
      fprintf(stderr, "failed to create event_queue!\n");
      al_destroy_bitmap(bouncer);
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }
 
   al_register_event_source(event_queue, al_get_display_event_source(display));
   al_register_event_source(event_queue, al_get_timer_event_source(timer));
   al_register_event_source(event_queue, al_get_mouse_event_source());
   al_clear_to_color(al_map_rgb(0,0,0));
   al_flip_display();
   al_start_timer(timer);
 
   while(1)
   {
      ALLEGRO_EVENT ev;
      al_wait_for_event(event_queue, &ev);
 
      if(ev.type == ALLEGRO_EVENT_TIMER) {
         redraw = true;
      }
      else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
         break;
      }
      else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES ||
              ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) {
 
         bouncer_x = ev.mouse.x;
         bouncer_y = ev.mouse.y;
      }
      else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
          break;
      }
      if(redraw && al_is_event_queue_empty(event_queue)) {
         redraw = false;
         al_clear_to_color(al_map_rgb(0,0,0));
         //al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
		 if(count(top)<=max)
	     {
			 add(top);
	     }
		 if(top==0)
		 {
			 throw "top is still 0";
		 }
		 // al_draw_bitmap(bouncer,a->x,a->y,0);
		 struct circle *a = top;
		 while (a->c!=0)
		 {
		     al_draw_bitmap(bouncer,a->x,a->y,0);
			 if(a->c!=0)
			 {
				 a=a->c;
			 }
		 }
		 move(top);
		 renew(top);
         al_flip_display();
      }
   }
   al_destroy_bitmap(bouncer);
   al_destroy_timer(timer);
   al_destroy_display(display);
   al_destroy_event_queue(event_queue);
 
   return 0;
}


That works perfect. I guess I just don't understand the difference.
ok I got it. That wasn't the issue.
closed account (o3hC5Di1)
So what was the issue?
I would be interested to read it an perhaps someone else will benefit from your solution.

All the best,
NwN
Last edited on
I'm curious about something. Why does he do things like this void deepcopy(struct circle*& a, struct circle*& b);? Why does he include struct in every function and such? Wouldn't it have worked as just void deepcopy(circle*& a, circle*& b);? Is this just programmer's preference issue?
Topic archived. No new replies allowed.
Pages: 12