SFML won't open a window? - c++

So, as the title suggests, I am trying to create a simple Window using SFML 1.6 in CodeBlocks (MinGW v.4.7.0) on Windows 7 (no, I'm not using an ATI GPU).
This is the code:
#include <SFML/Window.hpp>
int main()
{
sf::Window App(sf::VideoMode(800, 600, 16), "SFML Window");
App.Display();
return 0;
}
Whenever I try to run this code, it just says Program.exe is not responding and has to be shutdown using Close this program. The funny thing is, the first tutorial offered on the SFML tutorial website (the one utilizing sf::Clock in a console) works, so the libraries are loaded properly.
Can someone help me find the cause of the error I'm getting?
Other than the crash, I get no compiler or application errors.

The problem is that you haven't created the main loop which polls for events and handles OS messages. Append this to main() (yes, this is a snippet from the SFML documentation):
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Clear the screen
App.Clear();
// Put your update and rendering code here...
// Update the window
App.Display();
}
It is therefore not necessary for you to call App.Display() after you create the window.

For those who want the whole thing, this is a snippet extracted from the SFML website.
#include <SFML/Window.hpp>
int main()
{
sf::Window window(sf::VideoMode(800, 600), "SFML 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();
}
}
return 0;
}
You will get:

Related

SFML render window not appearing when running program

When attempting to run a program that is intended to bring forth a render window generated through the SFML library, it will successfully compile but refuse to actually launch a window. After several hours of debugging, I am unable to find where I may have potentially gone wrong in the code, and am now beginning to think it is a problem through my IDE (CLion). Nevertheless, the code used for the render window will be provided.
int main (int argc, const char * argv[])
{
// Create the main window
sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(800, 600), "SFML window");
#ifdef SHAPE
sf::Shape s = sf::Shape::Rectangle(0, 0, 100, 100, sf::Color::Blue);
#else
sf::Image img; img.create(100, 100, sf::Color::Blue);
sf::Texture t; t.loadFromImage(img);
sf::Sprite s(t);
#endif
// Start the game loop
while (window->isOpen())
{
// Process events
sf::Event event;
while (window->pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window->close();
if (event.type == sf::Event::KeyPressed)
if (event.key.code == sf::Keyboard::Escape)
window->close();
else {
delete window;
window = new sf::RenderWindow(sf::VideoMode(800, 600), "SFML window");
}
}
// Clear screen
window->clear();
s.rotate(30.0);
window->draw(s);
// Update the window
window->display();
}
delete window;
return EXIT_SUCCESS;
}
#endif
One of the most common issue with CLion users and launching an application is that they forget to provide the necessary SFML DLLs, so the application never starts but closes with a return value 0xC0000135.
If you launch the executable directly via explorer, you'll get the more well-known message box which tells you which DLL exactly is missing.
The solution is to copy the necessary DLLs next to your executable in the CLion's output directory.

SFML RenderWindow taking inconsistent time to display

I started a project a few days ago to get to grips with SFML. When I was working on it then, the main sf::RenderWindow constructor would be called instantly. Now that I've come back to the project, it's taking up to 42 seconds in Debug mode to continue beyond the sf::RenderWindow constructor function.
So whenever I run the console appears and 40 seconds later the RenderWindow will appear.
It can't be my computer specifications as I have a very high end PC.
Here's the code in my main function:
int main()
{
sf::RenderWindow window(sf::VideoMode(960, 640), "proj v0.0.1");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear(sf::Color::Black);
window.display();
}
return 0;
}
I'm honestly just very confused as to why there's such a long delay and it's making testing new features very difficult.

Getting an EXC_BAD_ACCES error running app template code

Preliminary Details:
OS X 10.11
Xcode 7.2
SFML 2.3.2
Clang C++11
Default SFML App template
Problem:
I have just done a fresh install of SFML, I double checked and triple checked all of the required files are in the correct places.
When I build and run the stock, SFML App template for Xcode, I get a EXC_BAD_ACCESS on line 81 of this file (Line 81: window.draw(sprite); - also commented clearly):
//
// Disclamer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resource, use the helper
// method resourcePath() from ResourcePath.hpp
//
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main(int, char const**)
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
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.setColor(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 (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite); // -------------- LINE 81
// Draw the string
window.draw(text);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
What I've Tried
I have searched online (including SO posts), the common cause seems to be some kind of stack overflow. But I can't see what would cause a stack overflow in the above program with my knowledge.
If you look for this kind of error of SFML's forums, you'll see that it was always due to some old header/binary files being present on the system and being used instead of the newer ones.
Make sure that you removed all versions of SFML before installing the latest one. As per Installing SFML tutorial, you need to look in:
/usr/local/include
/usr/local/lib
/Library/Frameworks

SFML Window Not Rendering Shape

#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
This is directly from the tutorial's page, and I've checked and rechecked, so I know this is not a syntax or linker error.
When I run this program, I get a window whose contents are the same as whatever the place it is located in. Yet, if I remove the window.draw(shape) command, I see a black window, like I should.
I'm compiling on Windows 7 (32 bit) using mingw32-g++.exe (4.7.1). Oh, and it's the same if I compile debug or release and static or dynamic, so that's not the problem either.
Your code is correct, but to me it looks like you need to set the position of your circle. I maybe wrong here but the position could be any value and that is why it isn't displaying.
Could you try updating your code to this please.
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
shape.setPosition(50f, 50f);
shape.setScale(2,2); // You can remove this line if you want to, I just put it there for debugging
while (window.isOpen())
{
...
Here is the SFML documentation for setPosition (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php#a4dbfb1a7c80688b0b4c477d706550208)
Here is the SFML documentation for setScale (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php#aaec50b46b3f41b054763304d1e727471)
Let me know if that fixes your problem.

C++/SFML Window creation fail

Well, I tried to do everything as shown in tutorial, but it just shows console and nothing more. Tried this clock program and it works just fine. I connected all libs, and copied all .dll files, so really don't know where I'm wrong. Please tell me what to do to show make it show window. I'm using VS2010, SFML 1.6 and here's my code.
#include <SFML\Window.hpp>
int main()
{
sf::Window App(sf::VideoMode(640, 480, 32), "wut");
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Window closed
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key pressed
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
};
App.Display();
}
};
Try this:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Clear the screen (fill it with black color)
App.Clear();
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
Do you link against sfml-main library? If not, try that, if that fails, try to do a WinMain instead of main() function. Also, make sure that Visual Studio has not set your project to a console program.
I have been using SFML a lot recently. Try these suggestions in order, keeping each previous change:
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
It should be sf::RenderWindow, not sf::Window
It should be while (App.isOpen()) not IsOpened()
If it doesnt seem to have the function "isOpen" on your RenderWindow , you probably don't have SFML2, which I recommend getting immediately.
Did you rebuild the libraries for your computer, or did you just try and use the .dll's they provide? You may need to rebuild them using Cmake, and whatever your compiler is. I know I did.
Finally, I also recommend using the latest Code::Blocks as your IDE instead, but I guess only switch as a last resort.
Old question.. but here is my answer: When you sayed; "I'm using VS2010, SFML 1.6...", in the download page says that for sfml-1.6 in Windows, it is possible to use VScode 2005 and VScode 2009. Try to use a different version of Vscode (and link everything to test) and see if it works.
(For me your code looks fine for SFML-1.6, so i don't think that's the problem)