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

Author Topic: How many classes in your average SFML program?  (Read 5811 times)

0 Members and 1 Guest are viewing this topic.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
How many classes in your average SFML program?
« on: November 10, 2012, 08:34:35 pm »
I'm writing my first C++ program and using SFML 2.0.  I'm wondering how many classes does a typical SFML program contain?  I know it depends on how simple or complex the program is so here's a list of classes I think my craps game program will contain:

Main.cpp
Display.h,  Display.cpp
PrimaryMenu.h,  Primary Menu.cpp
RealtimePlay.h,  RealtimePlay.cpp

GetBets.h,  GetBets.cpp  (parent class)
...GetBetsComeout.h,  GetBetsComeout.cpp (child class)
...GetBetsPoint.h,  GetBetsPoint.cpp  (child class)

PayBets.h,  PayBets.cpp (parent class)
...PayBetsComeout.h, PayBetsComeout.cpp (child class)
...PayBetsPoint.h,  PayBetsPoint.cpp  (child class)

RollDice.h,  RollDice.cpp
ShowError.h,  ShowError.cpp  (shows "file not found" or other errors in SFML window)
 
« Last Edit: November 11, 2012, 02:27:05 am by Raptor88 »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: How many classes in your average SFML program?
« Reply #1 on: November 10, 2012, 08:49:54 pm »
It depends on what you're doing, some games are more complex than others. Mine has around twice the amount of classes yours has and I am not even near half of what I need to have. Smaller games need less code, as simple as that. There are of course classes that all games must have, such as a holder for textures and/or images, a game state manager, main menu, movable object base class and many such.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How many classes in your average SFML program?
« Reply #2 on: November 10, 2012, 10:02:27 pm »
It depends on what you're doing, some games are more complex than others. Mine has around twice the amount of classes yours has and I am not even near half of what I need to have. Smaller games need less code, as simple as that. There are of course classes that all games must have, such as a holder for textures and/or images, a game state manager, main menu, movable object base class and many such.

I don't know what happened but I must have pressed a wrong key while typing and things got hung up.  I had not clicked "Post" to post my initial post yet, but when I checked just now, I saw my text had been posted but the list of classes was incomplete.  So I added the rest of the classes I have so far to my list.

From your response, I guess the amount of classes I have so far is probably OK.

Programming in "C++" with all the classes seems to be a lot harder than programming in "C" many years ago.  Composing in C++ needs so much tabbing between different files.  (I'm using Visual C++ 2010 Express)

Thanks,
Raptor
« Last Edit: November 10, 2012, 10:05:26 pm by Raptor88 »

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: How many classes in your average SFML program?
« Reply #3 on: November 10, 2012, 10:24:57 pm »
Don't feel too bad I'm still beating my head against the wall on an objectmanager class.

It's as masskiller said "It depends on what you're up to." :)
I have many ideas but need the help of others to find way to make use of them.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How many classes in your average SFML program?
« Reply #4 on: November 11, 2012, 07:30:29 am »
Don't feel too bad I'm still beating my head against the wall on an objectmanager class.

 :) :) :)

 Raptor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How many classes in your average SFML program?
« Reply #5 on: November 11, 2012, 11:42:35 am »
so here's a list of classes I think my craps game program will contain
You have to be careful that you don't overdo OOP and put everything into classes. Most of your classes have verbs as names, which may be an indicator that they had better be functions. If there is no state, there will usually be no reason for a class.


There are of course classes that all games must have, such as a holder for textures and/or images, a game state manager, main menu, movable object base class and many such.
Don't feel too bad I'm still beating my head against the wall on an objectmanager class.
You both have a rather conservative view, especially concerning the managers and the movable base classes. Sometimes, approaches different from the classical entity hierarchy prove more appropriate. The article Evolve Your Hierarchy explains an interesting alternative.

For example, my game Airport has 9 classes. Of course I could have split more, but since there is not too much code, the maintenance is still acceptable. It is a very good idea to plan in advance and to structure the game. Be aware however that it is also possible to exaggerate, so that masses of classes exist, while each class effectively contributes almost nothing to the overall functionality, leading to unnecessary complex dependencies and much code. As always, the difficulty is to find the balance ;)


Programming in "C++" with all the classes seems to be a lot harder than programming in "C" many years ago.  Composing in C++ needs so much tabbing between different files.  (I'm using Visual C++ 2010 Express)
Interesting criterion for the difficulty of programming ;D
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How many classes in your average SFML program?
« Reply #6 on: November 12, 2012, 01:52:42 am »
You have to be careful that you don't overdo OOP and put everything into classes. Most of your classes have verbs as names, which may be an indicator that they had better be functions. If there is no state, there will usually be no reason for a class.

Hi Nexus,

Guess the SFML website had problems for a while as I couldn't access it.

Looking at my list of classes, should I not create classes for "Display", "RollDice" and "ShowError"?  If I don't create classes for them, would you put them as subroutines in Main.cpp?  All three are used by several different classes.

Regarding the "GetBets" parent and child classes, "GetBets" handles the bets that are always the same when making bets.  "GetBetsComeout" and "GetBetsPoint" inherit from "GetBets" so they can get all of those bets plus get bets unique to the comeout  or point rolls respectively.  The "PayBets" parent and child classes work similarly.  --- Though they are all verbs, would making parent and child classes be the best way to handle them?  If not, how should I handle them?

I assume "Main", "PrimaryMenu", "RTPlay" (RealTimePlay) should definitely be classes.

Thanks,
Raptor
« Last Edit: November 12, 2012, 01:56:05 am by Raptor88 »

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How many classes in your average SFML program?
« Reply #7 on: November 12, 2012, 03:29:19 am »
What does ShowError do? If its not an object and its fairly simple then it can just be brought into your main window/UI class. Of course it depends on various things but typically an error message would be a mode of your program. Hence the main class that handles all the UI business might look like:

Code: [Select]
class GameWindow {
public:

  void showErrorMessage(...);
protected:
  enum Mode {kPlaying, kShowingErrorMessage, ...};
  Mode mode;
};

Where showErrorMessage(...) is just a function, the calling of which changes the state of the program. To access this from multiple parts of your program (which again may or may not be a problem), you could make GameWindow a singleton. Another approach would just be to let GameWindow know when errors occur, e.g., by throwing error messages from functions or having error flags in your other modules.

Fwiw, here's the current list of classes in my game: Moonman.

Code: [Select]
> grep -Rh "class [A-Za-z]* {" src\mm
class FileSystemWatcher {
class Profiler {
class Registry {
class Game {
class Widget {
class InputSystem {
class SensorTracer {
class BackgroundRenderer
class BlockSpriteManager
class Camera {
class Console {
class FunctionProfileVisu
class ImageResourceLoader {
class LightingManager {
class Renderer {
class SpriteLoader {
class Entity {
class FluidManager {
class ISystemBase {
class ItemManager {
class LuaSpecLoader {
class ResourceManagerListener {
class ResourceManagerBase {
class ResourceHash {
class ResourceManager {
class ResourceLoader {
class ResourcePackage {
class Root {
class ScriptManager {
class SoundManager {
class SubRegistry {
class SplitRegistry {
class SystemRegistry {
class BlockManager {
class SubChunkArray {
class ChunkArray {
class World {
class WorldGenerator {

So, at least for my app, most of the classes are just big controller/manager type things that operate on structs.. of which there are 74 and counting.. So it's more of a data-oriented app than a object-oriented app.
Code: [Select]
> C:\dev\moonman>grep -Rh "struct [A-Za-z]* {" src\mm | wc -l
74

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How many classes in your average SFML program?
« Reply #8 on: November 12, 2012, 04:23:31 am »
What does ShowError do? If its not an object and its fairly simple then it can just be brought into your main window/UI class. Of course it depends on various things but typically an error message would be a mode of your program. Hence the main class that handles all the UI business might look like:  ... snip ...

Hi eigenbom,

Thanks for showing me some examples of your classes and other data. 

ShowError in my program is a class that takes a string as an argument.  First it closes the game's SFML window and then opens a small SFML window.  Then it displays the string argument (error message) in the small SFML window and waits for the user to either click the close 'X' or press ESC.  Then it returns with a negative value which signals the main loop to exit.

ShowError can be called from any class.  For now, its primary purpose is if "LoadFromFile" cannot find a .PNG image or if the file is corrupt, ShowError is called with the appropriate error message as a string.   I'll be loading stuff from the hard drive for background images, sprites, help info, saved game data, etc.

Raptor

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How many classes in your average SFML program?
« Reply #9 on: November 12, 2012, 04:46:05 am »
ShowError in my program is a class that takes a string as an argument. 

You mean the constructor? Or is a static class?

Quote
First it closes the game's SFML window and then opens a small SFML window.  Then it displays the string argument (error message) in the small SFML window and waits for the user to either click the close 'X' or press ESC.  Then it returns with a negative value which signals the main loop to exit. ShowError can be called from any class.  For now, its primary purpose is if "LoadFromFile" cannot find a .PNG image ...

Ah right, so am I right in assuming you basically have this:

Code: [Select]
// loader.cpp
void loadStuff(){
  sf::Image img;
  bool success = img.loadFromFile("somejunk.png");
  if (!success){
    ShowError("Can't load file somejunk.png"); // ????
  }
}

If so... then this looks bad, and it will be tricky to trace the execution of the program. A better approach would be to either return an error code or struct, or to throw an exception. Then at the top level (e.g., in main.cpp) you grab the error, create the error window, etc. This also has the benefit that you loadStuff() etc. don't need to know about ShowError() anymore.



Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How many classes in your average SFML program?
« Reply #10 on: November 12, 2012, 06:59:26 am »
You mean the constructor? Or is a static class?

// ***** DisplayError.h *****
#pragma once

#include <string>

class DisplayError
{
    public:
        DisplayError(void);             // Constructor is empty.
        int DispErr(std::string);
};


Quote
Ah right, so am I right in assuming you basically have this:
Code: [Select]
// loader.cpp
void loadStuff(){
  sf::Image img;
  bool success = img.loadFromFile("somejunk.png");
  if (!success){
    ShowError("Can't load file somejunk.png"); // ????
  }
}
If so... then this looks bad, and it will be tricky to trace the execution of the program. A better approach would be to either return an error code or struct, or to throw an exception. Then at the top level (e.g., in main.cpp) you grab the error, create the error window, etc. This also has the benefit that you loadStuff() etc. don't need to know about ShowError() anymore.

Every error string will be unique so I'll know where it came from.  Mainly DisplayError (mistakenly called it ShowError before) is to let the user know why my program has to close.  It's not for my troubleshooting.  I'll definitely keep your suggestions in mind and will use them if I find my current method is tricky to trace.

Thanks much,
Raptor