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

Author Topic: Problem with drawing shapes  (Read 2709 times)

0 Members and 2 Guests are viewing this topic.

Ettur

  • Newbie
  • *
  • Posts: 6
    • View Profile
Problem with drawing shapes
« on: March 20, 2020, 03:44:21 am »
Hi,

Im quite new to C++ and SFML. I was following some tutorials to draw shapes and get keyboard input to make those shapes move. I got everything working nice, at that point I had everything declared and executed in main() method.

Then I thought to make my code more modular and OOP, as soon as I did that -> placed shapes in Player class and keyboard input as a method in Player class and then called those methods exactly where that code was previously in main() method, shapes wont draw.

It must be something simple, there is no errors, but I cant figure it out. Am hoping someone could point out what is wrong. Drawing shapes in main still works as I tried to draw circle and its fine, just drawing from my Player class seems faulty.

Heres my code:

main() method

#include <iostream>
#include <SFML/Graphics.hpp>
#include <math.h>
#include "Player.h"

using namespace std;


int main() {

        sf::RenderWindow window1(sf::VideoMode(1024, 768), "Tutorial", sf::Style::Close | sf::Style::Resize);
        Player player;

        while (window1.isOpen())
        {
                sf::Event evnt1;

                while (window1.pollEvent(evnt1))
                {
                        switch (evnt1.type) {
                        case sf::Event::Closed:
                                window1.close();
                                break;
                        }
                }

                window1.clear(sf::Color(0, 150, 150));
                window1.draw(player.player);
                window1.draw(player.sigth);
                player.movePlayer();
                sf::Vector2i mousePos = sf::Mouse::getPosition(window1);
                player.sigthRotation(mousePos);
                window1.display();

        }

        return 0;
}
 

And Player class
Player.h

#pragma once
#include <SFML/Graphics.hpp>
#include <math.h>
#define PI 3.14159265

class Player
{
public:
        sf::CircleShape player;
        sf::RectangleShape sigth;
       
        Player();
        void movePlayer();
        void sigthRotation(sf::Vector2i mousePos);
};
 


and Player.cpp

#include "Player.h"

Player::Player() {
        sf::CircleShape player (25);
        sf::RectangleShape sigth (sf::Vector2f(150.0f, 2.0f));

        player.setPosition(206.0f, 206.0f);
        sigth.setPosition(231.0f, 231.0f);
        player.setFillColor(sf::Color::Blue);
       
}

void Player::movePlayer() {

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
                player.move(-0.1f, 0.0f);
                sigth.move(-0.1f, 0.0f);

        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
                player.move(0.1f, 0.0f);
                sigth.move(0.1f, 0.0f);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
                player.move(0.0f, -0.1f);
                sigth.move(0.0f, -0.1f);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
                player.move(0.0f, 0.1f);
                sigth.move(0.0f, 0.1f);
        }
}

void Player::sigthRotation(sf::Vector2i mousePos) {
        sf::Vector2f linePos = sigth.getPosition();


        float dx = linePos.x - mousePos.x;
        float dy = linePos.y - mousePos.y;
        double rotation = (atan2(dy, dx)) * 180 / PI;

        sigth.setRotation((float)rotation + 180);
}

 
« Last Edit: March 20, 2020, 03:46:15 am by Ettur »

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Problem with drawing shapes
« Reply #1 on: March 20, 2020, 10:51:20 am »
Hi,

Im quite new to C++ and SFML. I was following some tutorials to draw shapes and get keyboard input to make those shapes move. I got everything working nice, at that point I had everything declared and executed in main() method.

Then I thought to make my code more modular and OOP, as soon as I did that -> placed shapes in Player class and keyboard input as a method in Player class and then called those methods exactly where that code was previously in main() method, shapes wont draw.

It must be something simple, there is no errors, but I cant figure it out. Am hoping someone could point out what is wrong. Drawing shapes in main still works as I tried to draw circle and its fine, just drawing from my Player class seems faulty.

Heres my code:

main() method

#include <iostream>
#include <SFML/Graphics.hpp>
#include <math.h>
#include "Player.h"

using namespace std;


int main() {

        sf::RenderWindow window1(sf::VideoMode(1024, 768), "Tutorial", sf::Style::Close | sf::Style::Resize);
        Player player;

        while (window1.isOpen())
        {
                sf::Event evnt1;

                while (window1.pollEvent(evnt1))
                {
                        switch (evnt1.type) {
                        case sf::Event::Closed:
                                window1.close();
                                break;
                        }
                }

                window1.clear(sf::Color(0, 150, 150));
                window1.draw(player.player);
                window1.draw(player.sigth);
                player.movePlayer();
                sf::Vector2i mousePos = sf::Mouse::getPosition(window1);
                player.sigthRotation(mousePos);
                window1.display();

        }

        return 0;
}
 

And Player class
Player.h

#pragma once
#include <SFML/Graphics.hpp>
#include <math.h>
#define PI 3.14159265

class Player
{
public:
        sf::CircleShape player;
        sf::RectangleShape sigth;
       
        Player();
        void movePlayer();
        void sigthRotation(sf::Vector2i mousePos);
};
 


and Player.cpp

#include "Player.h"

Player::Player() {
        sf::CircleShape player (25);
        sf::RectangleShape sigth (sf::Vector2f(150.0f, 2.0f));

        player.setPosition(206.0f, 206.0f);
        sigth.setPosition(231.0f, 231.0f);
        player.setFillColor(sf::Color::Blue);
       
}

void Player::movePlayer() {

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
                player.move(-0.1f, 0.0f);
                sigth.move(-0.1f, 0.0f);

        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
                player.move(0.1f, 0.0f);
                sigth.move(0.1f, 0.0f);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
                player.move(0.0f, -0.1f);
                sigth.move(0.0f, -0.1f);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
                player.move(0.0f, 0.1f);
                sigth.move(0.0f, 0.1f);
        }
}

void Player::sigthRotation(sf::Vector2i mousePos) {
        sf::Vector2f linePos = sigth.getPosition();


        float dx = linePos.x - mousePos.x;
        float dy = linePos.y - mousePos.y;
        double rotation = (atan2(dy, dx)) * 180 / PI;

        sigth.setRotation((float)rotation + 180);
}

 

U're drawing the members of the Player obj, but they are not configured. When u're doing as example "sf::CircleShape player (25);" in the Player(), u're actually making a new sf::CircleShape, and not use the class member

Ettur

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem with drawing shapes
« Reply #2 on: March 20, 2020, 12:39:57 pm »
Thank you!
It's my C++ understanding, very low.
I managed to get them drawn now! It was in the constructor to just define them correctly.

Could you also help me understand how to make methods for keyboard input and sight rotation to work in main method?

As of now  they are not reacting to input when I call them as class methods. I would think the event scope is different now, might have something to do with it.?

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Problem with drawing shapes
« Reply #3 on: March 21, 2020, 11:19:22 am »
Thank you!
It's my C++ understanding, very low.
I managed to get them drawn now! It was in the constructor to just define them correctly.

Could you also help me understand how to make methods for keyboard input and sight rotation to work in main method?

As of now  they are not reacting to input when I call them as class methods. I would think the event scope is different now, might have something to do with it.?

The way u are handling keyboard and rotation are valid. Doesn't seem to be a syntax problem, but rather ur logic if it doesn't work. I don't have ur modified code. Use a debugger to detect was is not working

Ettur

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem with drawing shapes
« Reply #4 on: March 21, 2020, 12:09:08 pm »
Thanks again!

Problem was so obvious its ridiculous :D I got a couple different projects open with similar code , I kept working on one while trying to figure out these issues.
I pasted here correctly, but my other window where I was working these issues out, I had mistakenly declared Player player; inside main loop !!!!
If I looked close enough I could see player move 0.1f ... but then in the following loop Player would be made a new in original position.

Again in the code above Player player; is made an instance of before game loop, but in my other file it was not.

Thank you! I continue working on my project :)

 

anything