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
1012If Python and Arcade are installed, this example can be run from the command line with:
1113python -m arcade.examples.drawing_text_objects_batch
1214"""
1315import 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
2326class 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
386389def main ():
387- MyGame (SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_TITLE )
390+ MyGame (WINDOW_WIDTH , WINDOW_HEIGHT , WINDOW_TITLE )
388391 arcade .run ()
389392
390393
0 commit comments