From a8b5762b4e5cb579ca42409c66c43320d42c1c8a Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 27 Apr 2023 02:04:32 -0400 Subject: [PATCH 1/6] Move existing sprite depth demo to more appropriate file name --- arcade/experimental/{sprite_depth.py => sprite_depth_cosine.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename arcade/experimental/{sprite_depth.py => sprite_depth_cosine.py} (100%) diff --git a/arcade/experimental/sprite_depth.py b/arcade/experimental/sprite_depth_cosine.py similarity index 100% rename from arcade/experimental/sprite_depth.py rename to arcade/experimental/sprite_depth_cosine.py From 19b4887edc6ddfe59e3d52229adc297cf79627f7 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 27 Apr 2023 02:47:18 -0400 Subject: [PATCH 2/6] Add top-level docstring and comments for sprite depth example --- arcade/experimental/sprite_depth_cosine.py | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/arcade/experimental/sprite_depth_cosine.py b/arcade/experimental/sprite_depth_cosine.py index dea42343c..441d77d7e 100644 --- a/arcade/experimental/sprite_depth_cosine.py +++ b/arcade/experimental/sprite_depth_cosine.py @@ -1,3 +1,15 @@ +""" +Depth-sort sprites using a depth buffer in the GL context. + +During each update, the depth of each sprite is updated to follow a +cosine wave. Afterward, the following is drawn: + + * All sprites in depth-sorted order + * A white square for every sprite moving with the wave along the y axis + +If Python and Arcade are installed, this example can be run from the command line with: +python -m arcade.examples.experimental.sprite_depth_cosine +""" import math import arcade @@ -5,9 +17,11 @@ class MyGame(arcade.Window): def __init__(self): - super().__init__(800, 600, "Sprite Depth") - self.sprite_list = arcade.SpriteList() + super().__init__(800, 600, "Sprite Depth with Cosine Modulation") + texture = arcade.load_texture(":resources:images/test_textures/xy_square.png") + + self.sprite_list = arcade.SpriteList() self.time = 0.0 for i in range(10): @@ -16,16 +30,21 @@ def __init__(self): def on_draw(self): self.clear() + + # This context manager temporarily enables depth testing with self.ctx.enabled(self.ctx.DEPTH_TEST): self.sprite_list.draw() + # Draw wave visualization markers over each sprite for i, sprite in enumerate(self.sprite_list): arcade.draw_point(150 + 50 * i, 300 + sprite.depth, arcade.color.WHITE, 10) def on_update(self, delta_time): self.time += delta_time + for i, sprite in enumerate(self.sprite_list): sprite.depth = math.cos(self.time + i) * 50 -MyGame().run() +if __name__ == "__main__": + MyGame().run() From 2a77ce911516d1d3974cf5f125510c20df640690 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 27 Apr 2023 03:08:34 -0400 Subject: [PATCH 3/6] Move hard-coded values into top of file constants --- arcade/experimental/sprite_depth_cosine.py | 29 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/arcade/experimental/sprite_depth_cosine.py b/arcade/experimental/sprite_depth_cosine.py index 441d77d7e..e75798527 100644 --- a/arcade/experimental/sprite_depth_cosine.py +++ b/arcade/experimental/sprite_depth_cosine.py @@ -14,18 +14,34 @@ import arcade +# All constants are in pixels +WIDTH, HEIGHT = 800, 600 + +NUM_SPRITES = 10 + +SPRITE_X_START = 150 +SPRITE_X_STEP = 50 +SPRITE_Y = HEIGHT // 2 + +DOT_SIZE = 10 + + class MyGame(arcade.Window): def __init__(self): - super().__init__(800, 600, "Sprite Depth with Cosine Modulation") + super().__init__(WIDTH, HEIGHT, "Sprite Depth Testing Example") texture = arcade.load_texture(":resources:images/test_textures/xy_square.png") self.sprite_list = arcade.SpriteList() self.time = 0.0 - for i in range(10): - sprite = arcade.Sprite(texture, center_x=150 + 50 * i, center_y=300) + for i in range(NUM_SPRITES): + sprite = arcade.Sprite( + texture, + center_x=SPRITE_X_START + SPRITE_X_STEP * i, + center_y=SPRITE_Y + ) self.sprite_list.append(sprite) def on_draw(self): @@ -37,13 +53,16 @@ def on_draw(self): # Draw wave visualization markers over each sprite for i, sprite in enumerate(self.sprite_list): - arcade.draw_point(150 + 50 * i, 300 + sprite.depth, arcade.color.WHITE, 10) + arcade.draw_point( + SPRITE_X_START + SPRITE_X_STEP * i, SPRITE_Y + sprite.depth, + arcade.color.WHITE, DOT_SIZE + ) def on_update(self, delta_time): self.time += delta_time for i, sprite in enumerate(self.sprite_list): - sprite.depth = math.cos(self.time + i) * 50 + sprite.depth = math.cos(self.time + i) * SPRITE_X_STEP if __name__ == "__main__": From 24b357fad48d3a53df605ca43f60ca19349e3a26 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 27 Apr 2023 03:10:21 -0400 Subject: [PATCH 4/6] Correct docstring run command + touch up other doc --- arcade/experimental/sprite_depth_cosine.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arcade/experimental/sprite_depth_cosine.py b/arcade/experimental/sprite_depth_cosine.py index e75798527..831d5b1af 100644 --- a/arcade/experimental/sprite_depth_cosine.py +++ b/arcade/experimental/sprite_depth_cosine.py @@ -5,10 +5,11 @@ cosine wave. Afterward, the following is drawn: * All sprites in depth-sorted order - * A white square for every sprite moving with the wave along the y axis + * A white square centered over each sprite along the x-axis, and moving + with the wave along the y-axis If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.experimental.sprite_depth_cosine +python -m arcade.experimental.sprite_depth_cosine """ import math import arcade @@ -29,7 +30,7 @@ class MyGame(arcade.Window): def __init__(self): - super().__init__(WIDTH, HEIGHT, "Sprite Depth Testing Example") + super().__init__(WIDTH, HEIGHT, "Sprite Depth Testing Example w/ a Cosine Wave") texture = arcade.load_texture(":resources:images/test_textures/xy_square.png") From 6f0908a9c5639196527c61fbbbb9a632de186ca9 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 27 Apr 2023 03:17:28 -0400 Subject: [PATCH 5/6] Add spacebar toggle for depth testing during drawing --- arcade/experimental/sprite_depth_cosine.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arcade/experimental/sprite_depth_cosine.py b/arcade/experimental/sprite_depth_cosine.py index 831d5b1af..0574d8d60 100644 --- a/arcade/experimental/sprite_depth_cosine.py +++ b/arcade/experimental/sprite_depth_cosine.py @@ -1,6 +1,8 @@ """ Depth-sort sprites using a depth buffer in the GL context. +Press the space bar to toggle depth testing during drawing. + During each update, the depth of each sprite is updated to follow a cosine wave. Afterward, the following is drawn: @@ -36,6 +38,7 @@ def __init__(self): self.sprite_list = arcade.SpriteList() self.time = 0.0 + self.use_depth: bool = True for i in range(NUM_SPRITES): sprite = arcade.Sprite( @@ -48,8 +51,11 @@ def __init__(self): def on_draw(self): self.clear() - # This context manager temporarily enables depth testing - with self.ctx.enabled(self.ctx.DEPTH_TEST): + if self.use_depth: + # This context manager temporarily enables depth testing + with self.ctx.enabled(self.ctx.DEPTH_TEST): + self.sprite_list.draw() + else: self.sprite_list.draw() # Draw wave visualization markers over each sprite @@ -59,6 +65,10 @@ def on_draw(self): arcade.color.WHITE, DOT_SIZE ) + def on_key_press(self, symbol: int, modifiers: int): + if symbol == arcade.key.SPACE: + self.use_depth = not self.use_depth + def on_update(self, delta_time): self.time += delta_time From aba4a774abc9858dc6c7d167417d9e2867ddc9e6 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 27 Apr 2023 03:31:27 -0400 Subject: [PATCH 6/6] Add on-screen instructions / status display for depth test toggle --- arcade/experimental/sprite_depth_cosine.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/arcade/experimental/sprite_depth_cosine.py b/arcade/experimental/sprite_depth_cosine.py index 0574d8d60..27e332946 100644 --- a/arcade/experimental/sprite_depth_cosine.py +++ b/arcade/experimental/sprite_depth_cosine.py @@ -15,6 +15,7 @@ """ import math import arcade +from pyglet.graphics import Batch # All constants are in pixels @@ -35,10 +36,17 @@ def __init__(self): super().__init__(WIDTH, HEIGHT, "Sprite Depth Testing Example w/ a Cosine Wave") texture = arcade.load_texture(":resources:images/test_textures/xy_square.png") + self.text_batch = Batch() + + self.use_depth: bool = True + self.text_use_depth = arcade.Text( + "SPACE: Toggle depth testing (True)", + start_x=10, start_y=30, font_size=15, color=arcade.color.WHITE, + batch=self.text_batch + ) self.sprite_list = arcade.SpriteList() self.time = 0.0 - self.use_depth: bool = True for i in range(NUM_SPRITES): sprite = arcade.Sprite( @@ -65,9 +73,12 @@ def on_draw(self): arcade.color.WHITE, DOT_SIZE ) + self.text_batch.draw() + def on_key_press(self, symbol: int, modifiers: int): if symbol == arcade.key.SPACE: self.use_depth = not self.use_depth + self.text_use_depth.text = f"SPACE: Toggle depth testing ({self.use_depth})" def on_update(self, delta_time): self.time += delta_time