Introduction
Pyglet is a simple yet powerful library for creating aesthetically rich GUI programs on Windows, Mac OS, and Linux, such as games and multimedia. This Python-only package includes capabilities such as windowing, user interface event management, joysticks, OpenGL graphics, loading photos and movies, and playing sounds and music. Pyglet is released under the BSD open-source licence, which allows it to be used in both commercial and open-source projects with few restrictions. The article explains the details of the pyglet.graphics.allocation module, different classes of pyglet.graphics.allocation, along with its predefined functions.
pyglet.graphics.allocation
It defines an algorithm for allocating memory for vertex arrays and buffers. The area allocator is used to distribute vertex indices over many buffers in a vertex domain. (Any abstract buffer supplied by pyglet.graphics.vertexbuffer is referred to as a "buffer"). The allocator will occasionally ask the buffers for more space. When there isn't enough room to fulfil an allocation, the current policy is to double the buffer size. The buffer is never shrunk in size. The allocator merely keeps track of free space; the caller is responsible for keeping track of the allocated regions.
1. exceptionAllocatorMemoryException(requested_capacity)
The buffer is insufficient to complete an allocation.
When an operation fails owing to a lack of buffer space, Allocator methods raise this exception. After increasing the buffer to at least the requested capacity, the procedure should be retried (guaranteed to pass a second time).
2. classAllocator(capacity)
It is the implementation of buffer space allocation.
alloc(size)
In the buffer, it is used to allocate memory. If the allocation cannot be completed, an AllocatorMemoryException is thrown.
Parameters:
- size (int) – It represents the size of the region to allocate.
Return type: int
Returns: It returns the starting index of the allocated region.
dealloc(start, size)
It is used to deallocate a region of the buffer.
Parameters:
- start (int) – This is the starting index of the region.
- size (int) – This represents the size of the region.
get_allocated_regions()
It is used to retrieve a list of (aggregate) allocated regions. This method yields (starts, sizes), where starts is a list of the area starting indices and sizes are their corresponding lengths.
Return type: (list, list)
get_fragmentation()
Return the percentage of vacant space that cannot be expanded.
Return type: float
get_fragmented_free_size()
It returns the amount of space unused, excluding the final free block.
Return type: int
get_free_size()
Return the unused space amount.
Return type: int
get_usage()
Return a percentage of the current allocated capacity.
Return type: float
realloc(start, size, new_size)
It is used to reallocate some buffer space. The region may often be resized in-place. This is more efficient than making separate dealloc and alloc calls. If the allocation cannot be completed, an AllocatorMemoryException is thrown.
Parameters:
- start (int) – This is the current starting index of the region.
- size (int) – This represents the Current size of the region.
- new_size (int) – It is the new size of the region.
set_capacity(size)
It is used to resize the buffer size limit. It is impossible to limit the capacity.
Parameters:
- size (int) – The new buffer's maximum size.