Ooops
November 25, 2020, 02:34:49 AM

Author Topic: Tile Mesh  (Read 114 times)

Offline Hardcoal

  • Sr. Member
  • ****
  • Posts: 481
Tile Mesh
« on: October 21, 2020, 10:46:29 PM »
This code clones a Mesh so it will be tiled all over the screen.. no matter if you zoom in or out.. or move left or right
it will always fill the screen..

this may be good for some type of 2d games.. when you want same image repeating itself..



https://drive.google.com/file/d/1qjIOhu2qaBFQz2TthswiV7HpnVNcOSE2/view?usp=sharing

Im still improving it for my own needs, but this is good enough

Code: [Select]
Strict

Import xorsteam.xors3d

xKey("M1Rk6-581XS-g6J5W-M4UFg-M77f8")
xSetEngineSetting("Splash::TilingTime", "0")
xSetEngineSetting("Splash::AfterTilingTime", "0")
xSetEngineSetting("Engine::Multithreaded", "1")

xGraphics3D (800, 600, 32, 0, True)

Global Camera = XCreateCamera()
xPositionEntity(Camera, 0, 0, -20)
xCameraClsColor(Camera, 100, 100, 100)

Global cube = xCreateCube()
xCreateLight()
xHideEntity(cube)

Global Spr = xCreateSphere()
xPositionEntity(Spr, 0, 0, -0.1)
xEntityColor(Spr, 200, 123, 34)

Global MT:MeshTiler_Type = CreateNewTiler(Cube)

Global CamMove_Flg

 While Not xKeyHit(xKEY_ESCAPE) Or xWinMessage("WM_CLOSE")
 
xRenderWorld(5, 1)
xUpdateWorld(1)

CamMove_Flg = False

   'Move Cam
If xKeyDown(xKEY_A) Then xMoveEntity(Camera, -0.1, 0, 0) ; CamMove_Flg = True
If xKeyDown(xKEY_D) Then xMoveEntity(Camera, 0.1, 0, 0) ; CamMove_Flg = True
If xKeyDown(xKEY_W) Then xMoveEntity(Camera, 0, 0.1, 0) ; CamMove_Flg = True
If xKeyDown(xKEY_S) Then xMoveEntity(Camera, 0, -0.1, 0) ; CamMove_Flg = True
If xKeyDown(xKEY_E) Then xMoveEntity(Camera, 0, 0, 0.1) ; CamMove_Flg = True
If xKeyDown(xKEY_C) Then xMoveEntity(Camera, 0, 0, -0.1) ; CamMove_Flg = True

If CamMove_Flg Then PlayMeshTile(MT)

   'Print
xColor(0, 0, 0)
xRect (0, 0, 300, 80, 1)
xColor(255, 255, 255)
Local TA, Raw:RawHolder_Type
For Raw = EachIn MT.RawsList
TA = TA + Raw.TilesList.Count()
Next
xText (10, 10, "Raws Amount " + MT.RawsList.Count())
xText (10, 40, "Tiles Amount " + TA)

Delay(5)
xFlip()

 Wend

 End
 
 '---------------------------------'
 
 Function CreateTileSheet_Int(MeshTile:MeshTiler_Type)
  Local NR:RawHolder_Type, I, Y:Float

If MeshTile.Direction = Dir_Both Then

   'Down
Repeat
Y = -(xMeshHeight(MeshTile.MainHandle) + MeshTile.Gap) * I
NR = CreateNewRaw_Int(MeshTile, Y)
ListAddLast(MeshTile.RawsList, NR)
InitialTileHorizCreate(NR)
If MeshOutOfVertView(NR.Handle, Camera) Then Exit
I = I + 1
Forever

   'Up
I = 0
Repeat
Y = (xMeshHeight(MeshTile.MainHandle) + MeshTile.Gap) * I
NR = CreateNewRaw_Int(MeshTile, Y)
ListAddFirst(MeshTile.RawsList, NR)
InitialTileHorizCreate(NR)
If MeshOutOfVertView(NR.Handle, Camera) Then Exit
I = I + 1
Forever

Else

EndIf

 End Function
 
 Function InitialTileHorizCreate(Raw:RawHolder_Type)
  Local M:T_Type, I

   'Right
I = 0
  Repeat
M = New T_Type
I = I + 1
M.Handle = xCopyMesh(raw.Handle)
xPositionEntity(M.Handle, xEntityX(Raw.Handle) + (xMeshWidth(Raw.Handle) + Raw.Parent.Gap) * I, xEntityY(Raw.Handle) , 0)
ListAddLast(raw.TilesList, M)
xUpdateWorld(1)
If xEntityInView(M.Handle, Camera) = False Then Exit
Forever

   'Left
I = 0
  Repeat
M = New T_Type
I = I + 1
M.Handle = xCopyMesh(raw.Handle)
xPositionEntity(M.Handle, xEntityX(Raw.Handle) - (xMeshWidth(Raw.Handle) + Raw.Parent.Gap) * I, xEntityY(Raw.Handle) , 0)
ListAddFirst(raw.TilesList, M)
xUpdateWorld(1)
If xEntityInView(M.Handle, Camera) = False Then Exit
Forever
 
 End Function
 
 '------------Play--------'
 
 Function PlayMeshTile(Meshtile:MeshTiler_Type)
  Local Raw:RawHolder_Type, Y:Float

If Meshtile.RawsList.Count() = 0 Then Return

If MeshTile.Direction = Dir_Both Then

   'Down 
    Raw = RawHolder_Type(Meshtile.RawsList.Last())

   'Remove
If MeshOutOfVertView(raw.Handle, Camera) Then
raw.Free()
ListRemove(Meshtile.RawsList, raw)
Raw = Null
   'Add
Else
Y = xEntityY(Raw.Handle) - (xMeshHeight(MeshTile.MainHandle) + MeshTile.Gap)
Raw = CreateNewRaw_Int(MeshTile, Y)
ListAddLast(MeshTile.RawsList, Raw)
End If

If Meshtile.RawsList.Count() = 0 Then Return

   'Up
    Raw = RawHolder_Type(Meshtile.RawsList.First())

   'Remove
If MeshOutOfVertView(raw.Handle, Camera) Then
Raw.Free()
ListRemove(Meshtile.RawsList, Raw)
Raw = Null
   'Add
Else
Y = xEntityY(Raw.Handle) + (xMeshHeight(MeshTile.MainHandle) + MeshTile.Gap)
Raw = CreateNewRaw_Int(MeshTile, Y)
ListAddFirst(MeshTile.RawsList, Raw)
End If

   'Play
For Raw = EachIn Meshtile.RawsList
PlayTileHoriz_Int(Raw)
Next

Else

End If


 End Function
 
 Function CreateNewRaw_Int:RawHolder_Type(Meshtile:MeshTiler_Type, Y:Float)
  Local NR:RawHolder_Type, X:Float

NR = New RawHolder_Type
NR.Handle = xCopyMesh(Meshtile.MainHandle)
NR.Parent = Meshtile

X = MatrixValue_Int(xEntityX(Camera), xMeshWidth(Meshtile.MainHandle) + Meshtile.Gap)
xPositionEntity(NR.Handle, X, Y, 0)

InitialTileHorizCreate(NR)

Return NR
 End Function
 
 Function GetVisibleMeshFromRaw_Int(Raw:RawHolder_Type)
  Local T:T_Type
For T = EachIn raw.TilesList
If xEntityVisible(T.Handle, Camera) Then Return T.Handle
Next
 End Function
 
Function MatrixValue_Int:Float(Value:Float, Matrix:Float)
Local TempVal:Float
TempVal = Floor(Value / Matrix) * Matrix
Return TempVal
End Function
 
 Function PlayTileHoriz_Int(Raw:RawHolder_Type)
  Local M:T_Type

   'Right
  If xEntityInView(T_Type(raw.TilesList.Last()).Handle, Camera) = True Then
M = New T_Type
M.Handle = xCopyMesh(raw.Handle)
xPositionEntity(M.Handle, xEntityX(T_Type(raw.TilesList.Last()).Handle) + xMeshWidth(T_Type(raw.TilesList.Last()).Handle) + raw.Parent.Gap, xEntityY(T_Type(raw.TilesList.Last()).Handle), 0)
ListAddLast(raw.TilesList, M)
End If

   'Left
If xEntityInView(T_Type(raw.TilesList.First()).Handle, Camera) = True Then
M = New T_Type
M.Handle = xCopyMesh(Raw.Handle)
xPositionEntity(M.Handle, xEntityX(T_Type(Raw.TilesList.First()).Handle) - (xMeshWidth(T_Type(Raw.TilesList.First()).Handle) + Raw.Parent.Gap), xEntityY(T_Type(Raw.TilesList.First()).Handle), 0)
ListAddFirst(raw.TilesList, M)
End If

If raw.TilesList.Count() > 2 Then
If xEntityInView(T_Type(raw.TilesList.ValueAtIndex(1)).Handle, Camera) = False Then
xFreeEntity(T_Type(raw.TilesList.First()).Handle)
ListRemove(raw.TilesList, raw.TilesList.First())
End If
If xEntityInView(T_Type(raw.TilesList.ValueAtIndex(raw.TilesList.Count() - 2)).Handle, Camera) = False Then
xFreeEntity(T_Type(raw.TilesList.Last()).Handle)
ListRemove(raw.TilesList, raw.TilesList.Last())
End If
End If

 End Function
 
 Function MeshOutOfVertView(Mesh, Camera)
  Local KeepX:Float, Answer
KeepX = xEntityX(Mesh)
  xPositionEntity(Mesh, xEntityX(Camera), xEntityY(Mesh), 0)
xUpdateWorld()
If xEntityInView(Mesh, Camera) = False Then Answer = True
xPositionEntity(Mesh, KeepX, xEntityY(Mesh), 0)
Return Answer
 End Function
 
 '-------------------------------------'
 
 Const Dir_Horiz = 1
 Const Dir_Vert = 2
 Const Dir_Both = 3
 
Function CreateNewTiler:MeshTiler_Type(Mesh, Gap:Float = 0.1, Direction = Dir_Both)
Local NT:MeshTiler_Type

NT = New MeshTiler_Type
NT.MainHandle = Mesh
NT.Gap = Gap
NT.Direction = Direction

CreateTileSheet_Int(NT)

Return NT
End Function
 
 Type MeshTiler_Type
  Field MainHandle
Field Gap:Float = 0.1
Field Direction
  Field RawsList:TList = CreateList()
 End Type
 
 Type RawHolder_Type
  Field Handle
  Field TilesList:TList = CreateList()
Field Parent:MeshTiler_Type
Method Free()
Local M:T_Type
For M = EachIn TilesList
xFreeEntity(M.Handle)
Next
xFreeEntity(Handle)
ClearList(TilesList)
End Method
 End Type
 
Type T_Type
Field Handle
End Type


Go with the Flow

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal