diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-28 23:34:19 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-28 23:34:19 +0200 |
commit | 27c7e16e4bd808ce1176f18b7a78c02ff4fa88ee (patch) | |
tree | a585d5ef853a0f8ffd5000cbdc9b0c4257163e53 /src/sliceitoff/text | |
parent | f894c2fd09ed17540c49cb4083573861ded76554 (diff) |
fonts under text, more sprite.Groups
Diffstat (limited to 'src/sliceitoff/text')
-rw-r--r-- | src/sliceitoff/text/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/text/fonts.py | 39 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 6 |
3 files changed, 43 insertions, 3 deletions
diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py index eb17c6e..e37e2e0 100644 --- a/src/sliceitoff/text/__init__.py +++ b/src/sliceitoff/text/__init__.py @@ -1 +1,2 @@ from .text import TextPage +from .fonts import Font, Fonts diff --git a/src/sliceitoff/text/fonts.py b/src/sliceitoff/text/fonts.py new file mode 100644 index 0000000..a1687b6 --- /dev/null +++ b/src/sliceitoff/text/fonts.py @@ -0,0 +1,39 @@ +import os +import pygame + +DEBUG = os.getenv("DEBUG") + +class Fonts: + fonts = {} + + @staticmethod + def load_fonts(base_path): + filename_fontlist = os.path.join(base_path, "assets", "fonts.lst") + with open( filename_fontlist ) as fontlist_file: + for line in fontlist_file: + name, *path = line.strip().split() + filename = os.path.join(base_path, *path) + __class__.fonts[name] = Font(filename) + return True + + +class Font: + def __init__(self, filename, height = 16): + if DEBUG: + print(f"Loading font {filename = }") + self.surfaces = [] + with open(filename, mode="rb") as fnt_file: + for _ in range(256): + surface = pygame.Surface((8,height), pygame.SRCALPHA) + for line in range(16): + byte = fnt_file.read(1)[0] + if line >= height: + continue + for bit in range(8): + if byte & 0x80: + surface.set_at((bit,line),"white") + byte <<= 1 + self.surfaces.append(surface) + + def get(self, ch): + return self.surfaces[ch%256] diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index da48f6c..dd9b719 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -1,8 +1,8 @@ import pygame from random import randrange -from images import Fonts -from display import Scaling, EGA_COLORS +from .fonts import Fonts +from display import Scaling, CGA_COLORS scaled_fonts = {} @@ -62,7 +62,7 @@ class TextPage(pygame.sprite.Group): Fonts.fonts[font].get(ch), size[0]/8 * Scaling.factor) scaled_fonts[font_key][ch].fill( - EGA_COLORS[color], + CGA_COLORS[color], special_flags = pygame.BLEND_RGBA_MULT) image = scaled_fonts[font_key][ch] image_pos = Scaling.scale_to_display( (x+col*w, y+row*h) ) |