C
Christopher Pisz
My problem is my derived class is getting called twice instead of the base
and then the derived. I thought this was the purpose for virtuals and
dynamic casting :/
I want my base class to have its method called and then the derived class
have its method called. What am I not understanding?
Int the following code, my Event Tester class is getting called twice for
keyboard events when I step through the debugger:
//------------------------------------------------------------------------------------------
class GFXApplication : public EventHandler
{
public:
GFXApplication();
virtual ~GFXApplication();
virtual void Create(HINSTANCE hInstance);
virtual int Run();
virtual int ProcessEvent(Event * event);
..
..
..
class EventTester : public GFXApplication
{
public:
EventTester();
~EventTester();
int ProcessEvent(Event * event);
..
..
..
//--------------------------------------------------------------------------------------
unsigned EventManager:ostEvent(Event * event)
{
m_eventQueue.push(event);
return static_cast<unsigned>(m_eventQueue.size());
}
//--------------------------------------------------------------------------------------
unsigned EventManager:ostImmediateEvent(Event * event)
{
EventHandlerVector::iterator it;
for(it = m_eventHandlerVectors[event->m_type].begin(); it !=
m_eventHandlerVectors[event->m_type].end(); it++)
{
(*it)->ProcessEvent(event);
}
delete event;
return 0;
}
//--------------------------------------------------------------------------------------
unsigned EventManager::RegisterHandler(EventHandler * handler, EVENT_TYPE
type)
{
m_eventHandlerVectors[type].push_back(handler);
return static_cast<unsigned>(m_eventHandlerVectors[type].size());
}
//--------------------------------------------------------------------------------------
unsigned EventManager::RemoveHandler(EventHandler * handler, EVENT_TYPE
type)
{
EventHandlerVector::iterator result =
std::find(m_eventHandlerVectors[type].begin(),
m_eventHandlerVectors[type].end(), handler);
if(result != m_eventHandlerVectors[type].end())
m_eventHandlerVectors[type].erase(result);
return static_cast<unsigned>(m_eventHandlerVectors[type].size());
}
//--------------------------------------------------------------------------------------
void EventManager::Update()
{
// Have every registered event handler process thier events
while(!m_eventQueue.empty())
{
Event * event = m_eventQueue.front();
m_eventQueue.pop();
EventHandlerVector::iterator it;
for(it = m_eventHandlerVectors[event->m_type].begin(); it !=
m_eventHandlerVectors[event->m_type].end(); it++)
{
(*it)->ProcessEvent(event);
}
delete event;
}
}
..
..
..
//-----------------------------------------------------------------------------
void GFXApplication::Create(HINSTANCE hInstance)
{
// Save the module handle
m_instance = hInstance;
// Register event handling
EventManager::GetInstance()->RegisterHandler(dynamic_cast<GFXApplication
*>(this), EVENT_WINDOWS);
EventManager::GetInstance()->RegisterHandler(dynamic_cast<GFXApplication
*>(this), EVENT_KEYBOARD);
..
..
..
//---------------------------------------------------------------------------
EventTester::EventTester()
:
GFXApplication()
{
EventManager::GetInstance()->RegisterHandler(this, EVENT_KEYBOARD);
EventManager::GetInstance()->RegisterHandler(this, EVENT_MOUSE);
..
..
..
//-------------------------------------------------------------------------------
int GFXApplication:rocessEvent(Event * event)
{
switch(event->m_type)
{
case EVENT_KEYBOARD:
{
// Update the keystates
m_keystates[ event->m_data.keyboardEventData.key ] =
event->m_data.keyboardEventData.pressed;
break;
}
default:
break;
} // End main switch
return 0;
}
..
..
..
int EventTester:rocessEvent(Event * event)
{
switch(event->m_type)
{
// These keyboard events are straight from the message loop and
unsatisfatory
// for controls that allow the key to be held down without accepting the
delay
// between the first keypress and the repeat. There is a minimum of a 250ms
delay
// For those types of controls, like flight steering for example, poll the
// keystate in the PreRender method.
case EVENT_KEYBOARD:
if(event->m_data.keyboardEventData.pressed)
{
switch(event->m_data.keyboardEventData.key)
{
default:
break;
}
}
break;
..
..
..
and then the derived. I thought this was the purpose for virtuals and
dynamic casting :/
I want my base class to have its method called and then the derived class
have its method called. What am I not understanding?
Int the following code, my Event Tester class is getting called twice for
keyboard events when I step through the debugger:
//------------------------------------------------------------------------------------------
class GFXApplication : public EventHandler
{
public:
GFXApplication();
virtual ~GFXApplication();
virtual void Create(HINSTANCE hInstance);
virtual int Run();
virtual int ProcessEvent(Event * event);
..
..
..
class EventTester : public GFXApplication
{
public:
EventTester();
~EventTester();
int ProcessEvent(Event * event);
..
..
..
//--------------------------------------------------------------------------------------
unsigned EventManager:ostEvent(Event * event)
{
m_eventQueue.push(event);
return static_cast<unsigned>(m_eventQueue.size());
}
//--------------------------------------------------------------------------------------
unsigned EventManager:ostImmediateEvent(Event * event)
{
EventHandlerVector::iterator it;
for(it = m_eventHandlerVectors[event->m_type].begin(); it !=
m_eventHandlerVectors[event->m_type].end(); it++)
{
(*it)->ProcessEvent(event);
}
delete event;
return 0;
}
//--------------------------------------------------------------------------------------
unsigned EventManager::RegisterHandler(EventHandler * handler, EVENT_TYPE
type)
{
m_eventHandlerVectors[type].push_back(handler);
return static_cast<unsigned>(m_eventHandlerVectors[type].size());
}
//--------------------------------------------------------------------------------------
unsigned EventManager::RemoveHandler(EventHandler * handler, EVENT_TYPE
type)
{
EventHandlerVector::iterator result =
std::find(m_eventHandlerVectors[type].begin(),
m_eventHandlerVectors[type].end(), handler);
if(result != m_eventHandlerVectors[type].end())
m_eventHandlerVectors[type].erase(result);
return static_cast<unsigned>(m_eventHandlerVectors[type].size());
}
//--------------------------------------------------------------------------------------
void EventManager::Update()
{
// Have every registered event handler process thier events
while(!m_eventQueue.empty())
{
Event * event = m_eventQueue.front();
m_eventQueue.pop();
EventHandlerVector::iterator it;
for(it = m_eventHandlerVectors[event->m_type].begin(); it !=
m_eventHandlerVectors[event->m_type].end(); it++)
{
(*it)->ProcessEvent(event);
}
delete event;
}
}
..
..
..
//-----------------------------------------------------------------------------
void GFXApplication::Create(HINSTANCE hInstance)
{
// Save the module handle
m_instance = hInstance;
// Register event handling
EventManager::GetInstance()->RegisterHandler(dynamic_cast<GFXApplication
*>(this), EVENT_WINDOWS);
EventManager::GetInstance()->RegisterHandler(dynamic_cast<GFXApplication
*>(this), EVENT_KEYBOARD);
..
..
..
//---------------------------------------------------------------------------
EventTester::EventTester()
:
GFXApplication()
{
EventManager::GetInstance()->RegisterHandler(this, EVENT_KEYBOARD);
EventManager::GetInstance()->RegisterHandler(this, EVENT_MOUSE);
..
..
..
//-------------------------------------------------------------------------------
int GFXApplication:rocessEvent(Event * event)
{
switch(event->m_type)
{
case EVENT_KEYBOARD:
{
// Update the keystates
m_keystates[ event->m_data.keyboardEventData.key ] =
event->m_data.keyboardEventData.pressed;
break;
}
default:
break;
} // End main switch
return 0;
}
..
..
..
int EventTester:rocessEvent(Event * event)
{
switch(event->m_type)
{
// These keyboard events are straight from the message loop and
unsatisfatory
// for controls that allow the key to be held down without accepting the
delay
// between the first keypress and the repeat. There is a minimum of a 250ms
delay
// For those types of controls, like flight steering for example, poll the
// keystate in the PreRender method.
case EVENT_KEYBOARD:
if(event->m_data.keyboardEventData.pressed)
{
switch(event->m_data.keyboardEventData.key)
{
default:
break;
}
}
break;
..
..
..