Skip to main content
Adding splitscreen code
Source Link
dfour
  • 768
  • 5
  • 12

Camera Now you need to make 2 cameras. One for the main area and one for UI

This example is adapted from How to implement 2 cameras in a unique stage using LibGDX

mainCamera = new OrthographicCamera(GAME_WIDTH,GAME_HEIGHT);
mainCamera.position.set(GAME_WIDTH /2, GAME_HEIGHT /2, 0);
mainCamera.update();

secondCamera = new OrthographicCamera(UI_WIDTH,UI_HEIGHT);
secondCamera.position.set(UI_WIDTH /2,UI_HEIGHT /2, 0);
secondCamera.update();

In your render method you would add

//For the main cam
Gdx.gl.glViewport(0,0,GAME_WIDTH, GAME_HEIGHT); // set viewport to full screen
batch.setProjectionMatrix(mainCamera.combined);
batch.begin();
    // draw main screen items
batch.end();

//For the UI cam
Gdx.gl.glViewport(100,100,300, 400); // set smaller region for UI
batch.setProjectionMatrix(secondCamera.combined);
batch.begin();
    // draw UI items
batch.end();

The important bit being the glViewport which sets which part of the screen to draw to.

Camera Now you need to make 2 cameras. One for the main area and one for UI

This example is adapted from How to implement 2 cameras in a unique stage using LibGDX

mainCamera = new OrthographicCamera(GAME_WIDTH,GAME_HEIGHT);
mainCamera.position.set(GAME_WIDTH /2, GAME_HEIGHT /2, 0);
mainCamera.update();

secondCamera = new OrthographicCamera(UI_WIDTH,UI_HEIGHT);
secondCamera.position.set(UI_WIDTH /2,UI_HEIGHT /2, 0);
secondCamera.update();

In your render method you would add

//For the main cam
Gdx.gl.glViewport(0,0,GAME_WIDTH, GAME_HEIGHT); // set viewport to full screen
batch.setProjectionMatrix(mainCamera.combined);
batch.begin();
    // draw main screen items
batch.end();

//For the UI cam
Gdx.gl.glViewport(100,100,300, 400); // set smaller region for UI
batch.setProjectionMatrix(secondCamera.combined);
batch.begin();
    // draw UI items
batch.end();

The important bit being the glViewport which sets which part of the screen to draw to.

Source Link
dfour
  • 768
  • 5
  • 12

Since you are using 2 cameras for each separate area you can split the input for each area using an input multiplexer. Now you can have one input processor for the main camera and one for the UI. Then its simply a case of responding to mouse drag events in the UI processor and if the event is not in the UI area you return false in the input processor method to signify that this processor did not process the event and it needs to be passed to the next input processor(main camera input processor) in the multiplexer chain.

Example

import com.badlogic.gdx.InputProcessor;


public class UIInputProcessor implements InputProcessor{

    

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        if(screenX >= 50 && screenX <= 100
                && screenY >= 50 && screenY <= 150 ){
            // I assume the sreen UI window is 50px x 100px with its lower left corner at 50,50
            
            //TODO respond to UI events
            
            return true; // this event has been processed so respond true so events do not get passed to next input processor
        }
        
        return false;
    }

    //.. some events removed for clarity

}

Main Input Processor

import com.badlogic.gdx.InputProcessor;


public class MainInputProcessor implements InputProcessor{

    @Override
    public boolean keyDown(int keycode) {
        //  input events for main area
        return false;
    }

    //.. some events removed for clarity

    @Override
    public boolean scrolled(int amount) {
    //  input events for main area
        return false;
    }

}

Game/Screen Class

Using 2 normal InputProcessors

  MyProcessor = new InputMultiplexer(new UIInputMuliplexer(), new MainInputProcessor());

Or if you use scene2d stage

MyProcessor = new InputMultiplexer(MyStage, new MainInputProcessor());