 
// set window properties
SetWindowTitle( "AGK" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize(1)
SetSyncRate( 60,0 )
SetAntiAliasMode(1)
 
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetScissor(0,0,0,0)
SetCameraRange( 1, 0.1, 2000 )

// enable the sky box
SetSkyBoxVisible( 1 )

// load terrain key, colour and detail textures

ter_5xD_loadtexture( 1, "ter_key.png" )
ter_5xD_loadtexture( 2, "diffuse.png" )

SetGenerateMipmaps(1)

ter_5xD_loadtexture( 3, "grass.png" )		//Default detail map 
ter_5xD_loadtexture( 4, "sand.png" )		//Red channel on key texture
ter_5xD_loadtexture( 5, "dirt.png" )		//Green channel on key texture
ter_5xD_loadtexture( 6, "rock.png" )		//Blue channel on key texture
ter_5xD_loadtexture( 7, "patchygrass.png" ) //Alpha channel on key texture

// load the terrain shader to give the terrain color based on height
LoadShader( 1, "Terrain5xD.vs", "Terrain5xD.ps" )

// create the terrain object from a height map
CreateObjectFromHeightMap( 1, "heightmap.png", 768, 50, 768, 1, 16 )
SetObjectImage( 1, 1, 0 )
SetObjectImage( 1, 2, 1 )
SetObjectImage( 1, 3, 2 )
SetObjectImage( 1, 4, 3 )
SetObjectImage( 1, 5, 4 )
SetObjectImage( 1, 6, 5 )
SetObjectImage( 1, 7, 6 )

SetObjectUVScale( 1, 0, 1, 1 ) // Adjusted to disable UV repeat
SetObjectShader( 1, 1 )

SetCameraPosition(1, 383, 30, 383)
SetCameraLookAt( 1, 0,40,0,0 )

SetAmbientColor( 128,128,128 )

// buttons for mobile devices to control the camera 
addvirtualbutton( 1, GetScreenBoundsRight()-45,GetScreenBoundsBottom()-45,80 )
addvirtualbutton( 2, GetScreenBoundsRight()-45,GetScreenBoundsBottom()-130,80 )
addvirtualbutton( 3, GetScreenBoundsLeft()+45,GetScreenBoundsBottom()-45,80 )

SetPrintSize(15)

// add some atmospheric fog
SetFogMode( 1 )
SetFogColor( 161,183,209 )
SetFogRange( 50, 700 )
SetFogSunColor( 255,230,179 )

// camera starts in free mode
cameraMode = 0

do
	// move the camera
	speed# = 1.0
	if ( GetRawKeyState( 16 ) ) then speed# = 0.1
	if ( cameraMode = 1 ) then speed# = 0.25
	
	if ( GetRawKeyState( 87 ) ) then MoveCameraLocalZ( 1, speed# )
	if ( GetRawKeyState( 83 ) ) then MoveCameraLocalZ( 1, -speed# )

	if ( GetRawKeyState( 65 ) ) then MoveCameraLocalX( 1, -speed# )
	if ( GetRawKeyState( 68 ) ) then MoveCameraLocalX( 1, speed# )

	if ( GetRawKeyState( 81 ) ) then MoveCameraLocalY( 1, -speed# )
	if ( GetRawKeyState( 69 ) ) then MoveCameraLocalY( 1, speed# )
	
	if ( getvirtualbuttonstate(1) = 1 ) then MoveCameraLocalZ( 1, -speed# )
    if ( getvirtualbuttonstate(2) = 1 ) then MoveCameraLocalZ( 1, speed# )
	
	// rotate the camera
	if ( GetPointerPressed() )
        startx# = GetPointerX()
        starty# = GetPointerY()
        angx# = GetCameraAngleX(1)
        angy# = GetCameraAngleY(1)
        pressed = 1
    endif

    if ( GetPointerState() = 1 )
        fDiffX# = (GetPointerX() - startx#)/4.0
        fDiffY# = (GetPointerY() - starty#)/4.0

        newX# = angx# + fDiffY#
        if ( newX# > 89 ) then newX# = 89
        if ( newX# < -89 ) then newX# = -89
        SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
    endif
    
    // change camera mode
    if ( GetVirtualButtonPressed(3) = 1 ) then cameraMode = 1-cameraMode
    if ( cameraMode = 1 )
		// stick camera to the terrain
		x# = GetCameraX(1)
		z# = GetCameraZ(1)
		SetCameraPosition( 1, x#, GetObjectHeightMapHeight(1,x#,z#)+0.5, z# )
	endif
    
    // move the sun 
    time# = time# + GetFrameTime()*0.5
    xdir# = sin(time#)
    ydir# = -0.5
    zdir# = cos(time#)
    SetSunDirection( xdir#, ydir#, zdir# )
    
	// show some information
	Print( "FPS: " + str(ScreenFPS(),1) )
	Print( "Polygons: " + str(GetPolygonsDrawn()) )
	
    Sync()
loop

function ter_5xD_loadtexture(img,fn$)
	LoadImage( img,fn$ )
	SetImageWrapU( img, 1 )
	SetImageWrapV( img, 1 )
endfunction
