pyglet update

This commit is contained in:
shenjack 2022-11-20 17:45:54 +08:00
parent 9b1d8edd25
commit 0203253cc9
7 changed files with 68 additions and 196 deletions

View File

@ -0,0 +1,5 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by shenjack.
--- DateTime: 2022/11/20 11:57
---

View File

@ -1985,19 +1985,11 @@ class BufferManager:
def __init__(self):
self.color_buffer = None
self.depth_buffer = None
stencil_bits = GLint()
glGetFramebufferAttachmentParameteriv(
GL_DRAW_FRAMEBUFFER,
GL_STENCIL,
GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
stencil_bits,
)
self.free_stencil_bits = list(range(stencil_bits.value))
self.free_stencil_bits = None
self.refs = []
def get_viewport(self):
@staticmethod
def get_viewport():
"""Get the current OpenGL viewport dimensions.
:rtype: 4-tuple of float.
@ -2044,20 +2036,31 @@ class BufferManager:
:rtype: :py:class:`~pyglet.image.BufferImageMask`
"""
if self.free_stencil_bits is None:
try:
stencil_bits = GLint()
glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
GL_STENCIL,
GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
stencil_bits)
self.free_stencil_bits = list(range(stencil_bits.value))
except GLException:
pass
if not self.free_stencil_bits:
raise ImageException('No free stencil bits are available.')
stencil_bit = self.free_stencil_bits.pop(0)
x, y, width, height = self.get_viewport()
buffer = BufferImageMask(x, y, width, height)
buffer.stencil_bit = stencil_bit
bufimg = BufferImageMask(x, y, width, height)
bufimg.stencil_bit = stencil_bit
def release_buffer(ref, self=self):
self.free_stencil_bits.insert(0, stencil_bit)
def release_buffer(ref, owner=self):
owner.free_stencil_bits.insert(0, stencil_bit)
self.refs.append(weakref.ref(buffer, release_buffer))
self.refs.append(weakref.ref(bufimg, release_buffer))
return buffer
return bufimg
def get_buffer_manager():

View File

@ -376,7 +376,7 @@ class HIDDevice:
return elements
# Page and usage IDs are from the HID usage tables located at
# http://www.usb.org/developers/devclass_docs/Hut1_12.pdf
# https://usb.org/sites/default/files/hut1_3_0.pdf
def conforms_to(self, page, usage):
return bool(iokit.IOHIDDeviceConformsTo(self.deviceRef, page, usage))

View File

@ -230,9 +230,9 @@ class Vec2:
:returns: A new rotated vector of the same magnitude.
:rtype: Vec2
"""
mag = self.mag
heading = self.heading
return Vec2(mag * _math.cos(heading + angle), mag * _math.sin(heading + angle))
s = _math.sin(angle)
c = _math.cos(angle)
return Vec2(c * self.x - s * self.y, s * self.x + c * self.y)
def distance(self, other: Vec2) -> float:
"""Calculate the distance between this vector and another 2D vector."""

View File

@ -119,6 +119,7 @@ def ffmpeg_init():
protocols."""
pass
class MemoryFileObject:
"""A class to manage reading and seeking of a ffmpeg file object."""
buffer_size = 32768
@ -210,98 +211,6 @@ def ffmpeg_open_memory_file(filename, file_object):
return file, memory_file
class MemoryFileObject:
"""A class to manage reading and seeking of a ffmpeg file object."""
buffer_size = 32768
def __init__(self, file):
self.file = file
self.fmt_context = None
self.buffer = None
print("File object:", file)
if not getattr(self.file, 'seek', None) or not getattr(self.file, 'tell', None):
raise Exception("File object does not support seeking.")
# Seek to end of file to get the filesize.
self.file.seek(0, 2)
self.file_size = self.file.tell()
self.file.seek(0) # Put cursor back at the beginning.
def read_data_cb(_, buff, buf_size):
data = self.file.read(buf_size)
read_size = len(data)
memmove(buff, data, read_size)
return read_size
def seek_data_cb(_, offset, whence):
if whence == libavformat.AVSEEK_SIZE:
return self.file_size
pos = self.file.seek(offset, whence)
return pos
self.read_func = libavformat.ffmpeg_read_func(read_data_cb)
self.seek_func = libavformat.ffmpeg_seek_func(seek_data_cb)
def __del__(self):
"""These are usually freed when the source is, but no guarantee."""
if self.buffer:
try:
avutil.av_freep(self.buffer)
except OSError:
pass
if self.fmt_context:
try:
avutil.av_freep(self.fmt_context)
except OSError:
pass
def ffmpeg_open_memory_file(filename, file_object):
"""Open a media file from a file object.
:rtype: FFmpegFile
:return: The structure containing all the information for the media.
"""
file = FFmpegFile()
file.context = libavformat.avformat.avformat_alloc_context()
file.context.contents.seekable = 1
memory_file = MemoryFileObject(file_object)
av_buf = libavutil.avutil.av_malloc(memory_file.buffer_size)
memory_file.buffer = cast(av_buf, c_char_p)
ptr = create_string_buffer(memory_file.buffer_size)
memory_file.fmt_context = libavformat.avformat.avio_alloc_context(
memory_file.buffer,
memory_file.buffer_size,
0,
ptr,
memory_file.read_func,
None,
memory_file.seek_func
)
file.context.contents.pb = memory_file.fmt_context
file.context.contents.flags |= libavformat.AVFMT_FLAG_CUSTOM_IO
result = avformat.avformat_open_input(byref(file.context), filename, None, None)
if result != 0:
raise FFmpegException('avformat_open_input in ffmpeg_open_filename returned an error opening file '
+ filename.decode("utf8")
+ ' Error code: ' + str(result))
result = avformat.avformat_find_stream_info(file.context, None)
if result < 0:
raise FFmpegException('Could not find stream info')
return file, memory_file
def ffmpeg_open_filename(filename):
"""Open the media file.
@ -355,8 +264,7 @@ def ffmpeg_file_info(file):
info.title = asstr(entry.contents.value)
entry = avutil.av_dict_get(file.context.contents.metadata, asbytes('artist'), None, 0) \
or \
avutil.av_dict_get(file.context.contents.metadata, asbytes('album_artist'), None, 0)
or avutil.av_dict_get(file.context.contents.metadata, asbytes('album_artist'), None, 0)
if entry:
info.author = asstr(entry.contents.value)

View File

@ -214,38 +214,25 @@ class ShapeBase(ABC):
_anchor_y = 0
_batch = None
_group = None
_num_verts = 0
_vertex_list = None
def __del__(self):
if self._vertex_list is not None:
self._vertex_list.delete()
@abstractmethod
def _update_color(self):
"""
Send the new colors for each vertex to the GPU.
"""Send the new colors for each vertex to the GPU.
This method must set the contents of `self._vertex_list.colors`
using a list or tuple that contains the RGBA color components
for each vertex in the shape. This is usually done by repeating
`self._rgba` for each vertex. See the `ShapeBase` subclasses in
this module for examples of how to do this.
`self._rgba` for each vertex.
"""
raise NotImplementedError("_update_color must be defined"
"for every ShapeBase subclass")
self._vertex_list.colors[:] = self._rgba * self._num_verts
@abstractmethod
def _update_position(self):
"""
Generate up-to-date vertex positions & send them to the GPU.
This method must set the contents of `self._vertex_list.translation`
using a list or tuple that contains the new translation values for
each vertex in the shape. See the `ShapeBase` subclasses in this
module for examples of how to do this.
"""
raise NotImplementedError("_update_position must be defined"
"for every ShapeBase subclass")
def _update_translation(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
@abstractmethod
def _update_vertices(self):
@ -285,7 +272,7 @@ class ShapeBase(ABC):
@x.setter
def x(self, value):
self._x = value
self._update_position()
self._update_translation()
@property
def y(self):
@ -298,7 +285,7 @@ class ShapeBase(ABC):
@y.setter
def y(self, value):
self._y = value
self._update_position()
self._update_translation()
@property
def position(self):
@ -315,7 +302,7 @@ class ShapeBase(ABC):
@position.setter
def position(self, values):
self._x, self._y = values
self._update_position()
self._update_translation()
@property
def anchor_x(self):
@ -479,15 +466,9 @@ class Arc(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
vertices = (0,) * self._segments * 4
vertices = (0,) * (self._segments + 1) * 4
else:
x = -self._anchor_x
y = -self._anchor_y
@ -527,6 +508,32 @@ class Arc(ShapeBase):
self._rotation = rotation
self._vertex_list.rotation[:] = (rotation,) * self._num_verts
@property
def angle(self):
"""The angle of the arc.
:type: float
"""
return self._angle
@angle.setter
def angle(self, value):
self._angle = value
self._update_vertices()
@property
def start_angle(self):
"""The start angle of the arc.
:type: float
"""
return self._start_angle
@start_angle.setter
def start_angle(self, angle):
self._start_angle = angle
self._update_vertices()
def draw(self):
"""Draw the shape at its current position.
@ -581,12 +588,6 @@ class Circle(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
vertices = (0,) * self._segments * 6
@ -670,12 +671,6 @@ class Ellipse(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
vertices = (0,) * self._num_verts * 4
@ -802,12 +797,6 @@ class Sector(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
vertices = (0,) * self._segments * 6
@ -935,12 +924,6 @@ class Line(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
self._vertex_list.vertices[:] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
@ -1036,12 +1019,6 @@ class Rectangle(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
self._vertex_list.vertices[:] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
@ -1176,9 +1153,6 @@ class BorderedRectangle(ShapeBase):
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * 4 + self._border_rgba * 4
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
self._vertex_list.vertices[:] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
@ -1350,12 +1324,6 @@ class Triangle(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * 3
def _update_vertices(self):
if not self._visible:
self._vertex_list.vertices[:] = (0, 0, 0, 0, 0, 0)
@ -1473,12 +1441,6 @@ class Star(ShapeBase):
translation=('f', (x, y) * self._num_verts))
self._update_vertices()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
vertices = (0, 0) * self._num_spikes * 6
@ -1586,12 +1548,6 @@ class Polygon(ShapeBase):
self._update_vertices()
self._update_color()
def _update_color(self):
self._vertex_list.colors[:] = self._rgba * self._num_verts
def _update_position(self):
self._vertex_list.translation[:] = (self._x, self._y) * self._num_verts
def _update_vertices(self):
if not self._visible:
self._vertex_list.vertices[:] = tuple([0] * ((len(self._coordinates) - 2) * 6))

View File

@ -263,7 +263,7 @@ class SpriteGroup(graphics.Group):
self.blend_dest == other.blend_dest)
def __hash__(self):
return hash((id(self.parent), id(self.program),
return hash((self.parent, id(self.program),
self.texture.id, self.texture.target,
self.blend_src, self.blend_dest))