How to fix the Unbound module Graphics in an ocaml project

Image
From ~/pr/gitl/ocaml-gol In a constant effort to learn new programming languages, I'm currently trying to use ocaml , a free and open-source general-purpose, multi-paradigm programming language maintained at the Inria . It's basically an extension of Caml with object-oriented features. I'm mostly interested by its functionnal and pattern matching features but the module part of the language can be a bit difficult to understand for someone with little to none ML (Meta Language) background.   The error When trying to use the graphics module to create a graphical window and go just a little further than the simplest helloworld program, here is the result : If the project uses dune : (executable (name ocaml_project) (libraries lwt.unix graphics) ) with this code : let () = Printf.printf "Hello, world!\n";; Lwt_io.printf "Hello, world!\n";; Graphics.open_graph " 800x600";; The first times I built this project running the du...

How RainbruRPG will handle game states

This design-related post will try to explain how we're implementing RainbruRPG's game states.

As stated in the last announcement, we now have to handle multiple states : main menu and local test. The game states will allow us to have multiple screens, one at a time.

UML diagrams

The class diagrams show the StateManager as a separated class from the GameEngine one. The GameEngine class publicly inherits StateManager and has access to its members/functions.

RainbruRPG's StateManager UML class diagram
The sequence diagram shows how we'll call each function :
RainbruRPG's game state sequence diagram

This is a simplified version, without any event handling or class members.

C++ implementation

The StateManager class

The StateManager is pretty simple.
class StateManager
{
public:
  StateManager(GameEngine*);
  ~StateManager();

  GameState* getCurrentState()const;
  void setCurrentState(GameState*);
 
protected:
  GameEngine* mGameEngine;
  GameState* mCurrentState;
};
I removed all obvious functions implementation, like getter, destructor etc...
StateManager::StateManager(GameEngine* ge):
  mGameEngine(ge),
  mCurrentState(NULL)
{

}

void
StateManager::setCurrentState(GameState* gs)
{
  if (mCurrentState)
    mCurrentState->exit(mGameEngine);

  // Actually changing gamestate
  mCurrentState = gs;
  gs->enter(mGameEngine);
}

The GameEngine class

Here are the changes involved in the GameEngine class :
class GameEngine: public StateManager
{
  GameEngine();
};
And its constructor implementation :
GameEngine::GameEngine(void):
  StateManager(this)
{
  MainMenu mm;
  setCurrentState(&mm);
}

The GameState class

Note that this is just an interface, containing pure virtual functions you'll need to implement in subclasses.
class GameState
{
public:

  GameState(){};
  virtual ~GameState(){};
  
  virtual void enter(GameEngine*)=0;
  virtual void exit(GameEngine*)=0;

};

Conclusion

This is not the current implementation used in RainbruRPG but the state mechanism is here. The implementation already started here.

Comments

Popular posts from this blog

How to make a map of variant in C++