Page 1 sur 1

Module Parallax

Publié : mer. 12/sept./2018 17:36
par microdevweb
Image

Remarque : C'est le gif qui sacade, pas à la compilation :wink:

Voici module pour faire un fond en Parallax

Le zip depuis github

Maintenant je ne suis pas certain que ma technique soit bonne, cela prend pas de mal de ressource entre 20% et 28% du processeur c'est quant même bkp.<EDIT> Bon même si j'affiche pas les sprites c'est idem

exemple du code :

Code : Tout sélectionner

EnableExplicit
UsePNGImageDecoder()
#MainForm = 0
If Not InitSprite()
  MessageRequester("SYSTEM ERROR","cannot init sprite engine",#PB_MessageRequester_Error)
  End
EndIf
If Not InitKeyboard()
  MessageRequester("SYSTEM ERROR","cannot init keyboard engine",#PB_MessageRequester_Error)
  End
EndIf
If Not OpenWindow(#MainForm,0,0,800,600,"test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  MessageRequester("SYSTEM ERROR","cannot open window",#PB_MessageRequester_Error)
  End
EndIf
If Not OpenWindowedScreen(WindowID(#MainForm),0,0,WindowWidth(#MainForm),WindowHeight(#MainForm),#True,0,#PB_Screen_WaitSynchronization )
  MessageRequester("SYSTEM ERROR","cannot open the game screen",#PB_MessageRequester_Error)
  End
EndIf
IncludePath "Packages/"
XIncludeFile "PARALLAX.pbi"
UseModule PARALLAX
Global myParallax. Parallax = newParallax(0,0,800,600)
myParallax\addBackGround(newBackground("Assets/Parallax/plx-1.png",0))
myParallax\addBackGround(newBackground("Assets/Parallax/plx-2.png",0.1))
myParallax\addBackGround(newBackground("Assets/Parallax/plx-3.png",0.3))
myParallax\addBackGround(newBackground("Assets/Parallax/plx-4.png",0.4))
myParallax\addBackGround(newBackground("Assets/Parallax/plx-5.png",0.8))
Global gEvent,exit.b = #False
Repeat ; screen event
  Repeat ; window event
    gEvent = WindowEvent()
    If gEvent = #PB_Event_CloseWindow : exit = #True : EndIf
  Until gEvent = 0
  ClearScreen(RGB(255,255,255))
  myParallax\display(1)
  FlipBuffers()
Until  exit

Re: Module Parallax

Publié : mer. 12/sept./2018 20:00
par boby
Joli rendu, merci du partage.

Re: Module Parallax

Publié : mer. 12/sept./2018 21:16
par Ar-S
Sympa !
Chez moi ça prend de 0.8 à 1.1 % de charge. Donc rien à redire.

Re: Module Parallax

Publié : jeu. 13/sept./2018 7:04
par Micoute
Merci microdevweb pour le partage.

Re: Module Parallax

Publié : ven. 14/sept./2018 15:10
par Fig
Bonjour,
En utilisant tes images et la procédure de scrolling idoine, ça donne ça... 1.4% de CPU chez moi, et je scroll aussi l'arrière plan uniforme (ce qui ne sert à rien, j'en conviens).

Code : Tout sélectionner

Procedure Scroll(sprite.i, StepX.i, StepY.i)
    Protected SpriteW, SpriteH, y, Buffer, Pitch, PixelFormat, *Dest, *debut
    If StartDrawing(SpriteOutput(sprite))
        SpriteW = OutputWidth(): SpriteH = OutputHeight()
        StepX = (StepX + SpriteW) % SpriteW
        StepY = (StepY + SpriteH) % SpriteH
        If StepX = 0 And StepY = 0: StopDrawing(): ProcedureReturn: EndIf
        Buffer = DrawingBuffer()
        Pitch = DrawingBufferPitch()
        If StepX <> 0
            PixelFormat = DrawingBufferPixelFormat() & ~#PB_PixelFormat_ReversedY
            If PixelFormat >= #PB_PixelFormat_32Bits_RGB
                StepX <<2
            ElseIf PixelFormat >= #PB_PixelFormat_24Bits_RGB
                StepX * 3
            ElseIf PixelFormat >= #PB_PixelFormat_15Bits
                StepX <<1
            EndIf
            *Dest = AllocateMemory(StepX)
            For y = 0 To SpriteH - 1
                *debut = Buffer + Pitch * y
                CopyMemory(*debut, *Dest, StepX)
                CopyMemory(*debut + StepX, *debut, Pitch - StepX)
                CopyMemory(*Dest, *debut + Pitch - StepX, StepX)
            Next y
            FreeMemory(*dest)
        EndIf
        If StepY <> 0
            If DrawingBufferPixelFormat() & #PB_PixelFormat_ReversedY
                StepY = SpriteH - StepY ;reverse the Y value
            EndIf
            *Dest = AllocateMemory(Pitch * StepY)
            *debut = Buffer
            CopyMemory(*debut, *Dest, Pitch * StepY)
            CopyMemory(*debut + Pitch * StepY, *debut, Pitch * (SpriteH - StepY))
            CopyMemory(*Dest, *debut + Pitch * (SpriteH - StepY), Pitch * StepY)
            FreeMemory(*Dest)
        EndIf  
        StopDrawing()    
    EndIf
EndProcedure

UsePNGImageDecoder()
If InitSprite() = 0 Or InitKeyboard() = 0: MessageRequester("Error", "Can't open the sprite system", 0): End: EndIf
If OpenWindow(0, 0, 0, 800, 600, "Scroll Sprite", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0: MessageRequester("Error", "Can't open windowed screen!", 0): EndIf
If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0) = 0: MessageRequester("Error", "Can't open windowed screen!", 0): EndIf    

For i=0 To 4
    LoadSprite(i,"plx-"+Str(i+1)+".png",#PB_Sprite_AlphaBlending)
    ZoomSprite(i,SpriteWidth(i)*2,SpriteHeight(i)*2)
Next i
Repeat
    WindowEvent()
    ExamineKeyboard()
    FlipBuffers()
    ClearScreen(#Black)
    For i=0 To 4
        Scroll(i,i+1,0) ;  Scroll(sprite, vitesse horizontale en pixel, vitesse verticale en pixel)
        DisplayTransparentSprite(i, ScreenWidth() / 2 - SpriteWidth(i) / 2,ScreenHeight() / 2-SpriteHeight(i)/2)
    Next i
Until KeyboardPushed(#PB_Key_Escape)
Falsam avait fait un superbe parallax sur ce principe mais je ne le retrouve pas... (le code, pas Falsam ... ^^)

Re: Module Parallax

Publié : ven. 14/sept./2018 17:36
par pierre003
Bonsoir,
C’est super pour un programme aussi cours.
Chez moi c’est entre 0.3 et 0.7%

Re: Module Parallax

Publié : ven. 14/sept./2018 21:00
par falsam
Fig a écrit :Falsam avait fait un superbe parallax sur ce principe mais je ne le retrouve pas... (le code, pas Falsam ... ^^)
ici https://www.purebasic.fr/french/viewtop ... 29#p193929 tout est dans le Zip

Le code permettant de scroller est de toi ^-^

Re: Module Parallax

Publié : sam. 15/sept./2018 23:40
par Kwai chang caine
Bel effet 8O
Merci à vous deux pour cette promenade en foret 8)

Re: Module Parallax

Publié : dim. 14/oct./2018 19:13
par Shadow
Jolie :D