Esempio n. 1
0
	void InputManager::HandleEvent(const SDL_Event& event)
	{
		switch (event.type)
		{
		case SDL_KEYDOWN:
		{
			if (event.key.repeat == false)
			{
				KeyboardEventType type = KeyboardEventType::KEY_PRESSED;
				KeyboardInputEvent keyboardEvent(event.key.keysym.sym, type, 0, 0); // todo : pass in the cursor position so we can raycast from there.
				NotifyKeyboardListener(keyboardEvent);
			}
			break;
		}
		case SDL_KEYUP:
		{
			if (event.key.repeat == false)
			{
				KeyboardEventType type = KeyboardEventType::KEY_RELEASED;
				KeyboardInputEvent keyboardEvent(event.key.keysym.sym, type, 0, 0); // todo : pass in the cursor position so we can raycast from there.
				NotifyKeyboardListener(keyboardEvent);
			}
			break;
		}
		}
	}
Esempio n. 2
0
int processSdlEvents(void) {
    SDL_Event event;

    resetEvents();
    while (SDL_PollEvent(&event)) {
        switch(event.type) {
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym) {
            case SDLK_ESCAPE:
                return 1;
                break;
            default:
                keyboardEvent(&event.key,1);
                break;
            }
            break;
        case SDL_KEYUP:
            keyboardEvent(&event.key,0);
            break;
        case SDL_MOUSEMOTION:
            mouseMovedEvent(event.motion.x,event.motion.y,
                       event.motion.xrel,event.motion.yrel);
            break;
        case SDL_MOUSEBUTTONDOWN:
            mouseButtonEvent(event.button.button,1);
            break;
        case SDL_MOUSEBUTTONUP:
            mouseButtonEvent(event.button.button,0);
            break;
        case SDL_QUIT:
            exit(0);
            break;
        }
        /* If the next event to process is of type KEYUP or
         * MOUSEBUTTONUP we want to stop processing here, so that
         * a fast up/down event be noticed by Lua. */
        if (SDL_PeepEvents(&event,1,SDL_PEEKEVENT,SDL_ALLEVENTS)) {
            if (event.type == SDL_KEYUP ||
                event.type == SDL_MOUSEBUTTONUP)
                break; /* Go to lua before processing more. */
        }
    }

    /* Call the setup function, only the first time. */
    if (l81.epoch == 0) {
        setup();
        if (l81.luaerr) return l81.luaerr;
    }
    /* Call the draw function at every iteration.  */
    draw();
    l81.epoch++;
    /* Refresh the screen */
    if (l81.opt_show_fps) showFPS();
    SDL_Flip(l81.fb->screen);
    /* Wait some time if the frame was produced in less than 1/FPS seconds. */
    SDL_framerateDelay(&l81.fb->fps_mgr);
    /* Stop execution on error */
    return l81.luaerr;
}
Esempio n. 3
0
void idle() {
	keyboardEvent();
	int time_ = glutGet(GLUT_ELAPSED_TIME);
	float angle = time_ / 1000.0 * 10;

	Vector3 pos = player.getPosition();
	float angX = player.getRotationX(),
		  angY = player.getRotationY();

	Vector3 cpos = cube.getPosition(),
			cpos2= cube2.getPosition();


	player.updateForce();

	Matrix4 model = Matrix4().Translate(cpos).Rotate(1.0f, cube.getRotation()),
			model2 = Matrix4().Translate(cpos2).Rotate(1.0f, cube2.getRotation());
	Matrix4 projection = Matrix4().Perspective(45.0f, 1.0f*screen_width/screen_height, 0.1f, 50.0f),
			view = Matrix4().lookAt(pos, Vector3(pos.x-cos(angX), pos.y + sin(angY), pos.z + sin(angX)), Vector3(0.0f, 1.0f, 0.0f));
	Matrix4 anim = Matrix4().RotateA(angle*3.0f, Vector3(1, 0, 0)) * 	// X axis
					Matrix4().RotateA(angle*2.0f, Vector3(0, 1, 0)) *	// Y axis
					Matrix4().RotateA(angle*4.0f, Vector3(0, 0, 1));	// Z axis


	Matrix4 m_transform = projection * view * model,
			m_transform2= projection * view * model2;// * anim;

	glUseProgram(cube.getProgram());
	glUniformMatrix4fv(cube.getUniform("m_transform"), 1, GL_FALSE, m_transform);

	glUseProgram(cube2.getProgram());
	glUniformMatrix4fv(cube2.getUniform("m_transform"), 1, GL_FALSE, m_transform2);

	glutPostRedisplay();
}
Esempio n. 4
0
bool Screen::keyCallbackEvent(int key, int scancode, int action, int mods) {
    mLastInteraction = glfwGetTime();
    try {
        return keyboardEvent(key, scancode, action, mods);
    } catch (const std::exception &e) {
        std::cerr << "Caught exception in event handler: " << e.what() << std::endl;
        abort();
    }
}
Esempio n. 5
0
bool Screen::charCallbackEvent(unsigned int codepoint) {
    mLastInteraction = glfwGetTime();
    try {
        return keyboardEvent(codepoint);
    } catch (const std::exception &e) {
        std::cerr << "Caught exception in event handler: " << e.what()
                  << std::endl;
        abort();
    }

    return false;
}
Esempio n. 6
0
void Camera::onEvent(Event& e)
{
	if (e.IsInCategory(EventCategory::EventCategoryKeyboard)) {
		keyboardEvent(dynamic_cast<KeyEvent&>(e));
		toString();
	}
	else if (e.IsInCategory(EventCategory::EventCategoryMouse)) {
		mouseEvent(dynamic_cast<MouseMovedEvent&>(e));
	}
	else if (e.IsInCategory(EventCategory::EventCategoryMouseButton)) {
		mouseButtonEvent(e);
	}
}
Esempio n. 7
0
bool CustomEventFilter::eventFilter(QObject *obj, QEvent *e)
{
    if (e->type() == QEvent::KeyPress)
    {
        QKeyEvent *ev= static_cast<QKeyEvent*>(e);
        if(ev->key() == Qt::Key_Return ||
                ev->key() == Qt::Key_Tab ||
                ev->key() == Qt::Key_Escape)
        {
            emit keyboardEvent(ev->key(),ev->modifiers());
            return true;
        }

    }
    return QObject::eventFilter(obj, e);
}
Esempio n. 8
0
Common::Error Sword2Engine::run() {
	// Get some falling RAM and put it in your pocket, never let it slip
	// away

	_debugger = NULL;
	_sound = NULL;
	_fontRenderer = NULL;
	_screen = NULL;
	_mouse = NULL;
	_logic = NULL;
	_resman = NULL;
	_memory = NULL;

	initGraphics(640, 480, true);
	_screen = new Screen(this, 640, 480);

	// Create the debugger as early as possible (but not before the
	// screen object!) so that errors can be displayed in it. In
	// particular, we want errors about missing files to be clearly
	// visible to the user.

	_debugger = new Debugger(this);

	_memory = new MemoryManager(this);
	_resman = new ResourceManager(this);

	if (!_resman->init())
		return Common::kUnknownError;

	_logic = new Logic(this);
	_fontRenderer = new FontRenderer(this);
	_sound = new Sound(this);
	_mouse = new Mouse(this);

	registerDefaultSettings();
	readSettings();

	initStartMenu();

	// During normal gameplay, we care neither about mouse button releases
	// nor the scroll wheel.
	setInputEventFilter(RD_LEFTBUTTONUP | RD_RIGHTBUTTONUP | RD_WHEELUP | RD_WHEELDOWN);

	setupPersistentResources();
	initialiseFontResourceFlags();

	if (_features & GF_DEMO)
		_logic->writeVar(DEMO, 1);
	else
		_logic->writeVar(DEMO, 0);

	if (_saveSlot != -1) {
		if (saveExists(_saveSlot))
			restoreGame(_saveSlot);
		else {
			RestoreDialog dialog(this);
			if (!dialog.runModal())
				startGame();
		}
	} else if (!_bootParam && saveExists() && !isPsx()) { // Initial load/restart panel disabled in PSX
		int32 pars[2] = { 221, FX_LOOP };                 // version because of missing panel resources
		bool result;

		_mouse->setMouse(NORMAL_MOUSE_ID);
		_logic->fnPlayMusic(pars);

		StartDialog dialog(this);

		result = (dialog.runModal() != 0);

		// If the game is started from the beginning, the cutscene
		// player will kill the music for us. Otherwise, the restore
		// will either have killed the music, or done a crossfade.

		if (shouldQuit())
			return Common::kNoError;

		if (result)
			startGame();
	} else
		startGame();

	_screen->initialiseRenderCycle();

	while (1) {
		_debugger->onFrame();

		// Handle GMM Loading
		if (_gmmLoadSlot != -1) {

			// Hide mouse cursor and fade screen
			_mouse->hideMouse();
			_screen->fadeDown();

			// Clean up and load game
			_logic->_router->freeAllRouteMem();

			// TODO: manage error handling
			restoreGame(_gmmLoadSlot);

			// Reset load slot
			_gmmLoadSlot = -1;

			// Show mouse
			_mouse->addHuman();
		}

		KeyboardEvent *ke = keyboardEvent();

		if (ke) {
			if ((ke->kbd.hasFlags(Common::KBD_CTRL) && ke->kbd.keycode == Common::KEYCODE_d) || ke->kbd.ascii == '#' || ke->kbd.ascii == '~') {
				_debugger->attach();
			} else if (ke->kbd.hasFlags(0) || ke->kbd.hasFlags(Common::KBD_SHIFT)) {
				switch (ke->kbd.keycode) {
				case Common::KEYCODE_p:
					if (isPaused()) {
						_screen->dimPalette(false);
						pauseEngine(false);
					} else {
						pauseEngine(true);
						_screen->dimPalette(true);
					}
					break;
#if 0
				// Disabled because of strange rumors about the
				// credits running spontaneously every few
				// minutes.
				case Common::KEYCODE_c:
					if (!_logic->readVar(DEMO) && !_mouse->isChoosing()) {
						ScreenInfo *screenInfo = _screen->getScreenInfo();
						_logic->fnPlayCredits(NULL);
						screenInfo->new_palette = 99;
					}
					break;
#endif
				default:
					break;
				}
			}
		}

		// skip GameCycle if we're paused
		if (!isPaused()) {
			_gameCycle++;
			gameCycle();
		}

		// We can't use this as termination condition for the loop,
		// because we want the break to happen before updating the
		// screen again.

		if (shouldQuit())
			break;

		// creates the debug text blocks
		_debugger->buildDebugText();

		_screen->buildDisplay();
	}

	return Common::kNoError;
}
int main(void)
{
	char* error = "";
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_FONT *font = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;

	HANDLE mutex = CreateMutex(NULL, FALSE, "Chapter3_CloseMutex-Mutex");
    if (GetLastError() == ERROR_ALREADY_EXISTS) {
		MessageBoxA(GetForegroundWindow(), "Only one instance of the game can run at a time.", "Chapter3_CloseMutex", MB_OK);
        return 0;
    }

	do
	{
		loadHighscore();
		if(!al_init())
		{
			error = "failed to initialize allegro!";
			break;
		}
		if(!al_init_primitives_addon())
		{
			error = "failed to initialize primitives!";
			break;
		}
		al_init_font_addon();
		if(!al_init_ttf_addon())
		{
			error = "failed to initialize ttfs!";
			break;
		}
		if(!al_install_keyboard())
		{
			error = "failed to initialize keyboard!";
			break;
		}
		if(!(font = al_load_ttf_font("arial.ttf", 18, 0)))
		{
			error = "failed to initialize font!";
			break;
		}
		if(!(timer = al_create_timer(1.0 / 20)))
		{
			error = "failed to initialize timer!";
			break;
		}
		if(!(display = al_create_display(640, 480)))
		{
			error = "failed to initialize display!";
			break;
		}
		if(!(event_queue = al_create_event_queue()))
		{
			error = "failed to initialize event_queue!";
			break;
		}

		al_register_event_source(event_queue, al_get_display_event_source(display));
		al_register_event_source(event_queue, al_get_keyboard_event_source());
		al_register_event_source(event_queue, al_get_timer_event_source(timer));

		al_start_timer(timer);

		while(1)
		{
			ALLEGRO_EVENT ev;
			al_wait_for_event(event_queue, &ev);

			if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
				break;
			else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
			{
				if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
					break;
				else if (ev.keyboard.keycode <= 255)
					keys[ev.keyboard.keycode] = true;
			}
			else if (ev.type == ALLEGRO_EVENT_KEY_UP)
			{
				if (ev.keyboard.keycode <= 255)
					keys[ev.keyboard.keycode] = false;
			}
			else if (ev.type == ALLEGRO_EVENT_KEY_CHAR)
				keyboardEvent();

			if (al_is_event_queue_empty(event_queue) && ev.type == ALLEGRO_EVENT_TIMER)
				drawFrame(font);
		}
	} while (0);

	if (mutex)
        CloseHandle(mutex);

	if (strlen(error) > 0)
	{
		al_show_native_message_box(NULL, NULL, "error", error, NULL, NULL);
		return -1;
	}

	al_destroy_timer(timer);
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	saveHighscore();


	return 0;
}
Esempio n. 10
0
void idle() {
	keyboardEvent();
	int time_ = glutGet(GLUT_ELAPSED_TIME);
	float angle = time_ / 1000.0 * 10;

	Vector3 pos = player.getPosition();
	float angX = player.getRotationX(),
		  angY = player.getRotationY();

	Vector3 cpos = cube.getPosition(),
			cpos2= cube2.getPosition();

	GLfloat D = cpos.distance(&cpos2),
			W = (cube.dimension() + cube2.dimension())/2;
	float bound = 6.0;
	//if(time_%100 < 10)
	//	gravity = Vector3(rand()%10-5, rand()%10-5, rand()%10-5);
	cvelocity.add(&gravity);
	Vector3 s = cvelocity;
	s.mult(-mass/1000).add(&cpos2);
	bool collides = true;
	if(cpos2.x < -bound && cvelocity.x > 0.0f){
		cvelocity.x = -abs(cvelocity.x)*0.5;
		gravity.x = -abs(gravity.x);
	}
	else if(cpos2.x > bound && cvelocity.x < 0.0f){
		cvelocity.x = abs(cvelocity.x)*0.5;
		gravity.x = abs(gravity.x);
	}
	else if(cpos2.y < -bound && cvelocity.y > 0.0f){
		cvelocity.y = -abs(cvelocity.y)*0.5;
		gravity.y = -abs(gravity.y);
	}
	else if(cpos2.y > bound && cvelocity.y < 0.0f){
		cvelocity.y = abs(cvelocity.y)*0.5;
		gravity.y = abs(gravity.y);
	}
	else if(cpos2.z < -bound && cvelocity.z > 0.0f){
		cvelocity.z = -abs(cvelocity.z)*0.5;
		gravity.z = -abs(gravity.z);
	}
	else if(cpos2.z > bound && cvelocity.z < 0.0f){
		cvelocity.z = abs(cvelocity.z)*0.5;
		gravity.z = abs(gravity.z);
	}
	else{
		collides = false;
	}
	
	if(!collides){
		if(cpos.x + cube.min.x <= cpos2.x + cube2.max.x && cpos.x + cube.max.x >= cpos2.x + cube2.min.x && 
			cpos.z + cube.min.z <= cpos2.z + cube2.max.z && cpos.z + cube.max.z >= cpos2.z + cube2.min.z && 
			cpos.y + cube.min.y <= cpos2.y + cube2.max.y && cpos.y + cube.max.y >= cpos2.y + cube2.min.y){
			collides = true;
			cvelocity.x = cvelocity.y = cvelocity.z = 0.0f;
		}
		
		if(!collides)
			cube2.setPosition(s);
	}

	player.updateForce();
	for (int i = 0; i < BALLS; i++){
		balls[i].updateForce();
		Vector3 ball_pos = balls[i].getPosition(),
				ball_vel = balls[i].dynamics.velocity;
		//printf("x: %f, y: %f, z %f: %f\n", ball_pos.x, ball_pos.y, ball_pos.z, BOUNDARY);
		if(ball_pos.x < -BOUNDARY || ball_pos.x > BOUNDARY)
			balls[i].addVelocity(Vector3(-2 * ball_vel.x, 0.0f, 0.0f));
		if(ball_pos.y < -BOUNDARY || ball_pos.y > BOUNDARY)
			balls[i].addVelocity(Vector3(0.0f, -2 * ball_vel.y, 0.0f));
		if(ball_pos.z < -BOUNDARY || ball_pos.z > BOUNDARY)
			balls[i].addVelocity(Vector3(0.0f, 0.0f, -2 * ball_vel.z));
	}

	Matrix4 model = cube.getTransformMatrix(),
			model2 = cube2.getTransformMatrix();
	Matrix4 projection = Matrix4().Perspective(45.0f, 1.0f*screen_width/screen_height, 0.1f, 50.0f),
			view = Matrix4().lookAt(pos, Vector3(pos.x-cos(angX), pos.y + sin(angY), pos.z + sin(angX)), Vector3(0.0f, 1.0f, 0.0f));
	Matrix4 anim = Matrix4().RotateA(angle*3.0f, Vector3(1, 0, 0)) * 	// X axis
					Matrix4().RotateA(angle*2.0f, Vector3(0, 1, 0)) *	// Y axis
					Matrix4().RotateA(angle*4.0f, Vector3(0, 0, 1));	// Z axis


	Matrix4 m_transform = projection * view;

	glUseProgram(cube.getProgram());
	glUniformMatrix4fv(cube.getUniform("m_transform"), 1, GL_FALSE, m_transform * model);

	glUseProgram(cube2.getProgram());
	glUniformMatrix4fv(cube2.getUniform("m_transform"), 1, GL_FALSE, m_transform * model2);

	for (int i = 0; i < BALLS; i++){
		glUseProgram(balls[i].getProgram());
		glUniformMatrix4fv(
			balls[i].getUniform("m_transform"), 	// m_transform is the transformation of balls[i] in shaders
			1, 										// one uniform-buffer 
			GL_FALSE, 								// no nprmalization
			m_transform * balls[i].getTransformMatrix()
		);
	}
	for (int i = 0; i < DETAIL_GRID*3; i++){
		grid[i].updateForce();
		glUseProgram(grid[i].getProgram());
		glUniformMatrix4fv(
			grid[i].getUniform("m_transform"), 	// m_transform is the transformation of grid[i] in shaders
			1, 										// one uniform-buffer 
			GL_FALSE, 								// no nprmalization
			m_transform * grid[i].getTransformMatrix()
		);
	}

	glutPostRedisplay();
}
//-----------------------------------------------------------------------------
// specialEvent()
//-----------------------------------------------------------------------------
void GlutDisplay::specialEvent(const int key)
{
   if (key == GLUT_KEY_LEFT)           keyboardEvent(BACK_SPACE);
   else if (key == GLUT_KEY_RIGHT)     keyboardEvent(FORWARD_SPACE);
   else if (key == GLUT_KEY_UP)        keyboardEvent(UP_ARROW_KEY);
   else if (key == GLUT_KEY_DOWN)      keyboardEvent(DOWN_ARROW_KEY);
   else if (key == GLUT_KEY_PAGE_UP)   keyboardEvent(PAGE_UP_KEY);
   else if (key == GLUT_KEY_PAGE_DOWN) keyboardEvent(PAGE_DOWN_KEY);
   else if (key == GLUT_KEY_HOME)      keyboardEvent(HOME_KEY);
   else if (key == GLUT_KEY_END)       keyboardEvent(END_KEY);
   else if (key == GLUT_KEY_INSERT)    keyboardEvent(INSERT_KEY);
   else if (key == GLUT_KEY_F1)        keyboardEvent(F1_KEY);
   else if (key == GLUT_KEY_F2)        keyboardEvent(F2_KEY);
   else if (key == GLUT_KEY_F3)        keyboardEvent(F3_KEY);
   else if (key == GLUT_KEY_F4)        keyboardEvent(F4_KEY);
   else if (key == GLUT_KEY_F5)        keyboardEvent(F5_KEY);
   else if (key == GLUT_KEY_F6)        keyboardEvent(F6_KEY);
   else if (key == GLUT_KEY_F7)        keyboardEvent(F7_KEY);
   else if (key == GLUT_KEY_F8)        keyboardEvent(F8_KEY);
   else if (key == GLUT_KEY_F9)        keyboardEvent(F9_KEY);
   else if (key == GLUT_KEY_F10)       keyboardEvent(F10_KEY);
   else if (key == GLUT_KEY_F11)       keyboardEvent(F11_KEY);
   else if (key == GLUT_KEY_F12)       keyboardEvent(F12_KEY);
   else { /* ignore */ }
}
Esempio n. 12
0
void CPPBot::keyboardRelease(const int key)
{
    keyboardEvent(false,key);
}
Esempio n. 13
0
void CPPBot::keyboardPress(const int key)
{
    keyboardEvent(true,key);
}