Announcement

Collapse
No announcement yet.

State Manager to manage screens - unit.pas files for separate scenes in each file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • State Manager to manage screens - unit.pas files for separate scenes in each file

    Hello my friend!

    Seems that this framework is missing a class to manage the screens/scenes unit.pas files for separate scenes in each file .pas

    Here is my sample using a class provided by Andreas Lago from PhoenixLib, I'm using ZenGL on this sample.

    To jump on the screens you can use "F" from your keyboard.

    Thanks
    Attached Files

  • #2
    Here is a sample showing how to use multiple screens using just a Form of Delphi and Canvas.
    Attached Files

    Comment


    • #3
      ### The Problems with the Singleton

      **Invisible coupling** — any unit can access and modify the state, and you don’t know who is changing what.

      **Hard to test** — you cannot create an isolated manager for tests; it is always the same global instance.

      **Creation order** — if two units try to use the Singleton before it is created, you get a crash or undefined behavior.

      **Multiple instances are impossible** — what if you want two managers? One for the game and another for the HUD? With a Singleton, you can’t.

      **Silent death** — who is responsible for destroying it? No one really knows.

      ---

      ### This Class — The Real Advantages

      **Explicit dependency** — whoever needs the manager receives it in the constructor. It becomes clear in the code who depends on whom:

      ```delphi
      TScreenMenu.Create(AManager, ...)
      // obvious: TScreenMenu needs a TScreenManager
      ```

      **Controlled lifecycle** — the Form creates it, the Form destroys it. No mystery:

      ```delphi
      procedure TFormMain.FormCreate;
      begin
      FScreenManager := TScreenManager.Create(Canvas, 800, 600);
      end;

      procedure TFormMain.FormDestroy;
      begin
      FScreenManager.Free; // you know exactly where it dies
      end;
      ```

      **Multiple instances** — if you need two separate contexts, it’s trivial:

      ```delphi
      FMenuManager := TScreenManager.Create(Canvas1, 800, 600);
      FHUDManager := TScreenManager.Create(Canvas2, 200, 100);
      ```

      **Testable** — you can pass a fake canvas and test the logic in isolation.

      **Naturally thread-safe** — each instance has its own state, without contention for a global resource.

      ---

      | Aspect | Singleton | This Class |
      | ------------------- | ---------- | ---------- |
      | Coupling | High | Low |
      | Testability | Difficult | Easy |
      | Lifecycle | Obscure | Clear |
      | Multiple instances | Impossible | Trivial |
      | Who depends on whom | Invisible | Explicit |


      ---

      **The Singleton solves the problem of “I need to access this from anywhere” — but that problem is usually a symptom of poor design.**

      **This class solves the problem at its root.**
      Last edited by anthonyedwardstark; 03-12-2026, 06:58 PM.

      Comment


      • #4
        Originally posted by anthonyedwardstark View Post
        Here is a sample showing how to use multiple screens using just a Form of Delphi and Canvas.
        please Lifepower and DraculaLin, test this sample with pure Delphi

        Comment

        Working...
        X