Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing lots of shapes  (Read 2317 times)

0 Members and 1 Guest are viewing this topic.

DinoEntrails

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing lots of shapes
« on: May 24, 2011, 04:36:22 pm »
How do you cycle through your objects and draw/update them in a typical program? I am currently using a std::vector of pointers to Entity objects(a class I wrote) that each have a Draw() and Update() function. However, when I initialize a "bullet" for my game and pass in a reference to the App, the image it will use, and some information regarding its location and animation and then add it to the vector using push_back(*this) (I don't have the code in front of me, so I don't know if I actually used an asterisk there). However, on the first iteration after it is added and Entity.Draw() is called, it throws an error(something about  an "unhandled exception at 0x01290f71"). When I investigated this using the debugger, I found that the object is completely "naked" i.e. none of its variables have been initialized/saved from when the object was created.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing lots of shapes
« Reply #1 on: May 24, 2011, 04:45:09 pm »
Please show your code.
Laurent Gomila - SFML developer

DinoEntrails

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing lots of shapes
« Reply #2 on: May 24, 2011, 08:50:46 pm »
Here you go:
Code: [Select]




class Entity{


public:
sf::Sprite mySprite;
sf::Image *myImage;
std::vector<sf::IntRect> &frames;
sf::RenderWindow &myApp;
Vector2 dir;
float speed;

public:
Entity(sf::RenderWindow &app, sf::Image &myImage, std::vector<sf::IntRect> &framesVec, float x, float y, float dirx = 0.0f, float diry = 0.0f)
: myApp(app), frames(framesVec), myImage(&myImage)
{
dir.setXY(dirx,diry);

mySprite.SetImage(myImage);

mySprite.SetPosition(x,y);    

}

void update(float delta){

}




void draw(){
myApp.Draw(mySprite);
}


};


std::vector<Projectile*> entities;

void AddEntity(Projectile *ent){
entities.push_back(ent);
}

class Projectile: public Entity{
public:
Direction direction;
Animator myAnimator;

Projectile(sf::RenderWindow &app, sf::Image *myImage, std::vector<sf::IntRect> &framesVec, float x, float y, Direction DIR)
: Entity(app, *myImage, framesVec, x,  y)  {

speed = 10.0f;

if (DIR == RIGHT)
dir.setXY(speed,0);
else
dir.setXY(-speed,0);

myAnimator.AddNewSequence("start", 0, 0);
myAnimator.AddNewSequence("fly", 1, 3);

myAnimator.PlayAnimation("start","fly");
mySprite.SetSubRect(frames[myAnimator.UpdateAndGetFrame(.0001)]);
AddEntity(this);
}

...


class Character: public Entity{
public:

bool moving;
Direction direction;
const float speed;
const float jumpSpeed;

bool shooting;
bool falling;
bool jumping;
int jumpStartHeight;
int fallStartHeight;
int maxJumpHeight;

Animator myAnimator;

Character(sf::RenderWindow &app, sf::Image &myImage, std::vector<sf::IntRect> &framesVec, float x, float y, float dirx = 0.0f, float diry = 0.0f)
: Entity(app, myImage, framesVec, x,  y,  dirx, diry,  maskr,  maskg,  maskb), speed(120.0f), jumpSpeed(370.0f), maxJumpHeight(140)

...

Character::Shoot(){
Projectile FIRE(myApp, myImage, LoadProjectileFrames(), mySprite.GetPosition().x, mySprite.GetPosition().y, direction);

}


When the new Projectile's Draw() is called, it stops, throws the above error, and doesn't remember anything about the Entity. I am sure the problem is something stupid, but I can't seem to find the problem.

DinoEntrails

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing lots of shapes
« Reply #3 on: May 24, 2011, 09:32:42 pm »
Is it at all possible that the object instantiated in Projectile() goes out of scope and the pointer is left pointing to nothing?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing lots of shapes
« Reply #4 on: May 24, 2011, 09:55:28 pm »
In Character::Shoot() you instanciate a Projectile that will be destroyed at the end of the function. But the vector of projectiles still has a (invalid) pointer to this destroyed instance.

The important thing that you must do is to clearly identify ownship: who creates, who handles and who destroys entites. Right now nobody owns the projectiles, that's why they get destroyed too early while they shouldn't. This is something that you must decide, and modify your design and code accordingly.
Laurent Gomila - SFML developer

DinoEntrails

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing lots of shapes
« Reply #5 on: May 25, 2011, 04:04:24 am »
Do you know of any examples I can check out?

 

anything