SFML community forums

Help => General => Topic started by: Kamaitachi on March 16, 2011, 01:20:26 pm

Title: App.Draw() trouble
Post by: Kamaitachi on March 16, 2011, 01:20:26 pm
:( Another problem came up. The following (I believe) piece of code isn't drawing anything. What is wrong this time?

Code: [Select]
void Field::FDisplay (float pos1, float pos2, int pl)
{
    for(int i = 0; i < 3; i++)
    {
        if (Farray[i] != 0 && Farray[i] != 1)
        {
            Farray[i] = 0;
        }
    }

    App.Clear();

    sprite[1].SetX(0.f);
    sprite[1].SetY(0.f);
    App.Draw(sprite[1]); //Corrected
}

//...

F.FDisplay(1.f, 1.f, 1);
Title: App.Draw() trouble
Post by: danman on March 16, 2011, 01:36:57 pm
Did you forgot App.Display() / Clear() ?
Title: App.Draw() trouble
Post by: Kamaitachi on March 16, 2011, 02:08:09 pm
Added App.Display, as following:

Code: [Select]
   App.Clear();

    sprite[1].SetX(pos1);
    sprite[1].SetY(pos2);
    App.Draw(sprite[1]);

    App.Display();


Still not drawing anything.[/code]
Title: App.Draw() trouble
Post by: Tank on March 16, 2011, 02:32:56 pm
That code snippet is not very helpful. What's in the sprite array, especially sprite[0]? How got it initialized? Why do you set the position for sprite[1] but draw sprite[0]?
Title: App.Draw() trouble
Post by: Kamaitachi on March 16, 2011, 02:48:05 pm
Okay:
1- sprite[0] contain a black and white sprite (the background).
2- They were initialized with the following code:

Code: [Select]
void Field::FDImage(std::string filename, int arpos)
{
    if(!image[arpos].LoadFromFile(filename))
    {
        App.Close();
    }
    sprite[arpos].SetImage(image[arpos]);
}


and

Code: [Select]
FDImage("Field.bmp", 0);
    FDImage("O.bmp", 1);
    FDImage("X.bmp", 2);


3- That was an error I corrected already.
Title: App.Draw() trouble
Post by: Tank on March 16, 2011, 03:18:48 pm
The image array is valid for all the time the sprites are being used?
Title: App.Draw() trouble
Post by: Kamaitachi on March 16, 2011, 05:17:09 pm
I think so. The array is initialized before the use of FDisplay, and no function modifies it afterwards.
Title: App.Draw() trouble
Post by: devlin on March 16, 2011, 05:39:53 pm
Minimal compilable code that reproduces the problem?

You can't expect us to guess what your code does. :)
Title: App.Draw() trouble
Post by: Kamaitachi on March 16, 2011, 06:32:08 pm
Found the problem, while "minimizing" the problem:
The order of these two functions:

Code: [Select]
while(App.IsOpened())
{
        FEvent();
        FDisplay(1.f, 1.f, 1);
}


Makes it so that FDisplay is never called. If I call FEvent(), FDisplay() isn't called, and nothing is displayed. If FDisplay() is called first (and FEvent() is slightly tweaked, making a FDisplay call), everything works fine. I should have noticed that before... Anyway, thanks for the help!
Title: App.Draw() trouble
Post by: OniLinkPlus on March 16, 2011, 11:22:43 pm
That shouldn't happen. Show us the FEvent code.
Title: App.Draw() trouble
Post by: Tank on March 17, 2011, 09:16:53 am
I'm with devlin: We can't know what all those F-things do, neither it's possible to follow your program flow.

Having two statements next to each other without conditionals and when the second statement is not being executed, there's really something weird going on (might happen due to exceptions of any kind, but you should get noticed about it by program abortions -- at least if you don't catch them).
Title: App.Draw() trouble
Post by: Kamaitachi on March 20, 2011, 02:18:58 pm
This was FEvent():

Code: [Select]
void Field::FEvent ()
{
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if (Event.Type == sf::Event::MouseButtonPressed)
                App.Close();
        }
    }
}
Title: App.Draw() trouble
Post by: devlin on March 20, 2011, 06:28:44 pm
Ehm, of course it never makes it to FDisplay?

You have an infinite loop in FEvent - until your program closes.
Title: App.Draw() trouble
Post by: OniLinkPlus on March 21, 2011, 04:13:11 am
Quote from: "Kamaitachi"
This was FEvent():

Code: [Select]
void Field::FEvent ()
{
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if (Event.Type == sf::Event::MouseButtonPressed)
                App.Close();
        }
    }
}
Code: [Select]
void Field::FEvent ()
{
    sf::Event Event;
    while (App.GetEvent(Event))
    {
        if (Event.Type == sf::Event::Closed)
            App.Close();
        if (Event.Type == sf::Event::MouseButtonPressed)
            App.Close();
    }
}

Fixed it.