Window not opening in SFML - c++

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.

Related

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

Why does SFML window not show when window is in a class?

I am trying to create a Screen class for SFML, however for some reason, the application works when using the Xcode example but as soon as I put the window into it's own class, it does not work. Why is this and how would I fix it?
Here is my code (adapted form the example):
Edit:
After reading the comments, I have changed to the following code. This still does not show a screen and the program still quits.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
class Screen{
public:
sf::RenderWindow window;
Screen(){
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
}
};
int main(int, char const**)
{
Screen* screen = new Screen();
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
screen->window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sf::Sprite sprite(texture);
// Create a graphical text to display
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return EXIT_FAILURE;
}
sf::Text text("Hello SFML", font, 50);
text.setFillColor(sf::Color::Black);
// Load a music to play
sf::Music music;
if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
return EXIT_FAILURE;
}
// Play the music
music.play();
// Start the game loop
while (screen->window.isOpen())
{
// Process events
sf::Event event;
while (screen->window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
screen->window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
screen->window.close();
}
}
// Clear screen
screen->window.clear();
// Draw the sprite
screen->window.draw(sprite);
// Draw the string
screen->window.draw(text);
// Update the window
screen->window.display();
}
return EXIT_SUCCESS;
}
You have a logic error in your constructor. The way you created it, you leave Screen::window uninitialized, and create another RenderWindow object which becomes inaccessible as soon as execution leaves constructor.
Instead, you should do
class Screen{
public:
sf::RenderWindow window;
Screen():
window(sf::VideoMode(800, 600), "SFML window") {}
};
Everything else should work as expected, unless it does not have any other bugs. If you're curious about the syntax I've used, you can visit this link.
Hard to say. Maybe your accessing a memory address that isn't part of your program? Try using unique pointers and make_unique to define some of your pointers.
For example:
std::unique_ptr<sf::RenderWindow> window;
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT), "PushBlox",sf::Style::Default);
I did make a project using sfml before, so feel free to check out my code here --> https://github.com/FromAlaska/ComputerScience/tree/2017/Projects/Frontier

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.

Opening SFML window gives memory read error

I'm trying to open a SFML window, but every time it is launched it says "Access violation reading location: 0xCCCCCCC0." The error is occuring in the init() method. Relevant code:
class AirportGame {
private:
sf::RenderWindow window;
public:
void init();
int run();
/
void AirportGame::init() {
window.create(sf::VideoMode(800, 600), "SFML window");
}
int AirportGame::run() {
init();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
return 0;
}
int main() {
AirportGame* app = new AirportGame();
return app->run();
}
It happens sometime after init, because the actual window is open. There is no mention in the debugger of 0xCCCCCC0.
Fixed it!
Turns out under the C++ pre-processor I set the definition to SFML_STATIC instead of SFML_DYNAMIC
Set window to a
RenderWindow *window;
and create it with
window = new sf::RenderWindow( /*your stuff or default initialize*/ );
and then call
window->create( /*your settings*/ );
if you didn't already initialize it.
From then on just access window using '->' instead of '.'