view pytouhou/ui/sdl/texture.pyx @ 792:11bc22bad1bf

python: Replace the image crate with png We weren’t using any of its features anyway, so the png crate is exactly what we need, without the many heavy dependencies of image. https://github.com/image-rs/image-png/pull/670 will eventually make it even faster to build.
author Link Mauve <linkmauve@linkmauve.fr>
date Sat, 17 Jan 2026 22:22:25 +0100
parents 7e940ebeb5fd
children
line wrap: on
line source

# -*- encoding: utf-8 -*-
##
## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

from pytouhou.lib.sdl cimport create_rgba_surface
from pytouhou.lib.sdl import SDLError
from pytouhou.game.text cimport NativeText

import os

from pytouhou.utils.helpers import get_logger
logger = get_logger(__name__)


cdef class TextureManager:
    def __init__(self, loader, window):
        self.loader = loader
        self.window = window


    cdef bint load(self, dict anms) except True:
        for anm in sorted(anms.values(), key=is_ascii):
            for entry in anm:
                if entry.texture is None:
                    texture = decode_png(self.loader, entry.first_name, entry.secondary_name)
                #elif not isinstance(entry.texture, self.texture_class):
                #    texture = entry.texture
                entry.texture = self.load_texture(texture)
        anms.clear()


    cdef load_texture(self, Surface surface):
        return self.window.create_texture_from_surface(surface)


def is_ascii(anm):
    return anm[0].first_name.endswith('ascii.png')


cdef class FontManager:
    def __init__(self, fontname, fontsize=16, window=None):
        self.font = Font(fontname, fontsize)
        self.window = window


    cdef bint load(self, dict labels) except True:
        cdef NativeText label

        for i, label in labels.items():
            if label.texture is None:
                try:
                    surface = self.font.render(label.text)
                except SDLError as e:
                    logger.error(u'Rendering of label “%s” failed: %s', label.text, e)
                    del labels[i]  # Prevents it from retrying to render.
                    continue

                label.width, label.height = surface.surface.w, surface.surface.h

                if label.align == 'center':
                    label.x -= label.width // 2
                elif label.align == 'right':
                    label.x -= label.width
                else:
                    assert label.align == 'left'

                label.texture = self.window.create_texture_from_surface(surface)


cdef Surface decode_png(loader, first_name, secondary_name):
    if secondary_name:
        image = loader.get_image_with_alpha(first_name, secondary_name)
    else:
        image = loader.get_image(first_name)
    width, height = image.dimensions
    return create_rgba_surface(image.pixels, width, height)