예제 #1
0
void
platform_handle_events(Input *in)
{
	Window active;
	int revert_to;
	XGetInputFocus(g_pctx.display, &active, &revert_to);
	// TODO: Stop updating the camera if the mouse pointer isn't inside our window?
	if (active == g_pctx.window)
		platform_update_mouse_pos(&in->mouse);

	// TODO: Set up an auto-pause when we lose focus?
	XEvent xev;
	while (XPending(g_pctx.display)) {
		XNextEvent(g_pctx.display, &xev);
		switch(xev.type) {
		case KeyPress: {
			input_key_down(&in->keyboard, xev.xkey.keycode);
		} break;
		case KeyRelease: {
			input_key_up(&in->keyboard, xev.xkey.keycode);
		} break;
		case ButtonPress: {
			input_mbutton_down((Mouse_Button)xev.xbutton.button, &in->mouse);
		} break;
		case ButtonRelease: {
			input_mbutton_up((Mouse_Button)xev.xbutton.button, &in->mouse);
		} break;
		case FocusOut: {
			in->mouse.motion = {0,0}; // Clear residual mouse motion so we don't keep using it in cam calculations.
		} break;
		case FocusIn: {
			platform_update_mouse_pos(&in->mouse); // Reset mouse position so the view doesn't "jump" when we regain focus.
		} break;
		case ClientMessage:
			if ((Atom)xev.xclient.data.l[0] == g_pctx.wm_delete_window)
				platform_exit();
		}
	}
}
예제 #2
0
void controller_system_update(ControllerSystem* self) {
    GET_SYSTEM_COMPONENTS(self);

    for (u32 i = 0; i < components->count; ++i) {
        Entity entity = GET_ENTITY(i);
        ControllerComponent* controller = (ControllerComponent*)GET_SYSTEM_COMPONENT(i);

        MovementComponent* movement =
            (MovementComponent*)GET_COMPONENT(entity, COMPONENT_MOVEMENT);

        TransformComponent* transform =
            (TransformComponent*)GET_COMPONENT(entity, COMPONENT_TRANSFORM);

        REQUIRED_COMPONENTS(transform, movement, controller);

        f32 x = 0;
        f32 y = 0;

        if (input_key(SDL_SCANCODE_LEFT)) {
            x -= 1.f;
        }

        if (input_key(SDL_SCANCODE_RIGHT)) {
            x += 1.f;
        }

        if (input_key(SDL_SCANCODE_UP)) {
            y -= 1.f;
        }

        if (input_key(SDL_SCANCODE_DOWN)) {
            y += 1.f;
        }

        //printf("%f\n", controller->moveSpeed);

        x *= controller->moveSpeed;
        y *= controller->moveSpeed;

        vec2_set(&movement->velocity, x, y);

        if (input_key_down(SDL_SCANCODE_Z)) {
            for (u32 i = 0; i < controller->bulletSourceCount; ++i) {
                bullet_source_on(&controller->bulletSources[i]);
            }
        }
        if (input_key_up(SDL_SCANCODE_Z)) {
            for (u32 i = 0; i < controller->bulletSourceCount; ++i) {
                bullet_source_off(&controller->bulletSources[i]);
            }
        }

        for (u32 i = 0; i < controller->bulletSourceCount; ++i) {
            bullet_source_update(&controller->bulletSources[i], globals.time.delta, self->super.entityManager, &transform->position);
        }        

        //if (controller->fireTimer > 0.f) {
        //    controller->fireTimer -= globals.time.delta;
        //}

        //if (input_key(SDL_SCANCODE_Z) && controller->fireTimer <= 0.f) {
        //    Vec2 pos = vec2_clone(&transform->position);
        //    pos.x += 64;
        //    pos.y += 52;
        //    entity_create_bullet(self->super.entityManager,
        //        vec2_clone(&pos),
        //        textures_get("player_bullet_1.png"));
        //    controller->fireTimer = controller->fireDelay;
        //}
    }
}