https://github.com/martinrenou/pysfml11
Python binding for the SFML library, using pybind11
https://github.com/martinrenou/pysfml11
python python-binding sfml
Last synced: 4 months ago
JSON representation
Python binding for the SFML library, using pybind11
- Host: GitHub
- URL: https://github.com/martinrenou/pysfml11
- Owner: martinRenou
- License: other
- Created: 2020-02-08T07:47:53.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-09-27T07:09:21.000Z (over 2 years ago)
- Last Synced: 2025-03-17T13:22:48.222Z (12 months ago)
- Topics: python, python-binding, sfml
- Language: C++
- Homepage:
- Size: 472 KB
- Stars: 13
- Watchers: 6
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pysfml11
A Python binding for the [SFML](https://www.sfml-dev.org) library, based on [pybind11](https://github.com/pybind/pybind11).
## Installation
This requires that you install SFML on your machine.
```bash
git clone https://github.com/martinRenou/pysfml11
cd pysfml11
pip install .
```
## Usage
```python
import pysfml11 as sf
width = 800
height = 600
# Create your window
window = sf.RenderWindow(sf.VideoMode(width, height), 'My awesome SFML application!')
clock = sf.Clock()
rectangle = sf.RectangleShape(sf.Vector2f(200, 300))
rectangle.fill_color = sf.Color.Blue
rectangle.position = sf.Vector2f(width / 2., height / 2.)
# Simple rendering loop: poll events/clear/draw/display
while (window.is_open()):
event = sf.Event()
while window.poll_event(event):
if event.type == sf.Event.Closed:
window.close()
# Do something with the event (mouse event/keyboard event/resize event...)
window.clear(sf.Color.White)
elapsed = clock.elapsed_time.as_seconds() * 40
rectangle.rotation = elapsed
window.draw(rectangle)
window.display()
```
## API differences with SFML
Most of the C++ API has been ported to Python using [pybind11](https://github.com/pybind/pybind11), except features that did not make sense to implement because already available in Python (Threading features are available through the built-in [threading](https://docs.python.org/3/library/threading.html) module. String features are already supported by Python).
There are some API differences:
### camelCase to snake_case
All method/function names are renamed using snake_case.
### Setters/Getters
Those are replaced by properties (read-only properties if there is not getter available). So for example with the RectangleShape, this C++ code:
```cpp
sf::RectangleShape rectangle;
rectangle.setPosition(10, 20);
sf::Vector2f position = rectangle.getPosition();
```
Becomes the following in Python:
```python
rectangle = RectangleShape()
rectangle.position = Vector2f(10, 20)
position = rectangle.position
```
### C-like functions
C-like functions are made simpler. For example with the InputSoundFile's read method, the samples buffer is the returned value (no need to allocate it before calling the method), and the samples counts actually read can be computed getting the length of the returned value.
This C++ code:
```cpp
sf::InputSoundFile file;
file.openFromFile("music.ogg");
sf::Int16 samples[1024];
sf::Uint64 count = file.read(samples, 1024);
```
Becomes the following in Python:
```python
file = InputSoundFile()
file.open_from_file("music.ogg")
samples = file.read(1024)
count = len(samples)
```
## Status
`pysfml11` is under development and not yet finished. Basic operations are implemented, but the full API is not there yet. Contributions are very welcome!
## License
`pysfml11` is provided under a BSD-style license that can be found in the LICENSE
file. By using, distributing, or contributing to this project, you agree to the
terms and conditions of this license.