   2d engine design
  Is it a good idea to use the singleton pattern in a 2d engine? The reason I ask is because I have seen alot of engines do so.Another question..Currenty in my engine I have a Image class witch can load and draw images. Everytime I call draw() I bind the texture and call glBegin and glEnd.. Would it be a good idea to not draw the images when draw is called but instead save wich images needs to be drawn and then have a function wich sorts the images by texture and draws them all in one big swoop?Something like this:image1.draw();image2.draw();engine->drawImages();Thanks.. =) 
  1) It is a matter of preference, personally I''d say no but others likely have other opinions.2) Depends,  are you using the z-buffer to determine layers?  If not then sorting on texture/state probably isn''t worth it (in 2D you typically won''t be changing state often enough in one frame to warrant it).  Without the z-buffer you have a specific order you need to draw in anyway (i.e. the painters algorithm) so potential for state optimization is limited.  That said it can still be a good idea to collect your drawing commands into a central place and draw them all at once, but whether it will help *your* system you''ll have to find out yourself (hint: KISS  ) ;  Generally it''s better to plan drawing orders in 2D engines by hand - any algorithm you use will have limitations that can only be avoided by knowing your own engine/game.EG particle systems only need one texture binding and can store all their data into a large array and use vertex array drawing instead of begin/end which should be much more efficient.As mentioned above, you need a specific order of drawing in 2D so you can''t really sort into texture groupings.  You can however sort some together because only you know what needs to be drawn when.  Chances are you won''t get massive speed increase though (excpet maybe on older video cards).With 2D your best for optimisation is in deciding what NOT to draw.  Dirty Rectangles can be good for this (basically you divide the screen into sections and only update each section if something in it has changed since the last frame) ;quote:Original post by JuNC 1) It is a matter of preference, personally I'd say no but others likely have other opinions.<hand state="raised" />I would be one with another opinion. If there is only supposed to be one instance of your Engine class in the entire application, then I would say yes to implementing the Singleton pattern. When you implement the Singleton pattern, you might find yourself using a lot less static global variables in your engine. It also comes down to which subsystems we are talking about. For the main "brain" class, I think it would be a good idea. The access to the other subsystems could be done through the brain class. For example, if you have a message bus implemented under the main engine class, you might be able to have clean code like the following with the Singleton pattern (the following is just pseudo-code though):Engine.GetInstance().MessageBus.SendMessage(myMessage);This is just my taste though. Like JuNC points out, it is simply a matter of preference. I hope this helps!   ----------------------------------"English? English? Why would I need to know English? I'm never going to England!"  [edited by - bL0wF1sH on February 15, 2004 11:46:42 PM]