A simple cross-platform graphics abstraction layer with support for multiple backends (SDL2, Win32, X11).
- SDL2 (Windows, Linux, macOS) - Fully implemented
- Win32 (Windows native) - Fully implemented
- X11 (Linux native) - Fully implemented
- Window creation and management
- Double buffering for smooth rendering
- Basic drawing primitives:
- Lines
- Rectangles (filled and outlined)
- Circles (filled and outlined)
- Pixels
- Color support with alpha channel (SDL only)
- Simple event handling (ESC key, window close)
- Cross-platform delay function
REM Build with SDL2
build.bat sdl sample1
REM Build with Win32
build.bat win32 sample1
REM Clean
build.bat clean
REM Show help
build.bat help# Build with SDL2
.\build.ps1 -Backend sdl -Example sample1
# Build with Win32
.\build.ps1 -Backend win32 -Example sample2
# Build and run
.\build.ps1 -Backend sdl -Example sample1 -Run
# Build all examples
.\build.ps1 -Backend sdl -All
# Clean
.\build.ps1 -Clean
# Show help
.\build.ps1 -Help# Show help
make
# Build specific example
make build BACKEND=sdl EXAMPLE=sample1
make build BACKEND=win32 EXAMPLE=sample2
# Build all examples
make build-all BACKEND=sdl
# Build and run
make run BACKEND=sdl EXAMPLE=sample1
# Clean
make clean- MinGW with g++ compiler
- SDL2 (only for SDL backend):
- Download from libsdl.org
- Get
SDL2-devel-2.x.x-mingw.tar.gz - Extract to
C:\SDL2or setSDL2_PATHenvironment variable
Note: Win32 backend has no external dependencies!
# Build with SDL2
make build BACKEND=sdl EXAMPLE=sample1
# Build with X11 (native Linux)
make build BACKEND=x11 EXAMPLE=sample1
# Build all examples
make build-all BACKEND=x11
# Clean
make cleanFor SDL2 backend:
sudo apt-get install libsdl2-dev # Debian/Ubuntu
sudo dnf install SDL2-devel # FedoraFor X11 backend:
sudo apt-get install libx11-dev # Debian/Ubuntu
sudo dnf install libX11-devel # FedoraRecommended for production use. The Makefile and other build files in this repository are designed for demonstration purposes.
g++ -std=c++11 -DUSE_SDL -o build\sample1.exe ^
examples\sample1.cpp lib\graphics.cpp ^
-IC:\SDL2\include ^
-LC:\SDL2\lib ^
-lmingw32 -lSDL2main -lSDL2 -mwindowsg++ -std=c++11 -DUSE_WIN32 -o build\sample1.exe ^
examples\sample1.cpp lib\graphics.cpp ^
-lgdi32 -luser32g++ -std=c++11 -DUSE_SDL -o build/sample1 \
examples/sample1.cpp lib/graphics.cpp \
$(sdl2-config --cflags --libs)g++ -std=c++11 -DUSE_X11 -o build/sample1 \
examples/sample1.cpp lib/graphics.cpp \
-lX11#include "graphics.h"
int main() {
// Create a window
WindowHandle* window = createWindow("My Window", 800, 600);
// Main loop
while (!windowShouldClose(window)) {
pollEvents(window);
// Clear screen
clearScreen(window, Color(0, 0, 0));
// Draw shapes
drawLine(window, 10, 10, 100, 100, Color(255, 0, 0));
drawFilledCircle(window, 200, 200, 50, Color(0, 255, 0));
drawRectangle(window, 300, 300, 100, 100, Color(0, 0, 255));
// Present
swapBuffers(window);
delay(16); // ~60 FPS
}
// Cleanup
destroyWindow(window);
return 0;
}WindowHandle* createWindow(const char* title, int width, int height)- Create a new windowvoid destroyWindow(WindowHandle* window)- Destroy window and free resourcesbool windowShouldClose(WindowHandle* window)- Check if window should closevoid pollEvents(WindowHandle* window)- Process window eventsvoid swapBuffers(WindowHandle* window)- Present rendered frame
void clearScreen(WindowHandle* window, const Color& color)- Clear screen with colorvoid drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color)- Draw a linevoid drawRectangle(WindowHandle* window, int x, int y, int w, int h, const Color& color)- Draw rectangle outlinevoid drawFilledRectangle(WindowHandle* window, int x, int y, int w, int h, const Color& color)- Draw filled rectanglevoid drawCircle(WindowHandle* window, int cx, int cy, int radius, const Color& color)- Draw circle outlinevoid drawFilledCircle(WindowHandle* window, int cx, int cy, int radius, const Color& color)- Draw filled circlevoid drawPixel(WindowHandle* window, int x, int y, const Color& color)- Draw a single pixel
void setDrawColor(WindowHandle* window, const Color& color)- Set current drawing colorvoid delay(uint32_t milliseconds)- Delay execution
struct Color {
uint8_t r, g, b, a;
Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
};| Feature | SDL2 | Win32 | X11 |
|---|---|---|---|
| Platform | Cross-platform | Windows only | Linux only |
| Dependencies | SDL2 library | None (native) | X11 library |
| Performance | Good | Excellent | Good |
| Complexity | Easy | Medium | Medium |
| Alpha blending | Yes | Limited | Limited |
libgraffik/
├── lib/
│ ├── graphics.h # Header file with API declarations
│ ├── graphics.cpp # Implementation for all backends
│ └── SDL2.dll # SDL2 DLL (Windows only)
├── examples/
│ ├── sample1.cpp # Basic shapes demo
│ ├── sample2.cpp # Animation demo
│ └── sample3.cpp # Interactive demo
├── build/ # Output directory (created automatically)
├── Makefile # Unix-style Makefile
├── build.ps1 # PowerShell build script
├── build.bat # Batch build script
└── README.md # This file
- Windows native: Use
build.batfor CMD orbuild.ps1for PowerShell. - Linux users: Use the universal Makefile, also most recommended for all OSes.
- Win32 backend: Windows-native, uses CPU for rendering, may cause lack of performance, although may use less resources.
- SDL2 backend: Best for cross-platform development, works natively on both Linux and Windows, uses GPU for rendering. May be more resource heavy than these simplier backends like X11 or Win32.
- X11 backend: Native Linux performance, uses CPU for rendering. Will work for desktops using Xorg server.
MIT License. See LICENSE file for details.
AI had a hand in this. I try to review and improve everything it generates.