Skip to content

Commit

Permalink
Remove sprite texture draw (pythonarcade#2161)
Browse files Browse the repository at this point in the history
* Remove draw functions for sprite and texture.

* Fix full_screen_example

* Fix net_process_animal_facts example

* Fix drawing_primitives

* remove debug print

* Fix gui surface

* fix light_demo

* fix background_blending

* fix background_groups example

* fix background_parallax

* Fix background_scrolling

* fix background_stationary

* Always enable alpha blending for background images

* Fix sprite properties example

* Fix sprite_rooms.py example

* Fix dual_stick_shooter

* Fix particle_systems

* Fix minimap

* Fix sprite_animated_keyframes

* Fix sprite_collect_coins_background

* Fix sprite_collect_coins_diff_levels

* Fix unit tests

* fix sprite_collect_coins_background
  • Loading branch information
einarf authored Jun 30, 2024
1 parent 9ec913d commit 9307dea
Show file tree
Hide file tree
Showing 32 changed files with 209 additions and 415 deletions.
12 changes: 6 additions & 6 deletions arcade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def configure_logging(level: Optional[int] = None):
from .draw import draw_lines
from .draw import draw_lrbt_rectangle_filled
from .draw import draw_lrbt_rectangle_outline
from .draw import draw_lbwh_rectangle_textured
from .draw import draw_parabola_filled
from .draw import draw_parabola_outline
from .draw import draw_point
Expand All @@ -131,8 +130,9 @@ def configure_logging(level: Optional[int] = None):
from .draw import draw_polygon_outline
from .draw import draw_rect_filled
from .draw import draw_rect_outline
from .draw import draw_scaled_texture_rectangle
from .draw import draw_texture_rectangle
from .draw import draw_texture_rect
from .draw import draw_sprite
from .draw import draw_sprite_rect
from .draw import draw_triangle_filled
from .draw import draw_triangle_outline
from .draw import draw_lbwh_rectangle_filled
Expand Down Expand Up @@ -312,7 +312,6 @@ def configure_logging(level: Optional[int] = None):
"draw_line",
"draw_line_strip",
"draw_lines",
"draw_lbwh_rectangle_textured",
"draw_lrbt_rectangle_filled",
"draw_lrbt_rectangle_filled",
"draw_lrbt_rectangle_outline",
Expand All @@ -325,9 +324,10 @@ def configure_logging(level: Optional[int] = None):
"draw_polygon_outline",
"draw_rect_filled",
"draw_rect_outline",
"draw_scaled_texture_rectangle",
"draw_text",
"draw_texture_rectangle",
"draw_texture_rect",
"draw_sprite",
"draw_sprite_rect",
"draw_triangle_filled",
"draw_triangle_outline",
"draw_lbwh_rectangle_filled",
Expand Down
11 changes: 10 additions & 1 deletion arcade/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def __init__(
self.sprite_list_program_cull["sprite_texture"] = 0
self.sprite_list_program_cull["uv_texture"] = 1

self.sprite_program_single = self.load_program(
vertex_shader=":system:shaders/sprites/sprite_single_vs.glsl",
geometry_shader=":system:shaders/sprites/sprite_list_geometry_no_cull_geo.glsl",
fragment_shader=":system:shaders/sprites/sprite_list_geometry_fs.glsl",
)
self.sprite_program_single["sprite_texture"] = 0
self.sprite_program_single["uv_texture"] = 1
self.sprite_program_single["spritelist_color"] = 1.0, 1.0, 1.0, 1.0

# Shapes
self.shape_line_program: Program = self.load_program(
vertex_shader=":system:shaders/shapes/line/unbuffered_vs.glsl",
Expand Down Expand Up @@ -185,7 +194,7 @@ def __init__(
self.shape_rectangle_filled_unbuffered_geometry: Geometry = self.geometry(
[BufferDescription(self.shape_rectangle_filled_unbuffered_buffer, "2f", ["in_vert"])]
)
self.atlas_geometry: Geometry = self.geometry()
self.geometry_empty: Geometry = self.geometry()

self._atlas: Optional[TextureAtlasBase] = None
# Global labels we modify in `arcade.draw_text`.
Expand Down
14 changes: 6 additions & 8 deletions arcade/draw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
draw_rect_filled,
draw_rect_outline_kwargs,
draw_rect_filled_kwargs,
draw_scaled_texture_rectangle,
draw_texture_rectangle,
# draw_texture_rect
draw_lbwh_rectangle_textured,
draw_texture_rect,
draw_sprite,
draw_sprite_rect,
)
from .helpers import get_points_for_thick_line
from .screenshot import get_pixel, get_image
Expand Down Expand Up @@ -70,10 +69,9 @@
"draw_rect_filled",
"draw_rect_outline_kwargs",
"draw_rect_filled_kwargs",
"draw_scaled_texture_rectangle",
"draw_texture_rectangle",
# draw_texture_rect
"draw_lbwh_rectangle_textured",
"draw_texture_rect",
"draw_sprite",
"draw_sprite_rect",
# helpers
"get_points_for_thick_line",
# screenshot
Expand Down
202 changes: 88 additions & 114 deletions arcade/draw/rect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,100 @@

from arcade import gl
from arcade.math import rotate_point
from arcade.types import RGBA255, PointList, AsFloat, Rect, LRBT, LBWH, Color
from arcade.types import RGBA255, PointList, AsFloat, Rect, LRBT, LBWH, XYWH, Color
from arcade.texture import Texture
from arcade.sprite import BasicSprite
from arcade.window_commands import get_window
from .helpers import _generic_draw_line_strip
from arcade.color import WHITE


def draw_texture_rect(
texture: Texture,
rect: Rect,
*,
color: Color = WHITE,
angle=0.0,
blend=True,
alpha=1.0,
pixelated=False,
) -> None:
"""
Draw a texture on a rectangle.
:param texture: identifier of texture returned from load_texture() call
:param rect: Rectangle to draw the texture on.
:param color: Color of the texture. Defaults to white.
:param angle: Rotation of the texture in degrees. Defaults to zero.
:param blend: If True, enable alpha blending. Defaults to True.
:param alpha: Transparency of image. 0.0 is fully transparent, 1.0 (default) is visible.
"""
ctx = get_window().ctx
if blend:
ctx.enable(ctx.BLEND)

atlas = ctx.default_atlas

texture_id, _ = ctx.default_atlas.add(texture)
if pixelated:
atlas.texture.filter = gl.NEAREST, gl.NEAREST
else:
atlas.texture.filter = gl.LINEAR, gl.LINEAR

atlas.texture.use(unit=0)
atlas.use_uv_texture(unit=1)

geometry = ctx.geometry_empty
program = ctx.sprite_program_single
program["pos"] = rect.center_x, rect.center_y, 0
program["color"] = color.normalized
program["size"] = rect.width, rect.height
program["angle"] = angle
program["texture_id"] = float(texture_id)
program["spritelist_color"] = 1.0, 1.0, 1.0, alpha

geometry.render(program, mode=gl.POINTS, vertices=1)

# if blend:
# ctx.disable(ctx.BLEND)


def draw_sprite(sprite: BasicSprite, *, blend: bool = True, alpha=1.0, pixelated=False) -> None:
"""
Draw a sprite.
:param sprite: The sprite to draw.
"""
draw_texture_rect(
sprite.texture,
rect=XYWH(sprite.center_x, sprite.center_y, sprite.width, sprite.height),
color=sprite.color,
angle=sprite._angle,
blend=blend,
alpha=alpha,
pixelated=pixelated,
)


def draw_sprite_rect(
sprite: BasicSprite, rect: Rect, *, blend: bool = True, alpha=1.0, pixelated=False
) -> None:
"""
Draw a sprite.
:param sprite: The sprite to draw.
"""
draw_texture_rect(
sprite.texture,
rect=rect,
color=sprite.color,
angle=sprite._angle,
blend=blend,
alpha=alpha,
pixelated=pixelated,
)


def draw_lrbt_rectangle_outline(
left: float,
right: float,
Expand Down Expand Up @@ -102,119 +189,6 @@ def draw_lbwh_rectangle_filled(
draw_rect_filled(LBWH(left, bottom, width, height), color)


def draw_scaled_texture_rectangle(
center_x: float,
center_y: float,
texture: Texture,
scale: float = 1.0,
angle: float = 0,
alpha: int = 255,
) -> None:
"""
Draw a textured rectangle on-screen.
.. warning:: This method can be slow!
Most users should consider using
:py:class:`arcade.Sprite` with
:py:class:`arcade.SpriteList` instead of this
function.
OpenGL accelerates drawing by using batches to draw multiple things
at once. This method doesn't do that.
If you need finer control or less overhead than arcade allows,
consider `pyglet's batching features
<https://pyglet.readthedocs.io/en/master/modules/graphics/index.html#batches-and-groups>`_.
:param center_x: x coordinate of rectangle center.
:param center_y: y coordinate of rectangle center.
:param texture: identifier of texture returned from
load_texture() call
:param scale: scale of texture
:param angle: rotation of the rectangle (clockwise). Defaults to zero.
:param alpha: Transparency of image. 0 is fully transparent,
255 (default) is fully visible
"""
texture.draw_scaled(center_x, center_y, scale, angle, alpha)


def draw_texture_rectangle(
center_x: float,
center_y: float,
width: float,
height: float,
texture: Texture,
angle: float = 0,
alpha: int = 255,
) -> None:
"""
Draw a textured rectangle on-screen.
:param center_x: x coordinate of rectangle center.
:param center_y: y coordinate of rectangle center.
:param width: width of texture
:param height: height of texture
:param texture: identifier of texture returned from load_texture() call
:param angle: rotation of the rectangle. Defaults to zero (clockwise).
:param alpha: Transparency of image. 0 is fully transparent, 255 (default) is visible
"""
texture.draw_sized(center_x, center_y, width, height, angle, alpha)


# def draw_texture_rect(texture: Texture, rect: Rect, blend=True) -> None:
# """
# Draw a texture on a rectangle.

# :param texture: identifier of texture returned from load_texture() call
# :param rect: Rectangle to draw the texture on.
# """
# ctx = get_window().ctx
# if blend:
# ctx.enable(ctx.BLEND)

# texture_id, region = ctx.default_atlas.add(texture)

# ctx.default_atlas.use_uv_texture(unit=0)
# program = None # texture program
# # program["texture"] = 0
# # program["alpha"] = 1.0
# geometry = None # texture geometry
# # geometry.render(program, mode=gl.GL_TRIANGLE_STRIP, vertices=4)

# if blend:
# ctx.disable(ctx.BLEND)


def draw_lbwh_rectangle_textured(
left: float,
bottom: float,
width: float,
height: float,
texture: Texture,
angle: float = 0,
alpha: int = 255,
) -> None:
"""
Draw a texture extending from bottom left to top right.
:param left: The x coordinate of the left edge of the rectangle.
:param bottom: The y coordinate of the bottom of the rectangle.
:param width: The width of the rectangle.
:param height: The height of the rectangle.
:param texture: identifier of texture returned from load_texture() call
:param angle: rotation of the rectangle. Defaults to zero (clockwise).
:param alpha: Transparency of image. 0 is fully transparent, 255 (default) is visible
"""

center_x = left + (width / 2)
center_y = bottom + (height / 2)
texture.draw_sized(center_x, center_y, width, height, angle=angle, alpha=alpha)


# Reference implementations: drawing of new Rect


def draw_rect_outline(
rect: Rect, color: RGBA255, border_width: float = 1, tilt_angle: float = 0
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/background_blending.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def on_draw(self):

self.background_1.draw()
self.background_2.draw()
self.player_sprite.draw()
arcade.draw_sprite(self.player_sprite)

def on_key_press(self, symbol: int, modifiers: int):
if symbol in (arcade.key.LEFT, arcade.key.A):
Expand Down
5 changes: 4 additions & 1 deletion arcade/examples/background_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ def on_draw(self):

self.camera.use()

self.ctx.enable(self.ctx.BLEND)
self.backgrounds.draw()
self.player_sprite.draw()
self.ctx.disable(self.ctx.BLEND)

arcade.draw_sprite(self.player_sprite)

def on_key_press(self, symbol: int, modifiers: int):
# Support arrow keys and ASWD
Expand Down
3 changes: 2 additions & 1 deletion arcade/examples/background_parallax.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ def on_draw(self):
bg.pos = self.camera.bottom_left # Follow the car to fake infinity

# Draw the background & the player's car
self.ctx.enable(self.ctx.BLEND)
bg.draw()
self.player_sprite.draw(pixelated=True)
arcade.draw_sprite(self.player_sprite, pixelated=True)

def update_car_direction(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/background_scrolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def on_draw(self):
self.background.texture.offset = self.camera.bottom_left

self.background.draw()
self.player_sprite.draw()
arcade.draw_sprite(self.player_sprite)

def on_key_press(self, symbol: int, modifiers: int):
if symbol in (arcade.key.LEFT, arcade.key.A):
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/background_stationary.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def on_draw(self):
self.camera.use()

self.background.draw()
self.player_sprite.draw()
arcade.draw_sprite(self.player_sprite)

def on_key_press(self, symbol: int, modifiers: int):
if symbol in (arcade.key.LEFT, arcade.key.A):
Expand Down
4 changes: 2 additions & 2 deletions arcade/examples/drawing_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
arcade.draw_text("draw_bitmap", 483, 3, arcade.color.BLACK, 12)
texture = arcade.load_texture(":resources:images/space_shooter/playerShip1_orange.png")
scale = .6
arcade.draw_scaled_texture_rectangle(540, 120, texture, scale, 0)
arcade.draw_scaled_texture_rectangle(540, 60, texture, scale, 45)
arcade.draw_texture_rect(texture, arcade.XYWH(540, 120, texture.width, texture.height).scale(scale))
arcade.draw_texture_rect(texture, arcade.XYWH(540, 60, texture.width, texture.height).scale(scale), angle=45)

# Finish the render.
# Nothing will be drawn without this.
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/dual_stick_shooter.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def on_draw(self):
# draw game items
self.bullet_list.draw()
self.enemy_list.draw()
self.player.draw()
arcade.draw_sprite(self.player)

# Put the score on the screen.
output = f"Score: {self.score}"
Expand Down
Loading

0 comments on commit 9307dea

Please sign in to comment.