OK, it's a few months later and I've got just about everything working how I'd like with this game. At this point, I'm now trying to make the switch from vectors to lists for my enemies and fireballs to boost my speed a little bit.
I seem to have correctly done the fireball switch, and I can already notice a bit of an increase there. Before there was a bit of a stutter when I'd shoot the first fireball, and it's gone now.
I'm not having as much luck with the enemies, though. I'm running into problems, and I know what the problem is, but I don't know how to fix it.
I'm using polymorphism for my enemies. They're all derived from a base enemy class, and it works perfectly with vectors. Here's how I've got it setup:
Code:
//in my header file
vector<Enemy*> vEnemy;
//in my cpp file, part of my blitting method
void update_gfx()
{
//this blits the enemies to the screen
//I'm using SDL, but you should be able to get what I'm doing. the main thing is
//vEnemy[i]->image
for(int i=0;i<vEnemy.size();i++) {
SDL_SetColorKey(vEnemy[i]->image, SDL_SRCCOLORKEY, SDL_MapRGB(vEnemy[i]->image->format, 255, 0, 255)); //set colorkey
SDL_BlitSurface(vEnemy[i]->image,&vEnemy[i]->r_image,screen,&vEnemy[i]->r_location);
}
}
And it would work fine. Like I said, I've ported over the fireballs fine, and I'm pretty pisitive it's because they aren't a list of pointers. When I try and change the enemy vector to an enemy list, I'm running into errors. Here's what I've tried to make it look like:
Code:
//in the header file. At the moment it's still vEnemy so I don't have to change letters all around
list<Enemy*> vEnemy; //out enemy list
list<Enemy*>::iterator eIter; //our enemy iterator
//in my blitting method in the cpp file
void update_gfx() {
//this blits the enemies to the screen
for(eIter = vEnemy.begin(); eIter!=vEnemy.end(); eIter++) {
SDL_SetColorKey(eIter->image, SDL_SRCCOLORKEY, SDL_MapRGB(eIter->image->format, 255, 0, 255)); //set colorkey
SDL_BlitSurface(eIter->image,&eIter->r_image,screen,&eIter->r_location);
}
}
OK, with the way I'm doing it now, I'm getting a bunch of errors with the ->image. This doesn't happen unless I make it a list of pointers, which is why I'm pretty sure it's because it's a list of pointers. Here are a few of the errors I'm getting:
error C2839: invalid return type 'std::list<_Ty>::_Tptr' for overloaded 'operator ->'
with
[
_Ty=Enemy *
]
error C2227: left of '->image' must point to class/struct/union
If i change it to list<Enemy> vEnemy I don't get these errors. The problem is, when I create my enemies, they don't become their specific type like they should. They all just become a generic enemy, and do nothing. Here's the code for that section when I don't have this set up as a list of pointers to enemies:
Code:
void generate_enemy() {
Enemy *NewEnemy; //this creates a new enemy.
//this chooses a random number based on the current level.
//It then creates an enemy based on that number, and puts
//it into the enemy vector.
switch (rand()%4) {
case 0:
NewEnemy=new Pig(level);
break;
case 1:
NewEnemy=new Skeleton(level);
break;
case 2:
NewEnemy=new Zombie(level);
break;
case 3:
NewEnemy=new Goblin(level);
break;
}
//this pushes the enemy back
/*this it the only way it'll work. This isn' t how I had it for the vectors.
For the vectors, it was vEnemy.push_back(NewEnemy); but with lists I'm getting this error:
error C2664: 'std::list<_Ty>::push_back' : cannot convert parameter 1 from 'ENEMY *' to 'const ENEMY &'
with
[
_Ty=ENEMY
]
Reason: cannot convert from 'ENEMY *' to 'const ENEMY'
No constructor could take the source type, or constructor overload resolution was ambiguous */
vEnemy.push_back(*NewEnemy);
}
I'm at an impass here. I don't know how to get rid of the -> errors if I make it a list of pointers, and if I don't make it a list of pointers, I can't have specific enemies.
Can someone help point me where I need to go?