Sign in to follow this  
raptorstrike

Whats wrong here!*unanswerd*

Recommended Posts

raptorstrike    181
ok this is just getting me ticked i see no reason this code is malfunctioning but here it is
vector<Enemy*> V_Enemys;
string Enemy_List[0]= "Enemy_1.bmp";
const char* Enemy_List_C;
SDL_Surface* I_Enemy=NULL;
string Master_Enemy_Files[] = {"Enemy_One.txt"}; 
//later in code


 for(int i=-1; i < One->Enemy_Vector.size(); i++)
         {
             Enemy_List_C = Enemy_List[0].c_str();
             I_Enemy = SDL_LoadBMP(Enemy_List_C);
             Enemy* New_Enemy = new Enemy(Master_Enemy_Files,50,50,&V_Enemys,0);
             delete New_Enemy;
            if(V_Enemys[i] == NULL)
             {
                 asprintf(&msg,"enemy creation failed",SDL_GetError ());
              };    
            TempRectBlit(I_Enemy,screen,V_Enemys[i]->R_Enemy);    
         }; 






thats the main file heres the Enemy constructor that is being called :
Enemy::Enemy(string File_Name[], int x, int y,vector<Enemy*> *V_Enemy,int NewID)
{
    this->ID = NewID;
    this->FileName = File_Name[ID].c_str();
    R_Enemy.x = x;
    R_Enemy.y = y;
    ifstream fin;
    this->Name = "";
    fin.open(FileName);
    getline(fin, Name);
    fin >> this->Life;
    getline(fin, Image);
    fin >> this->Weapon_ID; // index to an array;
    fin >> this->Speed;
    fin.close();
    V_Enemy->push_back(this);
};        
    






thanks edit oo gosh i do that alot heres your info error (runtime): Image isnt showed at coordinanths(50,50) as it should be it isnt showed at all, all other images are shown everything else works fine; c++ dev-cpp [Edited by - raptorstrike on November 10, 2004 8:58:49 PM]

Share this post


Link to post
Share on other sites
raptorstrike    181
oo gosh i do that alot heres your info

error (runtime): Image isnt showed at coordinanths(50,50) as it should be it isnt showed at all, all other images are shown everything else works fine; c++ dev-cpp

Share this post


Link to post
Share on other sites
Zahlman    1682
You delete the Enemy right after creating it. You seem to be counting on a copy of it being made and added to the vector, but this is a bad idea in general. Constructors are just supposed to, well, construct the object. Anyway, you have a vector of Enemy *, which is probably a good idea, but after you delete Enemy, where do you suppose the pointer in the vector is pointing to? o_O

Share this post


Link to post
Share on other sites
Deranged    668
Quote:
Enemy* New_Enemy = new Enemy(Master_Enemy_Files,50,50,&V_Enemys,0);
delete New_Enemy;


Right there, move the delete to when youre done with the newed object.

Share this post


Link to post
Share on other sites
sweetu    122
do one thing.just make a one function which cleanup the all sprites tht u saved in vector.and start from begin to end.and clean up tht sprite means enemy in urcase.i think it will help to u.try it out.may be u have to change ur code for some level but i think u will do this.



void CleanupSprites()
{
// Delete and remove the sprites in the sprite vector
vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
{
delete (*siSprite);
m_vSprites.erase(siSprite);
siSprite--;
}
}

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6929
deriving from the last post, you could change this:

for(int i=-1; i < One->Enemy_Vector.size(); i++)
{
Enemy_List_C = Enemy_List[0].c_str();
I_Enemy = SDL_LoadBMP(Enemy_List_C);
.....

to this:

for(int i=0; i < One->Enemy_Vector.size(); i++)
{

I_Enemy = SDL_LoadBMP(Enemy_List[i].c_str());
.....


unless i missed something

Share this post


Link to post
Share on other sites
izzo    437
Quote:
Original post by Alpha_ProgDes
deriving from the last post, you could change this:

..
Enemy_List_C = Enemy_List[0].c_str();
..

to this:

..
I_Enemy = SDL_LoadBMP(Enemy_List[i].c_str());
..


unless i missed something


Alpha, it doesn't look like that's what he wants to do. Enemy_List is an array of strings (containing only one string). It looks like he wants to load that same bmp multiple times.

raptorstrike, you don't need to load it every time in the loop. You can just load it once and use it multiple times (at least, it doesn't look to me like you require that many unique copies of it).

Also you don't need to pass the vector into your Enemy class. Just push it onto the vector in your loop.

You should also follow Endurion's comments about not starting at -1.

You don't seem to use One->Enemy_Vector anywhere; is your code correct? Your code doesn't look very well thought out.. :-)

cheers
sam

Share this post


Link to post
Share on other sites
raptorstrike    181
ok there is alot about my code that is diffrent and for right now is a little confusing like the whole [0] think in some of my arrays thats just their because zero is the ONLY part ive deffined really this will later turn into [rand()%X] were x is the # of elements in the array as far as the whole One->Enemy_Vector i thought i took it out but i guess not. Now i want a new object (Enemy*) to be created every loop when i < One->Respawn (yes respawn is > 0) but my problem is this

for(int i=0; i < One->Respawn; i++)
{
Enemy_List_C = Enemy_List[0].c_str();
I_Enemy = SDL_LoadBMP(Enemy_List_C);
Enemy* New_Enemy = new Enemy
(Master_Enemy_Files,50,50,&V_Enemys,0);//when loop repeats wont this be over written then what will the pointer in the vector point to?
One->Respawn++;
if(V_Enemys[i] == NULL)
{
asprintf(&msg,"enemy creation failed",SDL_GetError ());
};
TempRectBlit(I_Enemy,screen,V_Enemys[i]->R_Enemy);
};



as you can see ive fixed a few thing but i dont know what to do about the whole wild pointer thing. all i want is for a new object to be created and added to a vector when the loop is fufilled shoud i go ahead and make a function? also about the code it probibly makes less sience because its in fragments also when i do release the source it will be better looking and with more comments as of right now this is what i got

Share this post


Link to post
Share on other sites
sorry if this doesn't help, but i don't really understand. no offence, but it would help if you used periods and commas in your sentences. ack, i really hate it when people are anal about things like this, but im just being honest [smile]

why do you need to use dynamic memory in the first place? do you have different types of Enemy's which derive from your Enemy class which you want to group togeather? otherwise, i dont see the point.

Share this post


Link to post
Share on other sites
raptorstrike    181
i use dynamic memory because the amount of enemys is fixed to oo lets say 3 and that loop makes sure that the enemy count dosnt drop below 3 until the end of the lvl and so since there is no telling how many enemys will be killed then dynamic comes in real hand (get what im saying). all i neeed is a new object to be created when respawn < X the rest of the stuff is fine (in the loop that is).

Share this post


Link to post
Share on other sites
sorry, but i still don't follow. from what i understand, you don't need dynamic memory for your situation, unless your creating arrays by hand (which i dont think you are). if your using a std::vector, then dont use dynamic memory! one of the main reasons for using a vector is so that you dont have to deal with pointers or dynamic memory, the vector and the std commitee have done this for you, so use it!

i still dont really get what your trying to do. can you explain again (in english not code) what you want to do and what the problem is? some code is fine.

btw,


for(int i=0; i < One->Respawn; i++)
{
Enemy_List_C = Enemy_List[0].c_str();
I_Enemy = SDL_LoadBMP(Enemy_List_C);
Enemy* New_Enemy = new Enemy
(Master_Enemy_Files,50,50,&V_Enemys,0);//when loop repeats wont this be over written then what will the pointer in the vector point to?
One->Respawn++;
if(V_Enemys[i] == NULL)
{
asprintf(&msg,"enemy creation failed",SDL_GetError ());
};
TempRectBlit(I_Enemy,screen,V_Enemys[i]->R_Enemy);
};



theres a few things wrong with this. i see your loading a bmp here. resource loading shouldn't really be a run time thing. you should load resources when doing things like loading a new map or when a new level ends or something like this, otherwise your game will have a slight hiccup when you load that resource.

next, your causing a memory leak here. you create a new enemy, but you dont put him anywhere! you need to track this pointer, because if you dont, that memory is gone forever and the object is floating in outer space. this is what a memory leak is. theres other problems, but i dont really understand what your trying to do...

Share this post


Link to post
Share on other sites
raptorstrike    181
ok whats going on here is this:

i creat an enemy at runtime because not even i know what the chosen bit map will be its random and so thats why i load it like this

then i create a new enemy and to save me the work of calling the push back every time the constuctor dose it for me here

V_Enemy->push_back(this);// were V_Enemy is a function peramater

respawn is the maximum number of enemys that will be shown on the screen at any given time and is a public variable of my level class from whitch "One" comes. then i blit every image on the screen and move every image via the loop so rather then have X number of surfaces i just reasign the current one. Now that i have explained this a little more do you still think i should use dynamic memory?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this