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