C++ and SFML, no instance of the overloaded function "sf::RenderWindow.draw()" matches the argument list - c++

I am currently trying to make a simple pong game with SFML in C++. I consistently get the error in the main file.
#include <iostream>
#include "Ball.h"
Ball b;
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(1200, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
window.draw(b);
// end the current frame
window.display();
}
return 0;
}
This is the ball.h file:
#pragma once
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Ball
{
public:
double x, y;
int Radius;
void Render(int Rad, sf::Color BallColour) {
sf::CircleShape shape(Rad);
// set the shape color to green
shape.setFillColor(BallColour);
}
};
I am unsure as to why the error occurs. Any help would be appreciated.

You had not call function of render, you just wrote window.draw(b), and programm dont know what do you mean. In Ball.h you should write:
#pragma once
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Ball
{
public:
double x, y;
int Radius;
void Render(int Rad, RenderWindow& window) {
sf::CircleShape shape(Rad);
// set the shape color to green
shape.setFillColor(sf::Color::Green);
shape.setPosition(600, 300);//SETTING POSITION OF THE BALL
window.draw(shape);//if you know what is reference on variable, you will understand what is it
}
};
And in your main.cpp you shhould call your function Render:
#include <iostream>
#include "Ball.h"
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(1200, 600), "My window");
Ball b; //I advice you to create object of class in main function
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
b.Render(10, window);// give the function reference on window
// end the current frame
window.display();
}
return 0;
}

sf::RenderWindow::draw requires an implementation of sf::Drawable. You can either pass sf::CircleShape (it's a sf::Drawable) directly or implement sf::Drawable and delegate the call.
class Ball : public sf::Drawable {
public:
double x, y;
int Radius;
void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
sf::CircleShape shape(Radius);
shape.setFillColor(BallColour);
shape.draw(target, states);
}
};

Related

SFML Sprite isn't showing on the window even though it isn't giving me any errors

I'm working with a ref class and using SFML for the graphics, so at the beginning the problem I encounter was facing non-managed members in a managed class, I fixed that problem but now, I'm unable to see the sprite in the window i'm working
#include <SFML/Graphics.hpp>
#pragma make_public(sf::Sprite)
using namespace sf;
using namespace System::Collections::Generic;
namespace GameModel {
public ref class Grass {
public:
Sprite* Sprite;
Texture* Texture;
int Size;
Grass(int size) {
Size = size;
Texture = new sf::Texture();
Sprite = new sf::Sprite(*Texture);
Texture->loadFromFile("Imagenes/Grass.png");
//Image is 500x500px
Sprite->setTexture(*Texture,true);
Sprite->setScale(float(Size / 500), float(Size / 500));
}
void Draw(sf::RenderTarget& rt) {
rt.draw(*Sprite);
}
void Move(float x, float y) {
Sprite->setPosition(x, y);
}
};
public ref class Garden {
public:
List <Grass^>^ Grass;
Garden() {
Grass = gcnew List<GameModel::Grass^>();
}
};
}
using namespace GameModel;
int main(){
sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
Grass^ T = gcnew Grass(25);
T->Move(25, 0);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
T->Draw(window);
window.display();
}
return 0;
}
Your render window should be drawing your sprites, not the opposite.
This should work :
window.draw(T);
I fixed the problem, turns out the function setScale wasn't really working, so I changed the member:
int Size;// I changed this
float Size;// for this
after that the function work just fine and the project work as it should

Window not opening in SFML

Basically, I am making a pong clone in c++ and sfml, and I'm using classes, which I have very little knowledge on. The problem is, I'm first of all trying to open the window and clear it in black. The files compile without errors, and run without errors, but the window just doesn't appear.
I believe it has something to do with the constructor, but again, I'm not sure. I looked at all the other questions to see if any answer my question and none of them have given me an answer. Ignore the other header files, they aren't doing anything at the moment.
game.hpp
class Game
{
public:
Game();
void run();
public:
sf::RenderWindow window;
private:
void processEvents();
void update();
void draw();
};
pong.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "game.hpp"
#include "players.hpp"
#include "ball.hpp"
Game::Game() {
sf::RenderWindow window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default);
window.setFramerateLimit(60);
}
void Game::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
void Game::draw() {
window.clear(sf::Color::Black);
window.display();
}
void Game::run() {
while (window.isOpen()) {
processEvents();
draw();
}
}
int main(int argc, char const *argv[]) {
Game game;
game.run();
return 0;
}
The window is meant to open and be black, but when the program is run, it runs fine, but the window doesn't pop up. I've been looking at it for a few hours now, have asked some people on a discord server but can't find an answer.
In your Game constructor, you are creating a local window object, that is immediately destroyed when the constructor ends.
Instead of this:
Game::Game() {
sf::RenderWindow window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default);
window.setFramerateLimit(60);
}
Do this:
Game::Game() : window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default)
{
window.setFramerateLimit(60);
}
in order to initialise the window data member with a non-default initialisation.

How to draw an object onto sfml window when a function is called?

I have a character sprite moving across the screen and when I press 'a' I want the program to draw the sword sprite onto the screen and make the sword go away when I am not pressing 'a'. Currently I have an attack function that sets a showweapon boolean as true, and a if statement that is supposed to draw the weapon onto the screen but nothing happens. Is there a better way to do this?
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Character.h"
#include "Projectile.h"
#include "Weapon.h"
using namespace std;
bool scroll = false;
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Player.png");
Weapon woodsword("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/WoodSword.png");
bool showweapon;
int main() {
// insert code here...
int windowWidth = 5000;
int windowHeight = 5000;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
sf::Texture dungeon;
dungeon.loadFromFile("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/DungeonBack.png");
sf::Sprite backround;
backround.setTexture(dungeon);
while (window.isOpen()){
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event)){
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
player.left();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
player.right();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
player.forward();
}
if (sf:: Keyboard::isKeyPressed(sf::Keyboard::Down)){
player.backward();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
player.Highspeed();
}
else{
player.Lowspeed();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
woodsword.attack();
}
window.clear(sf::Color(255, 255, 255));
window.draw(backround);
if(showweapon == true){
window.draw(woodsword.getSprite());
window.display();
cout << "Hello";
}
window.draw(player.getSprite());
window.display();
window.setFramerateLimit(70);
}
}
I am also not receiving the "hello" message
weapon.h
class Weapon : Character{
bool showweapon;
public:
sf::Texture texture;
//Constructor
Weapon(string wep)
: Character(wep){
texture.loadFromFile(wep);
showweapon = false;
}
sf::Sprite getSprite() {
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, 100, 100));
sprite.setPosition(x_pos, y_pos);
return sprite;
}
//Methods
void attack();
};
void Weapon::attack(){
showweapon = true;
}
If you need to see anything else let me know. Thanks for any help!
I changed the if statement to : if (woodsword.showweapon == true) and I also made a sheath method in the weapon class that sets showweapon false so the sword goes away when I don't press 'a'. Thanks for everyones help.

Drawing in the main window from another class with SFML

I need some help because I'm trying to make a Tetris game, as my first project in C++, but I have some problems.
I would like to keep the main window declaration in the main file, and draw on this same window from a class BoxRenderer to draw all the Tetrominos, background etc.
But my Sprite doesn't show up, I have a black screen, here's my code:
main.cpp:
// SpaceOdyssey.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include "BoxRenderer.h"
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 280), "Tetris-like by Orionss");
sf::Texture bgTexture;
if (!bgTexture.loadFromFile("sprites\\background.png"))
return EXIT_FAILURE;
BoxRenderer renderer(bgTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
window.clear();
renderer.render(window);
window.display();
}
return 0;
}
BoxRenderer.cpp:
#include "stdafx.h"
#include "BoxRenderer.h"
BoxRenderer::BoxRenderer(sf::Texture bgTexture)
{
m_bgTexture = bgTexture;
}
void BoxRenderer::render(sf::RenderWindow& win)
{
m_bgTexture.update(win);
sf::Sprite background(m_bgTexture);
win.draw(background);
}
From void sf::Texture::update(const Window &window) reference:
Update the texture from the contents of a window.
You are clearing the window just before that, and this overwrites your texture, so it's completely black. You don't want to be calling m_bgTexture.update(win); at all.

SFML update function not moving sprite

The current update function should be moving the sprite to the left 10 pixels every time it's called, but the sprite (cup1Sprite) is static and not moving. I tried using sprite.rotate(); and the sprite rotated every frame (which was expected), so something is wrong with the way I've written the update function for updating the sprite position. Can anyone suggest what's going wrong?
Code:
DrawCups.h
class DrawCups
{
public:
DrawCups(sf::RenderWindow& window);
~DrawCups();
void update();
void drawCup1();
private:
sf::RenderWindow& _window;
//Cup1
sf::Texture _cup1; // the texture which will contain our pixel data
sf::Sprite _cup1Sprite; // the sprite which will actually draw it
};
Update function in DrawCups.cpp
void DrawCups::update()
{
sf::Vector2f pos = this->_cup1Sprite.getPosition();
pos.x+=10;
this->_cup1Sprite.setPosition(pos);
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
window.setFramerateLimit(60);
DrawCups drawToWindow(window);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
drawToWindow.update();
window.clear(sf::Color::White);
drawToWindow.drawBench();
drawToWindow.drawCup1();
window.display();
}
return 0;
}