PureBASIC and scrolling

Advanced game related topics
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

PureBASIC and scrolling

Post by Olliv »

(From a discussing which started here, I picked up 3 codes I published in the forum before.

These codes are representative. They show different methods (3 methods) to display a scrolling. Each of them have advantages and inconveniences.
The inventory of these characteristics are such exact as others members give theirs observations.

I keep actually the original discussing. I will change certainly this first part to structure it.)

First method - A grid of regular tiles - Two examples of horizontal scrolling >>>

@Leonhardt

Hello and excuse me but I think you ignore the importance of the help of this forum. :D Here is there which can help you about a scrolling. For example... ME !!

Well... No too longtime, but others can help you after...

I have got THREE methods to display a scrolling... So, THREE codes... ! Please use the arrow keys to watch the scrolling effects...
Thank you

1/3 = The more recent:

Code: Select all

Procedure CreateTileTest(No.I, Wi.I, He.I)
  CreateImage(No, Wi, He, 32)
  StartDrawing(ImageOutput(No) )
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, Wi, He, RGBA(0, 0, 0, 0) )
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    Box(0, 0, Wi, He, RGBA(255, 255, 255, 255) )
    Box(1, 1, Wi - 2, He - 2, RGBA(0, 0, 0, 255) )
  StopDrawing()
  CreateSprite(No, Wi, He, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(No) )
    DrawAlphaImage(ImageID(No), 0, 0)
  StopDrawing()
  CreateSprite3D(No, No)
  Start3D()
    DisplaySprite3D(No, 0, 0)
  Stop3D()
EndProcedure

  InitSprite()
  InitSprite3D()
  InitKeyboard()
  ExamineDesktops()
  Dw = DesktopWidth(0)
  Dh = DesktopHeight(0)
  Dd = DesktopDepth(0)
  
  Wi = 32
  He = 32
  OpenScreen(Dw, Dh, Dd, "")
    CreateTileTest(1, Wi, He)
    Repeat
      Delay(1)
      Start3D()
        For XS3 = 0 To Dw - 1 Step 32
          For YS3 = 0 To Dh - 1 Step 32
            DisplaySprite3D(1, XS3 + OffsetX, YS3)
          Next
        Next
      Stop3D()
      FlipBuffers()
      ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Right)
        If OffsetXAccel > -8
          OffsetXAccel - 2
        EndIf
      EndIf
      
      If KeyboardPushed(#PB_Key_Left)
        If OffsetXAccel < 8
          OffsetXAccel + 2
        EndIf
      EndIf
      
      OffsetX + OffsetXAccel
      If OffsetX > 31
        OffsetX - 32
      EndIf
      If OffsetX < 0
        OffsetX + 32
      EndIf
      If OffsetXAccel > 0
        OffsetXAccel - 1
      EndIf
      If OffsetXAccel < 0
        OffsetXAccel + 1
      EndIf
    Until KeyboardPushed(#PB_Key_Escape)
  CloseScreen()
2/3 The more light for the CPU (GPU is used via OGRE)

Code: Select all

;PB 4.30
;10/01/09 Substitution de l'instruction TextureOutput (Comtois)
;20/03/09 Test défilement 2D (Ollivier)
;23/03/09 Sécurité de dessin (Dr Dri)
;23/03/09 Chronométrie pour Linux (Cpl.Bator)
;25/03/09 Architecture propre pour débutants (djes)
Global Dim Test.I(3)
;___________________________________________________________
;{ Informations initiales
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
;___________________________________________________________
;{ Constantes
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
#R2D = 180.0 / #PI ; R2D = Conversion Radians/Degrés
Enumeration
#Texture
#TextureSol
EndEnumeration
Enumeration
#Matiere
#MatiereSol
EndEnumeration
Enumeration
#Entity
#EntitySol
EndEnumeration
;}
;___________________________________________________________
;{ Déclaration des procédures
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Declare.S TempPng(n.I)
Declare.S InitSmoothScrolling()
Declare.I Erreur(Message.S)
;}
;___________________________________________________________
;{ Structures
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Structure V3D ; Vecteur 3D
   X.F
   Y.F
   Z.F
EndStructure

Structure V2D ; Vecteur 2D
   U.F
   V.F
EndStructure

Structure Sommet
   P.V3D ; Position
   N.V3D ; Normale
   C.L   ; Couleur
   T.V2D ; Texture
EndStructure

Structure Triangle
   A.W
   B.W
   C.W
EndStructure
;}
;___________________________________________________________
;{ Variables globales
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Global PleinEcran.I = 1 ; 1 = FullScreen, 0 = Fenêtré
Global EcranLargeur.I
Global EcranHauteur.I
Global EcranBitsParPixel.I
Global Dim Sommet.Sommet(0)
Global ZI.I
Global XI.I
Global Dim Triangle.Triangle(0)
Global FigCarre.I ; N° de la structure fil de fer carré
Global Camera.I ; N° de la caméra (par défaut : 0)
Global RepertoireCaractere.S = "\"
Global Rapport.S = InitSmoothScrolling()

Global ScrollX.F
Global ScrollY.F
Global AngleTeta.F
Global Coef.F
Global AngleV.F
Global Angle.f, Vitesse.f

If Rapport <> "!Init"
   MessageRequester("Erreur", "Impossible d'initialiser le programme !")
   End
EndIf
;}
;}
;___________________________________________________________
;{ Crée les 2 éléments (Images,textures,matières et entités)
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

; Semblant de vaisseau
If CreateImage(#Texture, 64, 64)
   If StartDrawing(ImageOutput(#Texture))
      Box(0, 0, ImageWidth(#Texture), ImageHeight(#Texture), $FFFFFF)
      DrawingMode(#PB_2DDrawing_Outlined) ; Pour tracer le contour
      Box(0, 0, ImageWidth(#Texture), ImageHeight(#Texture), 0)
      StopDrawing()
      If SaveImage(#Texture, TempPng(1), #PB_ImagePlugin_PNG)
         FreeImage(#Texture)
         If LoadTexture(#Texture, TempPng(1) )
            If CreateMaterial(#Matiere, TextureID(#Texture))
               ; Crée le semblant de vaisseau
               If CreateEntity(#Entity, MeshID(FigCarre), MaterialID(#Matiere))
                  ; Agrandit et positionne le semblant de vaisseau
                  ScaleEntity(#Entity, 10, 10, 10) ; Agrandit l'entité
                  EntityLocate(#Entity, 500, 100, 500)
                  Rapport + "!E1" ; /!\
               EndIf
            EndIf
         EndIf
      EndIf
   EndIf
EndIf

; Texture du fond étoilé de l'écran
If CreateImage(#TextureSol, 512, 512)
   If StartDrawing(ImageOutput(#TextureSol)) ; On sécurise
      Box(0, 0, ImageWidth(#TextureSol), ImageHeight(#TextureSol), $0)
      For I = 0 To 500
         R = Random(127) + 128
         X = Random(511)
         Y = Random(511)
         Plot(X, Y, RGB(R, R, R) )
         For D = 1 To 5
            C = RGB(R / 1 << D, R / 1 << D, R / 1 << D)
            Line((X - D << 1) & 511, (Y) & 511, -2, 1, C)
            Line((X + D << 1) & 511, (Y) & 511, 2, 1, C)
            Line((X) & 511, (Y - D << 1) & 511, 1, -2, C)
            Line((X) & 511, (Y + D << 1) & 511, 1, 2, C)
            For DX = -1 To 1 Step 2
               For DY = -1 To 1 Step 2
                  Plot((X + DX * D) & 511, (Y + DY * D) & 511, C)
               Next DY
            Next DX
         Next D
      Next I
      StopDrawing()
      If SaveImage(#TextureSol, TempPng(2), #PB_ImagePlugin_PNG)
         If LoadTexture(#TextureSol, TempPng(2) )
            If CreateMaterial(#MatiereSol, TextureID(#TextureSol) )
               ; On rajoute 3 autres couches pour le parallaxe
               For I = 1 To 3
                  ; ******************************************************************************
                  Test(I) = AddMaterialLayer(#MatiereSol, TextureID(#TextureSol), #PB_Material_Modulate)
                  ; ******************************************************************************
               Next I
               If CountMaterialLayers(#MatiereSol) < 4: Erreur("Parallaxe impossible !"): EndIf
               ;Erreur(Str(CountMaterialLayers(#MatiereSol) ) )
               ; Crée le fond étoilé
               If CreateEntity(#EntitySol, MeshID(FigCarre), MaterialID(#MatiereSol) )
                  ; Agrandit et positionne le fond étoilé
                  ScaleEntity(#EntitySol, 1000, 2, 1000)
                  EntityLocate(#EntitySol, 500, -50, 500)
                  Rapport + "!E2" ; /!\
               EndIf
            EndIf
         EndIf
      EndIf
   EndIf
EndIf

If Rapport <> "!Init!E1!E2"
   CloseScreen()
   Delay(1)
   MessageRequester("Erreur à la création", "Rapport : " + Rapport)
   End
EndIf
;}
;___________________________________________________________
;{ Boucle principale
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Angle = #PI / 2.0
StartTime = ElapsedMilliseconds() + 16
Repeat
   Delay(1) ; On prévient XP qu'on est cool (XP)
   RealTime.I = ElapsedMilliseconds()
   If RealTime => StartTime ; On bride (Linux)
      StartTime + 16
      If PleinEcran
         Delay(1)
      Else
         WindowEvent()
      EndIf
      If ExamineKeyboard()
         If KeyboardPushed(#PB_Key_Up)
            Vitesse + 0.0001
         EndIf
         Vitesse * 0.99
         If KeyboardPushed(#PB_Key_Left)
            AngleV + 0.005
         ElseIf KeyboardPushed(#PB_Key_Right)
            AngleV - 0.005
         EndIf
         If KeyboardPushed(#PB_Key_Escape)
            Break
         EndIf         
      EndIf
      Angle + AngleV
      AngleV * 0.9
      ScrollX + Cos(Angle) * Vitesse
      ScrollY + Sin(Angle) * Vitesse
      RotateEntity(#Entity, 0.0, Angle * #R2D, 0.0, PB_Absolute)
      CameraLocate(Camera, 500 - Cos(-Angle - #PI / 2) * Vitesse * 16000, 500, 500 - Sin(-Angle - #PI / 2) * Vitesse * 16000)
      For I = 0 To 3
         Coef = 1.0 / (1 + I)
         ; ****************************************************************************************************
         ScrollMaterial(#MatiereSol, ScrollX / Coef, ScrollY / Coef, #PB_Material_Fixed, I)
         ; ****************************************************************************************************
         ;ScrollMaterial(#MatiereSol, 0.0, 0.0, #PB_Material_Fixed, 1)        
      Next I
      RenderWorld()
      FlipBuffers()
   EndIf
ForEver
CloseScreen()
Delay(1)
For I = 1 To 3
  Debug Test(I)
Next
End
;}
;___________________________________________________________

;___________________________________________________________
;{ Procédures
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Procedure Erreur(Message.S)
   CloseScreen()
   MessageRequester("Erreur", Message)
   End
EndProcedure

Procedure.S TempPng(n.I)
   ;________________________________________________________
   ; Retourne un nom de fichier PNG temporaire numéroté
   ;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   Protected Result.S
   Result = "Temp" + Str(n) + ".PNG"
   ProcedureReturn Result
EndProcedure

Procedure.S InitSmoothScrolling()
   ;________________________________________________________
   ; Intègre toute la partie initialisation 3D
   ;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   Protected Result.S
   CompilerIf Subsystem("DirectX9") = 0
      ;MessageRequester("Erreur dans les options de compilation", "Ajoutez la librairie sous-système : DirectX9 !")
      ;End
   CompilerEndIf    

;___________________________________________________________
;{ Initialisations générales
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
If InitEngine3D()       ; Initialise Ogre
   If InitSprite()         ; Initialise DirectX
      If InitKeyboard()       ; Initialise le clavier
         UsePNGImageEncoder() ; Prise en charge du format image PNG
         Add3DArchive(RepertoireCaractere, #PB_3DArchive_FileSystem)
;}
;___________________________________________________________
;{ Préparation de la zone de travail graphique
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
         If ExamineDesktops()
            EcranLargeur      = DesktopWidth(0)
            EcranHauteur      = DesktopHeight(0)
            EcranBitsParPixel = DesktopDepth(0)
            EcranLargeur      = 1024
            EcranHauteur      = 768
            EcranBitsParPixel = 32

            If PleinEcran
               OpenScreen(EcranLargeur, EcranHauteur, EcranBitsParPixel, "Smooth scrolling")
            Else
               OpenWindow(0, 0, 0, EcranLargeur, EcranHauteur, "Smooth scrolling", $80000000)
               OpenWindowedScreen(WindowID(0), 0, 0, EcranLargeur, EcranHauteur, 0, 0, 0)
            EndIf
            If AvailableScreenMemory() < (1 << 26)
               ;CloseScreen()
               ;MessageRequester("Conflit matériel", "La mémoire est trop faible ! (< 64 Mo)")
               ;End
            EndIf
;}

;___________________________________________________________
;{ Création d'une structure fil de fer de base (plan carré)
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
            Global Dim Sommet.Sommet(3) ; 4 sommets
            For ZI = 0 To 1
               For XI = 0 To 1
                  Index = 2 * ZI + XI
                  With Sommet(Index)
                     \P\X = XI - 0.5
                     \P\Y = 0.0
                     \P\Z = ZI - 0.5
                     \N\X = 0.0
                     \N\Y = 1.0
                     \N\Z = 0.0
                     \C   = RGB(0, 0, 0)
                     \T\U = ZI
                     \T\V = XI
                  EndWith     
               Next XI
            Next ZI

            Global Dim Triangle.Triangle(1) ; 2 triangles
            With Triangle(0)
               \A = 0
               \B = 2
               \C = 3
            EndWith
            With Triangle(1)
               \A = 3
               \B = 1
               \C = 0
            EndWith
            FigCarre = CreateMesh(#PB_Any, 2)
            If FigCarre
               Index = #PB_Mesh_Vertex
               Index | #PB_Mesh_Normal
               Index | #PB_Mesh_Color
               Index | #PB_Mesh_UVCoordinate
               SetMeshData(FigCarre, Index,         @Sommet(0),   4)
               SetMeshData(FigCarre, #PB_Mesh_Face, @Triangle(0), 2)
;}
;___________________________________________________________
;{ Initialisation de la caméra
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
               ; Création de la caméra
               If CreateCamera(Camera, 0, 0, 100, 100)
                  ; Préparation de la caméra
                  CameraLocate(Camera, 500, 500, 500)
                  RotateCamera(Camera, 270, 270, 0)
                  Result = "!Init"
               EndIf
;}
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
            EndIf
         EndIf
      EndIf
   EndIf
EndIf
ProcedureReturn Result
EndProcedure
;}
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
3/3 The most "classic" if your background is static

Code: Select all

EnableExplicit

Macro DisplayP2()
      DisplaySprite(Page4, P2PosX, P2PosY)
      DisplaySprite(Page5, P2PosX - PageW, P2PosY)
      DisplaySprite(Page6, P2PosX, P2PosY - PageH)
      DisplaySprite(Page7, P2PosX - PageW, P2PosY - PageH)
EndMacro

Macro Display()
   DisplayP2()
   DisplayTransparentSprite(Page0, PosX, PosY)
   DisplayTransparentSprite(Page1, PosX - PageW, PosY)
   DisplayTransparentSprite(Page2, PosX, PosY - PageH)
   DisplayTransparentSprite(Page3, PosX - PageW, PosY - PageH)
   FlipBuffers()
EndMacro

Macro SpriteDrawing()
   
   For I = 0 To 3
      CreateSprite(#Syst + I, 16, 16)
      StartDrawing(SpriteOutput(#Syst + I) )
         DrawText(0, 0, "P" + Str(I) )
      StopDrawing()
   Next I
   
   CreateSprite(#Deco, TileSize, TileSize)
   StartDrawing(SpriteOutput(#Deco) )
      Box(0, 0, TileSize, TileSize, 0)
   StopDrawing()
   CreateSprite(#Deco + 1, TileSize, TileSize)
   StartDrawing(SpriteOutput(#Deco + 1) )
      Box(0, 0, TileSize, TileSize, 1)
      Box(1, 1, TileSize - 2, TileSize - 2, #Gray)
      Line(0, 0, TileSize, TileSize, 1)
      Line(TileSize - 1, 0, - TileSize, TileSize, 1)
   StopDrawing()

   CreateSprite(#Deco + 2, TileSize, TileSize)
   StartDrawing(SpriteOutput(#Deco + 2) )
      Box(0, 0, TileSize, TileSize, 1)
      Box(1, 1, TileSize - 2, TileSize - 2, RGB(0, 0, 255) )
      Line(0, 0, TileSize, TileSize, 1)
      Line(TileSize - 1, 0, - TileSize, TileSize, 1)
   StopDrawing()

   CreateSprite(#DispMask, DispW, DispH)
   StartDrawing(SpriteOutput(#DispMask) )
      Box(0, 0, DispW, DispH, #White)
      Box(1, 1, DispW - 2, DispH - 2, #Black)
   StopDrawing()
   
   For I = 0 To 7
      CreateSprite(I, PageW, PageH)
   Next I
EndMacro

Macro P2LevelLoading()
   For Y = 0 To P2LevelTileMaxY
      For X = 0 To P2LevelTileMaxX
         P2Tile(X, Y) = #Deco
      Next X
   Next Y   
   For Y = 0 To P2LevelTileMaxY
      P2Tile(0, Y) = #Deco + 2
      P2Tile(P2LevelTileMaxX, Y) = #Deco + 2
      For X = 0 To P2LevelTileMaxX
         P2Tile(X, 0) = #Deco + 2
         P2Tile(X, P2LevelTileMaxY) = #Deco + 2
         If Random(9) = 0
            P2Tile(X, Y) = #Deco + 2
         EndIf
      Next X
   Next Y
EndMacro

Macro LevelLoading()
   For Y = 0 To LevelTileMaxY
      For X = 0 To LevelTileMaxX
         Tile(X, Y) = #Deco
      Next X
   Next Y   
   For Y = 0 To LevelTileMaxY
      Tile(0, Y) = #Deco + 1
      Tile(LevelTileMaxX, Y) = #Deco + 1
      For X = 0 To LevelTileMaxX
         Tile(X, 0) = #Deco + 1
         Tile(X, LevelTileMaxY) = #Deco + 1
         If Random(9) = 0
            Tile(X, Y) = #Deco + 1
         EndIf
      Next X
   Next Y
   P2LevelLoading()
EndMacro

Macro GetPageNo(X, Y) ; Calcul de la page concernée
   ((X / PageW) & 1) + (((Y / PageH) & 1) << 1)
EndMacro

Macro GetXValue(X) ; Calcul de X dans la page
   ((X % PageW) - (X % TileSize) )
EndMacro

Macro GetYValue(Y) ; Calcul de Y dans la page
   ((Y % PageH) - (Y % TileSize) )
EndMacro

Macro P2GetTile(XX, YY)
   Tile = -1
   TempX = XX / TileSize
   If (TempX => 0)
      If (TempX <= P2LevelTileMaxX)
         TempY = YY / TileSize
         If (TempY => 0)
            If (TempY <= P2LevelTileMaxY)
               Tile = P2Tile(TempX, TempY)
            EndIf
         EndIf
      EndIf
   EndIf
EndMacro

Macro GetTile(XX, YY)
   Tile = -1
   TempX = XX / TileSize
   If (TempX => 0)
      If (TempX <= LevelTileMaxX)
         TempY = YY / TileSize
         If (TempY => 0)
            If (TempY <= LevelTileMaxY)
               Tile = Tile(TempX, TempY)
            EndIf
         EndIf
      EndIf
   EndIf
EndMacro

Macro P2UpdateTile(XX, YY)
   P2GetTile(XX, YY) ; Retourne le global Tile.I
   If Tile <> -1
      P = GetPageNo(XX, YY)
      UseBuffer(P + 4)
         DisplaySprite(Tile, GetXValue(XX), GetYValue(YY) )
      UseBuffer(-1)
   EndIf
EndMacro

Macro UpdateTile(XX, YY)
   GetTile(XX, YY) ; Retourne le global Tile.I
   If Tile <> -1
      P = GetPageNo(XX, YY)
      UseBuffer(P)
         DisplaySprite(Tile, GetXValue(XX), GetYValue(YY) )
      UseBuffer(-1)
   EndIf
EndMacro

Macro P2UpdateTileLeftBorder()
   For Y = (P2LevelY - TileSize) To (P2LevelY + PageH + TileSize) ; Rajouté +TileSize
      P2UpdateTile((P2LevelX - TileSize), Y)
      Y + TileSize
      Y - 1
   Next Y
EndMacro

Macro UpdateTileLeftBorder()
   For Y = (LevelY - TileSize) To (LevelY + PageH + TileSize) ; Rajouté +TileSize
      UpdateTile((LevelX - TileSize), Y)
      Y + TileSize
      Y - 1
   Next Y
EndMacro

Macro P2UpdateTileRightBorder()
   For Y = (P2LevelY - TileSize) To (P2LevelY + PageH)
      P2UpdateTile((P2LevelX + PageW), Y)
      Y + TileSize
      Y - 1
   Next Y
EndMacro

Macro UpdateTileRightBorder()
   For Y = (LevelY - TileSize) To (LevelY + PageH) ; Rajouté -TileSize
      UpdateTile((LevelX + PageW), Y)
      Y + TileSize
      Y - 1
   Next Y
EndMacro

Macro P2UpdateTileUpBorder()
   For X = (P2LevelX - TileSize) To (P2LevelX + PageW + TileSize) ; Rajouté +TileSize
      P2UpdateTile(X, (P2LevelY - TileSize) )
      X + TileSize
      X - 1
   Next X
EndMacro

Macro UpdateTileUpBorder()
   For X = (LevelX - TileSize) To (LevelX + PageW + TileSize) ; Rajouté +TileSize
      UpdateTile(X, (LevelY - TileSize) )
      X + TileSize
      X - 1
   Next X
EndMacro

Macro P2UpdateTileDownBorder()
   For X = (P2LevelX - TileSize) To (P2LevelX + PageW) ;Rajouté -TileSize
      P2UpdateTile(X, (P2LevelY + PageH) )
      X + TileSize
      X - 1
   Next X
EndMacro

Macro UpdateTileDownBorder()
   For X = (LevelX - TileSize) To (LevelX + PageW) ;Rajouté -TileSize
      UpdateTile(X, (LevelY + PageH) )
      X + TileSize
      X - 1
   Next X
EndMacro

Macro P2DrawVisibleTiles()
   For Y = P2LevelY To P2LevelY + PageH
      For X = P2LevelX To P2LevelX + PageW
         P2UpdateTile(X, Y)
         X + TileSize
         X - 1
      Next X
      Y + TileSize
      Y - 1
   Next Y
EndMacro

Macro DrawVisibleTiles()
   For Y = LevelY To LevelY + PageH
      For X = LevelX To LevelX + PageW
         UpdateTile(X, Y)
         X + TileSize
         X - 1
      Next X
      Y + TileSize
      Y - 1
   Next Y
EndMacro

Macro P2MoveLeft()
   P2VitX = VitX / 2
   If P2LevelX > 0           
      If P2LevelX - P2VitX < 0 ; "Sort" du niveau?
         P2VitX = P2LevelX
      EndIf               
      P2PosX + P2VitX
      If P2PosX => PageW
         P2ShiftX + PageW
         P2PosX - PageW
         Page4 ! 1
         Page5 ! 1
         Page6 ! 1
         Page7 ! 1
      EndIf
      P2LevelX = 0 - (P2ShiftX + P2PosX)
      P2OldLevelTileX = P2LevelTileX
      P2LevelTileX = P2LevelX / TileSize
      If P2LevelTileX <> P2OldLevelTileX
         P2UpdateTileLeftBorder()
      EndIf
      DisplayFlag = 1
   EndIf
EndMacro

Macro MoveLeft()
   If LevelX > 0           
      If LevelX - VitX < 0 ; "Sort" du niveau?
         VitX = LevelX
      EndIf               
      PosX + VitX
      If PosX => PageW
         ShiftX + PageW
         PosX - PageW
         Page0 ! 1
         Page1 ! 1
         Page2 ! 1
         Page3 ! 1
      EndIf
      LevelX = 0 - (ShiftX + PosX)
      OldLevelTileX = LevelTileX
      LevelTileX = LevelX / TileSize
      If LevelTileX <> OldLevelTileX
         UpdateTileLeftBorder()
      EndIf
      DisplayFlag = 1
      P2MoveLeft()
   EndIf   
EndMacro

Macro P2MoveRight()
   P2VitX = VitX / 2
   If P2LevelX < (P2LevelW - DispW)
      If P2LevelX + P2VitX > (P2LevelW - DispW) ; "Sort" du niveau?
         P2VitX = (P2LevelW - DispW) - P2LevelX
      EndIf
      P2PosX - P2VitX
      If P2PosX < 0
         P2ShiftX - PageW
         P2PosX + PageW
         Page4 ! 1
         Page5 ! 1
         Page6 ! 1
         Page7 ! 1
      EndIf
      P2LevelX = 0 - (P2ShiftX + P2PosX)
      P2OldLevelTileX = P2LevelTileX
      P2LevelTileX = P2LevelX / TileSize
      If P2LevelTileX <> P2OldLevelTileX
         P2UpdateTileRightBorder()
      EndIf
      DisplayFlag = 1
   EndIf
EndMacro

Macro MoveRight()
   If LevelX < (LevelW - DispW)
      If LevelX + VitX > (LevelW - DispW) ; "Sort" du niveau?
         VitX = (LevelW - DispW) - LevelX
      EndIf
      PosX - VitX
      If PosX < 0
         ShiftX - PageW
         PosX + PageW
         Page0 ! 1
         Page1 ! 1
         Page2 ! 1
         Page3 ! 1
      EndIf
      LevelX = 0 - (ShiftX + PosX)
      OldLevelTileX = LevelTileX
      LevelTileX = LevelX / TileSize
      If LevelTileX <> OldLevelTileX
         UpdateTileRightBorder()
      EndIf
      DisplayFlag = 1
      P2MoveRight()
   EndIf
EndMacro

Macro P2MoveUp()
   P2VitY = VitY / 2
   If P2LevelY > 0
      If P2LevelY - P2VitY < 0 ; "Sort" du niveau?
         P2VitY = P2LevelY
      EndIf
      P2PosY + P2VitY
      If P2PosY => PageH
         P2ShiftY + PageH
         P2PosY - PageH
         Page4 ! 2
         Page5 ! 2
         Page6 ! 2
         Page7 ! 2
      EndIf
      P2LevelY = 0 - (P2ShiftY + P2PosY)
      P2OldLevelTileY = P2LevelTileY
      P2LevelTileY = P2LevelY / TileSize
      If P2LevelTileY <> P2OldLevelTileY
         P2UpdateTileUpBorder()
      EndIf
      DisplayFlag = 1
   EndIf
EndMacro

Macro MoveUp()
   If LevelY > 0
      If LevelY - VitY < 0 ; "Sort" du niveau?
         VitY = LevelY
      EndIf
      PosY + VitY
      If PosY => PageH
         ShiftY + PageH
         PosY - PageH
         Page0 ! 2
         Page1 ! 2
         Page2 ! 2
         Page3 ! 2
      EndIf
      LevelY = 0 - (ShiftY + PosY)
      OldLevelTileY = LevelTileY
      LevelTileY = LevelY / TileSize
      If LevelTileY <> OldLevelTileY
         UpdateTileUpBorder()
      EndIf
      DisplayFlag = 1
   EndIf
   P2MoveUp()
EndMacro

Macro P2MoveDown()
   P2VitY = VitY / 2
   If P2LevelY < (P2LevelH - DispH)
      If P2LevelY + P2VitY > (P2LevelH - DispH) ; "Sort" du niveau?
         P2VitY = (P2LevelH - DispH) - P2LevelY
      EndIf
      P2PosY - P2VitY
      If P2PosY < 0
         P2ShiftY - PageH
         P2PosY + PageH
         Page4 ! 2
         Page5 ! 2
         Page6 ! 2
         Page7 ! 2
      EndIf
      P2LevelY = 0 - (P2ShiftY + P2PosY)
      P2OldLevelTileY = P2LevelTileY
      P2LevelTileY = P2LevelY / TileSize
      If P2LevelTileY <> P2OldLevelTileY
         P2UpdateTileDownBorder()
      EndIf
      DisplayFlag = 1
   EndIf
EndMacro

Macro MoveDown()
   If LevelY < (LevelH - DispH)
      If LevelY + VitY > (LevelH - DispH) ; "Sort" du niveau?
         VitY = (LevelH - DispH) - LevelY
      EndIf
      PosY - VitY
      If PosY < 0
         ShiftY - PageH
         PosY + PageH
         Page0 ! 2
         Page1 ! 2
         Page2 ! 2
         Page3 ! 2
      EndIf
      LevelY = 0 - (ShiftY + PosY)
      OldLevelTileY = LevelTileY
      LevelTileY = LevelY / TileSize
      If LevelTileY <> OldLevelTileY
         UpdateTileDownBorder()
      EndIf
      DisplayFlag = 1
      P2MoveDown()
   EndIf
EndMacro

#Syst = 4096
#DispMask = 16
#Deco = 256

;-______________
;- Globals...   
;-¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Global TileSize.I ; Taille d'un Tile en pixels

Global ScrW.I ; Largeur de l'écran en pixels
Global ScrH.I ; Hauteur de l'écran en pixels
Global ScrD.I ; Profondeur de l'écran en bits par pixels

Global DispX1.I ; Coordonnée de la zone d'affichage
Global DispY1.I ; en pixels
Global DispX2.I ;
Global DispY2.I ;

Global DispW.I  ; Dimensions de l'écran d'affichage
Global DispH.I  ; en pixels

Global PageW.I ; Dimensions d'une page en pixels
Global PageH.I ;

Global PageTileW.I ; Dimensions d'une page en Tiles
Global PageTileH.I ;

Global I.I
Global P.I
Global X.I
Global Y.I
Global Tile.I
Global TempX.I
Global TempY.I

Global Message.S

Global Page0.I = 0
Global Page1.I = 1
Global Page2.I = 2
Global Page3.I = 3
Global Page4.I = 4
Global Page5.I = 5
Global Page6.I = 6
Global Page7.I = 7
;-Vitesse
Global Vit.I = 8

Global PosX.I ; Décalage dans une page
Global PosY.I ; en pixels

Global P2PosX.I ; Décalage dans une page
Global P2PosY.I ; en pixels

Global ShiftX.I ; Décalage page par page
Global ShiftY.I ; en pixels

Global P2ShiftX.I ; Décalage page par page
Global P2ShiftY.I ; en pixels

Global LevelX.I ; Décalage total (page + position)
Global LevelY.I ; en pixels

Global P2LevelX.I ; Décalage total (page + position)
Global P2LevelY.I ; en pixels

Global LevelTileX.I ; Décalage total (page + position)
Global LevelTileY.I ; en Tiles

Global P2LevelTileX.I ; Décalage total (page + position)
Global P2LevelTileY.I ; en Tiles

Global OldLevelTileX.I ; Dans la détection des mises à jour
Global OldLevelTileY.I ; Ancien décalage total en Tiles

Global P2OldLevelTileX.I ; Dans la détection des mises à jour
Global P2OldLevelTileY.I ; Ancien décalage total en Tiles

Global LevelTileW.I ; Dimensions du niveau en Tiles
Global LevelTileH.I ;

Global P2LevelTileW.I ; Dimensions du niveau en Tiles
Global P2LevelTileH.I ;

Global LevelTileMaxX.I ; Limites du niveau en Tiles
Global LevelTileMaxY.I ;

Global P2LevelTileMaxX.I ; Limites du niveau en Tiles
Global P2LevelTileMaxY.I ;

Global LevelW.I ; Dimensions du niveau en pixels
Global LevelH.I ;

Global P2LevelW.I ; Dimensions du niveau en pixels
Global P2LevelH.I ;

Global DisplayFlag.I

Global P2VitX.I
Global P2VitY.I

Global VitX.I
Global VitY.I

Global VitesseX.I
Global VitesseY.I

Global QuitGame.I

Global P2VitXCarry.I

TileSize = 24

LevelTileW = 256
LevelTileH = 256

P2LevelTileW = 192
P2LevelTileH = 192

LevelW = LevelTileW * TileSize
LevelH = LevelTileH * TileSize

P2LevelW = P2LevelTileW * TileSize
P2LevelH = P2LevelTileH * TileSize

LevelTileMaxX = LevelTileW - 1
LevelTileMaxY = LevelTileH - 1

P2LevelTileMaxX = P2LevelTileW - 1
P2LevelTileMaxY = P2LevelTileH - 1

Global Dim Tile.I(LevelTileMaxX, LevelTileMaxY)
Global Dim P2Tile.I(P2LevelTileMaxX, P2LevelTileMaxY)
Delay(99)

   InitSprite()
   InitKeyboard()

   ExamineDesktops()
   ScrW = DesktopWidth(0)
   ScrH = DesktopHeight(0)
   ScrD = DesktopDepth(0)
   
   DispX1 = 0
   DispY1 = 0
   DispX2 = ScrW - 1
   DispY2 = ScrH - 1
   DispW = (DispX2 - DispX1) + 1
   DispH = (DispY2 - DispY1) + 1
   
   PageW = DispW
   PageH = DispH
   PageW - (PageW % TileSize)
   If PageW < DispW: PageW + TileSize: EndIf
   PageH - (PageH % TileSize)
   If PageH < DispH: PageH + TileSize: EndIf

   PageTileW = PageW / TileSize
   PageTileH = PageH / TileSize
   
   OpenScreen(ScrW, ScrH, ScrD, "")
   
   SpriteDrawing()
   LevelLoading()

   DrawVisibleTiles()
   P2DrawVisibleTiles()
   Display()
;-______________
;- Repeat...   
;-¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   Repeat
      ;Delay(99)
      Delay(1)
      DisplayFlag = 0
     
      VitX = Vit
      VitY = Vit
      If ExamineKeyboard()
         If KeyboardPushed(#PB_Key_Left)
            If (VitesseX - 2) > (0 - TileSize)
               VitesseX - 2
            EndIf
         EndIf
         If KeyboardPushed(#PB_Key_Right)
            If (VitesseX + 2) < (TileSize)
               VitesseX + 2
            EndIf
         EndIf
         If KeyboardPushed(#PB_Key_Up)
            If (VitesseY - 2) > (0 - TileSize)
               VitesseY - 2
            EndIf
         EndIf
         If KeyboardPushed(#PB_Key_Down)
            If (VitesseY + 2) < (TileSize)
               VitesseY + 2
            EndIf
         EndIf
         If KeyboardPushed(#PB_Key_Escape)
            QuitGame = 1
         EndIf
      EndIf
      If VitesseX < 0
         VitX = -VitesseX
         MoveLeft()
         VitesseX + 1
      EndIf
      If VitesseX > 0
         VitX = VitesseX
         MoveRight()
         VitesseX - 1
      EndIf
      If VitesseY < 0
         VitY = -VitesseY
         MoveUp()
         VitesseY + 1
      EndIf
      If VitesseY > 0
         VitY = VitesseY
         MoveDown()
         VitesseY - 1
      EndIf
      If DisplayFlag
         Display()
      EndIf
   Until QuitGame
   
   CloseScreen()
   End
Last edited by Olliv on Wed Feb 17, 2010 9:34 pm, edited 2 times in total.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

@Olliv: To work properly, it should be noted now that your first and third example both require the DirectX7 subsystem.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Where can I get the <Programming 2D scrolling games>?

Post by Olliv »

@Demivec

What about the second code????
Here, exactly:

Code: Select all

               For I = 1 To 3
                  ; ******************************************************************************
                  Test(I) = AddMaterialLayer(#MatiereSol, TextureID(#TextureSol), #PB_Material_Modulate)
                  ; ******************************************************************************
               Next I
This code is written "properly", however it doesn't run exactly.
The evidence is here

So, have you got evidences the 1st code and the 3rd code have problems and... don't work properly?

:mrgreen:

Ollivier
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

Olliv wrote:@Demivec

What about the second code????
Here, exactly:

Code: Select all

               For I = 1 To 3
                  ; ******************************************************************************
                  Test(I) = AddMaterialLayer(#MatiereSol, TextureID(#TextureSol), #PB_Material_Modulate)
                  ; ******************************************************************************
               Next I
This code is written "properly", however it doesn't run exactly.
The evidence is here

So, have you got evidences the 1st code and the 3rd code have problems and... don't work properly?
Code 2 work properly under the default sub-system DirectX9 and also under DirectX7. The trouble reported with AddmaterialLayer() doesn't seem to hinder it.

Codes 1 and 3 both work fine when used with DirectX7 but not with the default DirectX9. I've not made a determination of why they have problems beyond that. When code 1 runs under DirectX9 it draws a grid that blinks rapidly; when left or right is pushed it then shows just an unchanging image of horizontal lines. When code 3 is run with DirectX9 it shows nothing.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Where can I get the <Programming 2D scrolling games>?

Post by Olliv »

@Demivec

Please decelerate! Code by code...

In the first code: I don't understand your problem. Would not it work too fine? If this is the problem, I am not worried! Modify (simplify) the main loop before checking the version of DirectX. 69 lines: it is short for a novice to understand it easily... Maybe adding a "breaking", you could see a scrolling...

Main loop example:

Code: Select all

    Repeat
      Delay(1)
      Delayed = Elapsed + 16
      Repeat
        Elapsed = ElapsedMilliseconds()
      Until Elapsed > Delayed
      Start3D()
        For XS3 = 0 To Dw - 1 Step 32
          For YS3 = 0 To Dh - 1 Step 32
            DisplaySprite3D(1, XS3 + OffsetX - 32, YS3)
          Next
        Next
      Stop3D()
      FlipBuffers()
      ExamineKeyboard()
      OffsetX + 1
      OffsetX % 32
    Until KeyboardPushed(#PB_Key_Escape)
Is it better?
:D

Ollivier
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

Olliv wrote:Please decelerate! Code by code...
@Olliv: I will go slower.
Olliv wrote:Is it better? :D
No.

The code does not display properly using DirectX9 at any speed.

I modified it to display the same things using DirectX9 as it does using DirectX7 by removing the alpha channel from the image (it was completely opaque), the alphaBlend from the sprite and by not using a 3D Sprite. The working version for Code 1 follows: :)

Code: Select all

Procedure CreateTileTest(No.I, Wi.I, He.I)
  CreateImage(No, Wi, He, 24)
  StartDrawing(ImageOutput(No) )
    Box(0, 0, Wi, He, RGB(255, 255, 255) )
    Box(1, 1, Wi - 2, He - 2, RGB(0, 0, 0) )
  StopDrawing()
  CreateSprite(No, Wi, He)
  StartDrawing(SpriteOutput(No) )
    DrawImage(ImageID(No), 0, 0)
  StopDrawing()
EndProcedure

InitSprite()
InitKeyboard()
ExamineDesktops()
Dw = DesktopWidth(0)
Dh = DesktopHeight(0)
Dd = DesktopDepth(0)
 
Wi = 32
He = 32
OpenScreen(Dw, Dh, Dd, "")
CreateTileTest(1, Wi, He)

Repeat
  Delay(1)
  For XS3 = 0 To Dw - 1 Step 32
    For YS3 = 0 To Dh - 1 Step 32
      DisplaySprite(1, XS3 + OffsetX, YS3)
    Next
  Next
  FlipBuffers()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Right)
    If OffsetXAccel > -8
      OffsetXAccel - 2
    EndIf
  EndIf
  
  If KeyboardPushed(#PB_Key_Left)
    If OffsetXAccel < 8
      OffsetXAccel + 2
    EndIf
  EndIf
  
  OffsetX + OffsetXAccel
  If OffsetX > 31
    OffsetX - 32
  EndIf
  If OffsetX < 0
    OffsetX + 32
  EndIf
  If OffsetXAccel > 0
    OffsetXAccel - 1
  EndIf
  If OffsetXAccel < 0
    OffsetXAccel + 1
  EndIf
Until KeyboardPushed(#PB_Key_Escape)
CloseScreen()
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

@Olliv: Code 3 does not work with DirectX9 because of UseBuffer().
The UseBuffer() isn't supported with the DirectX9 subsystem, only with DirectX7.
That is something that has been true for at least the past 8 months or so (since DirectX9 was made the default). I don't think there is a solution to that problem yet.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Where can I get the <Programming 2D scrolling games>?

Post by Olliv »

Demivec wrote:
Olliv wrote:Please decelerate! Code by code...
@Olliv: I will go slower.
Olliv wrote:Is it better? :D
No.

The code does not display properly using DirectX9 at any speed.

I modified it to display the same things using DirectX9 as it does using DirectX7 by removing the alpha channel from the image (it was completely opaque), the alphaBlend from the sprite and by not using a 3D Sprite. The working version for Code 1 follows: :)

Code: Select all

;Code
@Demivec

I thank you for your answer, the full explanation and the corrected code.

For this first code, is there other problems, or technical remarks? (the uglyness of the displayed grid being excepted...)

Ollivier
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

Olliv wrote:@Demivec

I thank you for your answer, the full explanation and the corrected code.

For this first code, is there other problems, or technical remarks? (the uglyness of the displayed grid being excepted...)
Just one problem. The gridlines on the left border are not overwritten properly unless OffsetX is zero. When doing fine scrolling (scrolling that is less than a tile's dimensions) an additional partial tile must also be drawn at the border. This can be corrected with a small change also:

Code: Select all

  For XS3 = -32 To Dw - 1 Step 32 ;start one tile to the left
    For YS3 = 0 To Dh - 1 Step 32 
      DisplaySprite(1, XS3 + OffsetX, YS3)
    Next
  Next
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Where can I get the <Programming 2D scrolling games>?

Post by Olliv »

Index
Previous example

@Demivec

You are right. Thank you for you to loose time to explain me different problems or compatibility.
After these corrections, I updated the first code. (I didn't remove the alpha channel neither the using of sprite 3D. Because sprites 3D get specific transformations which could be very useful for the future effects. If we remove also alpha channel and sprite 3D, processing ressources are different and allow us to use parallax which is executed in the 3rd code.)

Even if it's not electronic, this scrolling could be a good occasion to create a game, studying the basic mechanism.

Code: Select all

; >>>>  /!\ ONLY WITH DX7 (PB4.40)  <<<<

Procedure RedStone(X.I, Y.I, W.I, H.I)
    Box(X + 0, Y + 0, W - 1, H - 1, RGBA(255, 0, 0, 255) )
    Box(X + 1, Y + 1, W - 1, H - 1, RGBA(64, 0, 0, 255) )
    Box(X + 1, Y + 1, W - 2, H - 2, RGBA(128, 0, 0, 255) )
EndProcedure

Procedure CreateTileTest(No.I, Wi.I, He.I)
  CreateImage(No, Wi, He, 32)
  StartDrawing(ImageOutput(No) )
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, Wi, He, RGBA(0, 0, 0, 0) )
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For K = 0 To 1
      For J = -2 To 2 Step 2
        For I = 0 To 1
          RedStone((I + J) * 8, I * 8 + K * 16, 16, 8)
        Next
      Next
    Next
  StopDrawing()
  CreateSprite(No, Wi, He, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(No) )
    DrawAlphaImage(ImageID(No), 0, 0)
  StopDrawing()
  CreateSprite3D(No, No)
  Start3D()
    DisplaySprite3D(No, 0, 0)
  Stop3D()
EndProcedure

Procedure CreateTileMono(No.I, Wi.I, He.I, Coef.I)
  CreateImage(No, Wi, He, 32)
  StartDrawing(ImageOutput(No) )
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, Wi, He, RGBA(0, 0, 0, 0) )
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For I = 0 To 15
      Color = RGB(0, Coef * 8 + (7 - I / 2), Coef * 16 + (15 - I) )
      Box(0, I * 2, Wi, 2, RGBA(Red(Color), Green(Color), Blue(Color), 255) )
    Next
  StopDrawing()
  CreateSprite(No, Wi, He, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(No) )
    DrawAlphaImage(ImageID(No), 0, 0)
  StopDrawing()
  CreateSprite3D(No, No)
  Start3D()
    DisplaySprite3D(No, 0, 0)
  Stop3D()
EndProcedure

  InitSprite()
  InitSprite3D()
  InitKeyboard()
  ExamineDesktops()
  Dw = DesktopWidth(0)
  Dh = DesktopHeight(0)
  Dd = DesktopDepth(0)
  
  Wi = 32
  He = 32
  OpenScreen(Dw, Dh, Dd, "")
    CreateTileTest(1, Wi, He)
    For I = 0 To 15
      CreateTileMono(I + 2, Wi, He, I)
    Next I
    Global Dim Tile(255, 31)
    For I = 0 To 31
      For X = 0 To 255
        Tile(X, I) = 1
      Next
    Next
    For I = 0 To 15
      For X = 0 To 255
        Tile(X, 15 - I) = I + 2
      Next
    Next
    For X = 0 To 255
      For I = Int(10. + Cos(X / 8.) * 8.) To 15
        Tile(X, I) = 1
      Next
    Next

    Repeat
      Delay(1)
      Start3D()
        If OffsetTileX <= 0
          OffsetTileX = 0
          OffsetX = 0
        EndIf
        If OffsetTileX => ((255 - (Dw / 32) ) )
          OffsetTileX = ((255 - (Dw / 32) ) )
          OffsetX = 32 - 1
        EndIf
        For XS3 = 0 - 32 To Dw - 1 Step 32
          For YS3 = 0 To Dh - 1 Step 32
            ;Debug Str(XS3 / 32 + OffsetTileX) + "; " + Str(YS3 / 32)
            XX = XS3 / 32 + OffsetTileX
            If XX => 0
              Tile = Tile(XX, YS3 / 32)
              If Tile
                DisplaySprite3D(Tile, XS3 + OffsetX, YS3)
              EndIf
            EndIf
          Next
        Next
      Stop3D()
      FlipBuffers()
      ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Right)
        If OffsetXAccel > -16
          OffsetXAccel - 2
        EndIf
      EndIf
      
      If KeyboardPushed(#PB_Key_Left)
        If OffsetXAccel < 16
          OffsetXAccel + 2
        EndIf
      EndIf
      OffsetX + OffsetXAccel
      If OffsetX > 31
        OffsetX - 32
        OffsetTileX - 1
      EndIf
      If OffsetX < 0
        OffsetX + 32
        OffsetTileX + 1
      EndIf
      If OffsetXAccel > 0
        OffsetXAccel - 1
      EndIf
      If OffsetXAccel < 0
        OffsetXAccel + 1
      EndIf
    Until KeyboardPushed(#PB_Key_Escape)
  CloseScreen()
  
Last edited by Olliv on Wed Feb 17, 2010 9:39 pm, edited 2 times in total.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Where can I get the <Programming 2D scrolling games>?

Post by Michael Vogel »

Looks nice, I got remembered to 1980 when I saw the video game scramble and tried to implement that on my PET 2001...
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

@Olliv: Here is a small adjustment needed for the example posted for large view screens. I present it merely to keep your example working and hopefully it is not considered an irritant. :wink:

It is presented in two parts to avoid posting all the intervening code

Code: Select all

Wi = 32
He = 32
OpenScreen(Dw, Dh, Dd, "")
;Add the following lines to test if screen is larger vertically than needed and set maxheight 'Dh2' appropriately
If Dh > ((He - 1) * 32)
  Dh2 = ((He - 1) * 32)
Else
  Dh2 = Dh
EndIf 
This portion modifies only the line at the start of the inner display loop with YS3 (changing 'Dh' to 'Dh2').

Code: Select all

For XS3 = 0 - 32 To Dw - 1 Step 32
      For YS3 = 0 To Dh2 - 1 Step 32 ; <== use maxheight based on screen and map size
        ;Debug Str(XS3 / 32 + OffsetTileX) + "; " + Str(YS3 / 32)
        XX = XS3 / 32 + OffsetTileX
        If XX => 0
          Tile = Tile(XX, YS3 / 32)
          If Tile
            DisplaySprite3D(Tile, XS3 + OffsetX, YS3)
          EndIf
        EndIf
      Next
    Next
The difference is shown in two thumbnails, the left one is before the change.

Image Image
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Where can I get the <Programming 2D scrolling games>?

Post by Kaeru Gaman »

could a Moderator please cut the oldskool lessons off the discussing of the book?

I love oldskool and everything about tile-engines is my profession, but these are two topics, the original topic was the book.
oh... and have a nice day.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Where can I get the <Programming 2D scrolling games>?

Post by Demivec »

Kaeru Gaman wrote:could a Moderator please cut the oldskool lessons off the discussing of the book?

I love oldskool and everything about tile-engines is my profession, but these are two topics, the original topic was the book.
I second the request.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Where can I get the <Programming 2D scrolling games>?

Post by Olliv »

I understand nothing but I second the request too :mrgreen:

We could create an other topic in the coding question section about the problems a coder meets when he want to create a scrolling game. For the 3 codes I publish, there are already in the forum.
Post Reply