I have been building a game using SFML in c++ on windows. I have now finished the game and I have been trying to create a GUI using TGUI because it looked simple and easy to use. I have set it up in my VC++ project and compiled a seemingly simple piece of code.
#include <SFML/Window.hpp>
#include <TGUI/TGUI.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
tgui::Gui gui(window);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
gui.handleEvent(event);
}
window.clear();
gui.draw();
window.display();
}
return 0;
}
The code compiles fine, however immediately when I start the code I get an
Unhandled exception at 0x80002610 in Test.exe: 0xC0000005: Access violation executing location 0x80002610.
I was wondering where the access violation could possibly be coming from. Thanks for your help.
Related
I'm currently trying to use TGUI with SFML as the backend, everything works fine when I had this code
#include <iostream>
#include <TGUI/TGUI.hpp>
int main() {
sf::RenderWindow window{ {800, 600}, "TGUI window with SFML" };
tgui::GuiSFML gui{ window };
gui.loadWidgetsFromFile("menus/startMenu.txt");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
gui.handleEvent(event);
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
gui.draw();
window.display();
}
}
However, I then tried to add a line to refer to a button loaded from tgui::Button::Ptr aButton = gui.get<tgui::Button>("a");.
#include <iostream>
#include <TGUI/TGUI.hpp>
int main() {
sf::RenderWindow window{ {800, 600}, "TGUI window with SFML" };
tgui::GuiSFML gui{ window };
gui.loadWidgetsFromFile("menus/startMenu.txt");
tgui::Button::Ptr aButton = gui.get<tgui::Button>("a"); // right here
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
gui.handleEvent(event);
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
gui.draw();
window.display();
}
}
and it gives me this error
Exception thrown at 0x7956271B (tgui.dll) in maze 2.exe: 0xC0000005: Access violation reading location 0x000003E4
I'm currently dynamically linking tgui and sfml, using TGUI-0.9 and SFML-2.5.1 with Debug x86 on Visual Studio c++.
The error also tells me it's coming from
template <class T>
typename T::Ptr get(const String& widgetName) const
{
return std::dynamic_pointer_cast<T>(get(widgetName));
}
in Container.hpp in TGUI.
I think that the problem is the dynamic_pointer_cast throwing an error, but I don't know how to fix it. I also don't understand why everything else works except the gui.get<typename>("sometext"); function. Any help?
Edit 1: I've gone ahead and tested with gui.get(), which works perfectly fine. This means that the problem is definitely in the dynamic_pointer_cast, since gui.get<typename>() just calls gui.get() and runs dynamic_pointer_cast on it.
Ok so I just had to restart Visual studio, and it now works perfectly
I am getting started with SFML with Visual Studio 2019. I followed this tuttorial and launched the app successfuly. But without any changes to the code I launched it one more time and it gives me this error:
Exception thrown at 0x53582687 (sfml-system-2.dll) in Voidger.exe: 0xC0000005: Access violation reading location 0x0000016E. occurred
on this line:
RenderWindow window(VideoMode(200, 200), "SFML works!"); //not really
Ever since the app will output the same error and will occasionaly create a window if some magic stars allign. When I discovered the error I tried to create a new project but had issue with linker so abanded it as the other app didn't even start.
Here is the full code (provided on official website):
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
using namespace sf;
int main()
{
std::cout << "H\n";
RenderWindow window(VideoMode(200, 200), "SFML works!"); // error line
CircleShape shape(100.f);
shape.setFillColor(Color::Green);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
The funniest thing is that the code always launches, outputs the h in cmd and most of times exits with code -1073741819.
Edit:
Here is config manager settings that I use.
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 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!
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: