Sharing singletons

This is a discussion on Sharing singletons within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm getting back into C++, and the first task has been moving my game to C++. Basically as of ...

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Sharing singletons

    Hello,

    I'm getting back into C++, and the first task has been moving my game to C++.

    Basically as of now it looks like:

    Code:
    think_init();
    render_init();
    
    while(running == GL_TRUE)
    {
        /* ... */
        think_frame(delta, gameDelta);
    
        /* ... */
        render_frame();
    }
    
    think_cleanup();
    render_cleanup();
    Where 'think' & 'render' do the thinking (ie processing input, moving objects, changing the game state) and render just renders the scene according to the state (might be menu & game).

    Right now, the 'subsystems' access the data of other subsystems through extern global variables,

    render.c
    Code:
    struct render_s render;
    /* blah */
    render.h
    Code:
    extern struct render_s
    {
        int width, height;
        int fps;                         /* frames per second */
    } render;
    Anyone can see that's not very wise, anything in my program can change the width/height of 'struct render_s render'. Hence I have turned to C++,

    How do I give access to render members to each part of my program at any point in time? I've researched the idea of a singleton, Would it be best to do:

    Code:
    int main(void)
    {
    /* ... */
        application MyApp();               /* where the MyApp class holds instances of render & think classes */
    }
    And then share one instance of MyApp() ? (In a file included by all 'classes' perhaps?)

    Sorry if my post is so long, but I'm trying to sort this out
    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why should anyone but main() have to know about MyApp? I would expect MyApp to run the game loop, which would look similar to what you have above:

    Code:
    while (game->running()) {
        current_scene->get_input();
        current_scene->update();
        current_scene->draw();
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    The easiest solution would probably be to have render a static variable (or in an unnamed namespace), and then have a global const reference to render. Have a non-const reference to render that you expose to code that you want to be able to modify it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. false sharing confusion!!
    By mynickmynick in forum C Programming
    Replies: 4
    Last Post: 07-21-2008, 03:10 AM
  2. Sharing connection to wired PC from wireless
    By psychopath in forum Tech Board
    Replies: 1
    Last Post: 01-06-2008, 01:44 AM
  3. File Sharing legal in canada
    By Iamien in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-25-2003, 09:35 AM
  4. FIle Sharing
    By MethodMan in forum Tech Board
    Replies: 2
    Last Post: 11-26-2002, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21