Porting viDOOM

This document is provided in case anyone wishes to port viDOOM to a new platform. Below is an indication of what OS dependent routines must be provided and what configuration needs to be done. It is requested that any ports are also released as Free Software.


Makefile configuration

makefile

Set the variable MAKEPLAT to the name of your platform, eg.

MAKEPLAT=OS

You will then need to create a matching file the make sub directory called OS.cfg. Also all the OS dependent C source should go into a sub directory defined in the following make config file by the PLATFORM variable. See the Files section for an overview of these files.

make config file

The following values need to be set in the file make/OS.cfg:

CC Set to the name of the C compiler.
LD The name of the linker.
PLATFORM The name of the D containing the OS dependent C sources. The idea of defineing tdefining here, rather than using the MAKEPLAT variable defined in the top level makefile, is so that different configurations can share sources. i.e. An X11 port may use the same code for all the unix platforms, but each machine may require slightly different configuration (library search paths for example).
EXE_EXT An extension to add to the executable name, e.g.
EXE_EXT=.EXE
OBJ_EXT The extension used in this OS to denote object files produced by the C compiler. Generally:
OBJ_EXT=.o
LIBS Any extra libraries to link in with viDOOM for the OS dependent routines.
EXRACF Any extra C flags required when compiling the sources. Use it to enable optimisation and to include any extra include paths required by this OS.
EXTRALF Any extra flags required when linking viDOOM. Use it to enable include any extra library paths required by this OS.
DIRSEP The directory separator character for this OS. The character must be included in quotes, e.g.
DIRSEP="/"
MATHLIB The options required to include the maths library when linking.
TRACEFORM This variable is a printf format string and the arguments to the format. This string is used to provide a tracing function used to track bugs in the editor. A simple, portable example is:
TRACEFORM="%s:%d",__FILE__,__LINE__

It can just be defined to an empty string if you are not compiling the debug version.

EXEFLAG The flag to provide to the linker to generate an executable. The flag is used in a rule something like this:
$(LD) $(EXTRALF) $(EXEFLAG) vidoom$(EXE_EXT) $(ALL+VIDOOM_OBJECTS)
OBJFLAG The flag to provide to the C compiler to generate an object file from the supplied C source. The flag is used in a rule something like this:
$(CC) $(EXTRACF) $(OBJFLAG) file.c
DEFINEFLAG The flag to provide to the C compiler with pre-processor definition from the command line. The flag is used in a rule something like this (note no space after the DEFINEFLAG - if there is a space between the switch and the argument put it in this variable definition):
$(CC) $(EXTRACF) $(OBJFLAG) $(DEFINEFLAG)MACRO=value file.c
INCFLAG The flag to provide to the C compiler with extra directories in which the pre-processor searches for include files. The flag is used in a rule something like this (note no space after the INCFLAG - if there is a space between the switch and the argument put it in this variable definition):
$(CC) $(EXTRACF) $(OBJFLAG) $(INCFLAG)include_dir file.c
MAKEINSTALL The command used to execute the install makefile as described in the installation script section. The command must define the INSTALLDIR variable for the makefile and invoke the first rule in the install makefile.

For instance, using a normal unix/GCC type make command, this would be:
make INSTALL_DIR='$(INSTALL_DIR)' -f install


INI File

If your port requires or wants configuration to be set at tun-time from the INI file, it is best to place it a system-dependent section called the same as the OS value you set MAKEPLAT to for this platform, e.g.

[OS]
degreelessness mode=off


Files

The following files are the minimum that must be provided by the platform:

make/OS.cfg The make config file. Described in the previous section.
main.c This goes in the platform directory and provides the startup code for the operating system.
gfx.c This goes in the platform directory and provides the low level graphics and input.
platgui.c This goes in the platform directory and provides the system dependent GUI.
file.c This goes in the platform directory and provides the portable part of the file system interface.
mem.c This goes in the platform directory and provides the memory handling.
runcmd.c This goes in the platform directory and provides the method for running external commands.
vstring.c This goes in the platform directory and provides functions for string comparisons.
install This goes in the platform directory and is the makefile invoked to install viDOOM.

Main entry point

main.c

The OS dependent code must provide it's own main (this is to allow for various non-standard environments where main() is not the standard entry point). The entry point must do any OS dependent initialisations then invoke the following entry point to start up viDOOM:

int viDOOM(int argv, char *argv[])

If the OS uses main() as an entry point the following example could be enough:

int main(int argc,char *argv[])
{
    return (viDOOM(argc,argv));
}

Graphics and input

gfx.c

This provides the low-level graphics access and interfaces to keyboard and mouse. The GFX object is expected to work on a weak, semi-event driven basis for keyboard/mouse access. The following are the basic assumptions about the GFX interface:

The following types are defined and used by the GFX object :

typedef void *GFX_IMAGE; 
This is an opaque type provided to allow the GFX object to provide whatever is required to reference a bitmap on the machine.
typedef struct
    {
    int           w;
    int           h;
    int           pal[256];
    unsigned char *data;
    } GFX_BITMAP; 
This type represents the bitmap objects that viDOOM defines. These bitmaps are converted into GFX_IMAGE prior to use. The fields are:
w - width of bitmap
h - height of bitmap
pal[256] - The palette used to define the bitmap. Each bitmap pixel is an index into this array of RGB values. Each entry is an integer, that when represented in hex would define the RGB triplet as 0xRRGGBB.
*data - A pointer to the data of the bitmap. This should be accessed using pointer arithmetic as *(data+(x)+(y*w))
typedef struct
    {
    int    type;
    int    shift;
    int    ctrl;
    int    alt;
    char   ascii;
    int    code;
    } GFXKey;
This defines an object for reporting key presses. The fields are:
type - The type of event. This field is just used to line up with the event union defined later on. Should just hold GFX_KEY_EVENT.
shift - TRUE if the Shift key is being pressed.
ctrl - TRUE if the Control key is being pressed.
alt - TRUE if the Alt key is being pressed.
ascii - The ASCII code of the character read. If the key is not an ASCII key (e.g. a function key) this field should be zero.
code - Holds the code for non-ASCII keys, e.g. GFX_F1. This field should be set to GFX_ASCII for key presses reported through the ascii field.
typedef struct GFXMouse
    {
    int    type;
    int    shift;
    int    ctrl;
    int    alt;
    int    x;
    int    y;
    int    b;
    } GFXMouse;
This defines the type for reporting mouse movements and button presses. The fields are:
type - The type of event. This field is just used to line up with the event union defined later on. Should just hold GFX_MOUSE_EVENT.
shift - TRUE if the Shift key is being pressed.
ctrl - TRUE if the Control key is being pressed.
alt - TRUE if the Alt key is being pressed.
x - The X co-ordinate of the mouse, relative to the top left of the display.
y - The Y co-ordinate of the mouse, relative to the top left of the display.
b - The currently pressed buttons. This should be made up of a bit mask composed from GFX_BUTLEFT, GFX_BUTMIDDLE and GFX_BUTRIGHT.
typedef union GFXEvent
    {
    int      type;
    GFXKey   key;
    GFXMouse mouse;
    } GFXEvent;
This defines the type for reporting events (a combination of both mouse movements or key presses). The fields are:
type - The type of event. This field is just used to decide which of the other two fields should be accessed to get the event information. This field must be GFX_MOUSE_EVENT or GFX_KEY_EVENT.
key - The GFXKey structure defining the event if this is a GFX_KEY_EVENT.
mouse - The GFXMouse structure defining the event if this is a GFX_MOUSE_EVENT.

The following interfaces must be supplied by the GFX object:

void GFX_init(void)

Initialises the GFX object. No other GFX interfaces are called prior to this, with the possible (though current not used) exception of GFX_exit().

void GFX_close(void)

Called when viDOOM is terminating. Note that other (none system dependent) processing may go on between calling this and then invoking exit() or return().

GFX_IMAGE GFX_create_image(GFX_BITMAP *bm)

Should create a GFX_IMAGE from the passed bitmap bm.

void GFX_destroy_image(GFX_IMAGE img)

Release the bitmap object pointed to by img.

void GFX_draw_image(GFX_IMAGE img, int x, int y)

Draws img with it's top-left co-ordinate represented by x,y. This function should implement any necessary clipping when drawing the bitmap.

void GFX_fill_screen(GFX_IMAGE img)

Should fill the screen with the image, scaled if necessary. Note that this call is just used for the main menu backdrop, so if it cannot be honoured no harm will be done.

void GFX_open(int width, int height)

Opens the display (or window or whatever) with the specified width and height. Note that failures in here should terminate the program.

void GFX_clear(int col)

This clears the display to the passed colour col.

void GFX_redraw(void)

This redraws the contents of the screen. All drawing operations should not update the actual screen till this is called (i.e. the display should be buffered).

void GFX_line(int x1, int y1, int x2, int y2, int col)

Draw a line from x1,y1 to x2,y2 in colour col.

void GFX_plot(int x, int y, int col)

Plot the point x,y in colour col.

void GFX_circle(int x, int y, int r, int col)
void GFX_fcircle(int x, int y, int r, int col)

Draw a circle centred on x,y with a radius r and in colour col. The fcircle version should draw a filled circle.

void GFX_rect(int x, int y, int w, int h, int col)
void GFX_frect(int x, int y, int w, int h, int col)

Draw a rectangle with one corner at x,y and the other corner at (x+w),(y+h) in colour col. Note that zero length and negative width and heights must be allowed. The frect version should draw a filled rectangle.

void GFX_set_XOR_mode(void)
void GFX_clear_XOR_mode(void)

This should set and clear XOR mode. Normally all GFX operations should set the pixels to the colour specified, but when XOR mode is enabled the pixel values should be XORed into place.

void GFX_print(int x, int y, int col, char *fmt, ...)

Print the printf style arguments (fmt and ...) with their top left corner at x,y in colour col. Note that text should rendered transparently.

int GFX_fh(void)
int GFX_fw(void)

Return the height (GFX_fh) and width (GFX_fw) of the fixed width font used for display purposes.

int GFX_mouse_buttons(void)

Returns the number of mouse buttons. This is just used as check on initialisation as viDOOM expects at least 2 mouse buttons.

int GFX_mouse(int *x, int *y)

Return the current point position in x and y. If any of the passed pointers are NULL that variable should be ignored.

void GFX_waitkey(GFXKEy *key)

Waits for a key to be pressed and returns the key press in key. If key is NULL simply wait for a key press.

int GFX_key(GFXKey *key)

Returns TRUE if a key has been pressed and returns the keypress in key. Returns FALSE if there is no outstanding keypresses, in which case the contents of key are undefined.

void GFX_bounce(void)

Waits for all keys and mouse buttons to be released. On a real event-driven system could be ignored, or flush any outstanding events.

void GFX_await_input(GFXEvent *ev)

Waits for either a keypress or a mouse button to be pressed and fills in ev accordingly.

void GFX_await_input_full(GFXEvent *ev)

Waits for either a keypress, a mouse button to be pressed or the mouse to be moved and fills in ev accordingly.

void GFX_exit(int code, char *fmt, ...)

This call should do any necessary tidying of the display (switching from graphics mode, closing windows, whatever) then display the printf style arguments (fmt and ...) and the exit with the passed return code.

void GFX_save_screen(char *path)

This call need not be supported. It just allows screen grabs to be captured when viDOOM is compiled with debug information. If supported it should just save a bitmap in the file pointed to by path.


Platform GUI

platgui.c

This provides access to the platform's GUI routines.

The following types are defined and used by the PLATGUI object :

typedef struct
    {
    char      *text;
    GFX_IMAGE img;
    int       client_index;
    } PLAT_IMG_PICKLIST;
This structure is used to define a picklist that has graphical images and client defined values attached to them. This is used for selection of textures, flats and sprites (things) in viDOOM.

The fields in the structure are:

text - This defines the text for a picklist entry. NULL marks the end of the list of picklist entries.
img - The GFX_IMAGE to associate with this entry. Can be NULL to indicate show no image.
client_index - The value returned if this item is selected.
typedef struct
    {
    char   *text;
    int    client_index;
    } PLAT_PICKLIST;
This structure is used to define a picklist that has client defined values attached to the entries.

The fields in the structure are:

text - This defines the text for a picklist entry. NULL marks the end of the list of picklist entries.
client_index - The value returned if this item is selected.
typedef struct PLATMENU
    {
    char  *text;
    int   client_index;
    struct PLATMENU *child;
    } PLAT_MENU;
This structure is used to define menu entries that have client defined values attached to them.

The fields in the structure are:

text - This defines the text for the menu entry. NULL marks the end of the list of menu entries.
client_index - The value returned if this item is selected. If the child field is not NULL, this field is ignored.
child - If not NULL points to a further array of PLAT_MENU objects defining a child menu.
typedef struct
    {
    char   *text;
    int    group;
    int    val;
    } PLAT_MULTI;
This structure is used to entries for the multi box call. A multi box is a dialog that holds a mixture of radio buttons and check boxes.

The fields in the structure are:

text - This defines the text for the multio box entry. NULL marks the end of the list of entries.
group - The group this entry belongs to. Group zero means that it is a check box and can be checked/unchecked independently of other entries. Entries in the same group should act as radio buttons in that group.
val - The state of the check box/radio button. TRUE means that it's set, FALSE means it's clear. These fields are updated on exit once the multo box is completed.
typedef struct
    {
    char   *text;
    int    client_index;
    } PLAT_RADIO;
This structure is used to define entries for a radio style picklist (i.e. where only one option can be chosen) that have client defined values attached to them.

The fields in the structure are:

text - This defines the text for the radio button. NULL marks the end of the list of radio button entries.
client_index - The value returned if this item is selected.
typedef struct
    {
    int     no;
    int     current;
    char    **text;
    } PLAT_DIAL_PL;
typedef struct
    {
    char   *text;
    int    type;
    union  /* Data */
        {
        int          i;
        char         s[PLAT_DIAL_MAXSTRLEN+1];
        double       d;
        PLAT_DIAL_PL pl;
        } data;
    } PLAT_DIALOG;
These structures are used to define entries for a simple dialog.

The fields in the PLAT_DIAL_PL structure are:

no - The number of elements pointed to by text.
current - The currently selected item in the picklist. This is updated on exit if the dialog is accepted.
text - The text for the picklist entries. This is treated as an array of character pointers.

The fields in the PLAT_DIALOG structure are:

text - This defines the text for this field in the dialog. NULL marks the end of the list of dialog entries.
type - The type of field this is. Possible values are PLAT_DIAL_STRING, PLAT_DIAL_INTEGER, PLAT_DIAL_DOUBLE and PLAT_DIAL_PICKLIST.
data.i - This is the field that is displayed and updated on exit if the type is PLAT_DIAL_INTEGER.
data.s - This is the field that is displayed and updated on exit if the type is PLAT_DIAL_STRING.
data.d - This is the field that is displayed and updated on exit if the type is PLAT_DIAL_DOUBLE.
data.pl - This is the structure that defines how a PLAT_DIAL_PICKLIST is displayed. The current field in this is updated if the selected picklist value changes.

Note that along with the types, the following predefined values are set (these are read from the INI file). Note that they should be considered to be unset until immediately prior to viDOOM's call to GUI_setscreen():

GUI_HI The brightest colour used to draw the 3D looking interface.
GUI_MID The medium colour used to draw the 3D looking interface.
GUI_LO The darkest colour used to draw the 3D looking interface.
GUI_TEXT The colour of text.
GUI_TEXTSHADOW The colour of the shadow behind text. This is only really used by viDOOM's own portable GUI routines.
GUI_BOLD The colour of bold text (used for titles).

Functions

The following interfaces are defined by the PLATGUI object. Note that all these calls are assumed to not destroy screen contents (ie. the screen should be restored after displaying the GUI object):

void GUI_setscreen(int width, int height)

Once the display has been opened with GFX_open() then this is called to inform the platform's GUI routines of the display size.

int GUI_yesno(char *question)

Display an alert with question in it and Yes and No buttons. Returns TRUE if Yes is pressed and FALSE if No is pressed.

int GUI_yesno_all(char *question)

Works like GUI_yesno(), but displays to extra options - "Yes to All" and "No to All". If either of these options are selected then further calls to this function should return TRUE/FALSE accordingly, until GUI_start_yesno__all() is called.

int GUI_start_yesno_all(void);

Resets GUI_yesno_all().

int GUI_alert(char *title, char *text, char *button_text)

Display an alert with a title of title, containing text as the message and with a single button labelled button_text. Note that text is split into multiple lines by the pipe (|) character.

int GUI_menu(char *title, int x, int y, PLAT_MENU menu[], int defval)

Displays a menu with title title at position x,y. The displayed items are taken from menu. The return is the client_index field from the selected menu item, or from the selected option in a child menu, or defval if the menu is cancelled.

char *GUI_fsel(char *title, char *default_path, char filter)

Allows a file to be selected. The file selector should have title for it's title and start selecting from the default_path. If filter is NULL then all files should be displayed, otherwise only files ending in filter.

The return is NULL if the selector is cancelled. Otherwise a pointer is returned containing the fully qualified path of the selected file. This pointer must be dynamically allocated and will be freed using FRelease().

int GUI_picklist(char *title, char *opts[])

Displays a picklist with title title. The options are taken from the array of character pointers opts. The return value is the index of the selected item in opts if selected, or -1 if the picklist is cancelled.

int GUI_client_picklist(char *title, PLAT_PICKLIST opts[], int defval)

Displays a picklist with title title. The text items to display are taken from opts. The return is the client_index field from the selected picklist item, or defval if the picklist is cancelled.

int GUI_image_picklist(char *title, PLAT_IMG_PICKLIST opts[], int defval)

Displays a picklist with title title. The text items and associated image to display are taken from opts. The return is the client_index field from the selected picklist item, or defval if the picklist is cancelled.

int GUI_radio_box(char *title, PLAT_RADIO opts[], int current, int defval)

Displays a dialog containing radio buttons with title title. The text to display is taken from opts. The selected object when the the radio box is first displayed is the option who's client_index field matches current (or the first item if there is no match). The return is the client_index field from the selected radio button, or defval if the radio box is cancelled.

int GUI_multi_box(char *title, PLAT_MULTI opts[])

Display a mutli-selection radio box. The items are described opts. The return is TRUE if the dialog is accepted, otherwise FALSE.

int GUI_dialog(char *title, int no, PLAT_DIALOG dial[])

Displays a dialog with the title title. The fields for the dialog are extracted from dial, for which there is expected to be no elements. The return is TRUE if the dialog is accepted, or FALSE if it is cancelled. On being cancelled the contents of the data union within the dial elements is undefined.

void GUI_file_view(char *title, char *file)

Displays the contents of a text file, allowing the user to move around and view the file. What form this takes is no concern at all to viDOOM.

If is assumed this can view both DOS format (lines terminated with CR and LF) and UNIX format text files (lines terminated with LF).

char *GUI_text_edit(char *title, char *text)

Allows simple text editting. The form this display takes is of noconcern to viDOOM (if applicable it would be more than OK to start an external text editor). text is a pointer to the original text, which is one long string with line breaks denoted by the '\n' character.

The return is a newly allocated copy of the edited text is the text is OKed, or NULL if the text is cancelled. In either case, the original string pointed to by text should be as it was.


File interface

file.c

This provides access to various file system functions and also provides some filename manipulation routines. The following interfaces should be provided:

char *Pwd(void)

This call should return the current working directory. The return should be static.

void Cd(char *path)

This call should change the current working directory to path.

char *Dirname(char *path)

This call should return the directory part of path if any. The return should be static.

char *Basename(char *path)

This call should return the filename part of path. The return should be static, or a pointer into the path parameter.

int FileExists(char *path)

This call should return TRUE if the file pointed to by path exists.

int FilenamesEqual(char *path1, char *path2)

This call should return TRUE if the file pointed to by path1 and path2 are the same file. At it's most basic (e.g. like in the DOS port) it can simply makes sure that directory separators are in the same form and then does strcasecmp() on the paths.


Memory allocation

mem.c

This provides memory allocation. While memory allocation can generally be done portably using malloc() providing this library just covers for any possible OS dependent twist. Also these routines are expected to handle errors internally. In all the interfaces file and line parameters are included so that errors can be reported more accurately.

The following interfaces should be provided:

void *FGrab (char *file, int line, int len)

This call should allocate len bytes and return a pointer to it. A len of zero is valid. Memory should be initialised to zero. Failure to allocate the memory should terminate the program.

void *FReGrab (char *file, int line, void *ptr, int len)

This call should re-allocate the memory pointed to by ptr and return a new memory area of len bytes. The original data pointed to by ptr should be copied to the new memory area. Failure to allocate the memory should terminate the program.

char *FStrdup (char *file, int line, char *str)

This call should allocate enough bytes to copy the nul terminated str to it. The returned pointer should point to the new copy of str. Failure to allocate the memory should terminate the program.

void *FCopy (char *file, int line, void *ptr, int len)

This call should allocate len bytes and copy len bytes from ptr into the new area. The newly allocated memory should be returned. Failure to allocate the memory should terminate the program.

void FRelease (char *file, int line, void *ptr)

This call should release the memory pointed to by ptr, which will have been allocated by FGrab, FReGrab, FStrdup or FCopy.


External command execution

runcmd.c

Provides a mechanism to run an external command. The following interfaces should be provided:

int RunCommand(char *argv[], char *path)

Run a command. The output from the command (if there is any) should NOT disturb the screen contents. The call should return TRUE if the call succeeds, FALSE otherwise.

The argv list is an array of pointers to various sections of the command and it's arguments, terminated with a NULL pointer. Note that arguments may contain more than one argument in each line - the actual command is described simply by concatenating all the pointers together, eg.

argv[0]="bsp"
argv[1]="file.wad"
argv[2]="-o file.wad"
argv[3]=NULL

The path argument is a place to copy the path to a file where the output from the command has been stored. If this is not supported then the empty string should be assigned to it. viDOOM will remove() the file after it has read it.


Portable String routines

vstring.c

Provides common string functions that are not actually part of the ANSI standard. While these can easily be portably written, they are provided as functions in case local implementations supply them (which will probably more effecient):

int StrCaseCmp(char *a, char *b)

Performs in exactly the same way as the ANSI strcmp() function, save for the fact that the case of the strings being compared is ignored.

int StrNCaseCmp(char *a, char *b)

Performs in exactly the same way as the ANSI strncmp() function, save for the fact that the case of the strings being compared is ignored.


Installation script

Each platform should provide a makefile called install. This is invoked from the top level makefile like this:

cd $(PLATFORM) ; $(MAKEINSTALL)

Note that the install makefile will be invoked with the PLATFORM directory as the current working directory.

The following files must be copied (where $SRC represents the source build directory and $INSTALLDIR the install directory):

$SRC/vidoom $INSTALLDIR/vidoom

Note that this file may have a system specific extension (e.g. .EXE in DOS)

$SRC/LICENSE $INSTALLDIR/LICENSE

The GNU GPL should be copied into the installation directory so that binary distributions can be easily generated with the license included and so that the LICENSE can be viewed from viDOOM's main menu.

$SRC/base.ini $INSTALLDIR/vidoom.ini
$SRC/*.cfg $INSTALLDIR/*.cfg
$SRC/doc/*.htm $INSTALLDIR/doc/*.htm
$SRC/doc/*.gif $INSTALLDIR/doc/*.gif

Note that, obviously, any OS specific files should also be copied.


Documentation

If you release a port of viDOOM to any platform please update doc/bugs.htm with a contact address for problems on that platform. Also include a link to a system specific HTML document detailing how the GUI works and any know bugs, from doc/sys.htm.

For an example look at the DJGPP documentation. As you can see, it doesn't have to be too big.


Back to index

$Id$