-
A simple C++ library to generate BMP images with basic drawing primitives and bitmap-font text rendering.
-
If you get the fatal error "font file not found", check Debugging.
- Draw pixels, lines, rectangles (filled or outlined), and circles (filled or outlined).
- Load and render a cropped 8×8 monochrome font from a
.fntfile with scaling and word wrap. - Automatic clipping of out-of-bounds pixels.
- Pure C++17: no external dependencies beyond the standard library.
| Function | Description |
|---|---|
BMPImageCreator(int32_t width, int32_t height) |
Allocate canvas and initialize BMP headers (24-bit color). |
void setDefaultPixelRGB(int r, int g, int b) |
Fill entire canvas with a solid RGB color (clamped 0–255). |
void setPixel(int x, int y, int r, int g, int b) |
Set a single pixel at (x,y); ignores out-of-bounds and clamps color. |
void drawRectangle(int x0,int y0,int x1,int y1,int r,int g,int b,bool fill) |
Draw a filled or outlined rectangle; swaps coords internally. |
void drawLine(int x0,int y0,int x1,int y1,int r,int g,int b) |
Draw a line using Bresenham’s algorithm. |
void drawCircle(int cx,int cy,int radius,int r,int g,int b,bool fill) |
Draw a circle using the Midpoint algorithm (filled or outline). |
bool loadFont(const std::string &filename) |
Load and crop a bitpacked 8×8 .fnt font with 128 glyphs. |
void drawText(int x,int y,const std::string &text,int r,int g,int b,int scale,bool wrap) |
Render ASCII text with scaling and word-wrap. |
void saveFile(const std::string &filename) const |
Write the canvas to <filename>.bmp (BGR, bottom-up rows). |
src/
├─ bmp_image_creator.cpp
├─ bmp_image_creator.h
└─ font.fnt
example/
├─ example.cpp
└─ output_image.bmp
legacy/
└─ bmp_image_creator_legacy.cpp
.gitignore
Makefile
README
LICENSE
-
Open a terminal and navigate to the project root directory.
-
Build the example app:
make
-
Run the example:
make run
-
Output: The program generates
output_image.bmp.
From the project root, you can compile and run directly with g++:
g++ -std=c++17 -O2 -Wall -Wextra -pedantic \
example/example.cpp src/bmp_image_creator.cpp \
-o example/example_app
./example/example_appNote: Run from the repository root so src/font.fnt is found by the program. If you run from a different working directory, update the font path in src/bmp_image_creator.h or adjust your working directory.
If you encounter a "font file not found" error, see Debugging for instructions on setting the correct font path.
- If you the program can't find the font (throws an error), change the font path in the header file to the font file location (based on where you're compiling from).
#include "../src/bmp_image_creator.h"
int main() {
// Example usage of BMPImageCreator
BMPImageCreator bmp(100, 100);
// Set default pixel color to red
bmp.setDefaultPixelRGB(255, 0, 0);
// Draw a filled green rectangle
bmp.drawRectangle(10, 10, 90, 90, 0, 255, 0, true);
// Draw a blue line
bmp.drawLine(10, 10, 90, 90, 0, 0, 255);
// Draw a yellow circle outline
bmp.drawCircle(50, 50, 30, 255, 255, 0, false);
// Black wrapped text
bmp.drawText(10, 10, "Hello,\nBMP world!", 0, 0, 0, 1, true);
// Save the BMP image to a file
bmp.saveFile("output_image");
return 0;
}- Font from darkrose (https://opengameart.org/content/8x8-ascii-bitmap-font-with-c-source) (modified, converted to .fnt (bit images of each character) and cropped)
- Everyone is free to use, modify, and distribute this code for any purpose, with or without attribution.
- For more info, refer to the license.