Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a little tutorial STEP by STEP on scrolling in 2D graphic mode
#1
Hi friends
I want to share these basical knowledges about scrolling background in graphic mode using  the powerful _PUTIMAGE.
It is a basical 2D graphic mode 32bit.

I post the following 6 QB64pe code examples that start from a backgroung scrolling to right and leaving a black screen to arrive to a background scrolling in the 4 directions of a bidimensional field in which the user can start/stop the scrolling, change the directions of the scrolling and quit the program.

If you want to see the final result you must run the STEP 6. 
If you want follow the tutorial you must go step by step from 1 to 6.

Happy enjoyment  with this code.

STEP 1
Code: (Select All)

Rem Demonstration of scrolling towards right or left of the background
Rem  STEP 1 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String
_Title " Demo of background scrolling: step 1" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
    Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application

' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
    k = UCase$(InKey$) ' it takes the input from keyboard
    Cls 'it clears previous screen
    _PutImage (x, y), Back, Scr 'it paints screen now
    _PrintString (1, 1), "Press Q to quit program" ' it shows instruction for user on the screen
    x = x + 1 'it calculates the new position where starting to paint background, it is towards right
    _Display ' it forces hardware to show image
    _Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend

End

STEP 2
Code: (Select All)

Rem Demonstration of scrolling towards right or left of the background
Rem  STEP 2 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String
_Title " Demo of background scrolling: step 2" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
    Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application

' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
    k = UCase$(InKey$) ' it takes the input from keyboard
    Cls 'it clears previous screen
    _PutImage (x, y), Back, Scr 'it paints screen now
    _PrintString (1, 1), "Press Q to quit program, A to restart" ' it shows instruction for user on the screen
    x = x + 1 'it calculates the new position where starting to paint background, it is towards right
    If k = "A" Then x = 1 ' it corrects x when user restarts
    _Display ' it forces hardware to show image
    _Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend

End

STEP 3
Code: (Select All)

Rem Demonstration of scrolling towards right or left of the background
Rem  STEP 3 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String
_Title " Demo of background scrolling: step 3" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
    Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application

' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
    k = UCase$(InKey$) ' it takes the input from keyboard
    Cls 'it clears previous screen
    _PutImage (x, y), Back, Scr 'it paints screen now
    _PutImage (x - w, y), Back, Scr ' it completes the background on the left side
    _PrintString (1, 1), "Press Q to quit program" ' it shows instruction for user on the screen
    x = x + 1 'it calculates the new position where starting to paint background, it is towards right
    If x > w Then x = 1 ' it corrects x when is over
    _Display ' it forces hardware to show image
    _Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend

End

STEP 4
Code: (Select All)

Rem Demonstration of scrolling towards right or left of the background
Rem  STEP 4 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String, S As Integer
_Title " Demo of background scrolling: step 4" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
S = 1
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
    Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application

' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
    k = UCase$(InKey$) ' it takes the input from keyboard
    Cls 'it clears previous screen
    _PutImage (x, y), Back, Scr 'it paints screen now
    _PutImage (x - w, y), Back, Scr ' it completes the background on the left side
    _PrintString (1, 1), "Press Q to quit program, S to stop/start demo" ' it shows instruction for user on the screen
    If k = "S" Then If S > 0 Then S = -1 Else S = 1 ' it starts/stops the scrolling to right
    If S > 0 Then x = x + 1 'it calculates the new position where starting to paint background, it is towards right
    If x > w Then x = 1 ' it corrects x when is over
    _Display ' it forces hardware to show image
    _Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend

End

STEP 5
Code: (Select All)

Rem Demonstration of scrolling towards right or left of the background
Rem  STEP 5 a background scrolling towards right /left in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String, S As Integer, d As Integer
_Title " Demo of background scrolling: step 5" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
S = 1
d = 1
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
    Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application

' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
    k = UCase$(InKey$) ' it takes the input from keyboard
    Cls 'it clears previous screen
    _PutImage (x, y), Back, Scr 'it paints screen now
    _PutImage (x - w, y), Back, Scr ' it completes the background on the left side
    _PrintString (1, 1), "Press Q to quit program, S to stop/start demo, R to reverse direction" ' it shows instruction for user on the screen
    If k = "S" Then If S > 0 Then S = -1 Else S = 1 ' it starts/stops the scrolling to right
    If k = "R" Then d = d * -1 ' it changes the direction of scrolling +1 = to right / -1 = to left
    If S > 0 Then x = x + d 'it calculates the new position where starting to paint background, it is towards right
    If d > 0 And x > w Then x = 1 ' it corrects x when is over w and scrolling is to right
    If d < 0 And x < 1 Then x = w ' it corrects x when is under 1 and scrolling is to left
    _Display ' it forces hardware to show image
    _Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend

End

STEP 6
Code: (Select All)

Rem Demonstration of scrolling towards right or left of the background
Rem  STEP 6 a background scrolling towards right /left & down/up in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String, dx As Integer, dy As Integer, O As Integer, V As Integer, S As Integer
_Title " Demo of background scrolling: step 6" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
dx = 1
dy = 1
S = 1
O = 1
V = 1
Randomize Timer ' it starts the random generator of number
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 100
    Line ((Rnd * (780) + 10), Rnd * (580) + 10)-(Rnd * (780) + 10, Rnd * (580) + 10), _RGBA32(Rnd * (255), Rnd * (255), Rnd * (255), 255), B
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application

' it manages the keyboard input of user and the scrolling background
While k <> "Q"
    k = UCase$(InKey$) ' it takes the input from keyboard
    Cls 'it clears previous screen
    _PutImage (x, y), Back, Scr 'it paints screen now
    _PutImage (x - w, y), Back, Scr ' it completes the background on the left side
    _PutImage (x, y - h), Back, Scr ' it completes the background on the upper side
    _PutImage (x - w, y - h), Back, Scr ' it completes the background on the upper side
    _PrintString (1, 1), "Press Q to quit program, S to stop/start demo, R to reverse directions" ' it shows instruction for user on the screen
    _PrintString (1, 20), " O stop/start horizontal scrolling, V stop/start vertical scrolling"

    ' it starts/stops the scrolling
    If k = "S" Then S = S * -1
    If k = "R" Then
        ' it changes only scrolling activated
        If O > 0 Then dx = dx * -1
        If V > 0 Then dy = dy * -1
        ' it changes the direction of scrolling +1 = to right to bottom/ -1 = to left to up
    End If
    If k = "O" Then O = O * -1 'it sets the status of horizontal scrolling
    If k = "V" Then V = V * -1 ' it sets the status of vertical scrolling


    If S > 0 And O > 0 Then x = x + dx ' it scrolls only horizontal
    If S > 0 And V > 0 Then y = y + dy ' it scrolls only vertical
    If dx > 0 And x > w Then x = 1 ' it corrects x when it is over w and scrolling is to right
    If dx < 0 And x < 1 Then x = w ' it corrects x when it is under 1 and scrolling is to left
    If dy > 0 And y > h Then y = 1 ' it corrects y when it is over h and scrolling is to down
    If dy < 0 And y < 1 Then y = h ' it corrects when it is under 1 and scrolling is to up
    _Display ' it forces hardware to show image
    _Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend

End

Thanks for feedbacks
Reply


Messages In This Thread
a little tutorial STEP by STEP on scrolling in 2D graphic mode - by TempodiBasic - 12-01-2024, 11:08 AM



Users browsing this thread: 7 Guest(s)