SFML Crash using Clear function - c++

I'm new to SFML and am currently reading through the documentation. For some reason, however, my "program" crashes whenever I call the Clear() function. My program code is as follows:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <iostream>
void HandleEvents(sf::RenderWindow &App)
{
sf::Event Event;
while (App.GetEvent(Event)) {
if (Event.Type == sf::Event::Closed) {
App.Close();
exit(0);
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) {
App.Close();
exit(0);
}
}
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 640, 32), "SFML Graphics");
App.SetFramerateLimit(60); // cap the framerate at 60 FPS
while (App.IsOpened()) {
HandleEvents(App); // handle events... duh
App.Clear(); // When I remove this line, the program doesn't crash....
App.Display();
}
return EXIT_SUCCESS;
}
So do any SFML gurus have advice for me? I've tried researching this problem but the only other person I've seen who tried to ask this had a broken link (because of the new SFML forums).
Thanks.

Related

C++ SFML Font loading problems

I am having problems with loading my miranda.ttf file. I have the file in the same directory as main.cc and I am sure I am typing it the right way, but it still doesn't work for some reason. I have tried multiple other fonts without luck.
#include <SFML/Graphics.hpp>
#include "core/headers/util.hpp"
#include "core/headers/ui.hpp"
#include <time.h>
int main(void) {
srand((unsigned)time(NULL));
sf::RenderWindow window(sf::VideoMode(1000, 600, 32U), L"Kolonie Mravenců", sf::Style::Titlebar | sf::Style::Close);
sf::CircleShape circle;
circle.setFillColor(sf::Color::Black);
circle.setPosition(10, 10);
circle.setRadius(6);
sf::Font font;
if (!font.loadFromFile("miranda.ttf")){
return EXIT_FAILURE;
}
sf::Text text;
text.setFont(font);
text.setPosition(sf::Vector2<float>(200.0f, 600.0f / 2.0f));
text.setFillColor(sf::Color(0, 0, 0));
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(227, 227, 227));
window.draw(text);
window.display();
}
return 0;
}
This is the code of main.cc.
I am executing the code using terminal in VS Code Insiders on a Macbook Air.

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>

SFML window.draw(); only shows up for a small time

I'm attempting to get a picture to display by using SFML (just a test run). The program can find the picture, and open a new window, but when it opens the window it only pops up for half a second then returns with 1. Here is the code (which is just their example that I tweaked):
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::Texture Texture;
sf::Sprite Sprite;
if(!Texture.loadFromFile("resources/pepe.png"));
return 1;
Sprite.setTexture(Texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
I am assuming the error is coming from the return 1; after the loading, but I don't see what is wrong. Can someone post something that worked for them or give me tips on what may be going wrong?
Your code works just fine, except for the ; after the texture loading from a file, making your program always return 1, whatever was happening before.
It's a good idea to add error messages to know what's going wrong.
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::Texture Texture;
sf::Sprite Sprite;
if(!Texture.loadFromFile("resources/pepe.png")){ // there was a ; here.
// making the code below always run.
std::cerr << "Error loading my texture" << std::endl;
return 1;
}
Sprite.setTexture(Texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
window.close();
}
// you only get here when there is at least one event.
}
// but you always want to display to the screen.
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
My rule of thumb is to always enclose code blocks with curly braces so you never make these kind of mistakes (or someone else changing your code is less prone to make that mistake).

C++ sfml receives close event just after window opens

I am trying to follow a SFML tutorial from this link, however, for some reason my application seems to be caling the close event as soon as my program enters into the event loop.
Here is my code.
#include <SFML/Graphics.hpp>
#include <iostream>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main()
{
sf::RenderWindow Window(sf::VideoMode(500,400),"SFML tutorial");
/* Or we can do this
* sf::RenderWindow Window
* Window.create (sf::VideoMode(800,600),"SFML tutorial");
*/
//Game loop
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event));
{
if(Event.type == sf::Event::Closed)
{
Window.close();
std::cout << "Close" << std::endl;
}
}
}
return 0;
}
For some reason as soon as my program enters the event loop it receives a sf::Event::Closed event. I have done some research and this is not because the window is too big for my screen.
To fix my issue I have had to ad a int variable canClose to 0 then modify the event loop to this.
if(Event.type == sf::Event::Closed)
{
if(canClose == 1)
{
Window.close();
} else {
canClose = 1;
}
}
After I do this the window now opens and runs as expected.
P.S this is made using xcode and the SFML template.
Edit:
Just made a new xcode SFML project and removed some code in it to make a empty window and it seems to whork. Code looks exactly the same. This seams strange because it is exactly the same code.
From what i have seen, do not use an if statement for checking events, use a switch statement. Also Use a while(window.isOpen()) when trying to poll events
Have you read through the tutorials on http://www.sfml-dev.org ? Because i can guarantee that it is the best source for learning sfml. Use it before using another source.
Your problem is that you declared the variable Event Event type, you have to change the name of the variable to lowercase.
in this way
#include <SFML/Graphics.hpp>
#include <iostream>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main()
{
sf::RenderWindow Window(sf::VideoMode(500,400),"SFML tutorial");
/* Or we can do this
* sf::RenderWindow Window
* Window.create (sf::VideoMode(800,600),"SFML tutorial");
*/
//Game loop
while(Window.isOpen())
{
sf::Event event;
while(Window.pollEvent(event));
{
if(event.type == sf::Event::Closed)
{
Window.close();
std::cout << "Close" << std::endl;
}
}
}
return 0;
that way you will not have any problems

Control RenderWindow from another function

this may sound as a really simple question, but I have SFML 1.6 and I want to control my RenderWindow from another function... I'm not very good at C++ or SFML... :)
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Guessing Game");
App.SetFramerateLimit(60); //Set FPS limit to 60 FPS
//Variables
bool atmainmenu = true;
//End Variables
// Start main loop
while (App.IsOpened())
{
printmessage(atmainmenu);
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::M)){
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F)){
}
}
// Display window on screen
App.Display();
}
return EXIT_SUCCESS;
}
void printmessage(bool atmainmenu)
{
if(atmainmenu){
//$$$$$$$################# HERE <----
}
}
That is my code, but I want to control 'App' from atmainmenu. Is there anything I have to do? thanks
waco001
Pass your renderwindow as a parameter to the function like so:
void printmessage(bool thing, sf::RenderWindow& app)
{
app.doSomething();
}
Dont forget to the window as a reference&
Then call the function in main
printmessage(atmainmenu,app);