Thread: What design pattern to use here?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    What design pattern to use here?

    Hey,

    I am working on a "game engine" and right now Im trying to implement the part of it that is handeling lights.

    Im using openGL, and in order to create a light in openGL you do this, where lightID is the total amount of lights currently in the scene:

    Code:
    glEnable(GL_LIGHT0 + lightID)
    glLightfv(GL_LIGHT0 + m_lightID, OpenGLoptionFlag, yourdata);
    Right now I am keeping lightID as a global and increment it every time I create a new light. It works, but I would prefer to avoid using globals. Also, if I delete a light, there is no way of telling that that id is now free.

    Im thinking of changing it to work like this:
    Code:
    class LightFactory
    {
    public:
    	LightFactory();
    	~LightFactory();
    
    	bool initialise();
    	public DirectionalLight* createDirectionalLight(/*parameters*/);
    	public AmbientLight* createAmbientLight(/*parameters*/);
    	public void deleteLight(Light* light);
    
    private:
    	typedef std::pair<unsigned int, bool> LightID;
    	std::vector<LightID> m_lights
    };
    * Directional and ambient are derived from Light.

    * When the initialise function is called, it will populate the m_lights array with IDs that can be used for the lightID that is passes to openGL.

    * If a light is created, the std:air changes to be true, if it is deleted, it changes to false(can be used for creating a new light)

    * the destructor for the lights are private and the lights are friends of the factory, so the factory can call the destructor.

    * the factory holds pointers to all the lights created, so that It can delete them if the factory itself is destroyed.

    Is this a good way of doing it, or am I walking down the wrong road here?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    How about something like this:

    Code:
    class LightManager {
       std::map<int, Light*> lights;
    public:
        int createDirectionalLight( // parms);
        int createAmbientLight( // parms );
        void lightOn(int);
        void lightOff(int);
        void deleteLight(int);
    };
    ...where the int is the OpenGL light id. LM keeps a map of lights, keyed by light id. LightManager can shield the client classes from OpenGL altogether if you make the parameters generic enough and consider the returned id a "handle".
    Last edited by medievalelks; 04-16-2009 at 07:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-13-2008, 07:00 PM
  2. Virtual function design pattern
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2008, 07:18 AM
  3. what would be an appropriate design pattern?
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2006, 07:14 AM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM