text: #include "Globals.h"
  #include "Application.h"
  #include "ModuleInput.h"
  
  #include "SDL\include\SDL.h"
  
  
  ModuleInput::ModuleInput(Application* app, bool start_enabled) : Module(app, start_enabled)
  {
  	keyboard = new KEY_STATE[MAX_KEYS];
  	memset(keyboard, KEY_IDLE, sizeof(KEY_STATE) * MAX_KEYS);
  	memset(mouse_buttons, KEY_IDLE, sizeof(KEY_STATE) * MAX_MOUSE_BUTTONS);
  }
  
  // Destructor
  ModuleInput::~ModuleInput()
  {
  	delete[] keyboard;
  }
  
  // Called before render is available
  bool ModuleInput::Init()
  {
  	LOG("Init SDL input event system");
  	bool ret = true;
  	SDL_Init(0);
  
  	if(SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
  	{
  		LOG("SDL_EVENTS could not initialize! SDL_Error: %s\n", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }
  
  // Called every draw update
  update_status ModuleInput::PreUpdate()
  {
  	SDL_PumpEvents();
  
  	const Uint8* keys = SDL_GetKeyboardState(NULL);
  	
  	for(int i = 0; i < MAX_KEYS; ++i)
  	{
  		if(keys[i] == 1)
  		{
  			if(keyboard[i] == KEY_IDLE)
  				keyboard[i] = KEY_DOWN;
  			else
  				keyboard[i] = KEY_REPEAT;
  		}
  		else
  		{
  			if(keyboard[i] == KEY_REPEAT || keyboard[i] == KEY_DOWN)
  				keyboard[i] = KEY_UP;
  			else
  				keyboard[i] = KEY_IDLE;
  		}
  	}
  
  	Uint32 buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
  	mouse_x /= SCREEN_SIZE;
  	mouse_y /= SCREEN_SIZE;
  
  	for(int i = 0; i < MAX_MOUSE_BUTTONS; ++i)
  	{
  		if(buttons & SDL_BUTTON(i))
  		{
  			if(mouse_buttons[i] == KEY_IDLE)
  				mouse_buttons[i] = KEY_DOWN;
  			else
  				mouse_buttons[i] = KEY_REPEAT;
  		}
  		else
  		{
  			if(mouse_buttons[i] == KEY_REPEAT || mouse_buttons[i] == KEY_DOWN)
  				mouse_buttons[i] = KEY_UP;
  			else
  				mouse_buttons[i] = KEY_IDLE;
  		}
  	}
  
  	if(keyboard[SDL_SCANCODE_ESCAPE] == KEY_UP)
  		return UPDATE_STOP;
  
  	return UPDATE_CONTINUE;
  }
  
  // Called before quitting
  bool ModuleInput::CleanUp()
  {
  	LOG("Quitting SDL input event subsystem");
  	SDL_QuitSubSystem(SDL_INIT_EVENTS);
  	return true;
  }