added OverlayManager to ViewBase

main
Martin Felis (lola) 2010-06-06 00:58:11 +02:00
parent 54e14b41af
commit 50089c9755
8 changed files with 65 additions and 21 deletions

View File

@ -17,7 +17,7 @@ class MenuOverlay : public Engine::OverlayBase {
public: public:
MenuOverlay () { MenuOverlay () {
}; };
void Init (); virtual void Init ();
virtual ~MenuOverlay() {}; virtual ~MenuOverlay() {};
virtual bool OnKeyDown (const SDL_keysym &keysym); virtual bool OnKeyDown (const SDL_keysym &keysym);

View File

@ -34,13 +34,13 @@ int View::OnInit (int argc, char* argv[]) {
mMenuOverlay->SetModel ((Model*) mModel); mMenuOverlay->SetModel ((Model*) mModel);
mMenuOverlay->SetView (this); mMenuOverlay->SetView (this);
mMenuOverlay->Init(); mMenuOverlay->Init();
AddOverlay (mMenuOverlay); mOverlayManager.Register (mMenuOverlay, GameStateMainMenu);
// We want the console // We want the console
mConsoleOverlay = boost::shared_ptr<Engine::SimpleConsoleOverlay> (new Engine::SimpleConsoleOverlay); mConsoleOverlay = boost::shared_ptr<Engine::SimpleConsoleOverlay> (new Engine::SimpleConsoleOverlay);
// We also want to display the log bar // We also want to display the log bar
mConsoleOverlay->SetDrawLogBar (true); mConsoleOverlay->SetDrawLogBar (true);
AddOverlay (mConsoleOverlay); mOverlayManager.Register (mConsoleOverlay, GameStateMainMenu);
// This is a simple star field that makes the game so spacy // This is a simple star field that makes the game so spacy
int i; int i;

View File

@ -23,6 +23,7 @@ SET ( ENGINE_SRCS
PhysicsEntityBase.cc PhysicsEntityBase.cc
ViewBase.cc ViewBase.cc
EventsBase.cc EventsBase.cc
OverlayBase.cc
Commands.cc Commands.cc
DrawingsGL.cc DrawingsGL.cc

View File

@ -132,7 +132,7 @@ void ControllerBase::ProcessEvents () {
/** \brief Keyboard processing */ /** \brief Keyboard processing */
bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) { bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
if (mView->SendKeyDown (keysym)) if (mView->mOverlayManager.SendKeyDown (keysym))
return true; return true;
if (mBindings[keysym.sym].size () != 0) { if (mBindings[keysym.sym].size () != 0) {
@ -145,7 +145,7 @@ bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
/** \brief Keyboard processing */ /** \brief Keyboard processing */
bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) { bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) {
if (mView->SendKeyUp (keysym)) if (mView->mOverlayManager.SendKeyUp (keysym))
return true; return true;
if (mBindings[keysym.sym].size () != 0) { 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) { bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) {
MouseButton mouse_button = convert_sdl_button (button); MouseButton mouse_button = convert_sdl_button (button);
if (mView->SendMouseButtonDown (button, xpos, ypos)) if (mView->mOverlayManager.SendMouseButtonDown (button, xpos, ypos))
return true; return true;
if (mBindings[mouse_button].size () != 0) { 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) { bool ControllerBase::OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) {
MouseButton mouse_button = convert_sdl_button (button); MouseButton mouse_button = convert_sdl_button (button);
if (mView->SendMouseButtonUp (button, xpos, ypos)) if (mView->mOverlayManager.SendMouseButtonUp (button, xpos, ypos))
return true; return true;
if (mBindings[mouse_button].size () != 0) { if (mBindings[mouse_button].size () != 0) {

View File

@ -10,6 +10,7 @@ class Module;
class PhysicsBase; class PhysicsBase;
class Events; class Events;
class EntityFactoryBase; class EntityFactoryBase;
class OverlayManager;
struct EntityBase; struct EntityBase;
@ -110,6 +111,7 @@ class ModelBase : public Module {
float mDeltaSec; float mDeltaSec;
friend class ViewBase; friend class ViewBase;
friend class OverlayManager;
friend class Engine; friend class Engine;
friend class Controller; friend class Controller;
}; };

View File

@ -1,17 +1,25 @@
#ifndef OVERLAY #ifndef OVERLAYBASE
#define OVERLAY #define OVERLAYBASE
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <map>
#include <list>
namespace Engine { namespace Engine {
class ModelBase;
/** \brief Base class for user-interfaces
*/
class OverlayBase { class OverlayBase {
public: public:
OverlayBase () {}; OverlayBase () {};
virtual ~OverlayBase() {}; virtual ~OverlayBase() {};
virtual void Init() {};
virtual bool OnKeyDown (const SDL_keysym &keysym) { return false; }; virtual bool OnKeyDown (const SDL_keysym &keysym) { return false; };
virtual bool OnKeyUp (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; }; virtual bool OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; };
@ -24,6 +32,29 @@ class OverlayBase {
typedef boost::shared_ptr<OverlayBase> OverlayBasePtr; typedef boost::shared_ptr<OverlayBase> 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<unsigned int, std::list<OverlayBasePtr> > mOverlays;
};
};
#endif /* OVERLAYBASE */

View File

@ -57,8 +57,10 @@ int ViewBase::OnInit (int argc, char* argv[]) {
mCurrentFont = mFonts["console.ttf"]; mCurrentFont = mFonts["console.ttf"];
SetFontColor (1., 1., 1.); SetFontColor (1., 1., 1.);
// Overlays
OverlayBasePtr console_overlay(new SimpleConsoleOverlay); OverlayBasePtr console_overlay(new SimpleConsoleOverlay);
AddOverlay (console_overlay); mOverlayManager.Register (console_overlay, 0);
//AddOverlay (console_overlay);
mDrawAxis = false; mDrawAxis = false;
@ -72,9 +74,6 @@ int ViewBase::OnInit (int argc, char* argv[]) {
} }
void ViewBase::OnDestroy () { void ViewBase::OnDestroy () {
while (mOverlays.size() > 0)
mOverlays.pop_back();
std::map<std::string, OGLFT::Monochrome*>::iterator iter; std::map<std::string, OGLFT::Monochrome*>::iterator iter;
for (iter = mFonts.begin(); iter != mFonts.end(); ++iter) { for (iter = mFonts.begin(); iter != mFonts.end(); ++iter) {
@ -178,10 +177,7 @@ void ViewBase::Draw () {
DrawWorld (); DrawWorld ();
std::vector<OverlayBasePtr>::iterator overlay_iter; mOverlayManager.Draw();
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) {
(*overlay_iter)->Draw();
}
// and update the screen // and update the screen
SDL_GL_SwapBuffers (); 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); OGLFT::Monochrome *font = new OGLFT::Monochrome (font_path.c_str(), point_size);
if ( font == 0 || !font->isValid() ) { if ( font == 0 || !font->isValid() ) {
LogError ("Could not load font %s!", font_path.c_str()); LogError ("Could not load font %s!", font_path.c_str());
return false;
} }
font->setForegroundColor(1., 1., 1.); font->setForegroundColor(1., 1., 1.);
mFonts.insert(std::make_pair<std::string, OGLFT::Monochrome*>(font_name, font)); mFonts.insert(std::make_pair<std::string, OGLFT::Monochrome*>(font_name, font));
return true;
} }
void ViewBase::SelectFont (const char *font) { void ViewBase::SelectFont (const char *font) {
@ -311,6 +310,7 @@ void ViewBase::Resize (int width, int height) {
} }
} }
/*
bool ViewBase::SendKeyDown (const SDL_keysym &keysym) { bool ViewBase::SendKeyDown (const SDL_keysym &keysym) {
std::vector<OverlayBasePtr>::iterator overlay_iter; std::vector<OverlayBasePtr>::iterator overlay_iter;
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); 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; return false;
} }
*/
/* /*
* Global functions * Global functions

View File

@ -39,6 +39,10 @@ class ViewBase : public Module{
bool LoadFont (const char *font_name, float point_size=12); bool LoadFont (const char *font_name, float point_size=12);
void SelectFont (const char *font_name); void SelectFont (const char *font_name);
void SetFontJustification (FontJustification justification); 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); void SetFontColor (float r, float g, float b);
/** \brief Draws a string at the given position using current projection /** \brief Draws a string at the given position using current projection
* and modelview matrices */ * and modelview matrices */
@ -60,13 +64,15 @@ class ViewBase : public Module{
bool GetDrawGrid () { return mDrawGrid; }; bool GetDrawGrid () { return mDrawGrid; };
void SetGridSize (int x, int z) { mGridSizeX = x; mGridSizeZ = z; } 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 */ /* Input forwarding for the overlays */
/*
bool SendKeyDown (const SDL_keysym &keysym); bool SendKeyDown (const SDL_keysym &keysym);
bool SendKeyUp (const SDL_keysym &keysym); bool SendKeyUp (const SDL_keysym &keysym);
bool SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos); bool SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos);
bool SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos); bool SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos);
*/
protected: protected:
/** \brief Initializes the system */ /** \brief Initializes the system */
@ -86,7 +92,9 @@ class ViewBase : public Module{
ModelBase *mModel; ModelBase *mModel;
CameraBase *mCamera; CameraBase *mCamera;
std::vector<OverlayBasePtr> mOverlays; OverlayManager mOverlayManager;
// std::vector<OverlayBasePtr> mOverlays;
/** \brief The height of the canvas we're drawing on */ /** \brief The height of the canvas we're drawing on */
unsigned int mWindowHeight; unsigned int mWindowHeight;
@ -107,6 +115,7 @@ class ViewBase : public Module{
std::map<std::string, OGLFT::Monochrome*> mFonts; std::map<std::string, OGLFT::Monochrome*> mFonts;
friend class Engine; friend class Engine;
friend class ControllerBase;
}; };
} }