Viewports

Previous topic - Next topic

MrTAToad

Quick and simple code for creating viewports for multi-player games on one machine.  This code creates horizontal and/or vertical splits for two to four player games.

So, for one player games you use the full screen; two player has a choice of horizontal or a vertical split screen etc etc

Test code :

Code (glbasic) Select
LOCAL loop AS tViewPort

setupViewPorts(VIEWPORT_FOURPLAYER%)

WHILE KEY(1)=FALSE
displayAllPorts()

SHOWSCREEN
WEND


Main code :

Code (glbasic) Select
// --------------------------------- //
// Project: Viewport
// Start: Friday, March 06, 2009
// IDE Version: 6.184

TYPE tViewPort
x%
y%
width%
height%
ENDTYPE

GLOBAL playersViewPort[] AS tViewPort

GLOBAL VIEWPORT_SINGLEPLAYER% = 1
GLOBAL VIEWPORT_TWOPLAYER_HORZSPLIT% = 2
GLOBAL VIEWPORT_TWOPLAYER_VERTSPLIT% = 3
GLOBAL VIEWPORT_THREEPLAYER_HORZSPLIT% = 4
GLOBAL VIEWPORT_THREEPLAYER_VERTSPLIT% = 5
GLOBAL VIEWPORT_THREEPLAYER_TRISPLIT% = 6
GLOBAL VIEWPORT_FOURPLAYER% = 7

FUNCTION setupViewPorts:splitFlag%
LOCAL screenWidth%
LOCAL screenHeight%
LOCAL halfX%
LOCAL halfY%
LOCAL loop%

DIM playersViewPort[0]
GETSCREENSIZE screenWidth%,screenHeight%

SELECT splitFlag%
CASE VIEWPORT_SINGLEPLAYER%
// Single player, so use the full screen
addViewPort(0,0,screenWidth%,screenHeight%)

CASE VIEWPORT_TWOPLAYER_HORZSPLIT%
// Two player, horizontal split
halfX%=screenWidth%/2
addViewPort(0,0,halfX%-1,screenHeight%)
addViewPort(halfX%+1,0,screenWidth%-(halfX%+1),screenHeight%)

CASE VIEWPORT_TWOPLAYER_VERTSPLIT%
// Two player, horizontal split
halfY%=screenHeight%/2
addViewPort(0,0,screenWidth%,halfY%-1)
addViewPort(0,halfY%+1,screenWidth%,screenHeight%-(halfY%+1))

CASE VIEWPORT_THREEPLAYER_HORZSPLIT%
// Three player, horizontal split
halfX%=screenWidth%/3
FOR loop%=0 TO 2
addViewPort(loop%*(halfX%+1),0,halfX%-1,screenHeight%)
NEXT

CASE VIEWPORT_THREEPLAYER_VERTSPLIT%
// Three player, vertical split
halfY%=screenHeight%/3
FOR loop%=0 TO 2
addViewPort(0,loop%*(halfY%+1),screenWidth%,halfY%-1)
NEXT

CASE VIEWPORT_THREEPLAYER_TRISPLIT%
// Three player, triangle split
halfX%=screenWidth%/2
halfY%=screenHeight%/2

addViewPort((screenWidth%-halfX%)/2,0,halfX%-1,halfY%-1)
addViewPort(0,halfY%+1,halfX%-1,screenHeight%-(halfY%+1))
addViewPort(halfX%+1,halfY%+1,screenWidth%-(halfX%+1),screenHeight%-(halfY%+1))

CASE VIEWPORT_FOURPLAYER%
// Four player, horizontal and vertical split
halfX%=screenWidth%/2
halfY%=screenHeight%/2

addViewPort(0,0,halfX%-1,halfY%-1)
addViewPort(halfX%+1,0,screenWidth%-(halfX%+1),halfY%-1)
addViewPort(0,halfY%+1,halfX%-1,screenHeight%-(halfY%+1))
addViewPort(halfX%+1,halfY%+1,screenWidth%-(halfX%+1),screenHeight%-(halfY%+1))




ENDSELECT
ENDFUNCTION

FUNCTION addViewPort:x%,y%,width%,height%
LOCAL viewP AS tViewPort

viewP.x%=x%
viewP.y%=y%
viewP.width%=width%
viewP.height%=height%
DIMPUSH playersViewPort[],viewP
ENDFUNCTION

FUNCTION displayAllPorts:
LOCAL loop AS tViewPort

FOREACH loop IN playersViewPort[]
VIEWPORT loop.x%,loop.y%,loop.width%,loop.height%
DRAWRECT 0,0,1024,768,RGB(255,255,0)
NEXT
ENDFUNCTION


Note that if you want to use this for 3D graphics, the viewport system should be called before X_MAKE3D :

Code (glbasic) Select

FUNCTION displayPlayers:
LOCAL loop AS tPlayer

// X_MAKE3D 1.0,1000.0,45.0
FOREACH loop IN players[]
VIEWPORT loop.viewPortArea.x%,loop.viewPortArea.y%,loop.viewPortArea.width%,loop.viewPortArea.height%
X_MAKE3D 1.0,1000.0,45.0

X_CAMERA 0.0,10.0,-40.0,0.0,0.0,0.0
X_AMBIENT_LT 0,RGB(255,255,0)
X_DRAWAXES 0.0,0.0,0.0

X_MOVEMENT 0.0,0.0,0.0
// X_SCALING 3,3,3
// X_DRAWOBJ 0,0
NEXT
ENDFUNCTION