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

Author Topic: RectangleShape Not Drawing  (Read 4446 times)

0 Members and 2 Guests are viewing this topic.

Ago19

  • Newbie
  • *
  • Posts: 15
    • View Profile
RectangleShape Not Drawing
« on: November 17, 2020, 10:37:28 am »
The RectangleShape in the struct Box won't draw, it should draw a box where you click with your mouse

Shapes.h
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>


struct Box {
       
        sf::RectangleShape sprite;

        Box() {

                sprite.setSize(sf::Vector2f(100.f, 100.f));
                sprite.setFillColor(sf::Color::Red);
                sprite.setPosition(0, 0);
        }

}shape_box;

std::vector<Box> shapes;

 

logger.h
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>

void logVec2f(const sf::Vector2f& vec) {
        std::cout << "x: " << vec.x << " y: " << vec.y << std::endl;
}

void logMousePosRelToWin(sf::RenderWindow& Win) {
        std::cout << "x: " << sf::Mouse::getPosition(Win).x << " y: " << sf::Mouse::getPosition(Win).y << std::endl;
}
 

main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Shapes.h"
#include "logger.h"


int main()
{
    sf::RenderWindow window(sf::VideoMode(1200, 800), "Gravity Box");


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::MouseButtonPressed) {
                shape_box.sprite.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
                shapes.push_back(shape_box);
                logMousePosRelToWin(window);
                std::cout << "[Log] Shape Drawn " << shapes.size() << std::endl;
                logVec2f(shape_box.sprite.getPosition());
            }
        }

        window.clear();
        for (unsigned int i = 0; i < shapes.size(); i++) {
            window.draw(shapes[i].sprite);
        }

        window.display();
    }

    return 0;
}


 
This is the whole code, thanks in advance

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: RectangleShape Not Drawing
« Reply #1 on: November 17, 2020, 11:46:13 am »
I just tested your code (i've put everything in the same source file, except from preprocessors) and its working as it should...
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Ago19

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: RectangleShape Not Drawing
« Reply #2 on: November 17, 2020, 12:01:21 pm »
I just tested your code (i've put everything in the same source file, except from preprocessors) and its working as it should...
Sorry I can't understand. I've set the project to be SFML_STATIC. Maybe it's that? What do you mean with "except from preprocessor"?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: RectangleShape Not Drawing
« Reply #3 on: November 17, 2020, 01:42:25 pm »
no, I mean things like pragma once and include "Shapes.h"

I just put all together in a single source file:

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

struct Box {
        sf::RectangleShape sprite;

        Box() {
                sprite.setSize(sf::Vector2f(100.f, 100.f));
                sprite.setFillColor(sf::Color::Red);
                sprite.setPosition(0, 0);
        }
}shape_box;
std::vector<Box> shapes;

void logVec2f(const sf::Vector2f& vec) {
        std::cout << "x: " << vec.x << " y: " << vec.y << std::endl;
}

void logMousePosRelToWin(sf::RenderWindow& Win) {
        std::cout << "x: " << sf::Mouse::getPosition(Win).x << " y: " << sf::Mouse::getPosition(Win).y << std::endl;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(1200, 800), "Gravity Box");


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::MouseButtonPressed) {
                shape_box.sprite.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
                shapes.push_back(shape_box);
                logMousePosRelToWin(window);
                std::cout << "[Log] Shape Drawn " << shapes.size() << std::endl;
                logVec2f(shape_box.sprite.getPosition());
            }
        }

        window.clear();
        for (unsigned int i = 0; i < shapes.size(); i++) {
            window.draw(shapes[i].sprite);
        }

        window.display();
    }

    return 0;
}

 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Ago19

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: RectangleShape Not Drawing
« Reply #4 on: November 17, 2020, 02:48:40 pm »
no, I mean things like pragma once and include "Shapes.h"

I just put all together in a single source file:

I did the same but the Boxes don't appear on the screen when I click. Their coordinates are right on the debug window though. When I click everything goes fine except of the Box being drawn on the screen
 :'(

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: RectangleShape Not Drawing
« Reply #5 on: November 17, 2020, 07:29:23 pm »
weird. are you able to draw a single RectangleShape, or a vector of rectangleshapes, directly in a small main function?



Quote
x: 295 y: 261
[Log] Shape Drawn 1
x: 295 y: 261
x: 536 y: 512
[Log] Shape Drawn 2
x: 536 y: 512
x: 793 y: 375
[Log] Shape Drawn 3
x: 793 y: 375
x: 573 y: 196
[Log] Shape Drawn 4
x: 573 y: 196
x: 442 y: 428
[Log] Shape Drawn 5
x: 442 y: 428
x: 199 y: 565
[Log] Shape Drawn 6
x: 199 y: 565
x: 142 y: 338
[Log] Shape Drawn 7
x: 142 y: 338
x: 262 y: 104
[Log] Shape Drawn 8
x: 262 y: 104
x: 807 y: 98
[Log] Shape Drawn 9
x: 807 y: 98
x: 1030 y: 191
[Log] Shape Drawn 10
x: 1030 y: 191
x: 1077 y: 476
[Log] Shape Drawn 11
x: 1077 y: 476
x: 810 y: 603
[Log] Shape Drawn 12
x: 810 y: 603
x: 515 y: 86
[Log] Shape Drawn 13
x: 515 y: 86
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything