error for "Texture" sf not loading for cpp framework called sfml - c++

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>

Related

SFML cannot load image file no matter what I try

I am trying a simple SFML project and it is giving me an error saying that it cannot load the image file. Here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int main() {
RenderWindow window(VideoMode(600, 600), "Chess");
system("dir");
Texture texture;
texture.loadFromFile("board.png");
Sprite s(texture);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//draw
window.clear();
window.draw(s);
window.display();
}
}
I have tried every guide and fix for this problem I could find and not a single one of them has worked. Would anyone be able to help me? And yes, my image files are in the project directory. If it helps, I am using VS 2013.

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 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.

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).

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: