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.
Related
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
}
}
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.
I'm trying to learn C++. I'm trying to get into GUI-programming with the frame work SFML (based on OpenGL) and TGUI. When I run my basic program (just window initialisation and creating a button) in Visual Studio (debug mode), it throws the exception: Access violation reading location 0xCCCCCCCC. I found out, that 0xCCCCCCCC is a specific memory pattern and means I am doing stuff with uninitialised memory on the stack. But where am I doing this in my program? My Code:
#include <TGUI/Gui.hpp>
#include <TGUI\TGUI.hpp>
#include <TGUI\Widgets\Button.hpp>
using namespace std;
int main() {
tgui::Button::Ptr button = tgui::Button::create("Test"); //HERE THE EXCEPTION IS THROWN
sf::RenderWindow window(sf::VideoMode(800, 600), "Fenster");
tgui::Gui gui(window);
gui.add(button, "Hallo");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// When the window is closed, the application ends
if (event.type == sf::Event::Closed)
{
window.close();
} else if (event.type == sf::Event::Resized){
window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
gui.setView(window.getView());
}
// Pass the event to all the widgets
gui.handleEvent(event);
}
window.clear();
// Draw all created widgets
gui.draw();
window.display();
}
delete button;
button = NULL;
return EXIT_SUCCESS;
}
Any help would be nice. Thanks!
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).
I have the following code:
#include <SFML\Graphics.hpp>
#include <iostream>
int main(int argc, char* argv[])
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Render");
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
image.loadFromFile("D:/Project/Sprites/bt1.png");
texture.loadFromImage(image);
sprite.setTexture(texture);
sprite.setPosition(100.0f, 100.0f);
sf::Event event;
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}
It's very simple, but it didn't work.
I tried using different kinds of paths:
D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp
D:\\Project\\CPP\\Game_Engine\\Debug\\sprites\\first.bmp
d:\\Project\\CPP\\Game_Engine\\Debug\\sprites\\first.bmp
Then I tried using different files:
D:/Project/Sprites/bt.png
D:/Project/Sprites/anim.bmp
D:/Project/Sprites/boy.jpg
Compiler indicates at the following line:
image.loadFromFile("D:/Project/Sprites/bt1.png");
More precisely, Program crashes on this line.
My configuration is the following:
Error/crash message is the following:
Необработанное исключение по адресу 0x5007DEF8 (msvcr110.dll) в
SFML_ERROR.exe: 0xC0000005: нарушение прав доступа при чтении по
адресу 0x03BC1000.
Translation is the following:
Unhandled exception at 0x5007DEF8 (msvcr110.dll) in
SFML_ERROR.exe: 0xC0000005: Access violation reading on
Address 0x03BC1000.
My problem is mixed Debug/Release, I used sfml-window.lib, but I have to use `sfml-window-d.lib'. I can't use the debug SFML library because I am using VC++ 2013 (v120, but SFML requires v110). So, I recompiled the official library and it worked!