C++/SFML Window creation fail - c++

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)

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.

error for "Texture" sf not loading for cpp framework called sfml

I am trying to load my texture onto my window.... however when I compile in visual studio on windows 10 laptop It says "sf has no member texture" I'm honestly not sure what on earth is going on! for whatever reason its not loading the sf! if you have any ideas please let me know... thanks so much it means a great deal to me! (I have also tried to use "#include" and it has not worked out for me unfortunately)
#include <SFML/Window.hpp>
int main()
{
sf::Texture texture;
if (!texture.loadFromFile("sheet_1.png"))
{
// error...
}
sf::Window window(sf::VideoMode(800, 600), "My window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
return 0;
}
Since sf::Texture is not included in Window.hpp you need to also include Graphics.hpp in your project.
#include <SFML/Graphics.hpp>

sfml crashes while drawing text

I am using codeblocks with gcc compiler 5.1.0 with sfml 2.5.0.
Sfml works perfectly fine while drawing shapes but when i try to display my text the compiled program crashes.
I tried different .ttf fonts but none of them worked.
#include <SFML\Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow win(sf::VideoMode(640, 480), "SFML Test");
Font font;
if(!font.loadFromFile("arialbd.ttf"))
return(-2);
Text text;
text.setFont(font);
text.setString("raptor inc");
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
text.setPosition(100,100);
int i=0,j=0;
while (win.isOpen())
{
sf::Event event;
while (win.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
win.close();
}
}
win.clear();
win.draw(text);
win.display();
}
return 0;
}
I did not expect any error while executing the code but the program crashed.
The program is crashing at.
win.draw(text);
the code is working. and i doubt it's a font loading problem, since if it were, you should get an error on this line:
if(!font.loadFromFile("arialbd.ttf"))
return(-2);
have you tried updating SFML? even if for testing purposes, because it seems to be a bug.

Developing SFML framework

I created an exemple in which i have 2 windows opened at the same time and window1 cant handle events while window2 is open. I changed the current window's color to ilustrate that its working(while window1 is open if i move the mouse, window2 gets greener and greener). But even though window1 cant handle events while window 2 is open, i cant still change window1 position. I want to be unable to do that.
#include <SFML/OpenGL.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
sf::Event event;
int main()
{
sf::RenderWindow window1(sf::VideoMode(600, 600), "Window1");
sf::RenderWindow window2(sf::VideoMode(300, 300), "Window2");
int r=0,g=0;
while (window1.isOpen())
{
if(window2.isOpen()==false)
while (window1.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window1.close();
r=(r+1)%255;
}
else
{
while (window2.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window2.close();
g=(g+1)%255;
}
}
window1.clear(sf::Color(r,0,0));
window1.display();
window2.clear(sf::Color(0,g,0));
window2.display();
}
return 0;
}
How do i do that?
There isn't direct functionality in SFML for this (although I expect it's possible using platform specific code), but there are a couple of work arounds:
Change the window style to sf::Style::None. This is quick and easy, but has the effect of removing the title bar and window border, which you may not want.
Brute force the window position, either by manually setting the position each render, or checking for a position change and reversing it.

SFML won't open a window?

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: