3D for GML: Split screen

From ENIGMA
Jump to navigation Jump to search

14.1 Introducing the objects

When you are playing a single player game, one screen is enough to see what you’re doing. But what if you are playing with two players on one display? If only one monitor is available, you’ll need to split the screen. Game Maker allows you to split the screen using views. Let’s take a look at the gm6 file that comes with this tutorial. There are several objects that I will introduce briefly:

  • chi_wall: a textured wall (child) object that players will bump against,
  • obj_camera: the object that will show what each player sees,
  • obj_ground: a textured ground object that will players will walk on,
  • obj_ceiling: a textured ceiling object that will limit the room at the top,
  • obj_playerA: the textured player object which is displayed in the left view,
  • obj_playerB: same but displayed in the right view,
  • par_wall: the parent of chi_wall,
  • obj_radar: an object that draws radars displaying player positions.

Let’s take a look at the scripts that make this program work.

14.2 Drawing multiple views

Starting with the first script in the list, we see the script scr_draw_camera which draws the views of both players. Essential in this is the following code:

i f view_current=0 then
...
and:
i f view_current=1 then
...

This script is executed in each Draw event where all views are drawn. Both the views, for player A and B, are drawn as GM cycles through them within one draw event. Moving down in the listed scripts, in the next scripts scr_draw_ceiling and scr_draw_ground the ceiling and ground are drawn.

14.3 Drawing multiple players

The next two scripts draw the player objects: scr_draw_playerA and scr_draw_playerB. Both hold different but very similar code. Let’s take a look at the code in scr_draw_playerA and see if we can make sense of it:

//only draw when player B views
i f view_current=0 then exi t;
//draw player A
draw_set_color(c_lime);
d3d_draw_ellipsoid(x-16,y-16,z, x+16 ,y+16,z+32,
background_get_texture(bac_player),1,1,16);
draw_set_color(c_white);

The first line makes sure that Player A is only drawn when player B is viewing Player A using a bright green color (c_lime). The same is true for player B’s drawing script (scr_draw_playerB) which holds this code:

//only draw when player A views
if view_current=1 then exit;

//draw player B
draw_set_color(c_red);
d3d_draw_ellipsoid(x-16,y-16,z, x+16 ,y+16,z+32, background_get_texture(bac_player),1,1,16);
draw_set_color(c_w hite);

Note how the player is drawn in a red color, using the same player texture (reducing the overall file size of the game).

14.4 Drawing a radar

This game will have two radars, one for each player. The first part of the script that draws the radars (scr_draw_radar) will draw the radar for player A:

//draw radars
if view_current=0 then
{
	d3d_set_projection_ortho(0,0,320,480,0);
	d3d_set_depth(0);
	draw_circle_color(64,64,32,c_lime,c_lime,true);
	dist=point_distance(obj_playerA.x,obj_playerA.y,obj_Play erB.x,obj_PlayerB.y);

	if dist<192 then
	{
		dir=point_direction(obj_playerA.x,obj_playerA.y,obj_playerB.x,obj_playerB.y)-obj_playerA.direction-90;
		draw_set_color(c_red);
		draw_circle(64-cos(degt orad(dir))*dist/6,64+sin(degtorad(dir))*dist/6,2,false);
	}
}

If the current view is that of player A then we draw in orthographic projection, which results in flat, 2D or overlay drawing. I’m not going to go into this in detail but basically it draws shapes onto the two sides of the split screen revealing the other player’s position. The next three scripts in the list initialize some objects, which should not hold many surprises. The only thing worth mentioning here though is the following code in the ini_camera script:

//set values
z=16;
zdirection=0;
zoom= 4;
depth=8192;

The depth of the camera is set to prevent drawing problems, especially when using orthographic projection.

14.5 Moving multiple players

The first script in line is the motion script for both players bumping into walls (mot_bump). Maybe not the most elegant script but efficient. Each player will have his own set of keys on the keyboard to control his character. This is what is done in the last two scripts: mot_playerA and mot_playerB. This way you can create multiple views in multiplayer games or any other type of game where you need it. Split screens should hold less secrets for you now.


All rights reserved. Copyright © 2004 by John J.A.H. Weeren. Unauthorized use or reproduction, whole or in part, without written permission from the author is strictly prohibited. This page is part of the tutorial 3D For GML. Reproduced with permission. For more information, please see the talk page.