Problem with TGUI basics

Started by Jimmyee, 13 July 2013, 20:44:20

Jimmyee

Hello. I'm Jimmyee.
I just downloaded TGUI 0.5 and I am trying to compile my game using tgui::window. Seems like everything works fine but nothing is being drawn. There is just a white screen.
I use tdm-gcc-4.7.1-2.

This is my code:

void Game::init()
{
    int fps = v_conv::to_int(config["FPS"]);
    std::string fullscreen = config["Fullscreen"];

    if(fullscreen == "yes")
        window.create(sf::VideoMode(800, 600), "Lucyferian", sf::Style::Close | sf::Style::Titlebar | sf::Style::Fullscreen);
    else
        window.create(sf::VideoMode(800, 600), "Lucyferian", sf::Style::Close | sf::Style::Titlebar);
    window.setVerticalSyncEnabled(true);
    window.setFramerateLimit(fps);

    window.globalFont.loadFromFile("DejaVuSans.ttf");

    gfxLoader = std::shared_ptr<GFXLoader>(new GFXLoader("gfx/"));

    sgui.setTexture(gfxLoader->dir["gui/"].tex[0]);
    sgui.setPosition(0.0f, 0.0f);

    tileset[0].load(gfxLoader, 1);
    tileset[1].load(gfxLoader, 1);
}

Game::Game()
: config("config.ini")
, state(State::Run)
, minimap(false)
{
    init();
}

void Game::handleEvents()
{
    sf::Event event;
    while(window.pollEvent(event))
    {
        handleEvent(event);
        window.handleEvent(event);
    }

    realTimeKeyboardInput();

    tgui::Callback callback;
    while (window.getCallback(callback))
    {
        // here tgui events are handled
    }
}

void Game::draw()
{
    window.clear(sf::Color(0, 0, 0, 255));

    if(map.get())
    {
        window.draw(*map);
        if(minimap)
        {
            sf::RectangleShape rectangle(sf::Vector2f(240, 180));
            rectangle.setFillColor(sf::Color::Black);
            rectangle.setOutlineThickness(1);
            rectangle.setOutlineColor(sf::Color::White);
            rectangle.setPosition(440, 0);
            window.draw(rectangle);

            sf::View transformed_map(sf::FloatRect(0, 0, 800, 600));
            transformed_map.setCenter(((21/2)*32) - 16, ((16/2)*32) + 16);
            transformed_map.setViewport(sf::FloatRect(0.55f, 0, 0.3f, 0.3f));
            transformed_map.zoom(1.5);
            window.setView(transformed_map);
            window.draw(*map.get());
            window.setView(window.getDefaultView());
        }

        if(tileset[1].enabled)
        {
            tileset[1].draw(window);
        }
    }

    window.drawGUI();
    window.display();
}

void Game::run()
{
    MapFile mf("data/maps/testmap.map");
    map = std::shared_ptr<MapRender>(new MapRender(mf, (*this)));
    me = std::shared_ptr<Character>(new Character(*this, 0, 0));
    me->x = map->mapFile.width/2;
    me->y = map->mapFile.height/2;
    me->speed = 4;
    map->addObject(me);
    printf("Player speed: %i\n", me->speed);

    map->update();

    sf::Clock clock;
    while(this->window.isOpen())
    {
        handleEvents();
        sf::Time elapsed = clock.restart();
        process(elapsed);
        draw();
        sf::sleep(sf::milliseconds(5));
    }
}


I can't find the problem but maybe you will. Thanks in advance.

texus

If nothing would be drawn then the screen would be black, as this is the color you use to clear it. The fact that you get a white screen means that something is being drawn.

I can't find a mistake in such code.
You will either have to provide a minimal and complete code that reproduces it or just give me the full code and images. I just needs something that I can compile and test for myself.

Although there is one scenario that I can think of that would cause your trouble.
Are you sure that the textures u use for your map are not destroyed too early? If you would be completely filling your whole screen with tiles and the texture no longer exist (then sfml draws a white tile) then your screen would be completely white.

Jimmyee

#2
I was testing the game with tgui::Form instead of tgui::Window and the screen was black then. Also, the game works fine without the tgui code.
If you want, I can write a simple code working the same way as the game does and I'll post it here. Maybe it will give you more hints...

EDIT: the black screen appears when I add a button to the form. May it be a problem with the TGUI graphics? I'll check them twice.

EDIT2: I have no idea. This is how I add the button:

tgui::Button* button = tguiWindow.add<tgui::Button>();
    button->load("gfx/gui/objects/Button/Black"); // the game draws fine when I remove this line
    //button->setText("Button 1");
    button->setSize(160, 40);
    button->setPosition(20, 4);

texus

Very strange.
But you will have to provide some simple code that I can actually debug myself, otherwise I won't be able to help much.

Do you have v0.5.2?

Jimmyee

#4
Quote from: texus on 13 July 2013, 22:03:26
Do you have v0.5.2?

"TGUI v0.5.2 (with precompiled MinGW-4.7.1-TDM libraries)"

EDIT: I will make a simple program and test it without my game engine. I'll let you know hows it going.

EDIT: I compiled this and there's still black screen (it's from https://tgui.eu/example-code/v05/ example):


void run()
{
    // The window is created like you would create a sf::RenderWindow.
    tgui::Window window(sf::VideoMode(800, 600), "TGUI v0.5");

    // Set the font for all the objects.
    if (window.globalFont.loadFromFile("gfx/fonts/DejaVuSans.ttf") == false)
    {
       // Error
    }

    // In the init function the objects will be created
    tgui::Button* button = tguiWindow.add<tgui::Button>();
    button->load("gfx/gui/objects/Button/Black");
    button->setText("Button 1");
    button->setSize(160, 40);
    button->setPosition(20, 4);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            // The events must be passed to TGUI in order to make the objects work
            window.handleEvent(event);
        }

        tgui::Callback callback;
        while (window.getCallback(callback))
        {
            if (callback.callbackID == 1)
            {

            }
        }

        window.clear();

        window.drawGUI();

        window.display();

        sf::sleep(sf::milliseconds(1));
    }
}


EDIT: maybe it's something with the libraries I'm using?
Check this:


EDIT: I've checked the path for graphics and it's correct. No more ideas at this moment \:

texus

#5
If the example code doesn't work then the problem doesn't lie in the code.

I would be surprised if this would be caused by a library mismatch because I would expect errors in such case.
However I do see that you are linking to the static sfml libs but to the normal tgui lib (without ths '-s' postfix). The line should be 'libtgui-s.a'.

Edit: Also make sure nothing is being printed in the terminal.

Jimmyee

#6
Thank you for your help but when I use libtgui-s I get bunch of linker errors:
Quote
||=== SFML Test 2, Release ===|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getString() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getString() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getGlobalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setFont(sf::Font const&)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getString() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getString() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setString(sf::String const&)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getCharacterSize() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getLocalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getGlobalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getGlobalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getCharacterSize() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getLocalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setColor(sf::Color const&)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| more undefined references to `vtable for sf::Text' follow|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::Text()'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setColor(sf::Color const&)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getCharacterSize() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getColor() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setColor(sf::Color const&)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::getFont() const'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setFont(sf::Font const&)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp:(.text$_ZN2sf4TextD1Ev[__ZN2sf4TextD1Ev]+0x3e)||undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Button.cpp.obj):Button.cpp:(.text$_ZN2sf4TextD1Ev[__ZN2sf4TextD1Ev]+0x45)||undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setFont(sf::Font const&)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setColor(sf::Color const&)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setString(sf::String const&)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::getCharacterSize() const'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::getLocalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setCharacterSize(unsigned int)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setString(sf::String const&)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::getLocalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setString(sf::String const&)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::getLocalBounds() const'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::Text()'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `vtable for sf::Text'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::setString(sf::String const&)'|
E:\code\TGUI\lib\libtgui-s.a(Tab.cpp.obj):Tab.cpp|| undefined reference to `sf::Text::getLocalBounds() const'|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|

I think it's something with compatibility Should I compile TGUI myself?

texus

Thats because the libraries aren't linked in the right order.
The correct linking order is tgui, sfml-graphics, sfml-window, sfml-system.
So you should put tgui on top of that list instead of at the bottom.

Jimmyee

Well. I've compiled it and... still the same problem. BLACK SCREEN.

texus

I never had such problem before so all I can do is just guess.
You said that sfml itself works, so that rules out driver issues...

But just to be absolutely sure, does the example code really shows a black screen?
And don't change anything about it. Just compile the exact code as it is found in the 'example code' folder. Place the executable there and run it from that folder, not from codeblocks.
Only then I can be 100% sure that it can't be a problem in the code.

Also another thing to make sure, the sfml libs are the ones from the sfml site right? Not some newer version compiled from github.

Then you could try recompiling tgui yourself, but I'm not sure if that is going to change anything.
And if that doesn't work then I'm out of ideas.

Jimmyee

I have pasted the whole example code to the main.cpp file.
I get this error: "E:\code\SFML Test 2\src\main.cpp|50|error: 'setPosition' was not declared in this scope|"
It is about this line:
"checkbox-setPosition(20, 50);"

EDIT: nevermind, it was "-" instead of "->".

EDIT: I got the code compiled and it works O.o I'll try to find what did I do wrong...

EDIT: I found the cause. I was using the window object declared in the Game class instead of the tgui::Window object and it was just using the uninitialized/unused window object... Anyways, thanks for all your help and your time. Peace!