Skip to content

Commit 9a49616

Browse files
authored
Expose batch drawing text example in the web doc (pythonarcade#1987)
* Refresh top-level docstring for drawing text objects with pyglet example * Simplify imports by only importing pyglet.graphics.Batch * Rename window constants for accuracy * Explain the text drawing example's constants using comments * Add missing newline * Expose the batch drawing example in the doc * Rename and update the text object eexample page * Update the draw_text example page
1 parent 282f618 commit 9a49616

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

arcade/examples/drawing_text_objects_batch.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
"""
2-
This is the same as the examples for drawing text using the one-off draw_text function
3-
as well as the Text objects example. This takes it one step further though. Here we are
4-
creating Text objects, and then adding them to a Pyglet Batch object, and drawing them
5-
all with one draw command.
2+
The current fastest way to draw text with arcade.
63
7-
Using this method, we can draw thousands and thousands of text objects with nearly the
8-
same cost as drawing just one Text object directly.
4+
This example improves on the other two text-drawing examples
5+
by using pyglet's batch functionality.
6+
7+
Although pyglet's batches do not support non-drawing features like
8+
arcade's SpriteList, they offer similar benefits for drawing. Adding
9+
arcade.Text objects to a batch allows drawing thousands of them with
10+
almost the same cost as drawing a single one directly.
911
1012
If Python and Arcade are installed, this example can be run from the command line with:
1113
python -m arcade.examples.drawing_text_objects_batch
1214
"""
1315
import arcade
14-
import pyglet.graphics
16+
from pyglet.graphics import Batch
17+
1518

16-
SCREEN_WIDTH = 1200
17-
SCREEN_HEIGHT = 800
18-
SCREEN_TITLE = "Drawing Text Example"
19-
DEFAULT_LINE_HEIGHT = 45
20-
DEFAULT_FONT_SIZE = 20
19+
WINDOW_WIDTH = 1200 # Window width in pixels
20+
WINDOW_HEIGHT = 800 # Window height in pixels
21+
WINDOW_TITLE = "Drawing Text Example" # Window title
22+
DEFAULT_LINE_HEIGHT = 45 # Line height to use in pixels
23+
DEFAULT_FONT_SIZE = 20 # Default font size in points
2124

2225

2326
class MyGame(arcade.Window):
@@ -32,26 +35,26 @@ def __init__(self, width, height, title):
3235
self.text_angle = 0
3336
self.time_elapsed = 0.0
3437

35-
self.batch = pyglet.graphics.Batch()
38+
self.batch = Batch()
3639

3740
# Add the screen title
3841
start_x = 0
39-
start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 1.5
42+
start_y = WINDOW_HEIGHT - DEFAULT_LINE_HEIGHT * 1.5
4043
self.title = arcade.Text(
4144
"Text Drawing Examples",
4245
start_x,
4346
start_y,
4447
arcade.color.BLACK,
4548
DEFAULT_FONT_SIZE * 2,
46-
width=SCREEN_WIDTH,
49+
width=WINDOW_WIDTH,
4750
align="center",
4851
batch=self.batch,
4952
)
5053

5154
# start_x and start_y make the start point for the text. We draw a dot to make it
5255
# easy too see the text in relation to its start x and y.
5356
start_x = 10
54-
start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 3
57+
start_y = WINDOW_HEIGHT - DEFAULT_LINE_HEIGHT * 3
5558
self.fonts = arcade.Text(
5659
"Fonts:",
5760
start_x,
@@ -219,7 +222,7 @@ def __init__(self, width, height, title):
219222

220223
# --- Column 2 ---
221224
start_x = 750
222-
start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 3
225+
start_y = WINDOW_HEIGHT - DEFAULT_LINE_HEIGHT * 3
223226
self.text_positioning = arcade.Text(
224227
"Text Positioning:",
225228
start_x,
@@ -384,7 +387,7 @@ def on_draw(self):
384387

385388

386389
def main():
387-
MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
390+
MyGame(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
388391
arcade.run()
389392

390393

doc/example_code/how_to_examples/drawing_text.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ Slow but Easy Text Drawing
1010
:align: center
1111
:alt: Screenshot of drawing text
1212

13-
This example shows how to draw text. While it is simple to draw using the
14-
techniques shown here, it is also slow. Using "text objects" can result in
15-
significant performance improvements. See :ref:`drawing_text_objects`.
13+
This example shows an easy but very slow way to draw text.
1614

15+
Its main benefit is ease of use. More complicated approaches
16+
with :py:class:`arcade.Text` objects can run thousands of times
17+
faster.
18+
19+
* See :ref:`drawing_text_objects` for a medium-performance approach
20+
* See :ref:`drawing_text_objects_batch` if you want the best performance
21+
possible
1722

1823
.. literalinclude:: ../../../arcade/examples/drawing_text.py
1924
:caption: drawing_text.py

doc/example_code/how_to_examples/drawing_text_objects.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
.. _drawing_text_objects:
44

5-
Fast Text Drawing
6-
=================
5+
Better Text Drawing with Text Objects
6+
=====================================
77

88
.. image:: drawing_text_objects.png
99
:width: 500px
1010
:align: center
1111
:alt: Screenshot of drawing with text objects
1212

13-
This example shows how to draw text using "text objects." It is the fastest
14-
way to draw text. If you are looking for something simpler, see :ref:`drawing_text`.
13+
This example shows how to draw text using "text objects." It can be many
14+
times faster than using :py:func:`arcade.draw_text`.
15+
16+
* If you want to be as efficient as possible, see :ref:`drawing_text_objects_batch`.
17+
* If you are looking for something simpler, see :ref:`drawing_text`.
1518

1619
.. literalinclude:: ../../../arcade/examples/drawing_text_objects.py
1720
:caption: drawing_text_objects.py
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
:orphan:
2+
3+
.. _drawing_text_objects_batch:
4+
5+
The Fastest Text Drawing: pyglet Batches
6+
========================================
7+
8+
.. image:: drawing_text_objects.png
9+
:width: 500px
10+
:align: center
11+
:alt: Screenshot of drawing with text objects
12+
13+
This example demonstrates the most efficient way to render
14+
:py:class:`arcade.Text` objects: adding them to pyglet's
15+
:py:class:`~pyglet.graphics.Batch`. Otherwise, it is the
16+
same as the :ref:`drawing_text_objects` example.
17+
18+
For a much simpler and slower approach, see :ref:`drawing_text`.
19+
20+
.. literalinclude:: ../../../arcade/examples/drawing_text_objects.py
21+
:caption: drawing_text_objects.py
22+
:linenos:

doc/example_code/how_to_examples/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ Drawing Primitives
4949

5050
:ref:`drawing_text_objects`
5151

52+
.. figure:: thumbs/drawing_text_objects.png
53+
:figwidth: 170px
54+
:target: drawing_text_objects_batch.html
55+
56+
:ref:`drawing_text_objects_batch`
57+
5258
Animating Drawing Primitives
5359
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5460

0 commit comments

Comments
 (0)