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

Author Topic: ConvexShape Problem  (Read 2779 times)

0 Members and 1 Guest are viewing this topic.

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
ConvexShape Problem
« on: October 16, 2016, 10:48:38 pm »
Hello,

I have an Entity class like that :

#ifndef Entity_hpp
#define Entity_hpp

#include <vector>
#include <cmath>
#include <iostream>
#include <SFML/Graphics.hpp>

class Entity
{
public:
    Entity(int width, int height);
   
    void update();
   
    void setDimension(int width, int height);
    void setDimension(sf::Vector2i dim);
    const sf::Vector2i getDimension() { return sf::Vector2i(mSpriteWidth, mSpriteHeight); };
   
    sf::Sprite getSprite() { return mSprite; };
    sf::ConvexShape getConvex() { return mConvex; };
   
    void setPosition(sf::Vector2f position) { mPosition = position; };
    sf::Vector2f getPosition() { return mPosition; };
   
private:
    sf::Sprite mSprite;
    sf::ConvexShape mConvex;
    int mSpriteWidth = 50;
    int mSpriteHeight = 50;
    sf::Vector2f mPosition;
};

#endif /* Entity_hpp */
 

My issue:
If in a class that inherit from my Entity class, I want to get my convex shape (or mSprite) with getConvex() (or getSprite()) it's doesn't showing anything... but if I put these variables from private to protected and I use them directly it's working perfectly, and I don't know why, someone could explain to me?

Thanks

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: ConvexShape Problem
« Reply #1 on: October 16, 2016, 11:09:53 pm »
can you show how the diverted class uses getConvex(). it looks like the issue is caused by copying return object. if i guess it correctly

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: ConvexShape Problem
« Reply #2 on: October 16, 2016, 11:23:26 pm »
I have an IsoTile class like that :

#ifndef IsoTile_hpp
#define IsoTile_hpp

#include "Entity.hpp"

class IsoTile: public Entity
{
private:
    sf::VertexArray mLine;
    float mThickness = 0;
    sf::Vector2f mPositionInWindow;
    sf::Vector2f mScale;
   
public:
    IsoTile(int tileWidth, int tileHeight, int thickness);
   
    const sf::Vector2f getPositionInWindow() { return mPositionInWindow; };
    const sf::Vector2f getTileScale() { return mScale; };
   
    void setPositionInWindow(float x, float y);
    void setPositionInWindow(sf::Vector2f &position);
   
    void setTexturePosition(int x, int y);
   
    void setDimension(int width, int height, int thickness);
    void setDimension(sf::Vector2i dim, int thickness);
   
    float getThickness() { return mThickness; };
};

#endif /* IsoTile_hpp */
 

And the constructor :

IsoTile::IsoTile(int tileWidth, int tileHeight, int thickness) : Entity(tileWidth, tileHeight)
{
    setDimension(tileWidth, tileHeight, thickness);
    getConvex().setFillColor(sf::Color::White);
    mScale.x = 1;
    mScale.y = 1;
}

The setFillColor here doesn't working...

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: ConvexShape Problem
« Reply #3 on: October 16, 2016, 11:34:33 pm »
if you want modify the returned object, this object must be taken by reference or pointer. try to change the function in Entity class to sf::ConvexShape& getConvex()
« Last Edit: October 17, 2016, 12:23:31 am by Mortal »

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: ConvexShape Problem
« Reply #4 on: October 16, 2016, 11:36:43 pm »
It's working nicely now, thanks to you, I was dumb to forget that x)

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: ConvexShape Problem
« Reply #5 on: October 16, 2016, 11:50:50 pm »
you most welcome,  :)

 

anything