Making split screen UI with Gameface
3/28/2022
Kaloyan Geshev
The concept of split-screen mode is widely embraced in many cooperative (co-op) multiplayer games. This mode allows two or more players to engage in gameplay together on a shared screen, each utilizing distinct controllers or keyboard bindings. Within these games, individual players receive unique feedback through UI widgets displayed on their portion of the screen.
Utilizing the capabilities of Gameface and web technologies, you can effortlessly create your game UI widgets just once and subsequently instantiate them for every player connected to the game. Additionally, you have the ability to link each player’s data model to their respective widgets, enabling all players to view their real-time stats within the game, particularly when engaged in split-screen mode.
Showcase overview
To demonstrate the simplicity of achieving this using Gameface, we’ll reuse certain widgets from the ‘Racing Game UI’ to create a split-screen UI for the game. For a comprehensive overview of all the UI widgets featured in this game, you can explore the entire ‘Racing Game UI’ series on our YouTube channel .
The main idea here is when two or more players are connected locally in the game the screen being split into two or more sections, each showcasing individual widgets (gauge, lap timer, map, etc.) to each of the players. For simplicity, we’ll focus on the gauge for now, although this approach is applicable to all other widgets.
Source location
You can review the complete sample source within the ${Gameface package}/Samples/uiresources/UI Tutorials/split-screen-ui
directory.
Sample demonstration
To showcase the functional split screen in the sample, we have introduced a mock JavaScript model that dynamically updates at runtime. Additionally, we’ve implemented the following keyboard bindings to test the split screen:
- Pressing
P
adds a new player. - Pressing
O
removes a player. - Each player’s screen has a tip message with controls that update the player’s gauge state such as rpm, gear, speed, or nitro.
Split screen UI in details
Binding widgets with the game data model
The gauge widget’s source is reused from the Racing Game UI, displaying the car’s rpm, gear, and speed. We’ve enhanced it by adding a nitro bar. The following HTML snippet outlines the widget’s structure at a high level.
Within each element, there are SVG elements bound to the player car model data for a visually appealing representation. The RPM, for instance, has the following structure:
Here, the car’s rpm is dynamically bound to the SVG path’s stroke dash offset, updating the stroke dynamically with the player’s model.
For more detailed information on data binding in Gameface, refer to the documentation.
Wrapping up the UI for split screen
The player’s UI is encapsulated and can be instantiated multiple times - for each connected player. The structure is defined as follows:
with the following styles:
Here are a few aspects that require our attention:
- We utilize the
data-bind-for
attribute to create multiple instances of the UI for individual players. To achieve this, we rely on the game’sPlayers
model, which is an array containing the state information for each player. An illustration of thePlayers
array might be:
- We establish the screen wrapper element responsible for containing the player’s widgets, which will be rendered individually for each player’s state in the
Player
array. - We bound the layout of the screen wrapper with the in-game screen data for each player ensuring proper positioning of all players’ screens within the UI. The required data from the game includes the top-left coordinates of a player’s screen and the width and height of the screen.
Scale down each player’s screen
When additional players join the game, the user interface within the split screen should adjust its size to ensure that the gauges fit appropriately within the allocated space. This adjustment can be accomplished by scaling down each player’s screen using CSS.
Firstly, we need to dynamically assign a class to the screen-wrapper
element based on the number of players:
This ensures that the class changes with the player count.
Next, we apply scaling adjustments via CSS for the widgets. Here’s an example of how this can be done:
Conclusion
By implementing this setup, you can effectively integrate split-screen functionality into your UI. Simply introducing an additional player into the game and appending its state to the Players
array, along with its corresponding screenRect
data, is enough for the UI to respond to the change, resulting in the automatic splitting of the screen.