BeginDraw

Syntax

Success = BeginDraw(Output)

Description

Initiates a drawing session on the specified output target (currently an image). BeginDraw() and EndDraw() are handled automatically inside draw event handlers for windows and widgets. All subsequent drawing commands will render onto this target until a corresponding EndDraw() is called. Calls to BeginDraw() can be nested, allowing drawing operations to be redirected, for example, to an off-screen image buffer.

Parameters

Output
The output target handle obtained from OutputImg().

Return Value

Returns #True if the drawing session was successfully started, or #False if the output handle was invalid.

Remarks

Every successful call to BeginDraw() must be balanced with a call to EndDraw(). Failure to do so can lead to resource leaks or unpredictable behavior. When drawing to an image (OutputImg()), drawing occurs on the entire image surface. The drawing state (current DPI, transformations, etc.) is inherited from the output target specified. Default drawing attributes (stroke, fill, etc.) are set at the beginning of the draw call.

Example

IncludeFile "ProGUI_PB.pbi"

StartProGUI()

MyWindow = CreateWindow(0, 0, 400, 300, "BeginDraw Example")

; Create an off-screen image
Global MyImage = CreateImg(100, 100, #PG_Img_Transparent)

; Pre-render something onto the image
BeginDraw(OutputImg(MyImage))
  DrawClear(RGB(255, 255, 0), 1) ; Yellow background
  DrawBox(10, 10, 80, 80, RGB(255, 0, 0), 1) ; Red box
EndDraw()

Procedure DrawHandler(Window, EventType, *EventData.PG_EventDraw, *UserData)
  
    DrawClear(RGB(200, 200, 200), 1) ; Grey background

    ; Draw the pre-rendered image onto the window
    DrawImg(MyImage, 50, 50)

    ; Draw directly onto the window
    DrawLine(0, 0, *EventData\width, *EventData\height, RGB(0,0,255))
    
EndProcedure

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

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

FreeImg(MyImage)
StopProGUI()

See Also

EndDraw, OutputWindow, OutputImg, WindowRedraw

Supported OS

Windows, Linux