Blitz3D+ Command Reference

Viewport x,y,width,height

Parameters

x - x coordinate of the top-left corner of the drawing area

y - y coordinate of the top-left corner of the drawing area

width - width of the drawing area, in pixels

height - height of the drawing area, in pixels

Description

Restricts drawing on the current buffer to a rectangular area.

Everything drawn after Viewport is clipped to the rectangle - pixels outside it are simply not touched. That is exactly what a game window needs: a scrolling play area that cannot spill over the HUD, a minimap in a corner, a split-screen half, a text box that long lines cannot escape. Note the last two numbers are the SIZE of the area, not its far corner.

Draw your fixed framework first, set the Viewport, draw the clipped content, and afterwards remember to open it up again - Viewport 0,0,GraphicsWidth(),GraphicsHeight() - or the rest of the frame will be mysteriously missing. SetBuffer also resets the viewport to the whole buffer.

Cls only clears inside the current viewport, which is handy for wiping just the play area each frame while panels drawn once stay put.

See also: Origin, Cls, SetBuffer, GraphicsWidth, GraphicsHeight.

Example

; Viewport Example
; ----------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

SeedRnd MilliSecs()

; Bouncing balls that roam the whole screen
Dim bx#(7),by#(7),vx#(7),vy#(7)
For i=0 To 7
    bx(i)=Rnd(0,600)
    by(i)=Rnd(40,440)
    vx(i)=Rnd(2,4)
    vy(i)=Rnd(2,4)
Next

port=1

While Not KeyDown(1)

    ; Space toggles the clipping viewport on and off
    If KeyHit(57) Then port=1-port

    ; Full-screen viewport first so Cls wipes everything
    Viewport 0,0,640,480
    Cls

    ; Show where the clipping window sits
    Color 80,80,80
    Rect 158,118,324,244,0

    ; Restrict all following drawing to a 320x240 window
    If port Then Viewport 160,120,320,240

    ; The balls still move everywhere, but drawing is clipped when on
    For i=0 To 7
        bx(i)=bx(i)+vx(i)
        by(i)=by(i)+vy(i)
        If bx(i)<0 Or bx(i)>604 Then vx(i)=-vx(i)
        If by(i)<0 Or by(i)>444 Then vy(i)=-vy(i)
        Color 60+i*24,180,255-i*24
        Oval bx(i),by(i),36,36,1
    Next

    ; Back to full screen so the overlay is never clipped
    Viewport 0,0,640,480
    Color 255,255,255
    Text 0,0,"Space: toggle clipping   Esc: exit"
    If port Then
        Text 0,20,"Viewport 160,120,320,240 - drawing is clipped to the grey box"
    Else
        Text 0,20,"Viewport 0,0,640,480 - full screen, no clipping"
    EndIf

    Flip

Wend

End

Index