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

Author Topic: Question about game structure  (Read 7043 times)

0 Members and 2 Guests are viewing this topic.

offbyone

  • Newbie
  • *
  • Posts: 1
    • View Profile
Question about game structure
« on: November 07, 2009, 02:58:15 am »
I have a question about making games using SFML. I'm already pretty familiar with C++ but I'm having trouble structuring my classes for a simple game (Nothing elaborate; just a simple ground image and a player sprite, to start). I only got as far as creating a CGame class and a run function for the game loop. But where do I go from here? Am I on the right track or am I totally off base? How are games usually structured?

I'm not asking for piles of code; I can (probably :)) write the code myself, I just need to know how games are structured. Thanks for the help.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Question about game structure
« Reply #1 on: November 07, 2009, 03:00:08 am »
Usually they are structured by "rooms", where you use a single variable to tell which room it is, and then choose the room function to run based on that. At least, that's how I (used to) do it.
I use the latest build of SFML2

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Question about game structure
« Reply #2 on: November 07, 2009, 02:14:48 pm »
Games are structured by rooms? Mh, I really didn't get that. ;)

Of course there're many approaches for game programming. I'm fine with the "game state" design. That is: You have an abstract base class (BaseState) that declares -- for example -- the methods "HandleEvent", "Render" and "Update".

Then there're the concrete implementations, like "MenuState", "GameState", "HighscoreState" or whatever.

In your program's main class (in your case this would be "CGame", I think) you glue all that stuff together. Technically you could store a pointer to the current state and call the methods (HandleEvent, Render etc.) respectively. The mainloop still takes part in your CGame class.

If this hasn't answered your question, you may want to go a bit further into detail. ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Question about game structure
« Reply #3 on: November 07, 2009, 02:43:00 pm »
I handle it similarly as OniLink10: I've got an abstract base class Surface.
Code: [Select]
class Surface
{
    public:
        virtual void Run();
};

Then, there are multiple classes which inherit from Surface, such as MainMenu, OptionMenu, Game, Editor. They implement the Run() method. The class Game is the essential one for gameplay: It stores several game-related states, such as player or enemy data (for example in an STL container) or what game mode has been chosen. Additionally, it contains methods to handle input, compute the game logics and draw the results on the screen. For that functionality, there's in each case an own, specific class, the Game class just delegates the work to them. For example, a GraphicManager class which is responsible of drawing the objects.

An important part is to widely separate graphics and logics. Ideally, the game class shouldn't know anything about SFML. Same applies to the logic objects like Player and Enemy. They don't need to know themselves how they are drawn, that's the task of another class.

By the way, there's no need for the "C" prefix at classes. This is an artifact of the Hungarian Notation, which intends to specifiy identifiers by their types. In C++, however, this concept can't be satisfied consequently. Regarding templates and different class types (class, struct, union), when shall there be a "C" and when not? Should typedefs on classes get a prefix, too? If you say no, why not? They can be used like the class itsself, so there's no reason to distinguish. And when you suddenly change the typedef to a non-class-type? Remove the "C" and refactor all dependent code?

Originally, MFC programmers used the "C" prefix to avoid ambiguous names. In C, you don't have namespaces, so you have to think of something else. Prefixes are standing to reason, for example OpenGL uses "gl" as prefix. In C++, this problem isn't relevant any more, either. To sum up, I've made the experience that the possibility to clarify types is a) mostly unnecessary (especially when you choose good identifiers) and b) can sometimes be an unjustified obstacle regarding flexibility and consistence.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Question about game structure
« Reply #4 on: November 07, 2009, 06:10:38 pm »
I have two base classes so far, "GameObject" and "GameState", both are derived from sf::Drawable, I derive my actual game classes from them. So for example I have my "Unit" class derive from GameObject and all of my game states derive from "GameState." I have true static classes as managers, like an image manager, script manager, etc. I accomplish this by:

Code: [Select]

namespace Moren
{
class Images
{
//functions
};

namespace
{
Images img;
}
}


so to use my image manager its as simple as:

Code: [Select]

namespace mor = Moren;
extern mor::Images mor::img;

mor::img.ret("image.jpg");


The state manager is much the same thing. I use a callback function that allows states to switch between one another using strings as id's.

add a new state: (sta is the global state manager, Menu() is derived from GameState)
Code: [Select]

mor::sta.Add( new Menu(), "Main Menu" );


Code: [Select]

void States::Add(GameState *State, StdString StateName, bool Switch, bool Pause)
{
// set the switch function in custom state
State->SetSwitchFunc( &sta.Switch );
        ...


then from within the state i can switch like this at anytime:
Code: [Select]

SwitchFunc("Main Menu", false);


My design the way I have it now has taken me 4 iterations so just think about how you want to design the game, but realize that you will probably be unhappy with how its coded (if youve never made a game before) because you learn so much through every iteration. You'll never have a "perfect" design so dont try any think of it, just code and you'll soon figure it out.
Eugene Alfonso
GTP | Twitter

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Question about game structure
« Reply #5 on: August 25, 2011, 03:54:12 am »
Quote from: "phear-"
because you learn so much through every iteration. You'll never have a "perfect" design so dont try any think of it, just code and you'll soon figure it out.


Be carefull with 'just' because without thinking you'll very soon end up in a mess and not a game.  :lol:
Planing and refractoring is very important to programm. Implementation should actually just take 1/3, 2/3 should be for planing.

But sure where is the fun in planing? I myself often tend/like just to start programming. You'l really learn much. So if you got the time do it and after some time, when you think you've learned enough, start planing before programming...

Quote
so to use my image manager its as simple as:
Code: [Select]
namespace mor = Moren;
extern mor::Images mor::img;

mor::img.ret("image.jpg");


I don't know where the simplicity is in that, but it's your idea and design...

eXpl0it3r
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Question about game structure
« Reply #6 on: August 25, 2011, 06:54:16 am »
Quote from: "eXpl0it3r"
Quote from: "phear-"
because you learn so much through every iteration. You'll never have a "perfect" design so dont try any think of it, just code and you'll soon figure it out.


Be carefull with 'just' because without thinking you'll very soon end up in a mess and not a game.  :lol:
Planing and refractoring is very important to programm. Implementation should actually just take 1/3, 2/3 should be for planing.

But sure where is the fun in planing? I myself often tend/like just to start programming. You'l really learn much. So if you got the time do it and after some time, when you think you've learned enough, start planing before programming...

Quote
so to use my image manager its as simple as:
Code: [Select]
namespace mor = Moren;
extern mor::Images mor::img;

mor::img.ret("image.jpg");


I don't know where the simplicity is in that, but it's your idea and design...

eXpl0it3r
Was there ANY reason to bump a 2-year-old thread?
I use the latest build of SFML2

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Question about game structure
« Reply #7 on: August 25, 2011, 09:41:34 am »
Woho did I break any laws?  :lol:
Didn't see it was 2 years old and also I've got to this over 'new posts' function...
Don't know how it got in there. (Someone posted somthing and deleted it again, maybe?)

I hope I didn't destroy now your archive ^^
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Question about game structure
« Reply #8 on: August 25, 2011, 09:48:11 pm »
Quote from: "eXpl0it3r"
Woho did I break any laws?  :lol:
Didn't see it was 2 years old and also I've got to this over 'new posts' function...
Don't know how it got in there. (Someone posted somthing and deleted it again, maybe?)

I hope I didn't destroy now your archive ^^
It doesn't break any rules, but it sure is annoying, especially since this thread was technically over. Your post literally added nothing to the conversation, as the op had all of his questions answered, and all you did was come in and say "No, do it like this!" Maybe back when the thread was new and it was still actively discussed and the op was still looking for answers, your response would have been acceptable, but not two years after the fact, when nobody even remembers the conversation anymore.
I use the latest build of SFML2

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Question about game structure
« Reply #9 on: August 26, 2011, 12:50:54 am »
I'm not even sure if I should answer to this...
You're talking to me as if it's the worst thing and EVERBODY is annoyed.

Like I've said before:
1. I didn't notice it was old, so it happend by accident.
2. If you believe it or not, it was in the 'new posts' list, so I assumed it was an active discussion.

Also as a comment, a thread is not only to answer something to one person, it should moreover be a discussion from everyone for everyone and as of this gamedesign is also after 2 years still discussed and maybe someone will find it helpfull.

Everyone is free to post also at old entries, so respect that and all users, also those who you don't like, and you'll earn also respect from them.
(As for me you'll have to work again on that...) :wink:

eXpl0it3r
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Question about game structure
« Reply #10 on: August 26, 2011, 01:49:36 am »
Quote from: "eXpl0it3r"
I'm not even sure if I should answer to this...
You're talking to me as if it's the worst thing and EVERBODY is annoyed.

Like I've said before:
1. I didn't notice it was old, so it happend by accident.
2. If you believe it or not, it was in the 'new posts' list, so I assumed it was an active discussion.

Also as a comment, a thread is not only to answer something to one person, it should moreover be a discussion from everyone for everyone and as of this gamedesign is also after 2 years still discussed and maybe someone will find it helpfull.

Everyone is free to post also at old entries, so respect that and all users, also those who you don't like, and you'll earn also respect from them.
(As for me you'll have to work again on that...) :wink:

eXpl0it3r
I'll admit, I overreacted slightly, but there's been a LOT of necroposting lately, where people have been asking "where's this file when the author clearly doesn't care about this project anymore" and similar things, so any necroposting has made me jumpy.
I use the latest build of SFML2