// Project: Zarch-Virus // Created: 2019-06-06 // show all errors SetErrorMode(2) // set window properties SetWindowTitle( "A Braben Zarch-Virus Remake of 2004 Stevie G. Remake" ) SetWindowSize( 1024, 768, 0 ) SetWindowAllowResize( 1 ) // allow the user to resize the window // set display properties SetVirtualResolution( 1024, 768 ) // doesn't have to match the window SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts // Some Types Type Vertex x as float y as float z as float nx as float ny as float nz as float u as float v as float color as integer endtype Type Triangle v1 as integer v2 as integer v3 as integer endtype Type Mesh VertexList as Vertex[] TriangleList as Triangle[] endtype Type Point Height as Float Index as Integer WaveFactor as Float R as Integer G as Integer B as Integer EndType //Some Variables global Terr1 as Point [256,256] Global Size as Integer = 256 Global Divs as Integer = 21 // <----- You can change Global Divg as Integer Global Scale as Float =16.0 Global Speed as Float = 0.1 Global x_pos as Float = 128.0 Global z_pos as Float = 128.0 Global last_x as Float Global last_z as Float global divd as float global ripple as float Global Surf as Integer Global SurfMem as Integer global count as integer global wavy as integer global Target as Integer Global MapQuad Divg = Divs*4+1 Divd = Sqrt(2*(Divs*Divs)) last_x = x_pos last_z = z_pos // Initialize MapQuad = CreateObjectPlane(1,1) Map = random_terrain(0) SetObjectImage(MapQuad,Map,0) Surf = Create_Grid() SurfMem = CreateMemblockFromObjectMesh( Surf, 1 ) Target = CreateObjectPlane(0,0) SetCameraPosition(1,0,60,-200) SetCameraLookAt(1,0,60,-100,0) do print("Nb of Cycles before Crash = "+str(Cycle)+" ---> 4344 for Divs=21 on W7 and a old G.Card") Cycle=cycle+1 print("Divs = "+str(divs)) print ("Nb of Vertices = "+str(GetMemblockInt( surfmem ,0 ))) drga = 1 //(GetRawKeyState(39)-GetRawKeyState(37)) haba = -1 //(GetRawKeyState(40)-GetRawKeyState(38)) x_pos=wrap( x_pos + drga * speed,size) z_pos=wrap( z_pos + haba * speed,size) Update_Terrain() update_camera() Print( "Fps = "+str(ScreenFPS()) ) Sync() loop // Meshes Functions by Puzzler ? Function AddVertex(m ref as Mesh, x as float, y as float, z as float, nx as float, ny as float, nz as float, u as float, v as float, color as integer) vert as vertex vert.x = x vert.y = y vert.z = z vert.nx = nx vert.ny = ny vert.nz = nz vert.u = u vert.v = v vert.color = color m.VertexList.Insert(vert) endfunction Function AddTriangle(m ref as Mesh, v1 as integer, v2 as integer, v3 as integer) t as Triangle t.v1 = v1 t.v2 = v2 t.v3 = v3 m.TriangleList.Insert(t) endfunction Function CreateObjectFromMesh(m ref as mesh) VertexCount = m.VertexList.Length + 1 IndexCount = (m.TriangleList.Length + 1) * 3 IndexOffset = 72 + VertexCount*36 memblock = CreateMemblock(IndexOffset+IndexCount*4) SetMemblockInt(memblock,0,VertexCount) SetMemblockInt(memblock,4,IndexCount) SetMemblockInt(Memblock,8,4) SetMemblockInt(memblock,12,36) SetmemblockInt(memblock,16,72) SetMemblockInt(memblock,20,IndexOffset) SetMemblockInt(memblock,24,0x0c000300) SetMemblockString(Memblock,28,"position") SetMemblockInt(memblock,40,0x08000300) SetMemblockString(memblock,44,"normal") SetMemblockInt(memblock,52,0x04000200) SetMemblockString(memblock,56,"uv") SetMemblockInt(memblock,60,0x08010401) SetMemblockString(memblock,64,"color") for i = 0 to m.VertexList.Length SetMemblockFloat(memblock,72+i*36,m.VertexList[i].x) SetMemblockFloat(memblock,76+i*36,m.VertexList[i].y) SetMemblockFloat(memblock,80+i*36,m.VertexList[i].z) SetMemblockFloat(memblock,84+i*36,m.VertexList[i].nx) SetMemblockFloat(memblock,88+i*36,m.VertexList[i].ny) SetMemblockFloat(memblock,92+i*36,m.VertexList[i].nz) SetMemblockFloat(memblock,96+i*36,m.VertexList[i].u) SetMemblockFloat(memblock,100+i*36,m.VertexList[i].v) SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) next for i = 0 to m.TriangleList.Length SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1) SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2) SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3) next id = CreateObjectFromMeshMemblock(memblock) DeleteMemblock(memblock) endfunction id // --------------- // Terrain Functions Function Create_Grid() m as Mesh For z=0 To divg For x=0 To divg AddVertex(m,x,0,z,0,1,0,mod(x,2),mod(z,2),0xFFFFFFFF) Next Next For z=0 To divg-1 For x=0 To divg-1 v0=x+z*(divg+1)*(z-4 And z >-4 then terr1[xp,zp].height=30 terr1[xp,zp].r=col terr1[xp,zp].g=col terr1[xp,zp].b=col Next Next //create map ClearScreen() For z=0 To size-1 For x=0 To size-1 rgb=MakeColor(terr1[x,z].r,terr1[x,z].g,terr1[x,z].b) terr1[x,z].wavefactor = Sqrt( (x-128)^2 + ( z-128)^2 ) * 45.0 DrawBox(x,z,x+1,z+1,rgb,rgb,rgb,rgb,1) Next Next MapTex = GetImage(0,0,255,255) EndFunction MapTex Function Update_Terrain() ax#=Floor(x_pos) az#=Floor(z_pos) fx#=(x_pos) - ax# fz#=(z_pos) - az# Ripple = WRAP ( Ripple +5,360) For z=-divs To divs+1 For x=-divs To divs+1 nx=wrap(ax#+x,size) nz=wrap(az#+z,size) vy#=terr1[nx,nz].height vx#=limit( x-fx#,-divs,divs) vz#=limit( z-fz#,-divs,divs) //brightness c#=0.25 + ( divd - Sqrt(vx#*vx#+vz#*vz#) ) / divd r=terr1[nx,nz].r * c# g=terr1[nx,nz].g * c# b=terr1[nx,nz].b * c# If Abs(vx#)=divs Or Abs(vz#)=divs vy#=get_height( x_pos+vx# , z_pos+vz#) EndIf if abs(vy#)<0.25 vy#=vy#+( 1.5-vy#)*(1+Cos( Ripple+terr1[nx,nz].WaveFactor)) EndIf //vertex positions x1 = ( x + divs ) * 2 - ( x > -divs ) z1 = ( z + divs ) * 2 - ( z > -divs ) For iz = 0 To ( z > -divs And z < divs+1 ) For ix = 0 To ( x > -divs And x < divs+1 ) x2=x1+ix z2=z1+iz v=x2+z2*(divg+1) SetMeshMemblockVertexPosition(surfmem,v,vx#*scale,vy#,-vz#*scale) Next Next // Vertex Colors If x>= -Divs And x =< Divs And z>= -Divs And z =< Divs x1 = ( x + Divs ) * 2 z1 = ( z + Divs ) * 2 For iz = 0 To 1 z2 = z1 + iz For ix = 0 To 1 x2 = x1 + ix v = x2 + z2 * ( Divg+1 ) SetMeshMemblockVertexColor(SurfMem,v,r,g,b,255) Next Next EndIf Next Next SetObjectMeshFromMemblock( Surf, 1, SurfMem ) EndFunction // ----------------- Function Wrap(q as Float, a as Float) If q >=a then q=q-a If q < 0 then q=a+q EndFunction q Function Limit(q as Float,lo as Float,hi as Float) If q < lo Then q = lo If q > hi Then q = hi EndFunction q Function update_camera() SetObjectPosition( target,0,divs*5.0+get_height(x_pos,z_pos)* 1.0,-divs*Divs) dx#=(getobjectX(target)-getcameraX(1))*0.1 dy#=(getobjectY(target)-getcameraY(1))*0.1 dz#=(getobjectZ(target)-getcameraZ(1))*0.1 MoveCameraLocalX(1,dx#) movecameralocaly(1,dy#) MoveCameraLocalZ(1,dz#) setcameraposition(1,0,divs*5.0+get_height(x_pos,z_pos)* 1.0,-500) setobjectposition(MapQuad,GetCameraX(1),GetCameraY(1),GetCameraZ(1)) MoveObjectLocalZ(mapquad,3) MoveObjectLocalX(mapquad,1.5) MoveObjectLocalY(mapquad,1) EndFunction