MinimumDraw Library (MDL) v0.92

User Manual - Comprehensive Guide for LiveCode Developers

Table of Contents

1. Introduction

The MinimumDraw Library (MDL) is a lightweight, powerful graphics library originally created by UDI (http://udimac.web.fc2.com) and adapted by Michael Roberts for use in LiveCode projects. Version 0.92 includes bug fixes, optimizations, and enhanced debugging capabilities compared to the original 0.91 version.

MDL provides direct pixel-level control for drawing operations, making it ideal for custom graphics that would be difficult to achieve with LiveCode's standard drawing tools. The library is compact, efficient, and includes a wide range of functions for buffer management, drawing primitives, color handling, pixel manipulation, text rendering, and visual effects.

Key Features

Note: This manual covers MDL version 0.92, which includes several enhancements and bug fixes over the original 0.91 version.

2. Getting Started

Installation

There are two primary methods to incorporate the MDL into your LiveCode project:

Method 1: Include in Stack Script

  1. Copy the entire MDL script
  2. Paste it into your stack script
  3. Create an image object in your stack to use as a drawing canvas

Method 2: Use as a Library Stack

  1. Save the MDL script as a standalone stack file (e.g., "lib_MDL_0.92.livecode")
  2. In your project, use the following command to load the library:
start using stack "lib_MDL_0.92.livecode"

Basic Setup

Follow these steps to set up a basic drawing environment with MDL:

Step 1: Create an Image Object

Create an image object in your LiveCode stack to serve as your drawing canvas:

create image "myCanvas"
set the width of image "myCanvas" to 400
set the height of image "myCanvas" to 300

Step 2: Initialize the Drawing Buffer

Initialize the MDL buffer using your image:

MdMakeBuffer "myCanvas"

Step 3: Set Colors

Set the pen and background colors:

MdSetPenColor "255,0,0" -- Red
MdSetBgColor "255,255,255" -- White

Step 4: Draw Something

Draw a simple shape:

MdDrawRect "50,50,150,150", true -- Draw a filled rectangle

Step 5: Update the Display

Update the image with your drawing:

MdUpdate "myCanvas"

Complete Basic Example

Here's a complete example that puts everything together:

-- Create a canvas image if it doesn't exist
if there is not an image "myCanvas" then
   create image "myCanvas"
   set the width of image "myCanvas" to 400
   set the height of image "myCanvas" to 300
   set the loc of image "myCanvas" to the loc of this card
end if

-- Initialize MDL buffer
MdMakeBuffer "myCanvas"

-- Clear the canvas
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Draw a red rectangle
MdSetPenColor "255,0,0" -- Red
MdDrawRect "50,50,150,150", true

-- Draw a blue circle
MdSetPenColor "0,0,255" -- Blue
MdDrawOval "200,50,300,150", true

-- Draw some text
MdSetPenColor "0,0,0" -- Black
MdDrawText "Hello, MDL!", "50,200", 15, "bold"

-- Update the display
MdUpdate "myCanvas"

Tip: Always remember to call MdUpdate after your drawing operations to display the results on your image.

3. Core Concepts

The Buffer System

MDL uses a buffer-based drawing system where operations are performed on a virtual canvas in memory before being applied to the visible image. This approach improves performance and enables more complex operations.

Key Buffer Concepts

Coordinate System

MDL uses a pixel-based coordinate system with the following characteristics:

+----------------------------> X | | (0,0) (100,0) | +----------+ | | | | | | | +----------+ | (0,100) (100,100) | v Y

Rectangle Specification

Rectangles in MDL are specified as a string of four comma-separated integers representing the coordinates of the top-left and bottom-right corners:

"left,top,right,bottom"

Example: "50,50,150,150" defines a 100x100 pixel rectangle with its top-left corner at (50,50).

Color Specification

MDL supports multiple formats for specifying colors:

Format Description Example
Single value Grayscale (0-255) "128" (medium gray)
Two values Alpha + Grayscale "128,255" (semi-transparent white)
Three values RGB "255,0,0" (red)
Four values Alpha + RGB "128,255,0,0" (semi-transparent red)
Hex (with #) RGB in hexadecimal "#FF0000" (red)
Hex with alpha Alpha + RGB in hexadecimal "#80FF0000" (semi-transparent red)

Note: Alpha values range from 0 (completely transparent) to 255 (completely opaque).

Pixel Array (Pix) Format

MDL uses a special format called "pix" for storing and manipulating image data. A pix consists of:

  1. Width (in pixels)
  2. Height (in pixels)
  3. Image data (binary format)

These elements are stored as a comma-separated list with the binary image data following the dimensions.

4. Buffer Management

Buffer management functions handle the creation, manipulation, and updating of drawing buffers.

MdMakeBuffer

Syntax: MdMakeBuffer pImageName

Initializes the drawing buffer based on an existing image object. This is typically the first MDL function called in an application.

Example:
MdMakeBuffer "myCanvas"

MdNewBuffer

Syntax: MdNewBuffer pWidth, pHeight

Creates a new buffer with specified dimensions. The buffer will be filled with the current background color.

Example:
MdNewBuffer 800, 600

MdUpdate

Syntax: MdUpdate pImageName [, pEffect [, pSpeed]]

Updates the image object with the current buffer content. Optionally applies visual effects.

Example:
MdUpdate "myCanvas"
MdUpdate "myCanvas", "pushLeft", "fast"

MdPushBuffer

Syntax: MdPushBuffer

Saves the current buffer state to temporary memory for later restoration.

Example:
MdPushBuffer

Note: In version 0.92, only one buffer state can be saved at a time.

MdPopBuffer

Syntax: MdPopBuffer

Restores the buffer state from temporary memory (previously saved with MdPushBuffer).

Example:
MdPopBuffer

MdGetBufferWidth

Syntax: MdGetBufferWidth()

Returns the width of the current buffer in pixels.

Returns: Integer width in pixels
Example:
put MdGetBufferWidth() into tWidth

MdGetBufferHeight

Syntax: MdGetBufferHeight()

Returns the height of the current buffer in pixels.

Returns: Integer height in pixels
Example:
put MdGetBufferHeight() into tHeight

MdGetBufferRect

Syntax: MdGetBufferRect()

Returns the dimensions of the buffer as a rectangle specification.

Returns: String in the format "0,0,width-1,height-1"
Example:
put MdGetBufferRect() into tBufferRect

MdFillScreen

Syntax: MdFillScreen

Fills the entire buffer with the current pen color.

Example:
MdSetPenColor "255,0,0" -- Red
MdFillScreen

MdEraseScreen

Syntax: MdEraseScreen

Fills the entire buffer with the current background color.

Example:
MdSetBgColor "255,255,255" -- White
MdEraseScreen

MdScroll

Syntax: MdScroll pX, pY

Scrolls the buffer content by the specified amounts.

Example:
MdScroll 10, 0 -- Scroll right 10 pixels
MdScroll 0, -20 -- Scroll up 20 pixels

Buffer Management Example

The following example demonstrates typical buffer management operations:

-- Initialize the main buffer
MdMakeBuffer "myCanvas"
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Save the empty buffer state
MdPushBuffer

-- Draw something
MdSetPenColor "255,0,0" -- Red
MdDrawRect "50,50,150,150", true
MdUpdate "myCanvas"

-- Wait briefly to show the red rectangle
wait 1 second

-- Restore empty buffer state
MdPopBuffer
MdUpdate "myCanvas", "dissolve", "fast"

-- Draw something else
MdSetPenColor "0,0,255" -- Blue
MdDrawOval "50,50,150,150", true
MdUpdate "myCanvas"

5. Drawing Tools

MDL provides a variety of drawing tools for creating shapes and lines.

Line Drawing

MdDrawLine

Syntax: MdDrawLine pPoints

Draws a line using the current pen color.

Example:
MdDrawLine "10,10,100,100" -- Diagonal line

MdEraseLine

Syntax: MdEraseLine pPoints

Draws a line using the current background color (effectively erasing).

Example:
MdEraseLine "10,10,100,100" -- Erase a diagonal line

Rectangle Drawing

MdDrawRect

Syntax: MdDrawRect pRect, pFillBool

Draws a rectangle using the current pen color.

Example:
MdDrawRect "50,50,150,150", true -- Filled rectangle
MdDrawRect "200,50,300,150", false -- Outline only

MdEraseRect

Syntax: MdEraseRect pRect, pFillBool

Draws a rectangle using the current background color (effectively erasing).

Example:
MdEraseRect "50,50,150,150", true -- Erase a filled area

Oval Drawing

MdDrawOval

Syntax: MdDrawOval pRect, pFillBool

Draws an oval or circle using the current pen color. The oval will fit within the specified rectangle.

Example:
MdDrawOval "50,50,150,150", true -- Filled circle
MdDrawOval "200,50,300,100", false -- Outline oval

MdEraseOval

Syntax: MdEraseOval pRect, pFillBool

Draws an oval using the current background color (effectively erasing).

Example:
MdEraseOval "50,50,150,150", true -- Erase a circular area

Diamond Drawing

MdDrawDia

Syntax: MdDrawDia pRect, pFillBool

Draws a diamond shape using the current pen color. The diamond will fit within the specified rectangle.

Example:
MdDrawDia "50,50,150,150", true -- Filled diamond

MdEraseDia

Syntax: MdEraseDia pRect, pFillBool

Draws a diamond using the current background color (effectively erasing).

Example:
MdEraseDia "50,50,150,150", true -- Erase a diamond-shaped area

Star Drawing

MdDrawStar

Syntax: MdDrawStar pRect, pFillBool

Draws a star shape using the current pen color. The star will fit within the specified rectangle.

Example:
MdDrawStar "50,50,150,150", true -- Filled star

MdEraseStar

Syntax: MdEraseStar pRect, pFillBool

Draws a star using the current background color (effectively erasing).

Example:
MdEraseStar "50,50,150,150", true -- Erase a star-shaped area

Drawing Example

Here's an example that demonstrates various drawing tools:

-- Initialize the buffer
MdMakeBuffer "myCanvas"
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Draw a red outline rectangle
MdSetPenColor "255,0,0" -- Red
MdDrawRect "20,20,120,80", false

-- Draw a blue filled oval
MdSetPenColor "0,0,255" -- Blue
MdDrawOval "150,20,250,80", true

-- Draw a green filled diamond
MdSetPenColor "0,255,0" -- Green
MdDrawDia "20,100,120,160", true

-- Draw a purple star outline
MdSetPenColor "128,0,128" -- Purple
MdDrawStar "150,100,250,160", false

-- Draw some connecting lines
MdSetPenColor "0,0,0" -- Black
MdDrawLine "120,50,150,50" -- Horizontal line connecting rectangle and oval
MdDrawLine "70,80,70,100" -- Vertical line connecting rectangle and diamond
MdDrawLine "200,80,200,100" -- Vertical line connecting oval and star

-- Update the display
MdUpdate "myCanvas"

6. Color Management

MDL provides several functions for managing colors and transparency.

MdSetPenColor

Syntax: MdSetPenColor pRGBN

Sets the current pen color used for drawing operations.

Example:
MdSetPenColor "255,0,0" -- Red in RGB format
MdSetPenColor "#00FF00" -- Green in hex format
MdSetPenColor "128" -- Medium gray in grayscale format
MdSetPenColor "128,255,0,0" -- Semi-transparent red

MdGetPenColor

Syntax: MdGetPenColor()

Returns the current pen color as an RGBA string.

Returns: String in the format "alpha,red,green,blue" (e.g., "255,255,0,0" for red)
Example:
put MdGetPenColor() into tCurrentColor

MdSetBgColor

Syntax: MdSetBgColor pRGBN

Sets the current background color used for erasing operations.

Example:
MdSetBgColor "255,255,255" -- White in RGB format
MdSetBgColor "#000000" -- Black in hex format
MdSetBgColor "200" -- Light gray in grayscale format

MdGetBgColor

Syntax: MdGetBgColor()

Returns the current background color as an RGBA string.

Returns: String in the format "alpha,red,green,blue" (e.g., "255,255,255,255" for white)
Example:
put MdGetBgColor() into tCurrentBgColor

MdGetBlendColor

Syntax: MdGetBlendColor(pColor1, pColor2, pBlend)

Returns a color that is a blend between two specified colors.

Returns: Blended color as RGBA string "alpha,red,green,blue"
Example:
-- Get a 50/50 blend of red and blue (purple)
put MdGetBlendColor("255,255,0,0", "255,0,0,255", 5) into tPurple

-- Get a 70/30 blend of red and blue
put MdGetBlendColor("255,255,0,0", "255,0,0,255", 3) into tRedPurple

MdGetBlendColorBin

Syntax: MdGetBlendColorBin(pColor1, pColor2, pBlend)

Similar to MdGetBlendColor, but works with binary color data for improved performance.

Returns: Blended color as binary RGBA data (4 bytes)

Note: This function is primarily for internal use or performance optimization.

Alpha Channel Operations

MdSetPixAlpha

Syntax: MdSetPixAlpha @rPix, pAlphaValue [, pRect]

Sets the alpha channel value for a pixel array (pix).

Example:
-- Create a pix and set half transparency
put MdBufferToPix() into tPix
MdSetPixAlpha tPix, 128
MdDrawPix tPix, "50,50"

MdSetBufferAlpha

Syntax: MdSetBufferAlpha pAlphaValue [, pRect]

Sets the alpha channel value for the current buffer.

Example:
-- Make the entire buffer semi-transparent
MdSetBufferAlpha 128

-- Make just a specific area semi-transparent
MdSetBufferAlpha 128, "50,50,150,150"

Color Management Example

Here's an example that demonstrates various color management techniques:

-- Initialize the buffer
MdMakeBuffer "myCanvas"
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Draw overlapping shapes with different colors
MdSetPenColor "255,0,0" -- Red
MdDrawRect "50,50,150,150", true

-- Create a blend of red and blue (purple)
put MdGetBlendColor("255,255,0,0", "255,0,0,255", 5) into tPurple
MdSetPenColor tPurple
MdDrawRect "100,100,200,200", true

-- Create a semi-transparent green overlay
MdSetPenColor "128,0,255,0" -- Semi-transparent green
MdDrawOval "75,75,175,175", true

-- Update the display
MdUpdate "myCanvas"

7. Pixel Manipulation

MDL provides functions for manipulating individual pixels and pixel arrays.

Individual Pixel Operations

MdSetPixel

Syntax: MdSetPixel pLoc

Sets a single pixel to the current pen color.

Example:
MdSetPixel "100,100" -- Set pixel at (100,100) to pen color

MdErasePixel

Syntax: MdErasePixel pLoc

Sets a single pixel to the current background color (effectively erasing it).

Example:
MdErasePixel "100,100" -- Set pixel at (100,100) to background color

MdGetPixel

Syntax: MdGetPixel(pLoc)

Returns the color of a specific pixel as an RGBA string.

Returns: Color as RGBA string "alpha,red,green,blue"
Example:
put MdGetPixel("100,100") into tPixelColor

Pixel Array Operations

MdNewPix

Syntax: MdNewPix(pWidth, pHeight)

Creates a new pixel array (pix) filled with the current background color.

Returns: A new pix in the format "width,height,imageData"
Example:
put MdNewPix(100, 100) into tEmptyPix

MdImageToPix

Syntax: MdImageToPix(pImageName [, pRect])

Converts an image object to a pixel array (pix).

Returns: A pix in the format "width,height,imageData"
Example:
-- Convert entire image
put MdImageToPix("sourceImage") into tFullPix

-- Convert just a portion of the image
put MdImageToPix("sourceImage", "10,10,110,110") into tPartialPix

MdBufferToPix

Syntax: MdBufferToPix([pRect])

Converts the current buffer to a pixel array (pix).

Returns: A pix in the format "width,height,imageData"
Example:
-- Convert entire buffer
put MdBufferToPix() into tBufferPix

-- Convert just a portion of the buffer
put MdBufferToPix("10,10,110,110") into tPartialPix

MdCopyPix

Syntax: MdCopyPix(pSrcPix [, pRect])

Creates a copy of a pixel array (pix), optionally selecting just a portion.

Returns: A new pix in the format "width,height,imageData"
Example:
-- Copy entire pix
put MdCopyPix(tSourcePix) into tCopyPix

-- Copy just a portion
put MdCopyPix(tSourcePix, "10,10,60,60") into tPartialCopy

MdDrawPix

Syntax: MdDrawPix pSrcPix, pDstLoc

Draws a pixel array onto the current buffer at the specified location.

Example:
MdDrawPix tSpritePix, "100,100" -- Draw sprite at position 100,100

MdDrawPixPat

Syntax: MdDrawPixPat pSrcPix, pDstRect

Draws a pixel array as a repeating pattern within the specified rectangle.

Example:
-- Create a small pattern
MdNewBuffer 10, 10
MdSetPenColor "0,0,0"
MdDrawRect "0,0,4,4", true
MdDrawRect "5,5,9,9", true
put MdBufferToPix() into tPatternPix

-- Restore original buffer and draw pattern as a fill
MdNewBuffer 400, 300
MdDrawPixPat tPatternPix, "50,50,350,250" -- Fill area with checkerboard pattern

Pixel Manipulation Example

Here's an example that demonstrates various pixel manipulation techniques:

-- Initialize the buffer
MdMakeBuffer "myCanvas"
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Create a small 10x10 pixel pattern
MdNewBuffer 10, 10
MdSetPenColor "255,0,0" -- Red
MdDrawRect "0,0,4,4", true
MdSetPenColor "0,0,255" -- Blue
MdDrawRect "5,5,9,9", true
put MdBufferToPix() into tPattern

-- Restore main buffer
MdNewBuffer 400, 300
MdEraseScreen

-- Draw the pattern as a repeating tile in a rectangle
MdDrawPixPat tPattern, "50,50,350,150"

-- Create a sprite by drawing a simple shape
MdNewBuffer 20, 20
MdSetPenColor "0,255,0" -- Green
MdDrawOval "0,0,19,19", true
MdSetPenColor "0,0,0" -- Black
MdDrawOval "4,4,15,15", false
put MdBufferToPix() into tSprite

-- Make the sprite semi-transparent
MdSetPixAlpha tSprite, 128

-- Restore main buffer and draw the sprite at three locations
MdNewBuffer 400, 300
MdDrawPix tSprite, "100,200"
MdDrawPix tSprite, "200,200"
MdDrawPix tSprite, "300,200"

-- Draw individual pixels to create a dotted line
MdSetPenColor "0,0,0" -- Black
repeat with x = 50 to 350 step 10
    MdSetPixel x & ",180"
end repeat

-- Update the display
MdUpdate "myCanvas"

8. Text and Number Rendering

MDL includes functions for rendering text and numbers using built-in vector fonts.

Vector Text Drawing

MdDrawText

Syntax: MdDrawText pText, pLoc [, pTextLen [, pTextStyle]]

Draws text using the built-in vector font.

Example:
-- Basic text drawing
MdDrawText "Hello World", "20,20"

-- Styled text
MdDrawText "BOLD TEXT", "20,50", 10, "bold"
MdDrawText "CENTERED", "200,80", 10, "center"
MdDrawText "LARGE", "20,110", 5, "X:2 Y:2"

-- Multi-line text
MdDrawText "Line 1" & return & "Line 2", "20,150"

Note: The built-in font supports: space, punctuation, digits 0-9, letters A-Z, and special characters.

MdDrawVec

Syntax: MdDrawVec pPointList, pLoc

Draws custom vector shapes defined by a point list.

Example:
-- Draw a simple arrow shape
MdDrawVec "254525", "50,50" -- Triangle

Note: This function is primarily used internally for text rendering but can be used for custom vector shapes.

Number Display

MdDrawNumber

Syntax: MdDrawNumber pN, pLoc [, pTextLen [, pWeight [, pAlign]]]

Draws numbers using a 7-segment display style.

Example:
-- Basic number display
MdDrawNumber 12345, "20,20"

-- Right-aligned with padding
MdDrawNumber 42, "100,50", 5, 0, "right" -- Shows "   42"

-- Thicker segments
MdDrawNumber 789, "20,80", 3, 2

MdDrawSegment

Syntax: MdDrawSegment pPattern, pLoc

Draws a custom 7-segment display pattern.

Example:
-- Segment numbering:
--  1
-- 2 3
--  4
-- 5 6
--  7

-- Draw a custom pattern
MdDrawSegment "1234567", "20,20" -- All segments (8)
MdDrawSegment "3567", "40,20" -- Letter "C"
MdDrawSegment "134567", "60,20" -- Letter "E"

Text and Number Example

Here's an example that demonstrates text and number rendering:

-- Initialize the buffer
MdMakeBuffer "myCanvas"
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Draw a title
MdSetPenColor "0,0,0" -- Black
MdDrawText "MDL TEXT DEMO", "50,30", 20, "bold center"

-- Draw standard text
MdSetPenColor "0,0,255" -- Blue
MdDrawText "Standard Text", "50,70"

-- Draw formatted text
MdSetPenColor "255,0,0" -- Red
MdDrawText "BOLD", "50,100", 4, "bold"
MdDrawText "WIDE", "120,100", 4, "X:1.5"
MdDrawText "TALL", "200,100", 4, "Y:1.5"
MdDrawText "BIG", "270,100", 3, "X:2 Y:2"

-- Draw multi-line text
MdSetPenColor "0,128,0" -- Green
MdDrawText "Line 1" & return & "Line 2" & return & "Line 3", "50,150"

-- Draw 7-segment numbers
MdSetPenColor "0,0,0" -- Black
MdDrawText "7-Segment Numbers:", "50,220"

-- Draw numbers with different weights
repeat with i = 0 to 3
    MdDrawNumber 8, (50 + i * 40) & ",250", 1, i
end repeat

-- Draw a digital clock display
MdSetPenColor "0,0,255" -- Blue
MdDrawNumber 12, "200,250", 2, 1
MdDrawText ":", "230,250", 1, "bold"
MdDrawNumber 34, "245,250", 2, 1

-- Custom segments to spell "HELLO"
MdSetPenColor "255,0,0" -- Red
MdDrawSegment "23567", "50,300" -- H
MdDrawSegment "13467", "80,300" -- E
MdDrawSegment "167", "110,300" -- L
MdDrawSegment "167", "140,300" -- L
MdDrawSegment "23567", "170,300" -- O

-- Update the display
MdUpdate "myCanvas"

9. Visual Effects

MDL provides a variety of visual effects and transitions that can be applied when updating the display.

MdGetEffectList

Syntax: MdGetEffectList()

Returns a list of all available visual effects.

Returns: Multi-line string listing all available effects
Example:
put MdGetEffectList() into tEffects

Applying Effects

Visual effects are applied using the MdUpdate function with additional parameters:

MdUpdate "myCanvas", pEffect, pSpeed

Effect Speed Options

Available Effects

Push Effects

Pushes the new content onto the screen from a direction, moving the old content out.

Scroll Effects

Scrolls the content in a specific direction.

Wipe Effects

Wipes the new content onto the screen in a specific direction.

Reveal Effects

Reveals the new content gradually in a specific direction.

Square Effects

Square-based transitions.

Clam Effects

Clam-shell style transitions.

Dissolve Effects

Dissolve-based transitions.

Visual Effects Example

Here's an example that demonstrates several visual effects:

-- Initialize the buffer
MdMakeBuffer "myCanvas"

-- Function to draw a test screen
on drawTestScreen pScreenNum
   MdEraseScreen
   MdSetPenColor "0,0,0" -- Black
   MdDrawText "Screen " & pScreenNum, "20,20", 10, "bold"
   
   -- Draw different shapes based on screen number
   if pScreenNum = 1 then
      MdSetPenColor "255,0,0" -- Red
      MdDrawRect "50,50,150,150", true
   else if pScreenNum = 2 then
      MdSetPenColor "0,0,255" -- Blue
      MdDrawOval "50,50,150,150", true
   else if pScreenNum = 3 then
      MdSetPenColor "0,255,0" -- Green
      MdDrawStar "50,50,150,150", true
   else
      MdSetPenColor "128,0,128" -- Purple
      MdDrawDia "50,50,150,150", true
   end if
end drawTestScreen

-- Draw the first screen
drawTestScreen 1
MdUpdate "myCanvas"

-- Wait briefly
wait 1 second

-- Test various effects
drawTestScreen 2
MdUpdate "myCanvas", "pushLeft", "fast"
wait 1 second

drawTestScreen 3
MdUpdate "myCanvas", "dissolve", "normal"
wait 1 second

drawTestScreen 4
MdUpdate "myCanvas", "wipeOpen", "slow"
wait 1 second

-- Return to first screen with a different effect
drawTestScreen 1
MdUpdate "myCanvas", "squareClose", "fast"

Tip: Effects are particularly useful for slide shows, animations, and interactive presentations.

10. Utility Functions

MDL provides a variety of utility functions for rectangle manipulation, data handling, and other common operations.

Rectangle Utilities

MdPackRect

Syntax: MdPackRect(pX1, pY1, pX2, pY2)

Creates a rectangle string from individual coordinates.

Returns: Rectangle as "left,top,right,bottom"
Example:
put MdPackRect(10, 20, 110, 120) into tRect -- "10,20,110,120"

MdUnpackRect

Syntax: MdUnpackRect pRect, @rX1, @rY1, @rX2, @rY2

Extracts individual coordinates from a rectangle string.

Example:
MdUnpackRect "10,20,110,120", tLeft, tTop, tRight, tBottom

MdOffsetRect

Syntax: MdOffsetRect @rRect, pX, pY

Moves a rectangle by the specified offset.

Example:
put "10,20,110,120" into tRect
MdOffsetRect tRect, 20, 30
-- tRect is now "30,50,130,150"

MdInsetRect

Syntax: MdInsetRect @rRect, pX, pY

Shrinks or enlarges a rectangle from its center.

Example:
put "10,20,110,120" into tRect
MdInsetRect tRect, 10, 10
-- tRect is now "20,30,100,110" (smaller)

put "10,20,110,120" into tRect
MdInsetRect tRect, -10, -10
-- tRect is now "0,10,120,130" (larger)

MdPtInRect

Syntax: MdPtInRect(pLoc, pRect)

Checks if a point is inside a rectangle.

Returns: Boolean - true if point is inside rectangle, false otherwise
Example:
if MdPtInRect("50,60", "10,20,110,120") then
   -- Point is inside the rectangle
end if

MdZoomRect

Syntax: MdZoomRect @rRect, pZoomX, pZoomY

Scales a rectangle while keeping its top-left corner fixed.

Example:
put "10,20,110,120" into tRect
MdZoomRect tRect, 2, 1.5
-- tRect is now "10,20,210,170" (wider and taller)

Location Utilities

MdOffsetLoc

Syntax: MdOffsetLoc @rLoc, pX, pY

Moves a location by the specified offset.

Example:
put "50,60" into tLoc
MdOffsetLoc tLoc, 10, 20
-- tLoc is now "60,80"

Data Utilities

MdPackData

Syntax: MdPackData(pLen, pData)

Repeats data to fill a specified length.

Returns: Binary data of the specified length
Example:
put MdPackData(100, "ABCD") into tRepeatedData
-- Results in "ABCDABCDABCD..." repeated to 100 bytes

Note: This function is primarily used internally but can be useful for creating patterns.

MdSwap

Syntax: MdSwap @p1, @p2

Swaps the values of two variables.

Example:
put 10 into tValue1
put 20 into tValue2
MdSwap tValue1, tValue2
-- tValue1 is now 20, tValue2 is now 10

MdShuffleItems

Syntax: MdShuffleItems @rItems

Randomly shuffles a list of comma-separated items.

Example:
put "1,2,3,4,5" into tList
MdShuffleItems tList
-- tList might now be something like "4,1,5,2,3"

MdGetShuffeItems

Syntax: MdGetShuffeItems(pN)

Returns a shuffled list of numbers from 1 to N.

Returns: Comma-separated list of shuffled numbers
Example:
put MdGetShuffeItems(10) into tShuffledList
-- tShuffledList might be something like "7,3,1,10,6,2,8,5,9,4"

MdGetHelp

Syntax: MdGetHelp()

Returns a simple help text describing MDL functions.

Returns: Text with function descriptions
Example:
put MdGetHelp() into tHelpText

Utility Functions Example

Here's an example that demonstrates various utility functions:

-- Initialize the buffer
MdMakeBuffer "myCanvas"
MdSetBgColor "255,255,255" -- White
MdEraseScreen

-- Create a rectangle and manipulate it
put "50,50,150,150" into tRect
MdSetPenColor "255,0,0" -- Red
MdDrawRect tRect, false

-- Offset the rectangle and draw it
MdOffsetRect tRect, 20, 20
MdSetPenColor "0,255,0" -- Green
MdDrawRect tRect, false

-- Inset the rectangle and draw it
MdInsetRect tRect, 10, 10
MdSetPenColor "0,0,255" -- Blue
MdDrawRect tRect, false

-- Create a shuffled list of positions
put MdGetShuffeItems(10) into tShuffledList

-- Draw dots at shuffled positions
MdSetPenColor "0,0,0" -- Black
repeat for each item i in tShuffledList
    put 200 + (i * 15) into tX
    put 100 into tY
    MdDrawOval (tX - 5) & "," & (tY - 5) & "," & (tX + 5) & "," & (tY + 5), true
end repeat

-- Use point-in-rectangle testing
put "50,200,250,300" into tTestRect
MdDrawRect tTestRect, false

-- Draw text explaining the test
MdDrawText "Click inside or outside the rectangle:", "50,180"

-- Update the display
MdUpdate "myCanvas"

-- In a mouseDown handler, you could do this:
on mouseDown
    if MdPtInRect(the mouseH & "," & the mouseV, tTestRect) then
        answer "You clicked inside the rectangle!"
    else
        answer "You clicked outside the rectangle!"
    end if
end mouseDown

11. Debugging and Optimization

MDL version 0.92 includes enhanced debugging capabilities to help you identify and resolve issues in your code.

Debugging

The primary debugging tool in MDL 0.92 is the debugLog function:

debugLog

Syntax: debugLog pMessage

Outputs a debug message to a field named "DebugOutput".

Example:
-- Create a debug output field if it doesn't exist
if there is not a field "DebugOutput" then
   create field "DebugOutput"
   set the rect of field "DebugOutput" to "10,400,500,600"
   set the vScrollbar of field "DebugOutput" to true
end if

-- Log debug messages
debugLog "Starting MDL operations..."
MdMakeBuffer "myCanvas"
debugLog "Buffer created with dimensions: " & MdGetBufferWidth() & "x" & MdGetBufferHeight()
MdDrawRect "50,50,150,150", true
debugLog "Rectangle drawn at 50,50,150,150"

Performance Optimization Tips

To get the best performance from MDL, consider these optimization strategies:

Buffer Management

Drawing Operations

Memory Management

Debugging Example

Here's an example that demonstrates debugging techniques:

-- Create a debug output field if it doesn't exist
if there is not a field "DebugOutput" then
   create field "DebugOutput"
   set the rect of field "DebugOutput" to "10,400,500,600"
   set the vScrollbar of field "DebugOutput" to true
   set the lockText of field "DebugOutput" to false
end if

-- Initialize MDL with debugging
debugLog "Starting MDL initialization"
MdMakeBuffer "myCanvas"
debugLog "Buffer initialized: " & MdGetBufferWidth() & "x" & MdGetBufferHeight()

-- Track memory usage and performance
put the milliseconds into tStartTime
MdSetBgColor "255,255,255"
MdEraseScreen
debugLog "Screen erased in " & (the milliseconds - tStartTime) & "ms"

-- Test drawing performance
put the milliseconds into tStartTime
MdSetPenColor "255,0,0"
repeat with i = 1 to 10
   MdDrawRect (i * 20) & "," & (i * 20) & "," & (i * 20 + 100) & "," & (i * 20 + 100), true
   debugLog "Drew rectangle " & i & " at " & (i * 20) & "," & (i * 20)
end repeat
debugLog "Drew 10 rectangles in " & (the milliseconds - tStartTime) & "ms"

-- Check pixel operations
put the milliseconds into tStartTime
put MdGetPixel("50,50") into tPixelColor
debugLog "Pixel at 50,50 is " & tPixelColor & " (retrieved in " & (the milliseconds - tStartTime) & "ms)"

-- Update with timing
put the milliseconds into tStartTime
MdUpdate "myCanvas"
debugLog "Updated display in " & (the milliseconds - tStartTime) & "ms"

-- Final summary
debugLog "All operations completed"

12. Known Limitations

While MDL is a powerful library, it has some limitations that you should be aware of:

Buffer Stack Limitations

Performance Considerations

Text and Font Limitations

Visual Effects Limitations

Memory Management

13. Version History

Version 0.92

Released: September 2024

Version 0.91

Released: April 2024

Version 0.9

Released: Unknown date (original version by UDI)