plot β A Minimal C++ Plotting Library
plot is a small C++ library that wraps SDL2 into a minimal drawing API. It manages the window, renderer, and event loop β you only implement three callbacks.
Download
| File | Description |
|---|---|
| plot.hpp | Header β include this in your program |
| plot.cpp | Implementation β compile alongside your code |
| vera_ttf.c | Embedded font (included automatically by plot.cpp) |
| demo.cpp | Example: animated sine wave with keyboard control |
| Makefile | Build file for the demo |
Dependencies
SDL2, SDL2_ttf, SDL2_image. On Debian/Ubuntu:
apt install libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev
Building
Compile plot.cpp together with your source file:
g++ -std=c++11 -o demo demo.cpp plot.cpp $(sdl2-config --cflags --libs) -lSDL2_ttf -lSDL2_image
Or use the included Makefile:
make
To build a different source file:
make SRC=myprogram.cpp OUT=myprogram
Usage
Include plot.hpp and implement the callbacks. Only frame() is required β all others are optional.
#include "plot.hpp"
void setup() {
PlotCanvas(640, 480, PLOT_MODE_CROSS);
}
void frame() {
PlotBackground(255, 255, 255);
PlotColor(200, 0, 0);
PlotLine(-100, 0, 100, 0); // horizontal line through origin
PlotLine(0, -100, 0, 100); // vertical line through origin
PlotText(5, 5, "Frame: %d", g_current_frame);
}
bool event(SDL_Event* e) {
return false; // return true if event was handled
}
Canvas Modes
PlotCanvas(width, height, mode) sets up the drawing area. The mode controls where the origin is and which direction y increases:
| Mode constant | Origin | Y direction |
|---|---|---|
PLOT_MODE_DEFAULT |
top-left | down |
PLOT_MODE_CROSS |
center | up |
PLOT_MODE_MATH |
bottom-left | up |
API Reference
// Setup
void PlotCanvas(int width, int height, int mode = PLOT_MODE_DEFAULT);
void PlotMode(int mode);
// Drawing
void PlotBackground(Uint8 r, Uint8 g, Uint8 b);
void PlotColor(Uint8 r, Uint8 g, Uint8 b);
void PlotColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
void PlotCross(); // draws x/y axes
void PlotPixel(double x, double y);
void PlotLine(double x1, double y1, double x2, double y2);
void PlotTriangle(PlotPoint a, PlotPoint b, PlotPoint c);
void PlotRectangle(double x1, double y1, double x2, double y2);
void PlotFilledRectangle(double x1, double y1, double x2, double y2);
void PlotText(double x, double y, const char* format, ...); // printf-style
// Timing
void PlotSetFPS(int fps);
void PlotSetFrameDurationMS(Uint32 ms);
void PlotSetFrameLimit(int frames);
uint32_t PlotGetTicks();
// Global state (read-only)
extern int plot_width, plot_height;
extern uint64_t g_current_frame;
All draw functions also accept PlotPoint structs (double x, y) as an alternative to separate coordinates.