Skip to content

SDL_AddEventWatch Usage Example

PeterJFerrarotto edited this page Sep 28, 2016 · 5 revisions
// TTcards/src/PlayState.hpp
int mouse_filter( void* data, SDL_Event* ev );

// TTcards.git/src/Playstate.cpp
int mouse_filter( void* data, SDL_Event* ev )
{
  nom::Point2i* m_data = static_cast<nom::Point2i*>(data);
  // NOM_DUMP(ev->button.x);
  // NOM_DUMP(ev->button.y);
  m_data->x = ev->button.x;
  m_data->y = ev->button.y;

  return 0; // Success
}

void PlayState::on_init ( nom::void_ptr data )
{
  // ...

  nom::Point2i* m_data = static_cast<nom::Point2i*>(new Point2i());
  SDL_AddEventWatch( mouse_filter, &m_data );

  // ...
}

void PlayState::on_mouse_left_button_down( const nom::Event& ev )
{
  // This could also be put inside the Game class within the on_event loop
  nom::Point2i* m_data = static_cast<nom::Point2i*>(new Point2i());
  mouse_filter(m_data, &this->game->event );

  NOM_DUMP(m_data->x);
  NOM_DUMP(m_data->y);
}
Clone this wiki locally