Standard SWOS menus
-------------------

Menu is laid out in memory as a variable length structure that consists of a 22
bytes header followed by entries, which are 56 bytes each.

Parameters to menu entry functions (before draw, after draw, etc.) are in the
following registers:
A5 - pointer to entry which is receiving the event
A0 - pointer to the function itself (since it's called indirectly through A0)

Upon entry, onInit, onReturn and onDraw functions of the menu will have A6
pointing to the menu's header.

Header (size 22 bytes):

           +0  - init function (invoked only once, when converting the menu)
           +4  - onReturn function (invoked when returning to this menu)
           +8  - onDraw function (invoked each frame, before drawing the items)
           +12 - pointer to currently selected entry
           +16 - number of entries in the menu
           +18 - pointer to the end of the menu (including additional
                 STDMENUTEXT strings)

Menu entries, following the header:

       0  ; has it already been drawn? (word)
            0 - no
            1 - yes
       2  ; ordinal (0-based)
       4  ; is it visible?
            0  - draw
            !0 - don't
       6  ; is disabled (word) (1 = disabled, 0 = not)
       8  ; left item     ; ordinal of entry that's next to be selected once
       9  ; right item    ; left, right, up, down is pressed on this entry,
       10 ; up item       ; respectively
       11 ; down item
       12 ; left          ; [values: 0 = left, 1 = right, 2 = up, 3 = down]
       13 ; right         ; direction of movement that is chosen once this
       14 ; up            ; entry is disabled or invisible
       15 ; down
       16 ; left entry    ; ordinal of entry that's to be selected in case this
       17 ; right entry   ; entry is disabled or invisible
       18 ; up entry
       19 ; down entry
       20 ; x
       22 ; y
       24 ; width
       26 ; height
       28 ; type1 [0,1,2,3] (word) -- this is something of a background type
            0: NOP -- nothing is drawn in the first cycle (no background)
            1: custom draw function -- don't draw anything, but invoke function
               from offset 30 (dword) with x and y arguments in D0 and D1
               (custom draw background function)
            2: standard entry -- has a colored background and frame
               Ofs. 30 low byte is background color (low nibble) and outer
               frame color (high nibble), high byte is color of the internal
               frame. If the color of internal frame is zero, don't draw it.
               Outer frame is drawn only if there is room (height is greater
               than 6 or 8). If background color is zero, no frame is drawn,
               and the background is colored with color 15
               (yellow-green-black).
            3: sprite -- at offset 30 is its index into sprite.dat (word)

       30 ; switch(ofs28) {
            case 0:
                do not draw
            case 1:
                ofs30 = pointer to a function getting x and y as parameters
                        (in D0 and D1)
            case 2:
                ofs30 = lo nibble: entry background and outer frame color
                        hi nibble: inner frame color
            case 3:
                ofs30 = sprite index
            }

       34 ; type2 [0,1,2,3,4,5,6,7] -- something of a foreground type

            0: NOP, no foreground
            1: ofs38 contains a function pointer, invoke it with x and y
               parameters (D0 and D1)
            2: ofs38 contains pointer to string.
               If this pointer has value -1 then additional 70 bytes are
               allocated after the menu end and string "STDMENUTEXT" is written
               there; this will be filled later by some of the entry functions
            3: ofs38 is index of a sprite that will be drawn
            4: ofs38 is a pointer to string table:
                 0 - pointer to index variable (word)
                 4 - word, value of starting index
                 6 - further we have array of pointers to strings
               write string from the table indexed by index variable
            5: multi-line text. First byte is number of lines, followed by that
               many null-terminated strings (not pointers). Ofs36, low nibble,
               represents color of the text
            6: ofs38 is a number (word). It's converted into string and
               displayed (same code as for type 2 is executed).
            7: sprite copy with color conversion. offset 38 is a pointer to the
               following structure:
                 0 - source sprite index
                 2 - destination sprite index
                 4 - color conversion table, 32 bytes long
               First sprite is copied into second, second is converted to 8
               colors and eventually drawn to screen.

       36 ; Low nibble of offset 36 represents color of the text. If it's zero
            then use color 2 (white). Nibble 1 represents font size -- small or
            big (bit 4 that is, bits 5,6,7 must be zero since we only have two
            possibilities). If bit 15 is set string is left-aligned, and if bit
            14 is set string is right-aligned. If neither of these bits is on,
            string is centered across the entry. If bit 9 isn't set and offset
            34 is not zero, entry is drawn without text. If bit 13 is set text
            will blink gently.
            Apparently the blink flag can be used for non-string entries as
            well.
       38 ; see ofs34
       42 ; onSelect function
       46 ; controls mask (gets AND-ed with control word)
       48 ; beforeDraw function, on entry esi = A5 -> entry that's drawn
       52 ; afterDraw function,            -//-

    sizeof(MenuItem) = 56

x coordinates of menu entries are shifted right by 8 pixels. Therefore entry
with x coordinate zero in fact begins at pixel eight of the screen.

beforeDraw function executes first, before anything has been drawn. onReturn
is executed after all the entry drawing has been done, but before flip so it's
still possible to alter the looks of the entry.

If type1 or type2 fall out of prescribed range, int3 is issued and endless loop
is entered. int3 will terminate the program if debugger isn't attached.

Type 1 is something of a background type, while type 2 relates to menu entry
foreground (contents).

All this relates to "screen menus", as we shall call them. They can be
displayed immediately. There is another kind of menu, which we shall refer to
as "static" menus -- to be used they must first be converted to screen menus;
that job is done by the ConvertMenu() procedure (they get copied to buffer
g_currentMenu in the process, variable which SWOS later exclusively uses to
access currently shown menu). Static menus have a different format, as their
purpose is to be space efficient and waste as little space as possible.

Static menu always starts with header. Header coincides with screen menu
header, and is followed by a number of entries of variable length. Entries are
comprised of variable number of records, each identified by a word. End of
entry is marked with record type 0, and end of entire menu is marked with
entry whose x coordinate is -999.

During the conversion, 8 is added to each entry x coordinate (as already
mentioned).

Static menu header:

        See screen menu header (first 16 bytes)
        Only thing that differs is offset 12 - in static menu that is the index
        of the initial entry, while the screen menu holds it as a pointer

sizeof(StaticMenuHeader) = 16

Example of an entry (variable size):

        0 - x         ;
        2 - y         ; these four fields are fixed,
        4 - width     ; ie. always present
        6 - height    ;
        8 - type (2, code will branch based on this, there are 22 types)
       10 - 4 bytes follow which are indices of entries we switch to when up,
       11   down, left, right is pressed, respectively
       12
       13
       14 - type (4)
       16 - frame and background and frame color
       18 - type (8)
       20 - string flags
       22 - pointer to entry text
       26 - type (13)
       28 - entry function (executes when fire is pressed on this entry)
       30 - type (0) - marking the end of entry

       (again, this is only an example, menus can be very varied)


Each record type has its own fields. Records inside the entry are interpreted
until we run into type 0 record. Various data from the record is copied into
offsets of a current screen menu entry.

There are special values for entry x coordinate that have special meaning:

-999   end of menu
-998   following two words are menuX and menuY (they will get added to x and
       y of all following entries)
-997   template entry follows - copy all records belonging to next entry to
       template entry which is used to initialize all entries
-996   return template entry back to normal (call InitMenuEntry)

Template entries are used to save space when initializing many entries with
same records (for example multiple entries that have uniform color and same on
select function).

There are 22 record types:

---------------------------
TYPE 0:

End of entry mark
---------------------------
TYPE 1:  (invisible entry)

ofs4 (isInvisible) = 1
---------------------------
TYPE 2:  (next entries)

Following 4 bytes set to offsets:
+8   left
+9   right
+10  up
+11  down
---------------------------
TYPE 3:  (function 1 -- custom background drawing function)

ofs28 = 1
ofs30 = dword (function)

When entry is drawn, this function is invoked, and no background is drawn
---------------------------
TYPE 4:  (entry frame)

ofs28 = 2
ofs30 = word (lo nibble: background and frame color
              hi nibble: inner frame color)
ofs32 = 0
---------------------------
TYPE 5:  (sprite 1 (background))

ofs28 = 3 (word)
ofs30 = word (sprite index)
ofs32 = 0 (word)
---------------------------
TYPE 6:  (invalid type)

ofs28 = 4
ofs30 = word
ofs32 = 0
Most likely artifact of code conversion -- something like this would cause
immediate program termination.
---------------------------
TYPE 7:  (function 2 -- custom foreground drawing function)

ofs34 = 1 (word)
ofs36 = 0 (word)
ofs38 = dword (function taking x and y parameters)
---------------------------
TYPE 8:  (string)

ofs34 = 2 (word)
ofs36 = word (string flags)
ofs38 = dword (text ptr)
---------------------------
TYPE 9:  (sprite 2 (foreground))

ofs34 = 3
ofs36 = 0
ofs38 = word (sprite index)
ofs40 = 0 (fill up to a dword)
---------------------------
TYPE 10: (string table)

ofs34 = 4
ofs36 = word (string flags)
ofs38 = dword (pointer to string table)
---------------------------
TYPE 11: (multi-line text)

ofs34 = 5
ofs36 = word (string flags)
ofs38 = dword (pointer to a byte - number of strings, followed by the strings,
               not pointers, strings are null-terminated)
---------------------------
TYPE 12: (number)

ofs34 = 6
ofs36 = word (string flags)
ofs38 = word (number to print)
ofs40 = 0 (filler to a dword)
---------------------------
TYPE 13: (on select function - fire only)

ofs46 = 32
ofs42 = dword (on select function)
---------------------------
TYPE 14: (on select function with mask)

ofs46 = word (mask)
ofs42 = dword (on select function)
---------------------------
TYPE 15: (before draw function)

ofs48 = dword (before draw function)
---------------------------
TYPE 16: (after draw function)

ofs52 = dword (after draw function)
---------------------------
TYPE 17: (left skip)

ofs16 = byte (next entry if this one is disabled or invisible)
ofs12 = byte (next direction of search, controls direction)
---------------------------
TYPE 18: (right skip)

ofs17 = byte
ofs13 = byte
---------------------------
TYPE 19: (up skip)

ofs18 = byte
ofs14 = byte
---------------------------
TYPE 20: (down skip)

ofs19 = byte
ofs15 = byte
---------------------------
TYPE 21: (color converted sprite)

ofs34 = 7
ofs36 = 0
ofs38 = dword (struct pointer, see description for entry offset 34, value 7)

======
Colors

Number of colors menu entry background can have is 16. They're converted via
menuItemColorTable table in DrawEmptyMenuItem(). The working menu palette
contains several groups of 16 color shades. Since menus can have only 16
colors, their color is converted to a start of a shade group, and the pixels
from 'fill.256' are added to it. Those pixels are designed to form a gradient
-- only question is in which color. 'fill.256' uses only 32 colors (indices
0..31), so the shade groups from working palette can have 32 colors at maximum.

Lower values are bottom left, higher top right. So the color is darker bottom
left and brighter top right.

Shade groups:

(lower index colors are darker)

32 ..63   - blue to black
64 ..95   - orange to light brown
96 ..127  - pinkish to brown
128..159  - light red to bright red (almost no difference)
160..191  - blue to purple
192..223  - light blue to blue
224..255  - yellow-green-black

Colors from 0 to 31 (for completeness):

16..31 - gradient from white to light brown

0  - nearly black     <  0,   0,  36>
1  - gray             <180, 180, 180>
2  - white            <252, 252, 252>
3  - true black       <  0,   0,   0>
4  - very dark brown  <108,  36,   0>
5  - light brown      <180,  72,   0>
6  - orange           <252, 108,   0>
7  - darker gray      <108, 108, 108>
8  - lighter gray     < 36,  36,  36>
9  - very dark gray   < 72,  72,  72>
10 - red              <252,   0,   0>
11 - blue             <  0,   0, 252>
12 - very dark color  <108,   0,  36>
13 - pale blue        <144, 144, 252>
14 - green            < 36, 144,   0>
15 - yellow           <252, 252,   0>

Having taken all this in consideration, the colors used in menu background
drawing are:
#    color:                         outer frame:
0  - pinkish/grayish to brown       [no background and frame]
1  -      -||-                      [gray]
2  -      -||-                      [-||-]
3  - blue to black                  [-||-]
4  - orange to brown                [orange]
5  -     -||-                       [ -||- ]
6  -     -||-                       [yellow]
7  - pinkish/grayish to brown       [gray]
8  - blue to black                  [-||-]
9  - pinkish/grayish to brown       [-||-]
10 - light red to bright red        [-||-]
11 - blue to purplish (pink)        [pale blue]
12 - orange to brown                [red]
13 - pale blue to blue              [gray]
14 - yellow-green-black             [-||-]
15 -       -||-                     [-||-]

From this follows that only 7 colors (shades) are effectively used for menu
item backgrounds.

If DrawEmptyMenuItem() is ordered to draw an entry of width or height 1 (single
line, most likely a frame), then fill.256 isn't used. Only thing added to a
color is g_deltaColor value.

--------------
DrawMenuItemOuterFrame(int color, int x, int y, int width, int height);

Parameters:
     D0 - color
     D1 - x
     D2 - y
     D3 - width
     D4 - height

frameColorsTable db 1, 9, 1, 9, 1, 9, 1, 9, 6, 9, 6, 4, 15, 4, 1, 9, 1, 9, 1,
                 db 3, 1, 9, 13, 8, 10, 9, 1, 9, 1, 9, 1, 9

- first convert the color with the help of the above table
- draw rectangle line by line around the entry:
    - vertical line x, y, width
    - horizontal line x, y, height
    - horizontal line x, y + height - 1, width
    - vertical line x + width - 1, y, height

Thickness of the frame is always one.
Only every other color is used from frameColorsTable (even), since it's indexed
by a word. Possible frame colors:

0  - gray
1  - gray
2  - gray
3  - gray
4  - orange
5  - orange
6  - yellow
7  - gray
8  - gray
9  - gray
10 - gray
11 - pale blue
12 - red
13 - gray
14 - gray
15 - gray

There are 5 unique colors.

Shining cursor
--------------

It is drawn by DrawSelected. The currentGameTick variable is incremented every
frame and is used as an index into the table containing the colors:
kColorTableShine dw 2, 2, 1, 1, 7, 7, 1, 1
The cursor is updated 70 times per second.


Play match menu
---------------
Drawing player names: player names start from entry number 13 and end with
entry 29 (16 entries + 1 invisible). These entries are only used for hit
testing (for user interaction), drawing is done manually in afterDraw function.
Entry 33 is player's name: background and text. Entry 31 is small x or check
sprite. Entry 37 is the same sprite again, but it with different criteria it
seems. Entry 34 is player's position (G, RB, D...). Entry 35 is that weird
3-letter string that comes after player name and position.


Menu stacking
-------------

Menus are displayed by ShowMenu procedure. It takes a menu in packed form as a
parameter in A6, unpacks it, and then calls MenuProc repeatedly until
g_exitMenu has been set to true.

Before calling PrepareMenu that will prepare given menu (or activate it so to
say) it pushes pointer to current menu's packed form and it's current entry to
the stack. This way information about previous menu is saved, and once the
current menu finishes and steps out of the menu loop, previous menu and it's
entry are popped off the stack and made into current menu. ShowMenu then
returns to the caller, as its main task (showing the menu given to it) has
been completed.

Returning to menu from the game is done differently: SWOS procedure handles
that by explicitly saving current menu and it's current entry.
