Landscape old school v1

Everything related to 3D programming
User avatar
minimy
Enthusiast
Enthusiast
Posts: 616
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Landscape old school v1

Post by minimy »

Hello, i did this landscape old school style. Use cursor to move and lshift to move fast.

The good thing about this system is that it allows you to make very large maps that are generated automatically. The actual measurements of the map are those of the image we use as a relief map.
If anyone can help to add collisions to the map I would be very grateful.

Code: Select all


; # minimy old school landscape - 2024

  UsePNGImageDecoder()
  UseJPEGImageDecoder()
  
  Global  fontLit= LoadFont(#PB_Any,"Arial",9)
  Global  fontMed= LoadFont(#PB_Any,"Arial",32)
  
  Global.l  niebla_color=       $aaaaaa
  Global.d  niebla_intensidad=  0.75
  Global.l  niebla_cerca=       50
  Global.l  niebla_lejos=       5000


  Procedure   dummyIni()
    Protected cubo_mesh=    CreateCube(#PB_Any,1)
    Protected cubo_mate=    CreateMaterial(#PB_Any, #Null,$0000ff)
    Global cubo_enti=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_mate));,4,1,4)
    HideEntity(cubo_enti,1)
  EndProcedure
  Procedure   dummy(x.f,y.f,z.f,color.l= $0000ff, sx.f=1,sy.f=1,sz.f=1)
    Protected m= CreateMaterial(#PB_Any,#Null,color)
    Protected a= CopyEntity(cubo_enti,#PB_Any)
    MoveEntity( a,x,y,z,#PB_Absolute)
    ScaleEntity(a,sx,sy,sz,#PB_Absolute)

    SetEntityMaterial(a,MaterialID(m))
    ProcedureReturn a
  EndProcedure
  Procedure   spr2DText(spr, txt.s, ink.l=$ff00ff00)
    Protected n,y=2,tl= CountString(txt,#CR$)
    Protected tx.s
    StartDrawing(SpriteOutput(spr))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0,0,OutputWidth(),OutputHeight(),$00000000)
    DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
      Box(0,0,OutputWidth(),OutputHeight(),$aa000000)
      DrawingFont(FontID(fontLit))
      For n=1 To tl+1
        tx= StringField(txt,n,#CR$)
        DrawText(5,y,tx,ink)
        y+16
      Next n
      DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Outlined)
      Box(0,0,OutputWidth(),OutputHeight(),ink)
    StopDrawing()
  EndProcedure
  
;{ landScape
Structure land_struc
    file.s
    img.i
    ;mapa físico
    mate.i
    mesh.i
    enti.i
    tileX.i
    tileZ.i
    tilesX.i
    tilesZ.i
    planX.i
    planZ.i
    ;mapa virtual
    sizeX.i
    sizeZ.i
    playX.i
    playZ.i
    oldPlayX.i
    oldPlayZ.i
  EndStructure
  Global land.land_struc
  Global Dim landData.MeshVertex(0)
  Global Dim landMapa.d(1,1)
  Procedure   landImg(size=512)
    Protected size2= size*2, x,y,b,r1,r2
    Protected.s t="minimy"
    Protected i=CreateImage(#PB_Any,size2,size2,24,0)
    StartDrawing(ImageOutput(i))
      DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Gradient)      
        BackColor($ffffffff)
        FrontColor($00000000)
      For p=0 To 200
        x= Random(OutputWidth())
        y= Random(OutputHeight())
        n=  OutputWidth()/8
        r1= Random(n,n*0.5)
        r2= Random(n,n*0.5)
        EllipticalGradient(x,y, r1, r2)
        Ellipse(x, y, r1,r2)   
      Next p
      DrawingMode(#PB_2DDrawing_Default | #PB_2DDrawing_Transparent)
      DrawingFont(FontID(fontMed))
      DrawText((OutputWidth()-TextWidth(t))/2,(OutputHeight()-TextHeight(t))/2,t,$ffffff)
      StopDrawing()
      ResizeImage(i,size,size)
;       showImage(i)
    ProcedureReturn i
  EndProcedure
  Procedure   landIni(planX=50,planZ=50, tilesX=50,tilesZ=50, file.s="test.png", altura=50, mate=0)
    Protected.l c, x,z
    ;mapa virtual
    land\file=      file
    If LCase(GetExtensionPart(file))="png"
      land\img=     LoadImage(#PB_Any,file)
    Else
      land\img=     Val(file)
    EndIf
    land\sizeX=     ImageWidth(land\img)
    land\sizeZ=     ImageHeight(land\img)
    FreeArray(landMapa())
    Dim landMapa(land\sizeX, land\sizeZ)
    StartDrawing(ImageOutput(land\img))
    For z=0 To land\sizeZ-1
      For x=0 To land\sizeX-1
        c=Point(x,z)
        landMapa(x,z)= (altura * Red(c)) /  255
      Next
    Next
    StopDrawing()
    ;mapa físico
    land\planX=     planX
    land\planZ=     planZ
    land\tilesX=    tilesX
    land\tilesZ=    tilesZ
    land\tileX=     planX / tilesX
    land\tileZ=     planZ / tilesZ
    land\mate=      mate
    land\mesh=      CreatePlane(#PB_Any,land\planX, land\planZ, land\tilesX, land\tilesZ, 1,1)
    land\enti=      CreateEntity(#PB_Any,MeshID(land\mesh),MaterialID(land\mate), 0, 0, 0)
    GetMeshData(land\mesh,0, landData(), #PB_Mesh_Vertex | #PB_Mesh_Normal  , 0, MeshVertexCount(land\mesh)-1)
    
  EndProcedure
  Procedure   landSet(player)
    x2= land\playX - (land\tilesX / 2)
    x1= land\playX + (land\tilesX / 2)
    z1= land\playZ - (land\tilesZ / 2)
    z2= land\playZ + (land\tilesZ / 2)
    If z1>-1 And z2<land\sizeZ And x2>-1 And x1<land\sizeX-1
    c=0
    For z = z1 To z2
      For x = x1 To x2 Step -1
        y3=z : If y3<0: y3= (land\sizeZ-1)+z: EndIf
        x3=x : If x3<0: x3= (land\sizeX-1)+x: EndIf
        height.f= landMapa(x3,y3)
        landData(c)\y =  height 
        c+1
      Next
    Next 
    MoveEntity(land\enti, land\playX * land\tileX, 0, land\playZ * land\tileZ, #PB_Absolute)
    
    SetMeshData(land\mesh,0, landData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(land\mesh)-1)     
    NormalizeMesh(land\mesh)
    UpdateMeshBoundingBox(land\mesh)
    EndIf
  EndProcedure

;}
  

  InitEngine3D()
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Parse3DScripts()

  ExamineDesktops()
  DesktopW = DesktopWidth(0)
  DesktopH = DesktopHeight(0)

  winMain= OpenWindow(#PB_Any, 0, 0, DesktopW, DesktopH, "landScap (old school)",#PB_Window_Maximize|#PB_Window_BorderLess)
  DesktopW = WindowWidth(winMain)
  DesktopH = WindowHeight(winMain)
  OpenWindowedScreen(WindowID(winMain), 0, 0, DesktopW, DesktopH, 0, 0, 0, #PB_Screen_NoSynchronization)
    AntialiasingMode(#PB_AntialiasingMode_None)
    WorldShadows(#PB_Shadow_Additive) 
    
    camara= 0
    cam1=   1
    CreateCamera(camara, 0, 0, 100, 100)
    MoveCamera(camara, 500, 320, 400, #PB_Absolute)
    CameraLookAt(camara,0,0,0)
    CameraBackColor(camara, niebla_color)
    
    CreateCamera(cam1, 1, 1, 30, 30)
    MoveCamera(cam1, 1100, 720, 1100, #PB_Absolute)
    CameraBackColor(cam1, $999999)
    
    CreateLight(0, RGB(255, 255, 255), 1000, 500, 0)

  
  suelo_tx3D=   CreateTexture(#PB_Any,128,128)
  StartDrawing(TextureOutput(suelo_tx3D))
    Box(0,0,OutputWidth(),OutputHeight(),$0)
    Box(2,2,OutputWidth()-4,OutputHeight()-4,$ff00ff00)
  StopDrawing()
  tiles= 51
  suelo_mate=   CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
  m1= CopyMaterial(suelo_mate,#PB_Any)
  ScaleMaterial(suelo_mate,1/tiles,1/tiles)
  MaterialCullingMode(suelo_mate, #PB_Material_NoCulling)
  
  i= landImg()
  landIni(500,500,50,50,Str(i),55,suelo_mate)
;   landIni(1500,1500,50,50,"test.png",50,suelo_mate)
  
  
  dummyIni()
  player= dummy( (land\sizeX* land\tileX)/2, 60, (land\sizeZ* land\tileZ)/2 ,$0000ff, 5,5,5)
  
  landSet(player)
  
  sprInfo=    CreateSprite(#PB_Any,100,200,#PB_Sprite_AlphaBlending)
  
  SetFrameRate(50)
camaraSpeed= 1
Repeat
  Repeat 
  Until WindowEvent()=0
          
      ExamineKeyboard()
      ExamineMouse()
      
      ;{ player
      If KeyboardPushed(#PB_Key_LeftShift)
        spdPlayer= 10
      Else
        spdPlayer= 1
      EndIf
      
      If KeyboardPushed(#PB_Key_Up)
        MoveEntity(player,0,0,spdPlayer,#PB_Local)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveEntity(player,0,0,-spdPlayer,#PB_Local)
      EndIf
      
      If KeyboardPushed(#PB_Key_Left)
        RotateEntity(player,0,1,0,#PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_Right)
        RotateEntity(player,0,-1,0,#PB_Relative)
      EndIf
      MoveCamera(cam1,EntityX(player),EntityY(player),EntityZ(player),#PB_Absolute)
      CameraDirection(cam1, EntityDirectionX(player)*-1,EntityDirectionY(player),EntityDirectionZ(player)*-1)
      ;}
      
      land\playX= Round(EntityX(player) / land\tileX, #PB_Round_Down)
      land\playZ= Round(EntityZ(player) / land\tileZ, #PB_Round_Down)
      If land\oldPlayX<>land\playX Or land\oldPlayZ<>land\playZ
        landSet(player)
        land\oldPlayX= land\playX
        land\oldPlayZ= land\playZ
      EndIf
      
      CameraFollow(camara,EntityID(land\enti), 30, 300,500,0.1,0.1)
      CameraLookAt(camara, EntityX(player),0,EntityZ(player))
      RenderWorld()
           
        txt.s= "minimy 2024"+ #CR$+
               "landscape" +#CR$+
               "old school" +#CR$+#CR$+
               "FPS: "+Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) +#CR$+
               Str(ScreenWidth())+" x "+Str(ScreenHeight()) +#CR$+
;                "X: "+StrF(CameraX(camara),2)+#CR$+"Y: "+StrF(CameraY(camara),2)+#CR$+"Z: "+StrF(CameraZ(camara),2); +#CR$+
               "X: "+StrF(land\playX) +#CR$+
               "Z: "+StrF(land\playZ) +#CR$+
               "▲►▼◄ Move " +#CR$+
               "LShift HiSpeed "; +#CR$+
        spr2DText(sprInfo,txt, $ff00ffff)
        DisplayTransparentSprite(sprInfo,ScreenWidth()-110,10)
        
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1



If translation=Error: reply="Sorry, Im Spanish": Endif
Sergey
User
User
Posts: 57
Joined: Wed Jan 12, 2022 2:41 pm

Re: Landscape old school v1

Post by Sergey »

If I press Up or Down keys the red box moves far to the sky, Left and Right keys work well
https://i.ibb.co/n6ySQtk/1437.png
User avatar
minimy
Enthusiast
Enthusiast
Posts: 616
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Landscape old school v1

Post by minimy »

Hi Sergey! After test again i see all work fine. Tested with PB6.02/win10-x64. and over 32bit system too.
The box moves over coord X and Z and the map is created in real time.
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Kiffi
Addict
Addict
Posts: 1500
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Landscape old school v1

Post by Kiffi »

Sergey wrote: Wed May 08, 2024 9:22 amIf I press Up or Down keys the red box moves far to the sky
Same here. Initially, X and Z have the value 256. After I have pressed Up or Down, they have the value 0.

PB 6.11 LTS Beta 2 (x64) / Win 10 Pro
Hygge
Sergey
User
User
Posts: 57
Joined: Wed Jan 12, 2022 2:41 pm

Re: Landscape old school v1

Post by Sergey »

minimy wrote: Wed May 08, 2024 10:11 am Hi Sergey! After test again i see all work fine. Tested with PB6.02/win10-x64. and over 32bit system too.
The box moves over coord X and Z and the map is created in real time.
You're right!
I tested on Win10 X64 PureBasic 6.02 - 6.04 OK
PureBasic 6.10 - 6.11 Beta 2 don't OK :)
Interesting example for me, thanks minimy
User avatar
minimy
Enthusiast
Enthusiast
Posts: 616
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Landscape old school v1

Post by minimy »

Thanks to you Sergei!

About X and Z goes to 0 is really weird... because the comand used to move the box (player) is this: MoveEntity(player,0,0,spdPlayer,#PB_Local) and only move in Z into the local coordinate and the var spdPlayer only goes betwen 1 and 10. Weird to jump from 256 to 0.

Kiffi and Sergei, after install PB6.1 and testing with PB6.1 my small computer crash :mrgreen: is a potato pc with Atom CPU.
I will try with the father :lol: is an I7 with rtx nvidia card.

Ok, teste with PB6.1, X and Z goes to 0... Why? Idk. But is really weird :shock:
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Landscape old school v1

Post by Caronte3D »

minimy wrote: Wed May 08, 2024 6:40 pm Ok, teste with PB6.1, X and Z goes to 0... Why? Idk. But is really weird :shock:
Use #PB_Relative to move:

Code: Select all

      If KeyboardPushed(#PB_Key_Up)
        MoveEntity(player,0,0,spdPlayer,#PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveEntity(player,0,0,-spdPlayer,#PB_Relative)
      EndIf
User avatar
minimy
Enthusiast
Enthusiast
Posts: 616
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Landscape old school v1

Post by minimy »

Use #PB_Relative to move:
Hi Caronte3D! Thanks for help. But,.. as I understand it, PB_Local uses the local coordinates of the object, so Z will always be the Z coordinate of the object (move forward).
If I use PB_Relative the coordinate will be relative to the value that Z already had, but the move will use the global Z coordinate.
In short, the object will not move forward, it will move in Z but globally, so it will never move forward in X, even if it's rotated.
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Landscape old school v1

Post by Caronte3D »

If you try it, you can see the move is perfect.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 616
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Landscape old school v1

Post by minimy »

If you try it, you can see the move is perfect.
Hello Charon3D! Yes, it's been tested, it works fine with PB6.1, you're right! But with version 6.02 it moves like I said... I don't know why. Thanks for the help and have a nice weekend!
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply