Skip to content

Using Squid

chris edited this page Dec 28, 2023 · 7 revisions

Implement ISquidRenderer

public sealed class NoRenderer : ISquidRenderer // This is the default value of Gui.Renderer
{
    public void StartBatch() { }
    public void EndBatch(bool final) { }
    public int GetFont(string name) { return -1; }
    public int GetTexture(string name) { return -1; }
    public Point GetTextSize(string text, int font) { return new Point(); }
    public Point GetTextureSize(int texture) { return new Point(); }
    public void Scissor(int x, int y, int width, int height) { }
    public void DrawBox(int x, int y, int width, int height, int color) { }
    public void DrawTexture(int texture, int x, int y, int width, int height, Rectangle source, int color) { }
    public void DrawText(string text, int x, int y, int font, int color) { }
    public void Dispose() { }
}

Initialize Squid with your renderer implementation

        Gui.Renderer = new MyCustomRenderer();

Create a desktop and controls

        var desktop = new Desktop();
        desktop.Controls.Add(new Button());

Update and Draw

        // gameloop update
        Gui.TimeElapsed = Time.DeltaMilliseconds;
        Gui.SetMouse(Input.MousePoint.X, Input.MousePoint.Y, -Input.MouseWheelDelta);
        Gui.SetButtons(Input.IsMouseDown(0), Input.IsMouseDown(1));

        keys.Clear();

        foreach (int key in Input.KeysDown)
        {
            int scancode = Input.VirtualKeyToScancode(key);
            keys.Add(new KeyData { Pressed = true, Scancode = scancode, Char = Input.ScancodeToChar(scancode) });
        }

        foreach (int key in Input.KeysReleased)
        {
            int scancode = Input.VirtualKeyToScancode(key);
            keys.Add(new KeyData { Pressed = false, Scancode = scancode, Char = Input.ScancodeToChar(scancode) });
        }

        Gui.SetKeyboard(keys.ToArray());

        desktop.Size = new Squid.Point((int)RenderView.Active.Size.X, (int)RenderView.Active.Size.Y);
        desktop.Update();

        // gameloop draw
        desktop.Draw();

Fonts - How do i draw text?

Squid is unaware of Font loading/rendering/measuring, so the implementation is entirely up to the user. There are many tools to generate Font atlases such as:

Note: Your GetTextSize implementation does not need to respect linebreaks. The Squid text layout engine breaks text into smaller elements and text measured with GetTextSize is guaranteed to be single line.