SFML - RenderWindow Runtime Errors with EVERYTHING - c++

For almost all of any object I've added so far (sf::Color, sf::Image, sf::Texture, etc.) to the sample code I grabbed from the SFML site, exceptions get thrown once I run it, to the accord of:
Unhandled exception at 0x61C71B86 (sfml-system-2.dll) in gameboiss.exe: 0xC0000005: Access violation reading location 0x00000074.
It's brought up in reference to the RenderWindow line for whatever reason. If I remove all lines referencing these objects, it works fines. Even if I leave only one line creating the variable (i.e. sf::Texture texture), the exception is still thrown. I've tried multiple things amongst looking at the locals/autos at debug and it doesn't seem to give much light.
I'm working on Visual Studio 2012, any help would be appreiciated. Below is the code. thanks :)
#include <iostream>
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "SFML/System.hpp"
int main(){
sf::RenderWindow window(sf::VideoMode(200,200), "game boi");
sf::CircleShape shape(100.f);
//source of error
sf::Texture texture;
texture.loadFromFile("char\\spartapix.png");
/*if(!hero.loadFromFile("char\\spartapix.png")){
std::cerr << "Error: sprite not loaded.\n";
return 1;
}*/
//sf::Image background;
//if (!background.loadFromFile("background.jpg"))
//return -1;
shape.setFillColor(sf::Color::Blue);
sf::Vector2i pos;
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::GainedFocus)
std::cout << "playing";
if (event.type == sf::Event::LostFocus)
std::cout << "notplaying\n";
if (event.type == sf::Event::MouseButtonPressed){
sf::Vector2i pos = sf::Mouse::getPosition(window);
std::cout << pos.x << " " << pos.y << std::endl;
}
if (event.type == sf::Event::KeyPressed){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
<code>

I agree with RetiredNinja's comment above: If it happens as soon as you create the render window, it's most likely some STL version difference (from the passed string; since sf::String is part of sfml-system), which is typically the case when you mix up versions (Visual Studio releases or Release/Debug builds).
Are you sure you've picked the correct download from SFML's download page? As an alternative, you could try downloading and compiling SFML from the official repository.

Related

Visual Studio doesn't want me to code or something (Deleting debug)

So I was checking the files for my C++ game, and I see the .exe file already created. I coded a bit to see if it works, and when I debugged it, it said the .exe file was not found. And indeed it was not there. Did I do something wrong? Here is the code
#include <iostream>
#include <SFMl\Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(320, 480), "The Game!");
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
window.clear(Color::White);
window.display();
}
return 0;
}
I tested your snippet and it worked fine.
Try to turn off your antivirus software, or test it on other devices.

Problem with SMFL Syntax in C++ Visual Studio importing png

-I am new to C++ and SFML and want to import a png file.
-It worked a view times, but afterwards I got the
message "Build failed, run last success?" most of the time. Sometimes its still working.
-there is no "real" error, so its hard to figure out, what the problem is
-I read earlier, that switching from Debug to Release-Mode could be a reason, but it didnt helped
its working when I dont use:
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
-> but, ofcourse, the sprite is missing then.
I would be happy to have a solution/reason for this or a topic I missed so far to read/ learn about.
Im happy about advices.
Thanks so far.
Alex
VISUAL STUDIO 2019
x64
SFML-2.5.1
WHOLE CODE:
#include"SFML/Graphics.hpp"
#include<iostream>
#include"main.h"
int main(int argc, char** argv[])
{
sf::RenderWindow window(sf::VideoMode(1200,800), "bimWindow");
sf::RectangleShape rs(sf::Vector2f(1000, 700));
rs.setFillColor(sf::Color::Green);
sf::Event event;
sf::Texture texture;
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(100,100);
//sprite.scale(sf::Vector2f(3, 3));
rs.setPosition(80, 80);
// run the program as long as the window is open:
while (window.isOpen())
{
//let window open i guess.
while (window.pollEvent(event)); //stay true as long aas it didnt happen or so
{
// "close requested" event:close the window
if(event.type == sf::Event::Closed)
window.close();
}
//RENDER:
window.clear();
window.draw(rs);
window.draw(sprite);
window.display();
}
return 0;
}
Try making sure that the "assets/player.png" file is inside your solution directory (the folder with the .sln file.) Your code seems to run fine on my end, and that is the only error I can think of. If you can, it would help to have the error message(s) that you are receiving. If the png file is in the right place and you still get errors, I would recommend reinstalling SFML and following a tutorial online to ensure that you get everything set up properly.

why loadFromFile produces access violation when reading location?

I am new to c ++ / SFML and I don't know why my program cannot load a texture, I tried putting the image in my project folder and in the debug folder but it didn't work.
Code:
int main() {
sf::RenderWindow window(VideoMode(600,600),"Window");
sf::Event event;
sf::Texture textura;
textura.loadFromFile("icono.png");
sf::Sprite sprite1(textura);
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type== sf::Event::EventType::Closed) {
window.close();
}
}
window.clear();
window.draw(sprite1);
window.display();
}
}
The error is "Unhandled exception at 0x5F6D3647 (vcruntime140.dll) in TestSFML.exe: 0xC0000005: Access violation while reading location 0x00701000" in the line "textura.loadFromFile("icono.png");"
I found the answer and share it in case it helps. The possible solution is to add the SFML debug libraries in "additional dependencies", the libraries are "sfml-graphics-d.lib" and "sfml-system-d.lib".

Cannot load an image file as texture using C++ and SFML 2.1

I am trying to output an image onto the screen using SFML 2.1, C++, and MS Visual Studio Professional 2013. I am getting an unexpected error when trying to load a file into a texture. It outputs a whole bunch of random characters. I'm sure if its how I configured the SFML library with Visual Studio or a problem with the code. Can anyone solve this problem? Thanks.
Here is a screenshot of what it looks like when I run the program (http://i.stack.imgur.com/uMdLT.png):
This is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
For all configuration properties, they look like this:
Linker -> General (http://i.stack.imgur.com/NZg7P.png):
Linker -> Input (http://i.stack.imgur.com/1tPaB.png):
**Please note that if I did not configured the SFML library as I did, then I would be getting an error from my system saying msvcr110d.dll is missing.
Ok I managed to fix this, here is what you need to do:
Set your SUBSYSTEM to WINDOWS:
Add "sfml-main.lib" to your libraries:
Change your Runtime library to /MD. This is because you're not using the debug versions of the SFML 2.1 libraries (and probably can't in VS2013).
Make sure your "fighter jet.png" image is in the right place. By default Visual Studio uses the Project Directory as the working directory. So put the png in the same folder as your vcxproj file.
Are you sure that the picture is in the execution directory of your program ?
You should avoid spaces in file's name (fighter_jet.png).
If you're not sure about execution directory, try with absolute path (to be sure it's a path problem, and not a picture's problem).
I hope it helps.
I've tried this code on my system (Xcode - OSX), with one of my picture, and it works.
Have you tried with another picture ?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}

C++/SFML program crashes on exit

I just recently started playing around with SFML and I wrote this simple program.
I'm using visual studio 2010 btw.
The program compiles and runs fine when using the "start debugging" option.
but if I open the .exe file as if I was running a normal desktop application or something, it will crash on exit.
I've spent a while trying to figure it out but all I can say is that it's probably a heap corruption.
here's all the code:
#include <iostream>
#include <sstream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
void moveSquare();
void avgFPS();
class displayFPS : public sf::Thread{
public:
private:
virtual void Run();
};
int checkEvent(sf::RenderWindow &win){
sf::Event Event;
while(win.GetEvent(Event)){
// Window closed
if (Event.Type == sf::Event::Closed){
return 0;
}
// Escape key pressed
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)){
return 0;
}
}
return -1;
}
sf::RenderWindow win(sf::VideoMode(800,600,32),"Mario Clone Test");
sf::Image img1(200,200,sf::Color(255,255,0));
sf::Sprite sprite1;
std::stringstream ss;
sf::String fps;
bool threadFPS;
int main(){
sprite1.SetImage(img1);
sprite1.SetCenter(-300,-300);
win.SetFramerateLimit(30);
moveSquare();
win.Close();
sf::Sleep(0.5);
return 0;
}
void moveSquare(){
displayFPS dispFPS;
threadFPS = true;
dispFPS.Launch();
fps.SetSize(20);
while(1){
if(!win.IsOpened() || checkEvent(win) == 0){
threadFPS = false;
dispFPS.Wait();
break;
}
win.Draw(sprite1);
win.Draw(fps);
win.Display();
win.Clear();
if(win.GetInput().IsKeyDown(sf::Key::Left)){
sprite1.Move(-100*win.GetFrameTime(),0);
}
if(win.GetInput().IsKeyDown(sf::Key::Right)){
sprite1.Move(100*win.GetFrameTime(),0);
}
if(win.GetInput().IsKeyDown(sf::Key::Up)){
sprite1.Move(0,-100*win.GetFrameTime());
}
if(win.GetInput().IsKeyDown(sf::Key::Down)){
sprite1.Move(0,100*win.GetFrameTime());
}
}
return;
}
void avgFPS(){
double frames=0.0,avg=0.0;
int j=0;
while(threadFPS){
if(win.GetFrameTime() != 0){
j++;
frames = frames+(1.0/win.GetFrameTime());
avg = frames/j;
}
ss << "avg FPS: " << avg << std::endl << "Arrow Keys to Move" << std::endl << "Press ESC to Exit";
fps.SetText(ss.str());
ss.str("");
}
return;
}
void displayFPS::Run(){
avgFPS();
}
I've had the same issue.
You need to recompile SFML when using VS2010.
Few things for you to try:
If you are suspecting heap corruption, run gflags (found in Debugging Tools for Windows) and enable page heap. Some instructions on how it works can be found here. Basically when page heap is enabled, your app will crash at the point of the memory error, not sometime later.
You said you get a crash on exit. When that happens, I'm assuming windows throws up a crash dialog box. Open one of those links that say something like "see what information is being uploaded". Somewhere among those files will be a minidump of your process. You can load that up in visual studio (open file and hit F5). Sometimes visual studio is glitchy, so another, more reliable but more difficult but more difficult to use alternative is WinDbg, also part of Debugging Tools for Windows.
SFML has multiple versions of their .lib's for release and debug.
Examples:
sfml-audio.lib
sfml-audio-d.lib
sfml-audio-s.lib
sfml-audio-s-d.lib
Make sure you are using the lib without the -d in it.
Also, when you put the .dll's with your exe (assuming you are using the dynamic libraries) make sure to use the normal versions not the debug (-d) versions.
Finally, when you are building the project make sure you build for release and not debug.