A modern Rust port of the classic Borland Graphics Interface (BGI) with extensible backends.
- Full BGI API compatibility with modern Rust ergonomics
- Multiple graphics backends: MiniFB (visual), Pixel Buffer (headless/testing), PGM output
- Multiple graphics drivers: CGA, MCGA, EGA, VGA, IBM 8514, Hercules, AT&T 400, PC 3270
- Double-buffering support with active/visual page management
- Keyboard and mouse input handling
- 16-color EGA/VGA palette with RGB color support
- Line styles and fill patterns with custom bit patterns
- Viewport clipping and coordinate management
- Zero-cost abstractions with
BatchDrawerand compile-time optimizations - Modern Rust: Edition 2024, MSRV 1.90
use bgi::*;
fn main() {
let mut driver = DETECT;
let mut mode = VGAHI;
initgraph(&mut driver, &mut mode, "");
if graphresult() != GraphResult::Ok {
eprintln!("Graphics initialization failed!");
return;
}
setcolor(Color::RED);
circle(320, 240, 50);
outtextxy(250, 300, "Press any key to exit");
getch();
closegraph();
}Run with visual output:
cargo run --example simple --features visual-backend# Basic drawing demo
cargo run --example simple --features visual-backend
# Mandelbrot set visualization
cargo run --example mandelbrot --features visual-backend
# Interactive demonstration with mouse/keyboard
cargo run --example interactive_demo --features visual-backend
# BGI compatibility test
cargo run --example compatibility --features visual-backend
# Batch rendering (optimized)
cargo run --example mandelbrot_batched --features visual-backendinitgraph()/closegraph()- Initialize and close graphics modedetectgraph()- Auto-detect best graphics driver/modegetgraphmode()/setgraphmode()- Get/set current modegraphresult()/grapherrormsg()- Error handling
line(),lineto(),linerel()- Line drawingcircle()- Circle with Bresenham algorithmarc(),ellipse()- Arc and ellipse drawingrectangle()- Rectangle outlineputpixel(),getpixel()- Pixel operationsfillpoly()- Polygon fill (outline only)
setcolor(),getcolor()- Drawing colorsetbkcolor(),getbkcolor()- Background colorsetpalette(),getpalette()- Palette managementsetrgbpalette()- RGB palette entry- 16 predefined colors:
Color::BLACK,Color::WHITE,Color::RED, etc.
setlinestyle()- Solid, dashed, dotted, dot-dashed, user-defined patternsgetlinesettings()- Get current line stylesetfillstyle()- Fill pattern and colorsetfillpattern()- Custom 8x8 fill patternsetwritemode()- COPY_PUT, XOR_PUT, OR_PUT, AND_PUT, NOT_PUT
setviewport()/getviewport()- Clipping regionclearviewport()/cleardevice()- Screen clearinggetmaxx(),getmaxy()- Screen dimensionsmoveto(),moverel(),getx(),gety()- Cursor control
outtextxy()- Display text at position (stub)settextstyle(),settextjustify()- Text configurationtextwidth(),textheight()- Text measurement
getch(),kbhit()- Keyboard inputgetmouse(),ismouseclick()- Mouse statemousex(),mousey()- Mouse positionsetmouse(),clearmouseclick()- Mouse control
setactivepage()/getactivepage()- Draw to off-screen pagesetvisualpage()/getvisualpage()- Display page
delay()- Millisecond delayis_headless()- Check if running without displayset_batch_mode(),refresh()- Batch rendering optimization
BatchDrawer- Batch multiple draw operationsDrawingPool- Pre-allocated buffers for repeated operationsPatternShape<N>- Compile-time optimized regular polygons
| Driver | Mode | Resolution | Colors |
|---|---|---|---|
| CGA | CGAC0-CGAC3 | 320x200 | 4 |
| CGA | CGAHI | 640x200 | 2 |
| MCGA | MCGAC0-MCGAC3 | 320x200 | 256 |
| MCGA | MCGAMED | 640x200 | 256 |
| MCGA | MCGAHI | 640x480 | 256 |
| EGA | EGALO | 640x200 | 16 |
| EGA | EGAHI | 640x350 | 16 |
| VGA | VGALO | 640x200 | 16 |
| VGA | VGAMED | 640x350 | 16 |
| VGA | VGAHI | 640x480 | 16 |
Cross-platform windowing using minifb. Provides real-time visual output with keyboard and mouse support.
Headless backend for testing and offline rendering. Stores pixels in memory without display output.
Outputs to Portable GrayMap format files for image export.
Complete:
- Graphics initialization and mode management
- All drawing primitives (line, circle, arc, ellipse, rectangle)
- Color and palette management
- Line styles with custom patterns
- Viewports and coordinate systems
- Keyboard and mouse input
- Page flipping / double buffering
Partial:
- Text rendering (outtextxy is a stub)
- Fill patterns (outline only for polygons)
- Image operations (getimage/putimage exist but limited)
Not Yet Implemented:
- Flood fill (
floodfill) - Filled shapes (
bar,fillellipse,sector,pieslice) - Font file loading
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.