I am coding a game , and just started some of the graphical part so I just started to try using SFML for it , but I had a bug where a sprite would not show on screen, so I tought that ther was bugs deep into my code , and so I made a fresh project with what could be considered like the most basic code to show a Sprite , but it is doing the same thing , and I can't understand why...
Here is the basic code(even if you could guess it x) ):
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
int main(){
RenderWindow window(VideoMode(500,500),"Test");
Texture lavaFlow;
lavaFlow.loadFromFile("lavaFlow.png");
Sprite lavaFlowSprite;
lavaFlowSprite.setTexture(lavaFlow);
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
if(event.type == Event::Closed){
window.close();
}
window.clear();
window.draw(lavaFlowSprite);
window.display();
}
}
return 0;
}
And few things to know , since I am compiling on Debug , some of the error shows , so I know that the Image has been loaded , and the image was firts in jpeg so I thank that it was maybe a type problem , so I converted it into png
Thanks to anyone who could help me !
Try setTexture(LavaFlow, true)
That will reset the texture rect. If you never set the texture rect, nothing will be "cut out" from the texture.
window.clear();
window.draw(lavaFlowSprite);
window.display();
This stuff should not be in the event loop ;)
Hi there, I have almost the same problem with @traxys, so I don't create new topic, hope anybody help me to solve it :)
This is my code:
#include <SFML/Graphics.hpp>
#include <sstream>
#include <iostream>
#define WND_WIDTH 400 // Window Width
#define WND_HEIGHT 400 // Window Height
#define S_WIDTH 100 // Square Width
#define S_HEIGHT 100 // Square Height
#define SPEED 10
int main(void)
{
sf::RenderTexture rtexture;
rtexture.create(S_WIDTH,S_HEIGHT);
rtexture.setSmooth(true);
sf::Font font;
if(!font.loadFromFile("arial.ttf")) std::cout << "Error ! Can't load font file [arial.ttf]";
sf::Text text;
text.setFont(font);
text.setColor(sf::Color::Red);
text.setString("1234");
text.setCharacterSize(30);
text.setOrigin((text.getLocalBounds().width+text.getLocalBounds().left)/2,(text.getLocalBounds().height+text.getLocalBounds().top)/2);
text.setPosition(S_WIDTH/2.0,S_HEIGHT/2.0);
sf::RectangleShape square;
square.setOrigin(S_WIDTH/2,S_HEIGHT/2);
square.setOutlineThickness(2);
square.setPosition(S_WIDTH/2,S_HEIGHT/2);
square.setSize(sf::Vector2f(S_WIDTH,S_HEIGHT));
square.setFillColor(sf::Color::Black);
sf::RenderWindow window(sf::VideoMode(WND_WIDTH,WND_HEIGHT),"Testing");
window.setFramerateLimit(60);
// this is the problem
sf::RectangleShape rect;
rect.setTexture(&rtexture.getTexture(),true);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) window.close();
}
window.clear(sf::Color::Black);
//window.draw(rect);
window.draw(square);
window.draw(text);
window.display();
}
}
Humm... I've created my texture had square and text in the middle of it by using rendertexture. But I can't getTexture() to use even though I can draw on main window ( like the code above ). When comment
window.draw(square);
window.draw(text);
and uncomment window.draw(rect), nothing on the screen, why ?