Drawing in the main window from another class with SFML - c++

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.

Related

SFML Touch position is not correct

I am testing a touch in sfml on android using app called cxx droid. My code set's the RectangleShape where i press. But even when i make the position relative to window its not relative to it. I touch in place shape's pos in another place!
My Code :
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
Font font;
int main() {
font.loadFromFile("font.otf");
RenderWindow window(VideoMode(500,700),"My window",Style::Titlebar | Style::Close);
window.setFramerateLimit(60);
RectangleShape shape(Vector2f(300,40));
while(window.isOpen()) {
window.setView(window.getDefaultView());
Event event;
while(window.pollEvent(event)) {
if(event.type == event.Closed) {
window.close();
}
}
if(Touch::isDown(0)) {
shape.setPosition(Touch::getPosition(0,window).x,Touch::getPosition(0,window).y);
}
window.clear(Color::Green);
window.draw(shape);
window.display();
}
return 0;
}
If the top left corner of the shape is at the position you touch on screen then it is correct but you need to set the origin of the shape to the center of the shape.
a few weeks i had the samd problem
Your Rectangle needs a FloatRect
sf::FloatRect shapeRect = shape.getGlobalBounds;
Then you have to use mapPixelToCoords in the while(window.isOpen()); loop
sf::Vector2i pixelPos = sf::Touch::getPosition(0, window);
sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);
Then you use both things with contains
if(shapeRect.contains(worlPos))
{
// type your stuff in
}
Now the Touch have to work only in the Shape, hope this help you

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

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);
}
};

Can't load image with SFML

Am trying to load an image with SFML using Xcode, but everytime I run the program, the window (which has been created using the code) flashes and disappears...
Here's the code I am using:
#include <SFML/Graphics.hpp>
#include <iostream>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 1024
int main()
{
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Orsome Game");
while(window.isOpen())
{
sf::Event e;
while(window.pollEvent(e))
{
switch (e.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
sf::Image image;
if(!image.loadFromFile("sprite.png")){
return -1;
}
window.clear(sf::Color(255,255,255));
window.display();
}
return EXIT_SUCCESS;
}
Have also put the image file where the c++ file is, but it still doesn't work!
I would put an sf::Style::Default in the RenderWindow arguments, straight after "Orsome Game" window name. Because you are using the pollEvent, that would allow you to exit the window by pressing the x button in the top right corner, like a default application layout.
Is the picture in the same directory as your solution file? Or your main.cpp?
It should be, of course.
And, maybe, try loading your image with Texture and Sprite, like this:
sf::Texture texture;
texture.loadFromFile("picturename.png");
sf::Sprite sprite;
sprite.setTexture(texture);
inside the while window is open loop.
To draw it:
// after window.clear() ...
//... your code
window.draw(sprite);
//... other things to draw
// and then, your window.display();
But, probably, your sf::Image should also work. Tell me how it goes
EDIT:
Try this code and see if the window opens.
Otherwise, that might be the problem of setting up the SFML on your computer and you will have to find some good tutorial on how to set it up properly! But let's see:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(640, 640), "Test", Style::Default);
while (window.isOpen()) {
Event _event;
while (window.pollEvent(_event)) {
switch (_event.type) {
case Event::Closed:
window.close();
break;
}
}
window.clear();
// draw
// ...
window.display();
}
return 0;
}

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.

Visual Studio error in SFML: <Information not available, no symbols loaded for sfml-audio-d-2.dll>

When I run the code, everything works except that the sound does not play. The link is fine, when I put a breakpoint and look at the sound object, it displays . I have added sfml-audio-d.lib and sfml-audio.lib in the right places. Any suggestions welcome.
#include <SFML/Graphics.hpp>
#include "SFML/Audio.hpp"
int main() {
sf::RenderWindow window(sf::VideoMode(600, 600), "SFML Application");
sf::SoundBuffer buffer;
buffer.loadFromFile("magicsound.mp3");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
//
sf::Sprite background;
sf::Texture texture;
texture.loadFromFile("crash.jpg");
background.setTexture(texture);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleAre(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleAre)); //background stays default size
//window can be resized without affecting background
}
}
window.clear();
window.draw(background);
window.display();
}
}
When you use quotes for including, ide looks for the library in your project's directory. If your project directory have own sfml library in itself I dont know; but it is like a typo to me.
So try this:
"SFML/Audio.hpp" --> <SFML/Audio.hpp>
and result is:
#include <SFML/Audio.hpp>