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

Author Topic: Help with general design  (Read 1793 times)

0 Members and 1 Guest are viewing this topic.

arthropod_example

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help with general design
« on: September 23, 2014, 03:21:43 am »
Hello!

I have been trying to learn C++ (reading recommended books and making simple programs) for some time and decided it would be nice to try and use a graphics library to have a bit more fun than just making console programs. Users from another forum recommended SFML. I saw some tutorials and then started looking at the documentation. So far I've been having a lot of fun!

I've made a simple game (using Conway's Game of Life rules) that basically consists of a grid of cells (each having state 0 or 1) and some random buttons for random effects. It worked!
(click to show/hide)

But considering my inexperience with design this has been very challenging. So I would like to ask some design advice.

At first I created a Grid class that handled the cell grid behavior.
Then I created a clickable Button class to handle whatever random effects the user decides to apply to the screen or the grid.
It all worked. But in order for it to work, I had to pass my game Window and my Grid as a parameter to each Button instance.

Then I created a Game class that contained the Buttons, the Grid and the Window (and some default initialization options to unclutter my main() ). I was trying to encapsulate things or something.

In some way or another I felt the Buttons had to get access to a lot of stuff. Maybe more than they should? How would you guys generally handle this kind of situation from a design perspective?

Thanks in advance!

Also if you could recommend any book where this kind of topic is addressed it would be really appreciated!

If this post does not conform to the standards please let me know since it is my first post in this forum.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Help with general design
« Reply #1 on: September 23, 2014, 07:46:33 am »
By deriving your buttons from sf::Drawable you can write window.draw(button) instead of button.draw(window), thus maybe removing the reference to the window.
What exactly do you need the grid reference then?

There's one book about SFML itself, written by some of this community: https://www.packtpub.com/game-development/sfml-game-development
« Last Edit: September 23, 2014, 07:49:09 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help with general design
« Reply #2 on: September 23, 2014, 09:28:12 am »
To what information do the buttons need access? In any case, you should try to separate the button's actions from the button itself -- that is, the button need not know what it triggers. The user should be able to link actions to the button, e.g. with an abstract base class or std::function.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Help with general design
« Reply #3 on: September 23, 2014, 04:21:14 pm »
A button shouldn't need to know anything besides how to check for a click onto it and how to draw itself. Then it returns that result and you can call some function to react if there was a click.
There is a tutorial on how to make a simple gui: http://iki.fi/sol/imgui/

arthropod_example

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Help with general design
« Reply #4 on: September 23, 2014, 05:01:31 pm »
Thank you all for all the ideas, they are enough to keep me busy thinking and researching for a while!

That book also sounds like exactly what I need   :D