DrawSave

Syntax

DrawSave()

Description

Saves the current state of the drawing context onto an internal stack. This includes the current transformation matrix, clipping region, layer parameters, anti-alias mode, blend mode, stroke settings, and current path definition. The saved state can be restored later using DrawRestore().

Parameters

This command takes no parameters.

Return Value

This command does not return a value.

Remarks

It is essential for isolating drawing operations. For example, you can save the state, apply a transformation (like translation or rotation) and/or clipping, draw some elements, and then restore the state to revert the transformation and clipping without affecting subsequent drawing operations. Every call to DrawSave() should be matched by a call to DrawRestore().

Example

IncludeFile "ProGUI_PB.pbi"

StartProGUI()

Procedure DrawHandler(Window, EventType, *EventData.PG_EventDraw, *UserData)
  
    DrawClear(RGB(255, 255, 255), 1)

    ; Draw an initial blue box
    DrawBox(10, 10, 50, 50, RGB(0, 0, 255))

    ; Save the current state
    DrawSave()

    ; Apply a transformation (translate) and clipping
    SetTranslation(70, 70) ; Move origin
    BeginClipBox(0, 0, 100, 100) ; Clip relative to new origin
    DrawBox(0, 0, 150, 150, RGB(255, 0, 0)) ; Draw red box (clipped)

    EndClip() ; Remove clip applied *within* the saved state

    ; Restore the state saved before translation and clipping
    DrawRestore()

    ; Draw another blue box - should appear at (10, 70) because
    ; the coordinate system is back to what it was before DrawSave()
    DrawBox(10, 70, 50, 50, RGB(0, 0, 255))

EndProcedure

MyWindow = CreateWindow(0, 0, 250, 250, "DrawSave/Restore Example")

If MyWindow
  AddEventHandler(MyWindow, #PG_Event_Draw, @DrawHandler())
  WindowShow(MyWindow, #True, #PG_Window_ScreenCentered)

  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf

StopProGUI()

See Also

DrawRestore, BeginDraw, EndDraw, SetTranslation, BeginClipBox, BeginLayer

Supported OS

Windows, Linux