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!
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'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 having a segmentation fault error when running this code
The error is triggered at the draw function of the custom object.
I believe that its related to the address of the window.
GameObject *obj = new Dummy;
//game loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
obj->draw(window);
window.display();
}
return 0;
}
I pasted the whole code here
I fixed it.
The problem was that I was loosing the texture cause it wasn't created with new, so after the constructor created it then the sprite was banished.
I'm trying to open a SFML window, but every time it is launched it says "Access violation reading location: 0xCCCCCCC0." The error is occuring in the init() method. Relevant code:
class AirportGame {
private:
sf::RenderWindow window;
public:
void init();
int run();
/
void AirportGame::init() {
window.create(sf::VideoMode(800, 600), "SFML window");
}
int AirportGame::run() {
init();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
return 0;
}
int main() {
AirportGame* app = new AirportGame();
return app->run();
}
It happens sometime after init, because the actual window is open. There is no mention in the debugger of 0xCCCCCC0.
Fixed it!
Turns out under the C++ pre-processor I set the definition to SFML_STATIC instead of SFML_DYNAMIC
Set window to a
RenderWindow *window;
and create it with
window = new sf::RenderWindow( /*your stuff or default initialize*/ );
and then call
window->create( /*your settings*/ );
if you didn't already initialize it.
From then on just access window using '->' instead of '.'
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: