High-performance 2D graphics and math library with ultra-optimized vector operations
Project description
e2D - High-Performance 2D Graphics and Math Library
e2D combines ultra-optimized vector mathematics with modern OpenGL rendering for high-performance 2D applications. Perfect for games, simulations, and real-time graphics.
✨ Features
🚀 Optimized Vector Operations
- Cython-compiled Vector2D class (10-500x faster than pure Python)
- Batch operations for processing thousands of vectors efficiently
- Direct memory access with zero-copy operations
- NumPy integration for seamless GPU data upload
🎮 Modern Graphics
- ModernGL rendering pipeline
- Shape rendering with instancing support
- Text rendering with custom styles and TTF fonts
- Screen recording with async video encoding
- Color system with 80+ pre-defined colors
- GLFW window management
🎯 Game Development Tools
- Keyboard and mouse input handling
- Collision detection
- Color manipulation
- Vector mathematics
📦 Installation
Basic Installation
pip install e2D
The package will automatically compile the Cython extensions during installation for optimal performance (like numpy). If compilation fails, it falls back to pure Python mode.
Optional Features
Install with screen recording support:
pip install e2D[rec]
Install for development (includes testing tools):
pip install e2D[dev]
Install with performance monitoring (includes Cython source):
pip install e2D[performance]
Install everything:
pip install e2D[all]
Legacy Version (1.x with Pygame)
If you need the old pygame-based version:
pip install "e2D<2.0"
Requirements
- Python 3.10+
- NumPy (required)
- ModernGL (required)
- GLFW (required)
- Pillow (required - for text rendering)
- attrs (required - for data structures)
- OpenCV-Python (optional, for recording - install with
[rec]extra)
Linux-Specific Setup (Fedora/RHEL/CentOS)
If you encounter OpenGL library errors on Fedora-based systems:
# Install Mesa OpenGL libraries
sudo dnf install mesa-libGL mesa-libEGL
# Create symlinks (ModernGL needs unversioned .so files)
sudo ln -s /usr/lib64/libGL.so.1 /usr/lib64/libGL.so
sudo ln -s /usr/lib64/libEGL.so.1 /usr/lib64/libEGL.so
Note: On Debian/Ubuntu systems, these symlinks are usually created automatically. If you encounter similar issues:
sudo apt-get install libgl1-mesa-glx libegl1-mesa
🚀 Quick Start
Optimized Vector Operations
from e2D import Vector2D, batch_add_inplace, vectors_to_array
# Create vectors
v1 = Vector2D(3.0, 4.0)
v2 = Vector2D(1.0, 2.0)
# Basic operations
v3 = v1 + v2
length = v1.length
dot = v1.dot_product(v2)
# In-place operations (faster!)
v1.iadd(v2)
v1.normalize()
v1.irotate(0.1)
# Process thousands of vectors instantly
positions = [Vector2D.random(-10, 10) for _ in range(10000)]
displacement = Vector2D(1.0, 0.5)
batch_add_inplace(positions, displacement) # 🚀 Lightning fast!
# Convert to numpy for GPU upload
pos_array = vectors_to_array(positions)
Graphics Rendering
from e2D import RootEnv, DefEnv
from e2D.vectors import V2
class MyApp(DefEnv):
def __init__(self) -> None:
pass
def update(self) -> None:
# Your game logic here
pass
def draw(self) -> None:
# Your rendering code here
pass
# Initialize and run
rootEnv = RootEnv(window_size=V2(1920, 1080), target_fps=60)
rootEnv.init(MyApp())
# Optional: Enable screen recording
rootEnv.init_rec(fps=30, draw_on_screen=True, path='output.mp4')
rootEnv.loop()
Color System
from e2D import Color, WHITE, RED, CYAN, normalize_color
from e2D.color_defs import MD_BLUE, PASTEL_PINK, NEON_GREEN
# Create colors
color1 = Color.from_hex("#FF5733")
color2 = Color.from_rgb255(100, 150, 200)
color3 = Color.from_hsv(0.5, 0.8, 1.0)
# Color operations
lighter = color1.lighten(0.2)
darker = color1.darken(0.2)
inverted = color1.invert()
rotated = color1.rotate_hue(120)
# Use pre-defined colors
from e2D import draw_circle
draw_circle((100, 100), 50, color=RED, fill_mode='fill')
📊 Performance
Vector2D benchmark (100,000 operations):
| Operation | Time | vs Pure Python |
|---|---|---|
| Creation | 42 ms | 10x faster |
| Addition | 64 ms | 15x faster |
| In-place ops | 3.8 ms | 100x faster |
| Normalization | 1.9 ms | 200x faster |
| Batch operations | 0.17 ms | 500x+ faster 🔥 |
Perfect for:
- Particle systems (10,000+ particles)
- Physics simulations
- Collision detection
- Path finding
- Real-time graphics
📚 Documentation
Feature Guides
- Vector Operations - Complete guide to Vector2D operations, batch processing, and optimization
- Color System - Color creation, manipulation, pre-defined palettes, and conversions
- Shape Rendering - Drawing circles, rectangles, lines, and using shape caching
- Text Rendering - Text rendering, styles, fonts, pivots, and cached labels
- Input Handling - Keyboard and mouse input with Keys and MouseButtons constants
Examples & API
- Examples - Interactive visual examples for all major features
- API Reference - Complete API documentation
- Developer Guide - Development workflow and contributing
🎯 Use Cases
Particle System
from e2D import Vector2D, batch_add_inplace, vectors_to_array
positions = [Vector2D.random(-10, 10) for _ in range(10000)]
velocities = [Vector2D.random(-1, 1) for _ in range(10000)]
def update(dt):
# Update all particles in milliseconds
for i in range(len(positions)):
temp = velocities[i].mul(dt)
positions[i].iadd(temp)
def render():
# Upload to GPU
pos_array = vectors_to_array(positions).astype(np.float32)
vbo.write(pos_array)
Physics Simulation
from e2D import Vector2D
class RigidBody:
def __init__(self, pos, vel):
self.position = Vector2D(*pos)
self.velocity = Vector2D(*vel)
self.acceleration = Vector2D(0, -9.8)
def update(self, dt):
# Optimized in-place physics
temp = self.acceleration.mul(dt)
self.velocity.iadd(temp)
temp = self.velocity.mul(dt)
self.position.iadd(temp)
🔧 Development
Building from Source
git clone https://github.com/marick-py/e2D.git
cd e2D
pip install -e .[dev]
Running Tests
pytest
Building Distribution
python -m build
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
👤 Author
Riccardo Mariani
- Email: riccardo.mariani@emptyhead.dev
- GitHub: @marick-py
🙏 Acknowledgments
- Built with ModernGL
- Optimized with Cython
- Inspired by the need for high-performance 2D mathematics in Python
📈 Version History
Version 2.x (ModernGL-based - Current)
- 2.1.8 (Current) - Bug fixes and documentation improvements
- 2.0.0 - Complete rewrite with ModernGL rendering, Cython-optimized vectors, modern color system, screen recording, removed pygame dependency
Version 1.x (Pygame-based - Legacy)
- 1.4.24 - Previous stable release with pure Python vectors and pygame
- Legacy versions available via:
pip install "e2D<2.0"
Made with ❤️ for high-performance 2D development
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file e2d-2.1.8.tar.gz.
File metadata
- Download URL: e2d-2.1.8.tar.gz
- Upload date:
- Size: 458.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d2332b69dde6c36244c69bb2e2882126ef52c05356e413ad28929275cb9bfde
|
|
| MD5 |
2b5152f46e8b3ffe41db1f8d41eda166
|
|
| BLAKE2b-256 |
3abd9ebe7a9d63ac8df70c7c244d8f5f4ce9bf279277806db4191c5f5d52dc83
|
Provenance
The following attestation bundles were made for e2d-2.1.8.tar.gz:
Publisher:
publish.yml on marick-py/e2D
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
e2d-2.1.8.tar.gz -
Subject digest:
6d2332b69dde6c36244c69bb2e2882126ef52c05356e413ad28929275cb9bfde - Sigstore transparency entry: 1003461353
- Sigstore integration time:
-
Permalink:
marick-py/e2D@896533582753c7d27a2781b0c400426119d877a2 -
Branch / Tag:
refs/tags/v2.1.8 - Owner: https://github.com/marick-py
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@896533582753c7d27a2781b0c400426119d877a2 -
Trigger Event:
release
-
Statement type: