Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

GameState.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "GameState.h"
00010 
00011 #include "StateManager.h"
00012 #include "InputHandler.h"
00013 #include "MultiDrawer.h"
00014 
00015 #include "Log.h"
00016 #include "LogicException.h"
00017 #include "InputAgent.h"
00018 #include "VideoAgent.h"
00019 #include "MessagerAgent.h"
00020 #include "SimpleMsg.h"
00021 
00022 //-----------------------------------------------------------------
00023 GameState::GameState()
00024 {
00025     m_nextState = NULL;
00026     m_handler = NULL;
00027     m_active = false;
00028     m_onBg = false;
00029     m_drawer = new MultiDrawer();
00030 }
00031 //-----------------------------------------------------------------
00032 GameState::~GameState()
00033 {
00034     if (m_handler) {
00035         delete m_handler;
00036     }
00037     delete m_drawer;
00038 }
00039 //-----------------------------------------------------------------
00040 /**
00041  * Obtain input handler.
00042  * @param new_handler new input handler
00043  */
00044     void
00045 GameState::takeHandler(InputHandler *new_handler)
00046 {
00047     if (m_handler) {
00048         delete m_handler;
00049     }
00050     m_handler = new_handler;
00051 }
00052 //-----------------------------------------------------------------
00053 /**
00054  * Returns wrapped input.
00055  */
00056     const InputProvider *
00057 GameState::getInput()
00058 {
00059     return m_handler;
00060 }
00061 //-----------------------------------------------------------------
00062     void
00063 GameState::initState(StateManager *manager)
00064 {
00065     LOG_DEBUG(ExInfo("initState").addInfo("name", getName()));
00066     MessagerAgent::agent()->addListener(this);
00067     m_manager = manager;
00068     m_active = true;
00069     m_onBg = false;
00070     own_initState();
00071 }
00072 //-----------------------------------------------------------------
00073 /**
00074  * @throws LogicException when state is not active
00075  */
00076     void
00077 GameState::updateState()
00078 {
00079     if (!m_active) {
00080         throw LogicException(ExInfo("update - state is not active")
00081                 .addInfo("name", getName()));
00082     }
00083 
00084     own_updateState();
00085 }
00086 //-----------------------------------------------------------------
00087 /**
00088  * @throws LogicException when state is not active
00089  */
00090     void
00091 GameState::pauseState()
00092 {
00093     if (!m_active) {
00094         throw LogicException(ExInfo("pause - state is not active")
00095                 .addInfo("name", getName()));
00096     }
00097 
00098     own_pauseState();
00099     m_active = false;
00100     m_onBg = false;
00101 }
00102 //-----------------------------------------------------------------
00103 /**
00104  * Reactivate state after pause.
00105  * @throws LogicException when state is already active
00106  */
00107     void
00108 GameState::resumeState()
00109 {
00110     if (m_active) {
00111         throw LogicException(ExInfo("resume - state is already active")
00112                 .addInfo("name", getName()));
00113     }
00114     m_active = true;
00115     own_resumeState();
00116 }
00117 //-----------------------------------------------------------------
00118 /**
00119  * Clean state after run.
00120  * @throws LogicException when state is not active
00121  */
00122     void
00123 GameState::cleanState()
00124 {
00125     LOG_DEBUG(ExInfo("cleanState").addInfo("name", getName()));
00126     if (!m_active) {
00127         throw LogicException(ExInfo("clean - state is not active")
00128                 .addInfo("name", getName()));
00129     }
00130     own_cleanState();
00131     unHandlers();
00132 
00133     m_active = false;
00134     m_onBg = false;
00135     m_manager = NULL;
00136     removeWatchers();
00137     MessagerAgent::agent()->removeListener(getName());
00138 }
00139 //-----------------------------------------------------------------
00140     void
00141 GameState::quitState()
00142 {
00143     if (m_nextState) {
00144         changeState(m_nextState);
00145     }
00146     else {
00147         m_manager->popState(this);
00148     }
00149 }
00150 //-----------------------------------------------------------------
00151     void
00152 GameState::pushState(GameState *new_state)
00153 {
00154     m_manager->pushState(this, new_state);
00155 }
00156 //-----------------------------------------------------------------
00157     void
00158 GameState::changeState(GameState *new_state)
00159 {
00160     m_manager->changeState(this, new_state);
00161 }
00162 //-----------------------------------------------------------------
00163     void
00164 GameState::noteBg()
00165 {
00166     LOG_DEBUG(ExInfo("noteBg").addInfo("name", getName()));
00167     own_noteBg();
00168     m_onBg = true;
00169 }
00170 //-----------------------------------------------------------------
00171     void
00172 GameState::noteFg()
00173 {
00174     LOG_DEBUG(ExInfo("noteFg").addInfo("name", getName()));
00175     m_onBg = false;
00176     own_noteFg();
00177 }
00178 //-----------------------------------------------------------------
00179 /**
00180  * Install own video and input handler.
00181  */
00182     void
00183 GameState::installHandlers()
00184 {
00185     LOG_DEBUG(ExInfo("installHandlers").addInfo("state", getName()));
00186     InputAgent::agent()->installHandler(m_handler);
00187     VideoAgent::agent()->acceptDrawer(m_drawer);
00188 }
00189 //-----------------------------------------------------------------
00190 /**
00191  * Uninstall own video and input handler.
00192  */
00193     void
00194 GameState::unHandlers()
00195 {
00196     InputAgent::agent()->installHandler(NULL);
00197     VideoAgent::agent()->removeDrawer(m_drawer);
00198 }
00199 
00200 //-----------------------------------------------------------------
00201 /**
00202  * Add new drawable to drawers fifo.
00203  * NOTE: order is important,
00204  * the first inserted drawer will be on background
00205  */
00206     void
00207 GameState::registerDrawable(Drawable *drawable)
00208 {
00209     m_drawer->acceptDrawer(drawable);
00210 }
00211 //-----------------------------------------------------------------
00212     void
00213 GameState::deregisterDrawable(const Drawable *drawable)
00214 {
00215     m_drawer->removeDrawer(drawable);
00216 }
00217 //-----------------------------------------------------------------
00218 /**
00219  * Handle incoming message.
00220  * Messages:
00221  * - quit ... quit state
00222  */
00223     void
00224 GameState::receiveSimple(const SimpleMsg *msg)
00225 {
00226     if (msg->equalsName("quit")) {
00227         quitState();
00228     }
00229     else {
00230         LOG_WARNING(ExInfo("unknown msg")
00231                 .addInfo("msg", msg->toString()));
00232     }
00233 }
00234 
00235 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2