Scroll to navigation

GRAPHICS(3) GRAPHICS(3)

NAME

graphics.h - compatibility header for SDL_bgi

SYNOPSIS

#include <graphics.h>

DESCRIPTION

This header file must be included by C or C++ programs that use the SDL_bgi library. graphics.h includes SDL_bgi.h, usually installed in /usr/include/SDL2, which contains all definitions and function prototypes.

SDL bgi is a graphics library (GRAPHICS.H) for C, C++, WebAssembly, and Python. It’s based on SDL2 and it’s portable on many platforms.

This library strictly emulates BGI functions, making it possible to compile SDL2 versions of programs written for Turbo C/Borland C++. ARGB colours, vector fonts, mouse support, and multiple windows are also implemented; further, native SDL2 functions may be used in SDL_bgi programs.

Python programs can use SDL_bgi by means of module sdl_bgi.py.

VARIABLES

extern SDL_Window   *bgi_window;
extern SDL_Renderer *bgi_renderer;
extern SDL_Texture  *bgi_texture;
extern Uint32        PALETTE_SIZE;
    

DEFINITIONS

 

Font definitions

 

enum {

DEFAULT_FONT, // 8x8 bitmap
TRIPLEX_FONT, // trip.h
SMALL_FONT, // litt.h
SANS_SERIF_FONT, // sans.h
GOTHIC_FONT, // goth.h
SCRIPT_FONT, // scri.h
SIMPLEX_FONT, // simp.h
TRIPLEX_SCR_FONT, // tscr.h
COMPLEX_FONT, // lcom.h
EUROPEAN_FONT, // euro.h
BOLD_FONT, // bold.h
LAST_SPEC_FONT }; enum { HORIZ_DIR, VERT_DIR }; #define USER_CHAR_SIZE 0 enum {
LEFT_TEXT, CENTER_TEXT, RIGHT_TEXT,
BOTTOM_TEXT = 0, TOP_TEXT = 2 };

Colour definitions

 

enum {

BLACK = 0, EGA_BLACK = 0,
BLUE = 1, EGA_BLUE = 1,
GREEN = 2, CGA_GREEN = 2, EGA_GREEN = 2,
CYAN = 3, CGA_CYAN = 3, EGA_CYAN = 3,
RED = 4, CGA_RED = 4, EGA_RED = 4,
MAGENTA = 5, CGA_MAGENTA = 5, EGA_MAGENTA = 5,
BROWN = 6, CGA_BROWN = 6, EGA_BROWN = 6,
LIGHTGRAY = 7, CGA_LIGHTGRAY = 7, EGA_LIGHTGRAY = 7,
DARKGRAY = 8, EGA_DARKGRAY = 8,
LIGHTBLUE = 9, EGA_LIGHTBLUE = 9,
LIGHTGREEN = 10, CGA_LIGHTGREEN = 10, EGA_LIGHTGREEN = 10,
LIGHTCYAN = 11, CGA_LIGHTCYAN = 11, EGA_LIGHTCYAN = 11,
LIGHTRED = 12, CGA_LIGHTRED = 12, EGA_LIGHTRED = 12,
LIGHTMAGENTA = 13, CGA_LIGHTMAGENTA = 13, EGA_LIGHTMAGENTA = 13,
YELLOW = 14, CGA_YELLOW = 14, EGA_YELLOW = 14,
WHITE = 15, CGA_WHITE = 15, EGA_WHITE = 15,
MAXCOLORS = 15 }; enum {
ARGB_FG_COL = 16,
ARGB_BG_COL = 17,
ARGB_FILL_COL = 18,
ARGB_TMP_COL = 19,
TMP_COLORS = 4 };

Drawing and filling styles

 

enum { NORM_WIDTH = 1, THICK_WIDTH = 3 };
enum { SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE };
enum { COPY_PUT, XOR_PUT, OR_PUT, AND_PUT, NOT_PUT };
enum {

EMPTY_FILL, // fills area in background color
SOLID_FILL, // fills area in solid fill color
LINE_FILL, // --- fill
LTSLASH_FILL, // /// fill
SLASH_FILL, // /// fill with thick lines
BKSLASH_FILL, // \\\ fill with thick lines
LTBKSLASH_FILL, // \\\ fill
HATCH_FILL, // light hatch fill
XHATCH_FILL, // heavy cross hatch fill
INTERLEAVE_FILL, // interleaving line fill
WIDE_DOT_FILL, // Widely spaced dot fill
CLOSE_DOT_FILL, // Closely spaced dot fill
USER_FILL // user defined fill };

Mouse buttons

 

#define WM_LBUTTONDOWN  SDL_BUTTON_LEFT
#define WM_MBUTTONDOWN  SDL_BUTTON_MIDDLE
#define WM_RBUTTONDOWN  SDL_BUTTON_RIGHT
#define WM_WHEEL        SDL_MOUSEWHEEL
#define WM_WHEELUP      SDL_USEREVENT
#define WM_WHEELDOWN    SDL_USEREVENT + 1
#define WM_MOUSEMOVE    SDL_MOUSEMOTION
    

Key codes

 

#define KEY_HOME        SDLK_HOME
#define KEY_LEFT        SDLK_LEFT
#define KEY_UP          SDLK_UP
#define KEY_RIGHT       SDLK_RIGHT
#define KEY_DOWN        SDLK_DOWN
#define KEY_PGUP        SDLK_PAGEUP
#define KEY_PGDN        SDLK_PAGEDOWN
#define KEY_END         SDLK_END
#define KEY_INSERT      SDLK_INSERT
#define KEY_DELETE      SDLK_DELETE
#define KEY_F1          SDLK_F1
#define KEY_F2          SDLK_F2
#define KEY_F3          SDLK_F3
#define KEY_F4          SDLK_F4
#define KEY_F5          SDLK_F5
#define KEY_F6          SDLK_F6
#define KEY_F7          SDLK_F7
#define KEY_F8          SDLK_F8
#define KEY_F9          SDLK_F9
#define KEY_F10         SDLK_F10
#define KEY_F11         SDLK_F11
#define KEY_F12         SDLK_F12
#define KEY_CAPSLOCK    SDLK_CAPSLOCK
#define KEY_LEFT_CTRL   SDLK_LCTRL
#define KEY_RIGHT_CTRL  SDLK_RCTRL
#define KEY_LEFT_SHIFT  SDLK_LSHIFT
#define KEY_RIGHT_SHIFT SDLK_RSHIFT
#define KEY_LEFT_ALT    SDLK_LALT
#define KEY_RIGHT_ALT   SDLK_RALT
#define KEY_ALT_GR      SDLK_MODE
#define KEY_LGUI        SDLK_LGUI
#define KEY_RGUI        SDLK_RGUI
#define KEY_MENU        SDLK_MENU
#define KEY_TAB         SDLK_TAB
#define KEY_BS          SDLK_BACKSPACE
#define KEY_RET         SDLK_RETURN
#define KEY_PAUSE       SDLK_PAUSE
#define KEY_SCR_LOCK    SDLK_SCROLLOCK
#define KEY_ESC         SDLK_ESCAPE
#define QUIT            SDL_QUIT
    

Graphics modes

 

enum {

DETECT = -1,
SDL = 0,
// all modes @ 320x200
SDL_320x200 = 1, SDL_CGALO = 1, CGA = 1, CGAC0 = 1, CGAC1 = 1,
CGAC2 = 1, CGAC3 = 1, MCGAC0 = 1, MCGAC1 = 1, MCGAC2 = 1,
MCGAC3 = 1, ATT400C0 = 1, ATT400C1 = 1, ATT400C2 = 1, ATT400C3 = 1,
// all modes @ 640x200
SDL_640x200 = 2, SDL_CGAHI = 2, CGAHI = 2, MCGAMED = 2,
EGALO = 2, EGA64LO = 2,
// all modes @ 640x350
SDL_640x350 = 3, SDL_EGA = 3, EGA = 3, EGAHI = 3,
EGA64HI = 3, EGAMONOHI = 3,
// all modes @ 640x480
SDL_640x480 = 4, SDL_VGA = 4, VGA = 4, MCGAHI = 4, VGAHI = 4,
IBM8514LO = 4,
// all modes @ 720x348
SDL_720x348 = 5, SDL_HERC = 5,
// all modes @ 720x350
SDL_720x350 = 6, SDL_PC3270 = 6, HERCMONOHI = 6,
// all modes @ 800x600
SDL_800x600 = 7, SDL_SVGALO = 7, SVGA = 7,
// all modes @ 1024x768
SDL_1024x768 = 8, SDL_SVGAMED1 = 8,
// all modes @ 1152x900
SDL_1152x900 = 9, SDL_SVGAMED2 = 9,
// all modes @ 1280x1024
SDL_1280x1024 = 10, SDL_SVGAHI = 10,
// all modes @ 1366x768
SDL_1366x768 = 11, SDL_WXGA = 11,
// other
SDL_USER = 12, SDL_FULLSCREEN = 13 };

Error messages

 

enum graphics_errors {

grOk = 0,
grNoInitGraph = -1,
grNotDetected = -2,
grFileNotFound = -3,
grInvalidDriver = -4,
grNoLoadMem = -5,
grNoScanMem = -6,
grNoFloodMem = -7,
grFontNotFound = -8,
grNoFontMem = -9,
grInvalidMode = -10,
grError = -11,
grIOerror = -12,
grInvalidFont = -13,
grInvalidFontNum = -14,
grInvalidVersion = -18 };

Struct definitions

 

struct arccoordstype {

int x;
int y;
int xstart;
int ystart;
int xend;
int yend; }; struct date {
int da_year;
int da_day;
int da_mon; }; struct fillsettingstype {
int pattern;
int color; }; struct linesettingstype {
int linestyle;
unsigned int upattern;
int thickness; }; struct palettetype {
unsigned char size;
signed char colors[MAXCOLORS + 1]; }; // SDL_bgi extension struct rgbpalettetype {
Uint32 size;
Uint32 *colors; }; struct textsettingstype {
int font;
int direction;
int charsize;
int horiz;
int vert; }; struct viewporttype {
int left;
int top;
int right;
int bottom;
int clip; };

FUNCTIONS

 

Standard BGI functions

 

Draws a circular arc centered at (x, y), with a radius given by radius, traveling counterclockwise from stangle to endangle.
Draws a filled-in rectangle (bar), using the current fill colour and fill pattern.
Draws a three-dimensional, filled-in rectangle (bar), using the current fill colour and fill pattern.
Draws a circle of the given radius at (x, y).
Clears the graphics screen, filling it with the current background colour.
Clears the viewport, filling it with the current background colour.
Closes the graphics system.
Waits for millisec milliseconds.
Detects the default graphics driver and graphics mode to use; SDL and SDL_FULLSCREEN, respectively.
Draws a polygon of numpoints vertices.
Draws an elliptical arc centered at (x, y), with axes given by xradius and yradius, traveling from stangle to endangle.
Draws an elliptical arc centered at (x, y), with axes given by xradius and yradius, and fills it using the current fill colour and fill pattern.
Draws a polygon of numpoints vertices and fills it using the current fill colour.
Fills an enclosed area, starting from point (x, y) bounded by the border colour. The area is filled using the current fill colour.
Returns the active page number.
Gets the coordinates of the last call to arc(), filling the arccoords structure.
Retrieves the current graphics mode’s aspect ratio. xasp and yasp are always 10000 (i.e. pixels are square).
Returns the current background colour.
Waits for a key and returns its ASCII or key code.
Returns the current drawing (foreground) colour.
Returns the default palette definition structure.
Returns a pointer to a string containing the name of the current graphics driver.
Copies the user-defined fill pattern, as set by setfillpattern(), into the 8-byte area pointed to by pattern.
Fills the fillsettingstype structure pointed to by fillinfo with information about the current fill pattern and fill colour.
Returns the current graphics mode.
Copies a bit image of the specified region into the memory pointed to by bitmap.
Fills the linesettingstype structure pointed to by lineinfo with information about the current line style, pattern, and thickness.
Returns the maximum colour value available (MAXCOLORS). If ARGB colours are being used, it returns PALETTE_SIZE (default is 4096).
Returns the maximum mode number for the current driver. In SDL_bgi, the default is SDL_FULLSCREEN.
Returns the maximum x screen coordinate.
Returns the maximum y screen coordinate.
Returns a pointer to a string containing the name of the specified graphics mode.
Returns the range of valid graphics modes. The graphdriver parameter is ignored.
Fills the palettetype structure pointed to by palette with information about the current palette size and colours.
Returns the size of the palette (MAXCOLORS + 1 in BGI mode, or MAXRGBCOLORS + 1 in ARGB mode).
Returns the colour of the pixel located at (x, y).
Fills the textsettingstype structure pointed to by texttypeinfo with information about the current text font, direction, size, and justification.
Fills the viewporttype structure pointed to by viewport with information about the current viewport.
Returns the visual page number.
Returns the current CP x coordinate, relative to the viewport.
Returns the current CP y coordinate, relative to the viewport.
Resets all graphics settings to their default values.
Returns a pointer to the error message string associated with errorcode, returned by graphresult().
Returns the error code for the last unsuccessful graphics operation and resets the error level to grOk.
Returns the size in bytes of the memory area required to store a bit image.
Initializes the graphics system. Use initgraph (NULL, NULL, ""); for default settings.
Unimplemented; not used by SDL_bgi.
Loads and installs a CHR font from disk. The function returns an integer to be used as first argument in settextstyle().
Returns 1 when a key is pressed, excluding special keys (Ctrl, Shift, etc.)
Returns the last key that was detected by kbhit().
Draws a line between two specified points.
Draws a line from the CP to a point that is (dx, dy) pixels from the CP. The CP is then advanced by (dx, dy).
Draws a line from the CP to (x, y), then moves the CP to (dx, dy).
Moves the CP by (dx, dy) pixels.
Moves the CP to the position (x, y), relative to the viewport.
Outputs textstring at the CP.
Outputs textstring at (x, y).
Draws and fills a pie slice centered at (x, y), with a radius given by radius, traveling from stangle to endangle.
Puts the bit image pointed to by bitmap onto the screen, with the upper left corner of the image placed at (left, top). op specifies the drawing mode (COPY_PUT, etc).
Plots a pixel at (x, y) using color.
Returns an integer random number between 0 and range - 1.
Draws a rectangle delimited by (left, top) and (right, bottom).
Unimplemented; not used by SDL_bgi.
Unimplemented; not used by SDL_bgi.
Hides the graphics window.
Draws and fills an elliptical pie slice centered at (x, y), horizontal and vertical radii given by xradius and yradius, traveling from stangle to endangle.
Makes page the active page for all subsequent graphics output.
Sets the current palette to the values given in palette.
Changes the default aspect ratio of the graphics.
Sets the current background colour.
Sets the current drawing colour.
Sets a user-defined fill pattern.
Sets the fill pattern and fill colour; e.g. SOLID_FILL, RED.
Unimplemented; not used by SDL_bgi.
Shows the window that was hidden by restorecrtmode().
Sets the line width and style. Default values: SOLID_LINE, 0, NORMAL_WIDTH.
Changes the standard palette colornum to color, which can also be specified using the COLOR() function.
Sets text justification. Text output will be justified around the CP horizontally and vertically.
Sets the text style. Default values: DEFAULT_FONT, HORIZ_DIR, 1.
Lets the user change the character width and height.
Sets the current viewport for graphics output.
Sets the visual graphics page number to page.
Sets the writing mode for line drawing (COPY_PUT, etc.)
Returns the height in pixels textstring.
Returns the width in pixels of textstring.

SDL_bgi extensions

 

Returns the alpha (transparency) component of color in the ARGB palette.
Returns the blue component of color in the ARGB palette.
Clears mouse event of kind type.
Closes the window identified by id.
Can be used as an argument for putpixel(), setalpha(), setcolor(), setbkcolor(), setfillpattern(), and setfillstyle() to set a colour specifying its ARGB components.
Can be used as an argument for putpixel(), setalpha(), setcolor(), setbkcolor(), setfillpattern(), and setfillstyle() to set a colour as ARGB integer.
Returns a string containing the colour name.
Can be used to compose a 32 bit colour with r g b components.
Copies surface to the rectangle defined by x1, y1, x2, y2.
Returns 1 if the last mouse click was a double click.
Waits for msec milliseconds. This function returns 1 if an event occurs during the delay, otherwise it returns 0.
Returns 1 if an event (mouse click or movement, key press, or QUIT) has occurred.
Returns the type of the last event: SDL_KEYDOWN, SDL_MOUSEMOTION, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_QUIT
Plots a point at (x, y) using the current drawing colour.
Copies the contents of the active window to buffer.
Waits for a mouse button click and returns its code.
Returns the id of the current window.
Waits for a keypress, mouse click, or SDL_QUIT event, and returns the code of the key, mouse button, or SDL_QUIT.
Waits for the left mouse button to be clicked and released.
Copies the y-th screen line to linebuffer.
Returns the maximum possible height for a new window (actual screen height in pixels).
Returns the maximum possible width for a new window (actual screen width in pixels).
Waits for the middle mouse button to be clicked and released.
Sets the x, y coordinates of the last btn button click expected by ismouseclick().
Fills the rgbpalettetype structure pointed to by palette with information about the current ARGB palette’s size and colours.
Returns the current size of the ARGB palette.
Waits for the right mouse button to be clicked and released.
Reports the screen width and height in width and height, regardless of current window dimensions.
Equivalent to getmaxy() (WinBGIm compatibility).
Equivalent to getmaxx() (WinBGIm compatibility).
Returns the green component of color in the ARGB palette.
Initialises the BGI palette to the standard 16 colours.
Initializes the graphics system, opening a width x height window. Set width or height equal to 0 for fullscreen.
As above, but it also sets the window title to title.
Returns 1 if the current drawing colour is a standard BGI colour (that is, not ARGB). The color argument is actually redundant.
Returns 1 if the btn mouse button was clicked.
Returns 1 if the current drawing colour is ARGB. The color argument is actually redundant.
Waits for msec milliseconds. This function returns 1 if a key is pressed during the delay, otherwise it returns 0.
Returns the code of the mouse button that is being clicked, or SDL_MOUSEMOTION, or 0.
Returns the X coordinate of the last mouse click.
Returns the Y coordinate of the last mouse click.
Copies buffer to the current window.
Copies linebuffer to the y coordinate in the current window.
Same as fputpixel().
Reads a .bmp file and displays it immediately at (x1, y1).
Returns the red component of color in the ARGB palette.
Updates the screen contents, i.e. displays all graphics.
Resets title and position of window id to title, (x, y).
Resizes the ARGB palette to newsize; returns 0 if successful, 1 otherwise.
Can be used as an argument for putpixel(), setalpha(), setcolor(), setbkcolor(), setfillpattern(), and setfillstyle() to set a colour from the ARGB palette color entry.
Triggers auto mode, i.e. refresh() is performed automatically.
Triggers fast mode, i.e. refresh() is needed to display graphics.
Triggers slow mode i.e. refresh() is not needed to display graphics.
Sets the current ARGB palette to the values given in palette.
Sets alpha transparency for colour col to alpha (0-255); 0 means full transparecy, 255 full opacity.
Sets the current background colour to the color entry in the ARGB palette.
Sets the blend mode to be used with screen refresh; either SDL_BLENDMODE_NONE or SDL_BLENDMODE_BLEND.
Sets the current active window to id.
Sets the current drawing colour to the color entry in the ARGB palette.
Sets the colornum entry in the ARGB palette specifying the r, g, and b components.
Sets the window title title, the initial position to (x, y), and SDL2 flags OR’ed together.
Sets the title of the window identified by id.
Opens an error message box with the specified message.
Opens an information message box with the specified message.
Swaps the current active and the current visual graphics pages.
Writes a .bmp file from the screen rectangle defined by left, top, right, bottom.
Returns 1 when any key is pressed, including special keys (Ctrl, Shift, etc.)

ENVIRONMENT

Native code and Python:

SDL_BGI_RES: when set to VGA, default resolution will be 640 x 480 instead of default 800 x 600.

SDL_BGI_RATE: when set to auto, automatic screen refresh will be performed.

SDL_BGI_PALETTE: when set to BGI, the first 16 colours will use the same RGB values as Turbo C 2.01.

Compiling programs with emscripten, variables SDL_BGI_RES and SDL_BGI_PALETTE can be replaced by files by the same name, possibly containing the strings VGA and BGI.

When run in a browser, the user can provide “environment files” in the assets directory. These text files have the same name as the corresponding environment variable, and contain the desired value (e.g. VGA for assets/SDL_BGI_RES).

KNOWN BUGS

Visualisation problems on NVIDIA GK208BM (GeForce 920M) with nvidia-driver-* on GNU/Linux and automatic refresh. As far as I can say, this is an NVIDIA driver problem.
On Raspios 10 ARM, the letter `p' is drawn incorrectly in SCRIPT_FONT. This bug does not affect Raspios 10 i386.

Please report bugs if you find any.

EXAMPLE

#include <graphics.h>
int main (int argc, char *argv[])
{

int i, gd = DETECT, gm;
initgraph (&gd, &gm, "");
setbkcolor (BLACK);
cleardevice ();
outtextxy (0, 0, "Drawing 1000 lines...");
for (i = 0; i < 1000; i++) {
setcolor (1 + random (15));
line (random(getmaxx()), random(getmaxy()),
random (getmaxx()), random(getmaxy()) );
}
getch ();
closegraph ();
return 0; }

To compile this program on GNU/Linux, macOS, or Raspios:

$ gcc -o program program.c -lSDL_bgi -lSDL2
    

To compile this program on MSYS2 + mingw-w64:

$ gcc -o program.exe program.c -lmingw32 -L/ucrt64/bin \

-lSDL_bgi -lSDL2main -lSDL2 # -mwindows

To compile this program to WebAssembly using emcc:

$ emcc --emrun -o program.html program.c -lSDL_bgi \

-std=gnu99 -O2 -Wall -lm \
-s USE_SDL=2 -s ALLOW_MEMORY_GROWTH=1 -s ASYNCIFY -s SINGLE_FILE

To initialise the graphics, you may try one of the following methods:

// open a 400x300 window
initwindow (400, 300);
// open a 400x300 window, setting position, title, and flags
setwinoptions ("SDL_bgi window", 100, 100, SDL_WINDOW_BORDERLESS);
initwindow (400, 300);
// go fullscreen
initwindow (0, 0);
// go fullscreen or VGA if SDL_BGI_RES is set
gd = DETECT;
gm = getmaxmode ();
initgraph(&gd, &gm, "");
// go fullscreen with non native resolution
setwinoptions ("", -1, -1, SDL_WINDOW_FULLSCREEN);
initwindow (800, 600);
    

AUTHORS

Main library: Guido Gonzato, PhD guido dot gonzato at gmail dot com

Automatic refresh patch, CHR font support: Marco Diego Aurélio Mesquita, marcodiegomesquita at gmail dot com

LICENSE

SDL_bgi is released under the ZLib License. opyright (c) 2014-2024 Guido Gonzato, PhD

This software is provided `as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1.
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2.
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3.
This notice may not be removed or altered from any source distribution.

AUTHORS

\.

April 2024 SDL_bgi 3.0.2