diff --git a/asteroids/MenuOverlay.h b/asteroids/MenuOverlay.h index e6f0bb4..ce8e4f0 100644 --- a/asteroids/MenuOverlay.h +++ b/asteroids/MenuOverlay.h @@ -17,7 +17,7 @@ class MenuOverlay : public Engine::OverlayBase { public: MenuOverlay () { }; - void Init (); + virtual void Init (); virtual ~MenuOverlay() {}; virtual bool OnKeyDown (const SDL_keysym &keysym); diff --git a/asteroids/View.cc b/asteroids/View.cc index 3abc3d3..2d772fa 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -34,13 +34,13 @@ int View::OnInit (int argc, char* argv[]) { mMenuOverlay->SetModel ((Model*) mModel); mMenuOverlay->SetView (this); mMenuOverlay->Init(); - AddOverlay (mMenuOverlay); + mOverlayManager.Register (mMenuOverlay, GameStateMainMenu); // We want the console mConsoleOverlay = boost::shared_ptr (new Engine::SimpleConsoleOverlay); // We also want to display the log bar mConsoleOverlay->SetDrawLogBar (true); - AddOverlay (mConsoleOverlay); + mOverlayManager.Register (mConsoleOverlay, GameStateMainMenu); // This is a simple star field that makes the game so spacy int i; diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 7942131..f8acc54 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -23,6 +23,7 @@ SET ( ENGINE_SRCS PhysicsEntityBase.cc ViewBase.cc EventsBase.cc + OverlayBase.cc Commands.cc DrawingsGL.cc diff --git a/engine/ControllerBase.cc b/engine/ControllerBase.cc index 6d1533b..e295a03 100644 --- a/engine/ControllerBase.cc +++ b/engine/ControllerBase.cc @@ -132,7 +132,7 @@ void ControllerBase::ProcessEvents () { /** \brief Keyboard processing */ bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) { - if (mView->SendKeyDown (keysym)) + if (mView->mOverlayManager.SendKeyDown (keysym)) return true; if (mBindings[keysym.sym].size () != 0) { @@ -145,7 +145,7 @@ bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) { /** \brief Keyboard processing */ bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) { - if (mView->SendKeyUp (keysym)) + if (mView->mOverlayManager.SendKeyUp (keysym)) return true; if (mBindings[keysym.sym].size () != 0) { @@ -164,7 +164,7 @@ bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) { bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) { MouseButton mouse_button = convert_sdl_button (button); - if (mView->SendMouseButtonDown (button, xpos, ypos)) + if (mView->mOverlayManager.SendMouseButtonDown (button, xpos, ypos)) return true; if (mBindings[mouse_button].size () != 0) { @@ -179,7 +179,7 @@ bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) bool ControllerBase::OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { MouseButton mouse_button = convert_sdl_button (button); - if (mView->SendMouseButtonUp (button, xpos, ypos)) + if (mView->mOverlayManager.SendMouseButtonUp (button, xpos, ypos)) return true; if (mBindings[mouse_button].size () != 0) { diff --git a/engine/ModelBase.h b/engine/ModelBase.h index b35187e..4aac708 100644 --- a/engine/ModelBase.h +++ b/engine/ModelBase.h @@ -10,6 +10,7 @@ class Module; class PhysicsBase; class Events; class EntityFactoryBase; +class OverlayManager; struct EntityBase; @@ -110,6 +111,7 @@ class ModelBase : public Module { float mDeltaSec; friend class ViewBase; + friend class OverlayManager; friend class Engine; friend class Controller; }; diff --git a/engine/OverlayBase.h b/engine/OverlayBase.h index 46fb544..77655d3 100644 --- a/engine/OverlayBase.h +++ b/engine/OverlayBase.h @@ -1,17 +1,25 @@ -#ifndef OVERLAY -#define OVERLAY +#ifndef OVERLAYBASE +#define OVERLAYBASE #include #include +#include +#include namespace Engine { +class ModelBase; + +/** \brief Base class for user-interfaces + */ class OverlayBase { public: OverlayBase () {}; virtual ~OverlayBase() {}; + virtual void Init() {}; + virtual bool OnKeyDown (const SDL_keysym &keysym) { return false; }; virtual bool OnKeyUp (const SDL_keysym &keysym) { return false; }; virtual bool OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; }; @@ -24,6 +32,29 @@ class OverlayBase { typedef boost::shared_ptr OverlayBasePtr; -} +/** \brief Takes care of all OverlayBase classes and proxies input and drawing with regard to the current game state of ModelBase + * + * \node You need to set the ModelBase pointer manually by calling OverlayManager::SetModel()! + */ +class OverlayManager { + public: + /** \brief Calls OverlayBase::Init() for all registered Overlays */ + void InitOverlays(); -#endif /* OVERLAY */ + void Draw(); + void Register (OverlayBasePtr overlay, unsigned int game_state); + + /* Input forwarding for the overlays */ + bool SendKeyDown (const SDL_keysym &keysym); + bool SendKeyUp (const SDL_keysym &keysym); + bool SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos); + bool SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos); + + private: + /** \brief Keeps the list of OverlayBase for each game state */ + std::map > mOverlays; +}; + +}; + +#endif /* OVERLAYBASE */ diff --git a/engine/ViewBase.cc b/engine/ViewBase.cc index 902595a..da44367 100644 --- a/engine/ViewBase.cc +++ b/engine/ViewBase.cc @@ -57,8 +57,10 @@ int ViewBase::OnInit (int argc, char* argv[]) { mCurrentFont = mFonts["console.ttf"]; SetFontColor (1., 1., 1.); + // Overlays OverlayBasePtr console_overlay(new SimpleConsoleOverlay); - AddOverlay (console_overlay); + mOverlayManager.Register (console_overlay, 0); + //AddOverlay (console_overlay); mDrawAxis = false; @@ -72,9 +74,6 @@ int ViewBase::OnInit (int argc, char* argv[]) { } void ViewBase::OnDestroy () { - while (mOverlays.size() > 0) - mOverlays.pop_back(); - std::map::iterator iter; for (iter = mFonts.begin(); iter != mFonts.end(); ++iter) { @@ -178,10 +177,7 @@ void ViewBase::Draw () { DrawWorld (); - std::vector::iterator overlay_iter; - for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { - (*overlay_iter)->Draw(); - } + mOverlayManager.Draw(); // and update the screen SDL_GL_SwapBuffers (); @@ -197,10 +193,13 @@ bool ViewBase::LoadFont (const char *font_name, float point_size) { OGLFT::Monochrome *font = new OGLFT::Monochrome (font_path.c_str(), point_size); if ( font == 0 || !font->isValid() ) { LogError ("Could not load font %s!", font_path.c_str()); + return false; } font->setForegroundColor(1., 1., 1.); mFonts.insert(std::make_pair(font_name, font)); + + return true; } void ViewBase::SelectFont (const char *font) { @@ -311,6 +310,7 @@ void ViewBase::Resize (int width, int height) { } } +/* bool ViewBase::SendKeyDown (const SDL_keysym &keysym) { std::vector::iterator overlay_iter; for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { @@ -350,6 +350,7 @@ bool ViewBase::SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; } +*/ /* * Global functions diff --git a/engine/ViewBase.h b/engine/ViewBase.h index c81d4a8..53415db 100644 --- a/engine/ViewBase.h +++ b/engine/ViewBase.h @@ -39,6 +39,10 @@ class ViewBase : public Module{ bool LoadFont (const char *font_name, float point_size=12); void SelectFont (const char *font_name); void SetFontJustification (FontJustification justification); + /** \brief Sets the color of the current font + * + * \note This function should be avoided as the glyphs have to be re-rendered + */ void SetFontColor (float r, float g, float b); /** \brief Draws a string at the given position using current projection * and modelview matrices */ @@ -60,13 +64,15 @@ class ViewBase : public Module{ bool GetDrawGrid () { return mDrawGrid; }; void SetGridSize (int x, int z) { mGridSizeX = x; mGridSizeZ = z; } - void AddOverlay (OverlayBasePtr overlay) { mOverlays.push_back (overlay); }; +// void AddOverlay (OverlayBasePtr overlay) { mOverlays.push_back (overlay); }; /* Input forwarding for the overlays */ + /* bool SendKeyDown (const SDL_keysym &keysym); bool SendKeyUp (const SDL_keysym &keysym); bool SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos); bool SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos); + */ protected: /** \brief Initializes the system */ @@ -86,7 +92,9 @@ class ViewBase : public Module{ ModelBase *mModel; CameraBase *mCamera; - std::vector mOverlays; + OverlayManager mOverlayManager; + + // std::vector mOverlays; /** \brief The height of the canvas we're drawing on */ unsigned int mWindowHeight; @@ -107,6 +115,7 @@ class ViewBase : public Module{ std::map mFonts; friend class Engine; + friend class ControllerBase; }; }