When I resize my sfml window, when I cut resize to make it smaller and resize to make it larger, it gives you a really weird effect.
How do I make the resizing more prettier? The code is from the installation tutorial for code::blocks.
Code (same as the code in the installation tutorial for code::blocks on the sfml website):
#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;
}
You need to manage the resize of the window. Otherwise the coordinates are wrong. Here is an excerpt of your code with the solution. Credits go to the author of this forum post, this is where I once found it when I was looking for a solution: https://en.sfml-dev.org/forums/index.php?topic=17747.0
Additionally you can set the new coordinates based on the new size. The link gives you more information.
// create own view
sf::View view = window.getDefaultView();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized) {
// resize my view
view.setSize({
static_cast<float>(event.size.width),
static_cast<float>(event.size.height)
});
window.setView(view);
// and align shape
}
}
Related
I'm trying to learn SFML, when I try a simple program it works.
The problem I struggle with is loading an image, it shows me an error that I don't know how to deal with
I did all by different tutorials but every time the same problem, I've located the image in the same folder
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "Sprites!");
sf::Texture playerTex;
playerTex.loadFromFile("tiles.png");
sf::Sprite playerSprite;
playerSprite.setTexture(playerTex);
playerSprite.setScale(1.5, 1.5);
playerSprite.setPosition(100, 100);
playerSprite.setOrigin(32, 32);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(playerSprite);
window.display();
}
}
Error is:Exception thrown at 0x70C13727 (vcruntime140.dll) in SFML-0.exe: 0xC0000005: Access violation reading location 0x00400000.
I am working on a basic game in C++ using SFML for graphics. It is designed to use a grid system with functions to determine what should be displayed in each square. However, the compiler won't recognize the references to the window in the functions.
To keep it easier to expand there are functions for each type of terrain to be displayed, taking the coordinates as inputs (possibly not the correct term).
Good Question. SFML recommends that you create your window in the main function, but, if you want to modify it, you can simply pass it by reference. EX:
#include <SFML/Graphics.hpp>
void doSomething(sf::RenderWindow& window) {
sf::RectangleShape shape(sf::Vector2f(100, 100));
shape.setFillColor(sf::Color::Green);
shape.setPosition(50, 50);
window.draw(shape);
window.display();
}
int main() {
sf::RenderWindow window(sf::VideoMode(500, 500), "Test", sf::Style::Close);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
//draw here
doSomething(window);
}
return 0;
}
You should take a look at https://www.sfml-dev.org/tutorials/2.5/. Specifically I would look at https://www.sfml-dev.org/tutorials/2.5/window-window.php
I am feeling really frustrated with draw() not working in my SFML project. My compiler gives off no errors, my eyes doesn't catch a thing that's off (as a reference I am using official tutorial). The problem is that when window load it doesn't draw a thing. It just stays a white window without any text in it.
Where could be the problem?
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800,600), "Trying to make a game");
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
//error
}
sf::Text text;
text.setFont(font);
text.setString("Hello, World!");
text.setCharacterSize(50);
text.setColor(sf::Color::Red);
text.setPosition(10, 50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
window.clear();
window.draw(text);
window.display();
return 0;
}
You're closing the window before drawing the text...
#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.
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).