Example #1
0
void InputSystem::update(float dt) {
	while (SDL_PollEvent(&e)) {
		if (e.type == SDL_QUIT)
			engine->quit();

		if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
			const char* key = SDL_GetKeyName(e.key.keysym.sym);

			if (key_binds.find(key) != key_binds.end()) {
				KeyBind kbind = key_binds[key];
				KeyEvent kevent = key_callbacks[kbind.keyevent];

				if (kevent.callback != 0 && kevent.type == e.type)
					kevent.callback();
			}

			if (axis_binds.find(key) != axis_binds.end()) {
				AxisBind abind = axis_binds[key];
				AxisEvent aevent = axis_callbacks[abind.axisevent];
				
				float value = (e.type == KEY_PRESSED) ? abind.scale : 0;
				if (aevent.callback != 0) 
					aevent.callback(value);
			}
		}

		if (e.type == SDL_MOUSEMOTION) {
			if (axis_binds.find("MouseX") != axis_binds.end()) {
				AxisBind abind = axis_binds["MouseX"];
				AxisEvent aevent = axis_callbacks[abind.axisevent];

				if (aevent.callback != 0)
					aevent.callback((float)e.motion.xrel*abind.scale);
			}
			
			if (axis_binds.find("MouseY") != axis_binds.end()) {
				AxisBind abind = axis_binds["MouseY"];
				AxisEvent aevent = axis_callbacks[abind.axisevent];

				if (aevent.callback != 0)
					aevent.callback((float)e.motion.yrel*abind.scale);
			}
		}
	}
}