PrevNextUpHome SophiaFramework UNIVERSE 5.3

12.1. Drawing Method

To draw a figure or a text on the screen, use the SFXGraphics class.

[Caution] Caution

The method to use the SFXGraphics class with GUI Framework is different from that without GUI Framework.

12.1.1. With GUI Framework

Specify the SFXGraphics instance as the argument of HANDLER_IMPLEMENT_VOIDRENDER macro for implementing the drawing handler, and then draw the figure or text on the Content Region by using the SFXGraphics instance.

Example 12.1. Drawing the rectangle and string using the GUI Framework

// Define window class
SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);

    // Declare drawing handler
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
};

// Constructor
// create window at coordinate (20, 20), width : 200, height : 250
// Title is "my window"
MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    // register drawing handler
    RegisterHandler(SREVT_RESPONDER_RENDER, 
                    SRP16_RENDER_CONTENT,
                    HANDLER_BEFORE, 
                    HANDLER_FUNCTION(OnRenderContent));

    return;
}

// Destructor
MyWindow::~MyWindow(Void)
{
    return;
}

// Define drawing handler
HANDLER_IMPLEMENT_VOIDRENDER(MyWindow, OnRenderContent, graphics)
{
    // SFXGraphics instance is passed as 3rd argument (graphics) of macro function
    
    // paint screen in white
    // get drawing region (rectangular) in window using GetContentWorld function
    // SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00) = white
    graphics->FillRectangle(GetContentWorld(), 
                            SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00));

    // display "Hello Window"
    // SFXRGBColor(0x00, 0x00, 0x00, 0x00) = black
    graphics->DrawText("Hello Window", 
                       GetContentWorld(),
                       SFXRGBColor(0x00, 0x00, 0x00, 0x00));

    // graphics->Update() is not called

    return;
}

Figure 12.1. Execution Result

Execution Result
[Note] Note

The drawing handler will be automatically called when needed.

12.1.2. Without GUI Framework

Get the SFXGraphics instance for drawing using the SFXGraphics::GetInstance function, and then draw the figure or text anywhere by using the SFXGraphics instance.

Example 12.2. Drawing the rectangle and string without using the GUI Framework

Void Draw(Void)
{
    // get SFXGraphics instance
    SFXGraphicsPtr graphics = SFXGraphics::GetInstance();

    if (graphics == null) { 

        // Error handler

        return;
    }

    // paint screen in white
    // get entire screen (rectangular) using SFXGraphics::GetDeviceRectangle()
    // SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00) = white
    graphics->FillRectangle(SFXGraphics::GetDeviceRectangle(),
                            SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00));

    // draw "Hello World" on screen
    // SFXRGBColor(0x00, 0x00, 0x00, 0x00) = black
    graphics->DrawText("Hello World", SFXGraphics::GetDeviceRectangle(),
                       SFXRGBColor(0x00, 0x00, 0x00, 0x00));

    // update screen
    graphics->Update();
}
[Caution] Call function

After drawing, the SFXGraphics::Update function must be called to update the screen.

[Note] Screen updating time

When the screen is actually redrawn after calling the SFXGraphics::Update function depends on the kind of device.

Figure 12.2. Execution Result

Execution Result