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

Author Topic: Resizing a grid properly, and making it infinite  (Read 590 times)

0 Members and 1 Guest are viewing this topic.

abcd1234567

  • Newbie
  • *
  • Posts: 23
    • View Profile
Resizing a grid properly, and making it infinite
« on: October 23, 2023, 10:53:12 am »
I programmed "game of life" using a grid, which is basically just a lot of rectangles positioned side by side.
On a fixed size window it works fine.
The user clicks on the squares they want to start with, press Enter, and the game starts.
This is the Github repo, 'main.cpp' is the only interesting part, and it's less than 70 lines:

Now I want to do 2 things:
1) Be able to resize the window to any size, without the grid to stretch.
Basically, I want to print a big grid, and in any window size, see only a small part of the same grid.
I know there's a lot of threads about it, but none of them seemed to solve it for me.
I looked at the [https://www.sfml-dev.org/tutorials/2.6/graphics-view.php#showing-more-when-the-window-is-resized]documentation[/https://www.sfml-dev.org/tutorials/2.6/graphics-view.php#showing-more-when-the-window-is-resized], but when I used the following code in the event loop, it did nothing, and I fail to see why.

2) Be able to drag with the mouse (or even WASD keys) the grid around, so it's ""infinite"".
I don't really know what to start on this one. I understand that I need to simply make the grid really big,
so that it looks infinite, but how can I shift the view based on mouse dragging or keys being pressed?

Also, bonus question: in game of life, most of the cells are empty most of the time.
From a resource perspective, does it make sense to redraw only changed rectangles, instead of redrawing the entire grid every time?

Thanks a lot in advance.
« Last Edit: October 24, 2023, 10:20:13 am by abcd1234567 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: Resizing a grid properly, and making it infinite
« Reply #1 on: October 24, 2023, 12:38:36 am »
I looked at the documentation, but when I used the following code in the event loop, it did nothing, and I fail to see why.
Hard to say what did or didn't work without knowing what you did exactly.

2) Be able to drag with the mouse (or even WASD keys) the grid around, so it's ""infinite"".
I don't really know what to start on this one. I understand that I need to simply make the grid really big,
so that it looks infinite, but how can I shift the view based on mouse dragging or keys being pressed?
Did you have a look at the events tutorial to see how to react to key or mouse inputs?
Then you can move the view, check the API documentation for the detailed API call.
Note though that you'll always have a limited space to operate in, as you have to do the simulation of all the cells as well, and thus the space can't be even close to infinite ;)

Also, bonus question: in game of life, most of the cells are empty most of the time.
From a resource perspective, does it make sense to redraw only changed rectangles, instead of redrawing the entire grid every time?
It's okay to redraw every time, but if you end up with a large grid, it might make sense to really only draw what you see at the given moment.
Additionally, if you run into performance issues, then you could consider using a vertex array.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

abcd1234567

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Resizing a grid properly, and making it infinite
« Reply #2 on: October 24, 2023, 10:18:06 am »
I looked at the documentation, but when I used the following code in the event loop, it did nothing, and I fail to see why.
Hard to say what did or didn't work without knowing what you did exactly.

2) Be able to drag with the mouse (or even WASD keys) the grid around, so it's ""infinite"".
I don't really know what to start on this one. I understand that I need to simply make the grid really big,
so that it looks infinite, but how can I shift the view based on mouse dragging or keys being pressed?
Did you have a look at the events tutorial to see how to react to key or mouse inputs?
Then you can move the view, check the API documentation for the detailed API call.
Note though that you'll always have a limited space to operate in, as you have to do the simulation of all the cells as well, and thus the space can't be even close to infinite ;)

Also, bonus question: in game of life, most of the cells are empty most of the time.
From a resource perspective, does it make sense to redraw only changed rectangles, instead of redrawing the entire grid every time?
It's okay to redraw every time, but if you end up with a large grid, it might make sense to really only draw what you see at the given moment.
Additionally, if you run into performance issues, then you could consider using a vertex array.



Quote
Hard to say what did or didn't work without knowing what you did exactly.

Apologies, I updated the repo on Github now. The only change is adding this case in the event loop:
case sf::Event::Resized:
                    {
                        sf::FloatRect visibleArea(0.f, 0.f, evnt.size.width, evnt.size.height);
                        window.setView(sf::View(visibleArea));
                    }

Which, as it seems, still stretches the grid when resizing, as seen here.

Quote
Did you have a look at the events tutorial to see how to react to key or mouse inputs?
Then you can move the view, check the API documentation for the detailed API call.
Note though that you'll always have a limited space to operate in, as you have to do the simulation of all the cells as well, and thus the space can't be even close to infinite ;)

I'm aware of KeyPressed event and of view.move, I thought the solution is more complex. I'll try them out once I sort the first problem, since calling "move" now just wrap the grids, just like in the video above.

I guess I'm missing something crucial about view.

Thanks a lot
« Last Edit: October 24, 2023, 10:19:47 am by abcd1234567 »

abcd1234567

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Resizing a grid properly, and making it infinite
« Reply #3 on: October 29, 2023, 07:31:51 pm »
bump

 

anything