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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - aSpookyMan

Pages: [1]
1
Yeah that makes sense, thank you!

2
Graphics / Re: sf::Color multiplication not working
« on: July 27, 2020, 06:09:03 am »
Just multiply each element individually like so:
sf::Color c1(100, 100, 100);
sf::Color c2(2, 2, 2);
sf::Color c3(c1.r * c2.r, c1.g * c2.g, c1.b * c1.b);
 
But if you're going to do this often, I suggest you do some operator overloading.

3
Graphics / Re: Exception thrown after trying to display sf::Text
« on: July 27, 2020, 05:59:59 am »
The problem is that you're setting the fonts as local varibales:
void someFunc()
{
sf::Font font;
        if (!font.loadFromFile("arial.ttf"))
        {
            std::cout << "Error occured. \n";
        }
} //font will delete itself here
 
So when the window tries to draw your text, it doesn't find its font, because it was deleted when it got out of scope. To fix this issue, I'd suggest adding the font to your class or main function so it doesn't get deleted.

4
I have this struct containing a sprite and a texture:
struct Layer
{
        Layer(const char* _path, sf::View& view) : deviation{ 0.0f }, mod{ 1 } {
                txtr.loadFromFile(_path);
                spr.setTexture(txtr);
                txtr.setRepeated(true);

                scale = { view.getSize().y / txtr.getSize().y ,
                                  view.getSize().y / txtr.getSize().y };

                sf::Vector2f pos(view.getCenter().x - view.getSize().x, view.getCenter().y + view.getSize().y / 2);
                sf::Vector2f sprdim(txtr.getSize().x* scale.x, txtr.getSize().y* scale.y);

                spr.setOrigin({0.0f, sprdim.y});
                spr.setPosition(pos);
                spr.setTextureRect({ 0, 0, static_cast<int>(txtr.getSize().x),
                                                               static_cast<int>(txtr.getSize().y) });
                spr.setScale(scale);
        }

        sf::Vector2f scale;

        sf::Sprite spr;
        sf::Texture txtr;

        float deviation;

        unsigned mod;
};
 
Then I made a vector of 'Layer' in the following class:
class Parallax {
public:

        std::vector<Layer> layers;

        sf::Sprite sprite;//test
        sf::Texture t;
       
        Player player;

        float deviation = 0.0f;
        float speed = 0.5f;
        unsigned mod = 1;

        bool canMove;

        void Init(Player& player, sf::View& view);
        void Update(const float& dt, sf::View& view);
        void Draw(sf::RenderTarget& target);
};
 
And lastly, the definition:
#include "Parallax.h"

void Parallax::Init(Player& player, sf::View& view) {
       
        this->player = player;

        //Further-most layers added first
        layers.push_back(Layer("Res/Textures/Parallax/glacial_mountains.png", view));

}
void Parallax::Update(const float& dt, sf::View& view) {
        for (size_t i = 0; i < layers.size(); i++) {
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && player.canMoveLeft) {
                        layers.at(i).deviation += this->speed * i * dt;
                        this->canMove = true;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && player.canMoveRight) {
                        layers.at(i).deviation -= this->speed * i * dt;
                        this->canMove = true;
                }
                else this->canMove = false;

                if (canMove) layers.at(i).spr.move(deviation * dt, 0.0f);
               
                sf::Vector2f sprpos(layers.at(i).spr.getPosition());
                sf::Vector2f sprdim(layers.at(i).txtr.getSize().x * layers.at(i).spr.getScale().x,
                        layers.at(i).txtr.getSize().y * layers.at(i).spr.getScale().y);

                if (sprpos.x + sprdim.x < view.getCenter().x + view.getSize().x / 2) {
                        layers.at(i).mod++;
                        layers.at(i).spr.setTextureRect({ 0, 0, static_cast<int>(layers.at(i).txtr.getSize().x * layers.at(i).mod),
                                                                                                        static_cast<int>(layers.at(i).txtr.getSize().y) });
                }
        }
}
void Parallax::Draw(sf::RenderTarget& target) {
        for (auto& i : this->layers) target.draw(i.spr);
}
 
I don't understand how that could possibly lose the pointer to its texture, since the texture itself is not a temporary variable. Maybe it's some dumb mistake that I'm making, but I can't really see it.
I tried pinting
std::cout << sprite.getTexture() << "\n";
and the texture's memory address was outputted just fine. Any ideas to what might be causing this?


5
I got the error fixed with the help of a dude on discord. I just had to remove const and set the RenderWindow variable as a reference, pretty simple mistake.

6
I just realized I didn't type out the error lol. Sorry about that

function "sf::RenderWindow(const sf::RenderWindow &)"(declared implicitly) cannot be referenced -- it is a deleted function

7
Quote
class UIManager
   {
   public:
      RenderWindow window;
      Event event;

      UIManager( const RenderWindow& win, const Event& e) : window(win), event(e)
      {
         
      }
   };
The code above is giving me an error "sf::RenderWindow::RenderWindow(const sf::RenderWindow &)"
when I set window = win. Is this happening because I have not yet defined window? And if so how can I fix this? I had thought about making a global RenderWindow variable, but from what I've read that's not optimal.

I need help!

Pages: [1]