Module Parallax

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Module Parallax

Message 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
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
boby
Messages : 261
Inscription : jeu. 07/juin/2007 22:54

Re: Module Parallax

Message par boby »

Joli rendu, merci du partage.
Avatar de l’utilisateur
Ar-S
Messages : 9472
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Module Parallax

Message par Ar-S »

Sympa !
Chez moi ça prend de 0.8 à 1.1 % de charge. Donc rien à redire.
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: Module Parallax

Message par Micoute »

Merci microdevweb pour le partage.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Avatar de l’utilisateur
Fig
Messages : 1176
Inscription : jeu. 14/oct./2004 19:48

Re: Module Parallax

Message 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 ... ^^)
Dernière modification par Fig le ven. 14/sept./2018 20:49, modifié 2 fois.
Il y a deux méthodes pour écrire des programmes sans erreurs. Mais il n’y a que la troisième qui marche.
Version de PB : 6.00LTS - 64 bits
pierre003
Messages : 60
Inscription : ven. 27/mai/2016 8:27
Localisation : 03

Re: Module Parallax

Message par pierre003 »

Bonsoir,
C’est super pour un programme aussi cours.
Chez moi c’est entre 0.3 et 0.7%
Toshiba satellite Windows 7 64bits
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Module Parallax

Message 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 ^-^
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Module Parallax

Message par Kwai chang caine »

Bel effet 8O
Merci à vous deux pour cette promenade en foret 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Shadow
Messages : 1373
Inscription : mer. 04/nov./2015 17:39

Re: Module Parallax

Message par Shadow »

Jolie :D
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Répondre