Manual
Graphic
 previous   up   next 

14. GRAPHIC LIBRARY

Seed7 provides a portable graphics library. Below is the graphic "hello world" program:

$ include "seed7_05.s7i";
  include "dialog.s7i";

const proc: main is func
  begin
    messageWindow("hello world");
  end func;

This program displays a popup window with the message "hello world". In the popup window there is an OK button, which ends the program. A program that creates an empty window and a message popup is:

$ include "seed7_05.s7i";
  include "dialog.s7i";

const proc: main is func
  begin
    screen(640, 480);
    messageWindow("hello world");
  end func;

The function screen(640, 480) opens window with a width of 640 and a height of 480. The window is assigned to the variable curr_win, which is used as default window by drawing functions.

Displaying an empty window without message box is done with:

$ include "seed7_05.s7i";
  include "draw.s7i";
  include "keybd.s7i";

const proc: main is func
  begin
    screen(640, 480);
    ignore(getc(GRAPH_KEYBOARD));
  end func;

This program waits until a key is pressed. The key is read from GRAPH_KEYBOARD Without reading from GRAPH_KEYBOARD the program would terminate immediately. Regarding the keyboard:

For that reason a graphic program should do the following assignment:

KEYBOARD := GRAPH_KEYBOARD;

Afterwards it should just use the variable KEYBOARD.

The program below clears the window with a color computed from the keys pressed:

$ include "seed7_05.s7i";
  include "draw.s7i";
  include "keybd.s7i";

const proc: main is func
  local
    var char: command is ' ';
  begin
    screen(640, 480);
    KEYBOARD := GRAPH_KEYBOARD;
    repeat
      command := getc(KEYBOARD);
      clear(color(1009 * ord(command) mod 65536,
                  4999 * ord(command) mod 65536,
                  9973 * ord(command) mod 65536));
    until command = KEY_ESC;
  end func;

The function color creates a color from red, green and blue lights in the additive color model. The red, green and blue lights are specified by integer values in the range 0 .. 65535.

Below is a "hello world" program that uses a Seed7 font:

$ include "seed7_05.s7i";
  include "draw.s7i";
  include "keybd.s7i";
  include "text.s7i";
  include "stdfont24.s7i";

const proc: main is func
  local
    var text: textWindow is text.value;
  begin
    screen(203, 45);
    textWindow := openPixmapFontFile(curr_win);
    setFont(textWindow, stdFont24);
    writeln(textWindow, " hello world");
    ignore(getc(GRAPH_KEYBOARD));
  end func;

The function screen opens a window with a width of 203 pixels and a height of 45 pixels. The window is stored in the global variable curr_win. The function openPixmapFontFile opens a pixmapFontFile for curr_win. Everything written to textWindow is displayed in the current window. The function setFont defines which font should be used. The font stdfont24.s7i is a bitmapFont with a cap-height of 24 pixels. The function writeln displays hello world to the current window with the specified font.

Seed7 fonts are not as perfect as professional fonts. They have the following properties:

Below is a "hello world" program which writes "hello world" at clicked positions

$ include "seed7_05.s7i";
  include "draw.s7i";
  include "keybd.s7i";
  include "text.s7i";
  include "stdfont24.s7i";

const proc: main is func
  local
    var text: textWindow is text.value;
    var char: command is ' ';
  begin
    screen(640, 480);
    textWindow := openPixmapFontFile(curr_win);
    setFont(textWindow, stdFont24);
    KEYBOARD := GRAPH_KEYBOARD;
    repeat
      command := getc(KEYBOARD);
      if command = KEY_MOUSE1 then
        setPosXY(textWindow, clickedXPos(KEYBOARD),
                             clickedYPos(KEYBOARD));
        color(textWindow, color(rand(0, 65535),
                                rand(0, 65535),
                                rand(0, 65535)));
        write(textWindow, "hello world");
      end if;
    until command <> KEY_MOUSE1;
  end func;

The functions clickedXPos and clickedYPos return the X and Y coordinates of the mouse cursor at the last time when a mouse button was clicked. The function setPosXY sets the current position of a text window to the given X and Y coordinates. There are two functions named color. The inner function color creates a color from random red, green and blue lights. The outer function color sets the foreground color of a text.

Below is a program which draws an hourglass at the cursor position

$ include "seed7_05.s7i";
  include "draw.s7i";
  include "keybd.s7i";
  include "pic32.s7i";
  include "time.s7i";
  include "duration.s7i";

const proc: main is func
  local
    var PRIMITIVE_WINDOW: hourglass is PRIMITIVE_WINDOW.value;
  begin
    screen(640, 480);
    hourglass := openSubWindow(curr_win, 1000, 1000, 
                               length(hourglass_pic[1]),
                               length(hourglass_pic));
    put(hourglass, 0, 0, createPixmap(hourglass_pic, 1, black));
    KEYBOARD := GRAPH_KEYBOARD;
    setCursorVisible(curr_win, FALSE);
    repeat
      setPos(hourglass, pointerXPos(curr_win) - width(hourglass) div 2,
                        pointerYPos(curr_win) - height(hourglass) div 2);
      wait(30000 . MICRO_SECONDS);
    until inputReady(KEYBOARD);
  end func;

The variable hourglass is of type PRIMITIVE_WINDOW. A variable of this type can hold a window (which is on screen) or a pixmap (which is off-screen). The function openSubWindow creates a sub window inside the parent window curr_win. The window curr_win has a title bar. The window created by openSubWindow does not have a title bar or a border. The function createPixmap is used to create the pixmap of an hourglass by using a pattern from the library "pic32.s7i". The function setCursorVisible disables the visibility of the mouse cursor in curr_win. The functions pointerXPos and pointerYPos return the current X and Y coordinates of the mouse cursor. The function setPos is used to position the hourglass to the cursor position (minus half of the width and height of the hourglass).

Besides other image formats Seed7 supports "poor man's images" where an array of strings is used to represent an image. E.g.:

const array string: hourglass_pic is [](
  " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ",       # Mapping from character to color:
  " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ",       # char  | color
  "   bb XWWWWWWWWWWWWWWWWWWX bb   ",       # ------+---------------
  "   bbXWWWWWWWWWWWWWWWWWWWWXbb   ",       # space | background color
  "   bbWWXXXXXXXXXXXXXXXXXXWWbb   ",       #   B   | light_blue
  "   bbWWX                XWWbb   ",       #   G   | light_green
  "   bbWWX      YYYY      XWWbb   ",       #   L   | lavender
  "   bbWWX     YYYYYY     XWWbb   ",       #   M   | light_magenta
  "   bbXWWX   YYYYYYYY   XWWXbb   ",       #   O   | orange
  "   bb XWWX YYYYYYYYYY XWWX bb   ",       #   P   | light_pink
  "   bb  XWWYYYYYYYYYYYYWWX  bb   ",       #   R   | light_red
  "   bb   XWWYYYYYYYYYYWWX   bb   ",       #   W   | white
  "   bb    XWWYYYYYYYYWWX    bb   ",       #   X   | black
  "   bb     XWWYYYYYYWWX     bb   ",       #   Y   | yellow
  "   bb      XWWYYYYWWX      bb   ",       #   b   | brown
  "   bb       XWWYYWWX       bb   ",       #   c   | light_cyan
  "   bb       XWWYYWWX       bb   ",       #   d   | dark_gray
  "   bb      XWWXYYXWWX      bb   ",       #   f   | forestgreen
  "   bb     XWWX YY XWWX     bb   ",       #   g   | dark_green
  "   bb    XWWX  YY  XWWX    bb   ",       #   i   | mint
  "   bb   XWWX   YY   XWWX   bb   ",       #   l   | middle_blue
  "   bb  XWWX    YY    XWWX  bb   ",       #   m   | dark_magenta
  "   bb XWWX     YY     XWWX bb   ",       #   n   | dark_blue
  "   bbXWWX     YYYY     XWWXbb   ",       #   p   | pink
  "   bbWWX     YYYYYY     XWWbb   ",       #   r   | dark_red
  "   bbWWX    YYYYYYYY    XWWbb   ",       #   x   | light_gray
  "   bbWWX   YYYYYYYYYY   XWWbb   ",       #   y   | middle_gray
  "   bbWWXXXYYYYYYYYYYYYXXXWWbb   ",       # other | black
  "   bbXWWWWWWWWWWWWWWWWWWWWXbb   ",
  "   bb XWWWWWWWWWWWWWWWWWWX bb   ",
  " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ",
  " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ");


 previous   up   next