SFML: Vector2<double> can't compile - c++

I have this code that I can't compile. It works fine if I replace sf::Vector2<double> with sf::Vector2f. I've tried typedef, but it didn't make any difference.
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2<double>(10.0, 10.0)),
sf::Vertex(sf::Vector2<double>(150.0, 150.0))
};
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
window.draw(line, 2, sf::Lines);
window.display();
}
return 0;
}
The first error:
test.cpp: In function ‘int main()’:
test.cpp:9:47: error: no matching function for call to ‘sf::Vertex::Vertex(sf::Vector2<double>)’

sf::Vertex constructor does not take double vectors, only float vectors. See the documentation.

Related

Why SFML rectangle textbox is not being formed and how to fix it?

I was making a program in SFML and C++, which like other program have a rectangle shaped box under which text is being filled but it does not even show a rectangle box. My code is:
int main()
{
sf::RenderWindow window({ 640 ,480}, "Window");
std::string input_text;
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text("", font);
sf::Clock clock;
sf::RectangleShape rectangle(sf::Vector2f(120, 50));
rectangle.setSize(sf::Vector2f(100, 100));
rectangle.setFillColor(sf::Color(150, 50, 250));
rectangle.setOutlineThickness(10);
rectangle.setOutlineColor(sf::Color(250, 150, 100));
window.draw(rectangle);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::TextEntered) {
if (std::isprint(event.text.unicode))
input_text += event.text.unicode;
}
else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::BackSpace) {
if (!input_text.empty())
input_text.pop_back();
}
if (event.key.code == sf::Keyboard::Return) {
std::string text1 = text.getString().toAnsiString();
std::cout << text1 << std::endl;
}
}
}
static sf::Time text_effect_time;
static bool show_cursor;
text_effect_time += clock.restart();
if (text_effect_time >= sf::seconds(0.5f))
{
show_cursor = !show_cursor;
text_effect_time = sf::Time::Zero;
}
text.setString(input_text + (show_cursor ? '_' : ' '));
window.clear();
window.draw(text);
window.display();
}
}
I learnt how to make rectangle shape from SFML webpage. I tried to put it under the while (window.isOpen()) and even outside but it doesn't appear to work. What is the issue and how can I fix it? And can text move to next line when it comes out of box?

RenderWindow not working across multiple functions

I'm new to SFML, and have been watching a tutorial that puts everything in a single main function. When making my own program, I tried to split it into multiple functions, but it isn't working properly, can anyone explain why this works:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
and this doesn't:
#include <SFML/Graphics.hpp>
#include <iostream>
sf::RenderWindow window;
void setup()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
}
int main()
{
setup();
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
They will both compile and run, but in the former, the window will stay open, and in the latter, it won't.
The window variable that you've declared inside setup() is shadowing the global window. object. Try the following:
void setup()
{
window.create(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
}

having trouble creating copies of objects using vector push_back code

I'm trying duplicate the RectangleShape rect1 every time I pressed the space button, but instead of doing that, it just seems to delete my rect1 object in the vector Vec as soon as I release the space key. I can't figure out why, anybody help me please?
here is my code:
int main() {
class shape {
public:
RectangleShape rect1;
};
shape getShape;
getShape.rect1.setSize(Vector2f(100, 100));
getShape.rect1.setFillColor(Color(0, 255, 50, 30));
RenderWindow window(sf::VideoMode(800, 600), "SFML Game");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
bool play = true;
Event event;
while (play == true) {
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
play = false;
}
}
window.clear();
vector <shape> Vec;
if (Keyboard::isKeyPressed(Keyboard::Space)) {
Vec.push_back(getShape);
}
for (int i = 0; i < Vec.size(); ++i) {
window.draw(Vec[i].rect1);
}
window.display();
}
window.close();
return 0;
}
You need to place the vector outside the loop, otherwise you create a new empty one every time:
int main() {
// If you need to use this class in something other than main,
// you will need to move it outside of main.
class shape {
public:
RectangleShape rect1;
};
// But in this particular case you don't even need a class,
// why not just use RectangleShape?
shape getShape;
getShape.rect1.setSize(Vector2f(100, 100));
getShape.rect1.setFillColor(Color(0, 255, 50, 30));
RenderWindow window(sf::VideoMode(800, 600), "SFML Game");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
bool play = true;
Event event;
std::vector<shape> Vec; // Put your vector here!
// play is already a bool, so you don't need == true
while (play) {
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
play = false;
}
}
window.clear();
if (Keyboard::isKeyPressed(Keyboard::Space)) {
Vec.push_back(getShape);
}
for (int i = 0; i < Vec.size(); ++i) {
window.draw(Vec[i].rect1);
}
window.display();
}
window.close();
return 0;
}

How ove the player in SFML with c++

I'm doing a game in c++ with SFML. I've written a code for move the player, but when the game start the player, moves but when i leave the button the player return at its original position.Can you help me, please?
The main:
#include <iostream>
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1320, 840), "Window");
window.setPosition(sf::Vector2i(150, 50));
window.setSize(sf::Vector2u(1320, 840));
window.getPosition();
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(5);
window.getSize();
while (window.isOpen()) {
window.clear(sf::Color::White);
//texture
sf::Texture texture;
if (!texture.loadFromFile("/users/gaetanodonnarumma/desktop/background1.png", sf::IntRect(0, 0, 1320, 840))) {
return -1;
}
sf::Texture playerTexture;
if (!playerTexture.loadFromFile("/users/gaetanodonnarumma/desktop/quadrato-giallo.jpg", sf::IntRect(0, 0, 70, 70))) {
return -1;
}
//sprite
sf::Sprite backgroud;
backgroud.setTexture(texture);
sf::Sprite player;
double pX = 0;
double pY = 770;
player.setTexture(playerTexture);
player.setPosition(sf::Vector2f(pX, pY));
player.getPosition();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::D) {
player.move(10.f, 0.f);
}
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::D) {
player.setPosition(pX + 10, 770);
}
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::A) {
player.move(-5.f, 0.f);
}
}
}
//draw
window.draw(backgroud);
window.draw(player);
window.display();
}
return 0;
}
All your initialization code is running on every frame. This means that on every frame, you are creating (and destroying) a new player, setting its texture, and setting its position to [0, 770].
Move all the initialization to before the main draw loop begins (before while (window.isOpen())).

I have a Segfault, bad allocation?

This is my code :
t_game_elements *initializeEnvironnement(sf::RenderWindow *window)
{
t_game_elements *gameElements;
playerBat playerBatOne(0, 200);
playerBat playerBatTwo(790, 200);
gameElements = (t_game_elements *)malloc(1000);
gameElements->playerOne = playerBatOne;
gameElements->playerTwo = playerBatTwo;
*gameElements->playerOne.getShape();
window->draw(*playerBatOne.getShape()); // this line don't segfault
window->draw(*gameElements->playerOne.getShape()); // this line segfault
return (gameElements);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 500), "Pong", sf::Style::Default);
t_game_elements *elements;
elements = initializeEnvironnement(&window);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
window.close();
}
}
if (event.type == sf::Event::Closed)
window.close();
}
// window.clear();
// window.draw(*elements->playerOne.getShape());
// window.draw(*elements.playerTwo->getShape());
window.display();
}
return 0;
}
class playerBat
{
public:
playerBat(int x, int y);
sf::RectangleShape *getShape();
void setShape(sf::RectangleShape *Shape);
int test = 10;
private:
sf::RectangleShape shapeBat;
};
typedef struct s_game_elements
{
playerBat playerOne;
playerBat playerTwo;
} t_game_elements;
playerBat::playerBat(int x, int y)
{
sf::RectangleShape rectangle(sf::Vector2f(120, 50));
rectangle.setSize(sf::Vector2f(10, 100));
rectangle.setPosition(x, y);
rectangle.setFillColor(sf::Color::Green);
shapeBat = rectangle;
}
void playerBat::setShape(sf::RectangleShape *Shape)
{
shapeBat = *Shape;
}
sf::RectangleShape *playerBat::getShape()
{
return &shapeBat;
}
I allocate 1000 bytes just to be sure I allocate enough memory, I will change it when I will fix the segfautlt first.
I tried to display the variable 'test' of my class and it works fine.
Have an idea why I segfault ?
Thanks