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

Author Topic: Question about initial state of chess game disappearing  (Read 1671 times)

0 Members and 1 Guest are viewing this topic.

tarstevs

  • Newbie
  • *
  • Posts: 3
    • View Profile
The below application does nothing but set the initial state for a chess game. Everything works as expected so long as I only resize the window in such a way that the window width >= the window height. My question relates to that resizing. I put the question at the bottom so that I could first share the code and highlight some relevant parts therein.

Here is the full application:
https://github.com/tarstevs/chess

Here are some relevant places in the application:
The initial game state is prepared by drawing to an instance of RenderTexture (see board::render_board in board.cpp).

When everything is ready, the texture is drawn to the window with an instance of RenderWindow (see board::draw_board in board.cpp).

The game loop is in game_loop.cpp. Lines 46 and 49 are the calls to the board::render_board and board::draw_board methods mentioned above.

Here is the question I have:
What can I change so that the chess board doesn't disapear when the window is resized such that the width of the window is less than the height of the window?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Question about initial state of chess game disappearing
« Reply #1 on: May 02, 2022, 01:42:27 pm »
Is it intentional that you're drawing the board only one time or when the window is resized?

Seems to me the clear/draw/display should be outside of the event loop.

Also, any reason to create a new board, instead of resizing the existing one?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tarstevs

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Question about initial state of chess game disappearing
« Reply #2 on: May 02, 2022, 10:15:40 pm »
Thanks for the quick reply!

Is it intentional that you're drawing the board only one time or when the window is resized?

For now, yes. In other words, my goal is just to have a "game" with this one rule: the "player" resizes the window and the chess board maintains its [square] aspect ratio under these two conditions:
1. The chess board is always centered horizontally and vertically within the window
2. By necessity [to meet the above conditions] the side length of the square board always equals the shorter length between the window width and window height.

Here are three screenshots showing the situation.

Screenshot 1: The first screenshot is the app when it first opens (everything is as expected).

Screenshot 2: The second screenshot is the app resized where the width is greater than the height (this always works as expected and the chess board "scales" and is positioned as mentioned above).

Screenshot 3: The third screenshot shows the issue I'm having (when the window is resized so that the width is less than the height, the board disappears.

Seems to me the clear/draw/display should be outside of the event loop.

Also, any reason to create a new board, instead of resizing the existing one?

I'm happy to do any of these things (I already tried them and am prepared to make a post explaining the results I observed)... but first I just wanted to ask: given the above explanation and the pictures, is there a clear cause for the board's disappearance? I've tried treating the positioning symmetrically with respect to x shifts and y shifts but that doesn't seem to be good enough somehow: https://github.com/tarstevs/chess/blob/master/src/board.cpp#:~:text=*/-,if%20(windowSize.x%20%2D%20windowSize.y%20%3C%200)%20%7B,%7D,-set_open_sans_font()%3B

tarstevs

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Question about initial state of chess game disappearing
« Reply #3 on: May 03, 2022, 09:21:48 pm »
The below commit is a good enough fix for me.

https://github.com/tarstevs/chess/commit/ef6ee49e5f6fd1101b782e6b2440b5d9889c71e7

It sets x_offset to zero. This causes the chess board to no longer be centered in the window (so that "centering feature" is lost). But, on the plus side, the board no longer disappears when the window width is less than the window height. All of the board-scaling is left unchanged.