# Copyright 2004-2026 Tom Rothamel <pytom@bishoujo.us>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from renpy.uguu.gl cimport *
from renpy.gl2.gl2shader cimport Program
from renpy.gl2.gl2model cimport GL2Model
from renpy.gl2.gl2draw cimport GL2Draw
from renpy.pygame.surface cimport Surface

cdef class TextureLoader:

    # The draw object associated with this TextureLoader
    cdef GL2Draw draw

    # All the texture number currently allocated by this loader.
    cdef set allocated

    # A list of (number, generation) pairs for textures that need to be freed.
    cdef list free_list

    # The total size (in bytes) of all the textures that have been allocated
    # but not deallocated.
    cdef object total_texture_size

    # The program used for fast texture loading
    cdef Program ftl_program

    # The queue of textures that need to be loaded.
    cdef object texture_load_queue

    # The maximum size of a texture.
    cdef public GLint max_texture_width
    cdef public GLint max_texture_height

    cdef public GLfloat max_anisotropy


cdef class GLTexture(GL2Model):

    # The number of the texture in OpenGL.
    cdef public unsigned int number

    # Has this texture been loaded yet?
    cdef public bint loaded

    # If we are doing in-place loading, this is the data that's used for
    # that.
    cdef Surface surface

    # The texture loader associated with this texture.
    cdef TextureLoader loader

    # The width and height of the texture. (Which may be a different size
    # than the model, if the texture is being rendered in the drawable
    # space.
    cdef public int texture_width
    cdef public int texture_height

    # Borders
    cdef public int bl
    cdef public int bt
    cdef public int br
    cdef public int bb

    # The last texture states set in gl2uniform.
    cdef public GLint wrap_s
    cdef public GLint wrap_t
    cdef public GLfloat anisotropy

    cdef public GLint mag_filter
    cdef public GLint min_filter
    cdef public GLint default_mag_filter
    cdef public GLint default_min_filter

    cdef bint has_mipmaps(self)

    cdef bint from_yuv_plane_pointer(self, const unsigned char *data, int width, int height, GLenum format, bint mipmap)

    cpdef subsurface(GLTexture self, t)

    cpdef GL2Model get_texture(self, int i)
