I don't really have an explanation and whoever thought that this new posting format was the way to go... idk what to say. I really don't like it. I can only post so much lines of code before something happens to the window and I can't scroll all the way down.
Anyway, its segfaulting when I assign a value to a reference. Not sure why that's happening.
repost from cplusplus.com. Just trying to get some answers don't mean to spam.
GDB:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000030
[Switching to process 27451]
0x0000000100007413 in explosionhandler::registerexplosion (this=0x0, ttype=@0x10000fa5c, b=@0x10000f960, seq=3, a=120, m=0, e=-18) at explosionhandler.cpp:222
222 ttype = num_types; //there was a lot of code commented out
.H:
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include "collarea.h"
#include <vector>
#ifndef EXP_H
#define EXP_H
using std::vector;
class ALLEGRO_BITMAP;
class collarea;
class explosionhandler {
public:
struct explosion {
int type;
int seq;
float x,y;
float radius;
};
vector<struct explosion> explosions;
struct explosion_type {
//int type;
int num_seq;
//equation: size(#) = a + m*# - e*#^2
float a,e,m;
ALLEGRO_BITMAP* exp;
explosion_type *next;
};// *type;
vector<struct explosion_type> type;
int num_types;
explosionhandler();
~explosionhandler();
void add(explosion_type* a);
void registerexplosion(int& ttype,ALLEGRO_BITMAP*& b,int seq, float a, float m,float e);
void createexplosion(int ttype,float x,float y);
void drawexplosions(ALLEGRO_BITMAP* screen);
float getfloat(float& a);
void gettype(explosion_type& a,ALLEGRO_BITMAP*& b,int& nseq, float& aa, float& ee, float& mm);
int checkkill(collarea a);
};
#endif
.cpp:
#include "explosionhandler.h"
#include <math.h>
explosionhandler::explosionhandler()
{
num_types=0;
}
void explosionhandler::registerexplosion(int& ttype,ALLEGRO_BITMAP*& b,int seq, float a, float m,float e)
{
explosion_type n;
n.num_seq=seq;
n.exp=b;
n.a=a;
n.m=m;
n.e=e;
ttype = num_types; //right here is the issue
num_types++;
type.push_back(n);
}
void explosionhandler::createexplosion(int ttype,float x,float y)
{
if((ttype < num_types)&&(ttype>-1))
{
explosion e;
e.type=ttype;
e.x=x;
e.y=y;
e.seq=1;
e.radius = (type.at(ttype).a) + (type.at(ttype).m) - (type.at(ttype).e);
explosions.push_back(e);
}
}
float explosionhandler::getfloat(float& a)
{
return a;
}
void explosionhandler::drawexplosions(ALLEGRO_BITMAP* screen)
{
al_set_target_bitmap(screen);
float a,m,e;
int ttype,seq;
ALLEGRO_BITMAP *b;
int hh = explosions.size();
for(int i=0;i<hh;i++)
{
explosion x = explosions[0];
explosions.erase(explosions.begin());
gettype(type.at(x.type),b,seq,a,e,m);
al_draw_scaled_bitmap(b,0,0,al_get_bitmap_width(b),al_get_bitmap_height(b),getfloat(x.x),getfloat(x.y),getfloat(x.radius)*2,getfloat(x.radius)*2,0);
x.seq++;
if(x.seq<=seq)
{
float oldradius=x.radius;
x.radius=(a+m*x.seq-e*x.seq*x.seq)/2;
x.x-=(x.radius-oldradius);
x.y-=(x.radius-oldradius);
explosions.push_back(x);
}
}
}
void explosionhandler::gettype(explosion_type& a, ALLEGRO_BITMAP*& b, int& nseq, float& aa, float& ee, float& mm)
{
b=a.exp;
nseq=a.num_seq;
aa=a.a;
ee=a.e;
mm=a.m;
}
int explosionhandler::checkkill(collarea a)
{
int yes=0;
int h=explosions.size();
for(int i=0;i<h;i++)
{
float x = explosions.at(i).x;
float y = explosions.at(i).y;
float seq = explosions.at(i).seq;
if(seq)
{
float xx,yy;
a.toxy(&xx,&yy,a.getbl());
float bx,by;
a.toxy(&bx,&by,a.getbr());
xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
yy = (abs(y-yy)<abs(y-by)) ? yy : by;
a.toxy(&bx,&by,a.gettl());
xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
yy = (abs(y-yy)<abs(y-by)) ? yy : by;
a.toxy(&bx,&by,a.gettr());
xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
yy = (abs(y-yy)<abs(y-by)) ? yy : by;
float dd = sqrt((x-xx)*(x-xx) + (y-yy)*(y-yy));
if(dd < explosions.at(i).radius)
{
yes++;
}
}
}
return yes;
}