-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8ddf97
commit a30bffa
Showing
23 changed files
with
911 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Advanced | ||
======== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:caption: Contents: | ||
|
||
backendapi | ||
contextapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Context API | ||
=========== | ||
|
||
This page documents the contract bentween the ``RenderCanvas`` and the context object. | ||
|
||
|
||
Context detection | ||
----------------- | ||
|
||
.. autofunction:: rendercanvas._context.rendercanvas_context_hook | ||
:no-index: | ||
|
||
|
||
Context | ||
------- | ||
|
||
.. autoclass:: rendercanvas._context.ContextInterface | ||
:members: | ||
:no-index: | ||
|
||
|
||
RenderCanvas | ||
------------ | ||
|
||
This shows the subset of methods of a canvas that relates to the context (see :doc:`backendapi` for the complete list). | ||
|
||
.. autoclass:: rendercanvas.stub.StubRenderCanvas | ||
:members: _rc_get_present_methods, get_context, get_physical_size, get_logical_size, | ||
:no-index: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ Welcome to the rendercanvas docs! | |
start | ||
api | ||
backends | ||
backendapi | ||
advanced | ||
|
||
|
||
Indices and tables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Simple example that uses the bitmap-context to show images of noise. | ||
""" | ||
|
||
import numpy as np | ||
from rendercanvas.auto import RenderCanvas, loop | ||
|
||
|
||
canvas = RenderCanvas(update_mode="continuous") | ||
context = canvas.get_context("bitmap") | ||
|
||
|
||
@canvas.request_draw | ||
def animate(): | ||
w, h = canvas.get_logical_size() | ||
shape = int(h) // 4, int(w) // 4 | ||
|
||
bitmap = np.random.uniform(0, 255, shape).astype(np.uint8) | ||
context.set_bitmap(bitmap) | ||
|
||
|
||
loop.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Simple snake game based on bitmap rendering. Work in progress. | ||
""" | ||
|
||
from collections import deque | ||
|
||
import numpy as np | ||
|
||
from rendercanvas.auto import RenderCanvas, loop | ||
|
||
|
||
canvas = RenderCanvas(present_method=None, update_mode="continuous") | ||
|
||
context = canvas.get_context("bitmap") | ||
|
||
world = np.zeros((120, 160), np.uint8) | ||
pos = [100, 100] | ||
direction = [1, 0] | ||
q = deque() | ||
|
||
|
||
@canvas.add_event_handler("key_down") | ||
def on_key(event): | ||
key = event["key"] | ||
if key == "ArrowLeft": | ||
direction[0] = -1 | ||
direction[1] = 0 | ||
elif key == "ArrowRight": | ||
direction[0] = 1 | ||
direction[1] = 0 | ||
elif key == "ArrowUp": | ||
direction[0] = 0 | ||
direction[1] = -1 | ||
elif key == "ArrowDown": | ||
direction[0] = 0 | ||
direction[1] = 1 | ||
|
||
|
||
@canvas.request_draw | ||
def animate(): | ||
pos[0] += direction[0] | ||
pos[1] += direction[1] | ||
|
||
if pos[0] < 0: | ||
pos[0] = world.shape[1] - 1 | ||
elif pos[0] >= world.shape[1]: | ||
pos[0] = 0 | ||
if pos[1] < 0: | ||
pos[1] = world.shape[0] - 1 | ||
elif pos[1] >= world.shape[0]: | ||
pos[1] = 0 | ||
|
||
q.append(tuple(pos)) | ||
world[pos[1], pos[0]] = 255 | ||
|
||
while len(q) > 20: | ||
old_pos = q.popleft() | ||
world[old_pos[1], old_pos[0]] = 0 | ||
|
||
context.set_bitmap(world) | ||
|
||
|
||
loop.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
A stub context imlpementation for documentation purposes. | ||
It does actually work, but presents nothing. | ||
""" | ||
|
||
import weakref | ||
|
||
|
||
def rendercanvas_context_hook(canvas, present_methods): | ||
"""Hook function to allow ``rendercanvas`` to detect your context implementation. | ||
If you make a function with this name available in the module ``your.module``, | ||
``rendercanvas`` will detect and call this function in order to obtain the canvas object. | ||
That way, anyone can use ``canvas.get_context("your.module")`` to use your context. | ||
The arguments are the same as for ``ContextInterface``. | ||
""" | ||
return ContextInterface(canvas, present_methods) | ||
|
||
|
||
class ContextInterface: | ||
"""The interface that a context must implement, to be usable with a ``RenderCanvas``. | ||
Arguments: | ||
canvas (BaseRenderCanvas): the canvas to render to. | ||
present_methods (dict): The supported present methods of the canvas. | ||
The ``present_methods`` dict has a field for each supported present-method. A | ||
canvas must support either "screen" or "bitmap". It may support both, as well as | ||
additional (specialized) present methods. Below we list the common methods and | ||
what fields the subdicts have. | ||
* Render method "screen": | ||
* "window": the native window id. | ||
* "display": the native display id (Linux only). | ||
* "platform": to determine between "x11" and "wayland" (Linux only). | ||
* Render method "bitmap": | ||
* "formats": a list of supported formats. It should always include "rgba-u8". | ||
Other options can be be "i-u8" (intensity/grayscale), "i-f32", "bgra-u8", "rgba-u16", etc. | ||
""" | ||
|
||
def __init__(self, canvas, present_methods): | ||
self._canvas_ref = weakref.ref(canvas) | ||
self._present_methods = present_methods | ||
|
||
@property | ||
def canvas(self): | ||
"""The associated canvas object. Internally, this should preferably be stored using a weakref.""" | ||
return self._canvas_ref() | ||
|
||
def present(self): | ||
"""Present the result to the canvas. | ||
This is called by the canvas, and should not be called by user-code. | ||
The implementation should always return a present-result dict, which | ||
should have at least a field 'method'. The value of 'method' must be | ||
one of the methods that the canvas supports, i.e. it must be in ``present_methods``. | ||
* If there is nothing to present, e.g. because nothing was rendered yet: | ||
* return ``{"method": "skip"}`` (special case). | ||
* If presentation could not be done for some reason: | ||
* return ``{"method": "fail", "message": "xx"}`` (special case). | ||
* If ``present_method`` is "screen": | ||
* Render to screen using the info in ``present_methods['screen']``). | ||
* Return ``{"method", "screen"}`` as confirmation. | ||
* If ``present_method`` is "bitmap": | ||
* Return ``{"method": "bitmap", "data": data, "format": format}``. | ||
* 'data' is a memoryview, or something that can be converted to a memoryview, like a numpy array. | ||
* 'format' is the format of the bitmap, must be in ``present_methods['bitmap']['formats']`` ("rgba-u8" is always supported). | ||
* If ``present_method`` is something else: | ||
* Return ``{"method": "xx", ...}``. | ||
* It's the responsibility of the context to use a render method that is supported by the canvas, | ||
and that the appropriate arguments are supplied. | ||
""" | ||
|
||
# This is a stub | ||
return {"method": "skip"} |
Oops, something went wrong.