Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Go Game : display issue  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

Fallows

  • Newbie
  • *
  • Posts: 4
    • View Profile
Go Game : display issue
« on: June 13, 2015, 05:51:18 pm »
EDIT : my problem has evolved, it is now described in post number 4 of the topic
Hi everyone,

I'm trying, with a few friends, to develop a Go game in C++ using the SFML. We are beginners (first time using a GUI) and our problem probably comes from the display of our pawns : we created a class pawn and an array of pawns that represents the game board (each intersection on the board has its own coordinates in pixels associated to a cell of that array). We are using the cells of our array as pawns and we're displaying their sprite if there is a click on an intersection. We're loading the texture in the sprite (each cell/pawn has a sprite because we created it as a member of the class pawn ) depending on whose turn it is (the pawn has a member that defines which player clicked on the board).

We know that our program is badly written, but I think (definitely not sure though) I know where the issue comes from, and I just don't know why it's buggued there.
I'll post where I believe our problem comes from, but if you guys need more, I can post the entire program (which isn't that beautiful).
Unfortunately we are French, so I apologize for the bad english and the reading issues you guys might encounter if I post the entire prog.

               //[...]
        window.clear(); //my window is called window
        window.draw(s_fondEcran);//that's a wooden background
        window.draw(s_goban9x9); //that's my plate
        window.draw(texte1); //a text above the plate
        window.draw(s_boutonPasser); //That's a button to pass the turn

   for (int i=0; i<=8; i++) {
            for (int j=0; j<=8; j++) {
                if(plateau[i][j].get_joueur()==1) //j1 = black color
                {
                    plateau[i][j].sprite.setTexture(t_pionNoir); //sets from texture loaded from black pawn earlier
                    window.draw(plateau[i][j].sprite);
                }
                else{
                    if(plateau[i][j].get_joueur()==2) //j2 = white color
                    {
                        plateau[i][j].sprite.setTexture(t_pionBlanc); //sets from texture loaded from white pawn earlier
                        window.draw(plateau[i][j].sprite);
                    }


                }
            }
    }
}  //End of the while (window.pollEvent(event))
     window.display();
} //End of the while (window.isOpen())
return EXIT_SUCCESS;
} //end of my function

 

So I thought my double for would go through my array and display the sprite of the cell if this one is loaded with a texture depending on the player.
Instead it kinda displays whatever it wants whenever it wants, even deleting other sprites, and eventually  the program crashes.
I think this is the display that buggs out because when we play the game on the terminal it works perfectly well (we display the game at the same time on the terminal and it does what it's supposed to do).
If it is something else that might cause the issue, please tell me.


Thanks for reading,

Fallows

PS : We set the sprites positions earlier, before the while(window.isOpen){} using this :
for (int i=0; i<=8; i++) {
        for (int j=0; j<=8; j++) {
               plateau[i][j].sprite.setPosition(234+(54*i), 134+(54*j));
               plateau[i][j].sprite.setOrigin(10,10);
        }
    }
« Last Edit: June 13, 2015, 10:07:20 pm by Fallows »

Fallows

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Go Game : display issue
« Reply #1 on: June 13, 2015, 06:19:02 pm »
EDIT :

Hi again :p
I just deleted the sprite from my pawn class, and I ended up with this. The display works at the right position.


         [...]

        window.clear();
        window.draw(s_fondEcran);
        window.draw(s_goban9x9);
        window.draw(texte1);
        window.draw(s_boutonPasser);

   for (int i=0; i<=8; i++) {
            for (int j=0; j<=8; j++) {
                if(plateau[i][j].get_joueur()==1) //j1 = couleur noire
                {
                    sf::Sprite sprite;
                    sprite.setTexture(t_pionNoir);
                    sprite.setPosition(234+(54*j), 134+(54*i));
                    sprite.setOrigin(10,10);
                    window.draw(sprite);
                }
                else{
                    if(plateau[i][j].get_joueur()==2) //j2 = couleur blanche
                    {
                        sf::Sprite sprite;
                        sprite.setTexture(t_pionBlanc);
                        sprite.setPosition(234+(54*j), 134+(54*i));
                        sprite.setOrigin(10,10);
                        window.draw(sprite);
                    }


                }
            }
    }
}// end of while(window.pollEvent(event));
window.display();
} // end of while(window.isOpen);
return EXIT_SUCCESS;
}//end of function


And now my issue is that my display ins't stable anymore : the pawns are at their good spot, only they seem to be "trembling" as if the display wasn't fast enough in the double for so that they appear as neat as possible.
(I don't know if that point was clear, it might not be what I said it was, just hypothesis, still working on it).

Thanks for reading,

Fallows
« Last Edit: June 13, 2015, 06:35:54 pm by Fallows »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Go Game : display issue
« Reply #2 on: June 13, 2015, 08:32:03 pm »
Please ask a specific question and only provide relevant code at best as a complete and minimal example. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Fallows

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Go Game : display issue
« Reply #3 on: June 13, 2015, 09:41:24 pm »
EDIT : what do you mean by "example" ? I have to post a video to show the problem :/
EDIT 2 : I can post the entire function (but it's enormous as me and my teamates are not very good programers  :-\ )
Alright, sorry about the messy posts :p

The display of my game is stuttering or trembling : the sprites (mainly of my pawns, but sometimes my board and my background) appear and disappear very fast (eventually they seem to stabilize).
 
I don't understand why, and would like to know if anyone would have a hint to make it stable ?
Here is the part where I display my pawns  (I don't think the issue come fromanywhere else, because there's nothing about the display of my board anywhere else, but I'm currently "cleaning my code" so that I can post it here) :

sf::RenderWindow window(sf::VideoMode(875, 675), "Goban 9x9");
[... the rest of the function catch the events and allow us
to get the position of the mouse clic event to fill the array
representing the board of the game ... ]

// So once we clic on the board, the cell of the array is determined and I display a pawn depending on who is playing

        window.clear();
        window.draw(s_fondEcran);
        window.draw(s_goban9x9);
        window.draw(texte1);
        window.draw(s_boutonPasser);

        for (int i=0; i<=8; i++) {
            for (int j=0; j<=8; j++) {
                    sf::Sprite sprite;
                if(board[i][j].get_player()==1) //j1 = black color
                {

                    sprite.setTexture(t_blackPawn);
                    sprite.setPosition(234+(54*j), 134+(54*i));
                    sprite.setOrigin(10,10);
                   // window.draw(sprite);

                }

                else{
                    if(board[i][j].get_player()==2) //j2 = white color
                    {

                        sprite.setTexture(t_whitePawn);
                        sprite.setPosition(234+(54*j), 134+(54*i));
                        sprite.setOrigin(10,10);

                    }
 
                 }

                 window.draw(sprite);

            }
           

       }
}//end of my while(window.pollEvent(event))

window.display();

} //end of my while(window.isOpen)
   
return EXIT_SUCCESS;

} // end of my function

I think I've put the window.draw in a wrong loop or something like that, but I'm not sure, that's why I'm asking for tips here.

Thanks for reading,

Fallows
« Last Edit: June 13, 2015, 10:19:35 pm by Fallows »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Go Game : display issue
« Reply #4 on: June 14, 2015, 02:14:39 am »
The pollEvent loop will loop as long as there events to process and then will leave the loop when all of the events have been polled.
There is no need to clear the window and draw everything for each event; it doesn't get shown until the window.display() anyway.
Outside of the event loop, you should have clear and display - always - and insert your draws inbetween. This is what happens every cycle and those cycles happen continuously.
window.display() swaps frames using a buffer and when there are no events, the window.display() is continuously being executed which means there will be continuous swaps.

tl;dr
Always insert this code into your main loop (while (window.isOpen())) and adjust for which objects you are drawing.
window.clear();
window.draw(object1);
window.draw(object2);
window.display();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Go Game : display issue
« Reply #5 on: June 14, 2015, 08:41:47 am »
EDIT : what do you mean by "example" ?
He means this: http://sscce.org/

Fallows

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Go Game : display issue
« Reply #6 on: June 14, 2015, 12:21:50 pm »
The pollEvent loop will loop as long as there events to process and then will leave the loop when all of the events have been polled.
There is no need to clear the window and draw everything for each event; it doesn't get shown until the window.display() anyway.
Outside of the event loop, you should have clear and display - always - and insert your draws inbetween. This is what happens every cycle and those cycles happen continuously.
window.display() swaps frames using a buffer and when there are no events, the window.display() is continuously being executed which means there will be continuous swaps.

tl;dr
Always insert this code into your main loop (while (window.isOpen())) and adjust for which objects you are drawing.
window.clear();
window.draw(object1);
window.draw(object2);
window.display();

It worked thanks ^^ I didn't pay attention  to where my event loop was ending in comparison to the basic example on the tuto for the first SFML window

EDIT : what do you mean by "example" ?
He means this: http://sscce.org/

Sorry, I didn't read that, but will do !