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.
Related
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;
}
I just started learning sfml and whenever I run the following code and try to move the window it crashes:
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(1920 , 1080), "Window", Style::Close | Style::Titlebar | Style::Resize);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
switch(event.type)
{
case event.Closed:
window.close();
break;
case event.Resized:
std::cout << "New Window Width:" << event.size.width <<std::endl;
std::cout << "New Window Height:"<< event.size.height<<std::endl<<std::endl;
break;
}
}
window.display();
}
return 0;
}
When I remove window.display() from the code I can move the window without crashing it.
I am using Codeblocks 16.01 and SFML 2.4.2
Any ideas on why that happens?
Hellow, did you try to clear the window before displaying it like this:
window.clear();
And if it doesn't work, try to draw something before display nothing like a sf::Sprite for example.
Another thing that may causes that, is the size of your window. Because your not in full-screen mode so a size of 1920x1080 is too big because a external frame will be created by the os and may be it draws out the screen... but that would be really strange...
PS: prefer to use sf::Event::Closed for your switch/case
so today I started looking into SFML and I found it quite interesting so decided to learn how to play with it, but I am already hitting some problems, I am trying to use textEntered event, but it is not working properly, it shows complete nonsense and text writes itself even without me pressing any key. Heres link
Code
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
std::string display;
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(30);
text.setStyle(sf::Text::Bold);
text.setColor(sf::Color::Red);
text.setPosition(50, 50);
while (window.isOpen())
{
sf::Event Revent;
while (window.pollEvent(Revent))
{
if (sf::Event::TextEntered)
{
std::cout << static_cast<char>(Revent.text.unicode);
//text.setString(display);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
window.clear();
//window.draw(text);
window.display();
}
return 0;
}
You wrote if (sf::Event::TextEntered), which evaluates to true (since it's not equal to 0).
You probably meant
if (Revent.type == sf::Event::TextEntered).
Using Revent.text is undefined behavior in this case (when you're not sure what type of event Revent contains) because sf::Event is an union, so only one of its members is usable at a time. You can read more about SFML events here.
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:
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)