02202e4d3d
- Added `image_clean.png` to the output directory for the clean image representation. - Added `image_clean_preview.png` for the preview of the clean image. - Introduced `image_svg_clean.png` for the SVG clean image representation.
857 lines
36 KiB
Python
857 lines
36 KiB
Python
import os
|
|
import random
|
|
import ctypes
|
|
from ctypes import *
|
|
|
|
import sdl2
|
|
import sdl2.ext
|
|
from sdl2.ext.compat import byteify
|
|
from sdl2 import SDL_AudioSpec
|
|
from PIL import Image
|
|
|
|
from runtime_paths import resolve_bundle_path
|
|
|
|
|
|
class GameWindow:
|
|
def __init__(self, width, height, cell_size, title="Default", key_callback=None):
|
|
# Display configuration
|
|
self.cell_size = cell_size
|
|
self.width = width * cell_size
|
|
self.height = height * cell_size
|
|
|
|
# Screen resolution handling
|
|
actual_screen_size = os.environ.get("RESOLUTION", "640x480").split("x")
|
|
actual_screen_size = tuple(map(int, actual_screen_size))
|
|
self.target_size = actual_screen_size if self.width > actual_screen_size[0] or self.height > actual_screen_size[1] else (self.width, self.height)
|
|
|
|
# View offset calculations
|
|
self.w_start_offset = (self.target_size[0] - self.width) // 2
|
|
self.h_start_offset = (self.target_size[1] - self.height) // 2
|
|
self.w_offset = self.w_start_offset
|
|
self.h_offset = self.h_start_offset
|
|
self.max_w_offset = self.target_size[0] - self.width
|
|
self.max_h_offset = self.target_size[1] - self.height
|
|
self.scale = self.target_size[1] // self.cell_size
|
|
|
|
# Cached viewport bounds for fast visibility checks
|
|
self._update_viewport_bounds()
|
|
|
|
print(f"Screen size: {self.width}x{self.height}")
|
|
|
|
# SDL2 initialization
|
|
sdl2.ext.init(joystick=True)
|
|
sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO)
|
|
|
|
# Window and renderer setup
|
|
self.window = sdl2.ext.Window(title=title, size=self.target_size)
|
|
# self.window.show()
|
|
self.renderer = sdl2.ext.Renderer(self.window, flags=sdl2.SDL_RENDERER_ACCELERATED)
|
|
self.factory = sdl2.ext.SpriteFactory(renderer=self.renderer)
|
|
|
|
# Font system
|
|
self.fonts = self.generate_fonts("assets/decterm.ttf")
|
|
|
|
# Initial loading dialog
|
|
# self.dialog("Loading assets...")
|
|
# self.renderer.present()
|
|
|
|
# Game state
|
|
self.running = True
|
|
self.delay = 30
|
|
self.performance = 0
|
|
self.last_status_text = ""
|
|
self.stats_sprite = None
|
|
self.mean_fps = 0
|
|
self.fpss = []
|
|
self.text_width = 0
|
|
self.text_height = 0
|
|
self.ammo_text = ""
|
|
|
|
# White flash effect state
|
|
self.white_flash_active = False
|
|
self.white_flash_start_time = 0
|
|
self.white_flash_opacity = 255
|
|
|
|
# Input handling
|
|
self.trigger = key_callback
|
|
self.button_cursor = [0, 0]
|
|
self.buttons = {}
|
|
|
|
# Audio system initialization
|
|
self._init_audio_system()
|
|
self.audio = True
|
|
# Input devices
|
|
self.load_joystick()
|
|
|
|
def _init_audio_system(self):
|
|
"""Initialize audio devices for different audio channels"""
|
|
audio_spec = SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048)
|
|
self.audio_devs = {}
|
|
self.audio_devs["base"] = sdl2.SDL_OpenAudioDevice(None, 0, audio_spec, None, 0)
|
|
self.audio_devs["effects"] = sdl2.SDL_OpenAudioDevice(None, 0, audio_spec, None, 0)
|
|
self.audio_devs["music"] = sdl2.SDL_OpenAudioDevice(None, 0, audio_spec, None, 0)
|
|
|
|
# ======================
|
|
# TEXTURE & IMAGE METHODS
|
|
# ======================
|
|
|
|
def create_texture(self, tiles: list, fill_color=None):
|
|
"""Create a texture from a list of tiles"""
|
|
bg_surface = sdl2.SDL_CreateRGBSurface(0, self.width, self.height, 32, 0, 0, 0, 0)
|
|
if fill_color is not None:
|
|
mapped_color = sdl2.SDL_MapRGB(bg_surface.contents.format, *fill_color)
|
|
sdl2.SDL_FillRect(bg_surface, None, mapped_color)
|
|
for tile in tiles:
|
|
dstrect = sdl2.SDL_Rect(tile[1], tile[2], self.cell_size, self.cell_size)
|
|
sdl2.SDL_BlitSurface(tile[0], None, bg_surface, dstrect)
|
|
bg_texture = self.factory.from_surface(bg_surface)
|
|
sdl2.SDL_FreeSurface(bg_surface)
|
|
return bg_texture
|
|
|
|
def create_color_surface(self, color, width=None, height=None):
|
|
"""Create a solid color surface matching the current cell size by default."""
|
|
width = width or self.cell_size
|
|
height = height or self.cell_size
|
|
image = Image.new("RGBA", (width, height), (*color, 255))
|
|
return sdl2.ext.pillow_to_surface(image)
|
|
|
|
def load_image(self, path, transparent_color=None, surface=False):
|
|
"""Load and process an image with optional transparency and scaling"""
|
|
image_path = resolve_bundle_path(os.path.join("assets", path))
|
|
image = Image.open(image_path)
|
|
|
|
# Handle transparency
|
|
if transparent_color:
|
|
image = image.convert("RGBA")
|
|
# Support single color tuple or sequence of color tuples
|
|
if isinstance(transparent_color[0], int):
|
|
color_set = {transparent_color}
|
|
else:
|
|
color_set = set(transparent_color)
|
|
datas = image.getdata()
|
|
new_data = [
|
|
(255, 255, 255, 0) if item[:3] in color_set else item
|
|
for item in datas
|
|
]
|
|
image.putdata(new_data)
|
|
|
|
# Scale image: tiles are now 64px (was 20px), multiply by 5/8 to reach cell_size (40px)
|
|
image = image.resize((image.width * 5 // 8, image.height * 5 // 8), Image.NEAREST)
|
|
|
|
if surface:
|
|
return sdl2.ext.pillow_to_surface(image)
|
|
temp_surface = sdl2.ext.pillow_to_surface(image)
|
|
texture = self.factory.from_surface(temp_surface)
|
|
sdl2.SDL_FreeSurface(temp_surface)
|
|
return texture
|
|
|
|
def get_image_size(self, image):
|
|
"""Get the size of an image sprite"""
|
|
return image.size
|
|
|
|
# ======================
|
|
# FONT MANAGEMENT
|
|
# ======================
|
|
|
|
def generate_fonts(self, font_file):
|
|
"""Generate font managers for different sizes"""
|
|
fonts = {}
|
|
font_path = str(resolve_bundle_path(font_file))
|
|
for i in range(10, 70, 1):
|
|
fonts.update({i: sdl2.ext.FontManager(font_path=font_path, size=i)})
|
|
return fonts
|
|
|
|
# ======================
|
|
# DRAWING METHODS
|
|
# ======================
|
|
|
|
def draw_text(self, text, font, position, color):
|
|
"""Draw text at specified position with given font and color"""
|
|
sprite = self.factory.from_text(text, color=color, fontmanager=font)
|
|
|
|
# Handle center positioning
|
|
if position == "center":
|
|
position = ("center", "center")
|
|
if position[0] == "center":
|
|
position = (self.target_size[0] // 2 - sprite.size[0] // 2, position[1])
|
|
if position[1] == "center":
|
|
position = (position[0], self.target_size[1] // 2 - sprite.size[1] // 2)
|
|
|
|
sprite.position = position
|
|
self.renderer.copy(sprite, dstrect=sprite.position)
|
|
|
|
def draw_background(self, bg_texture):
|
|
"""Draw background texture with current view offset"""
|
|
self.renderer.copy(bg_texture, dstrect=sdl2.SDL_Rect(self.w_offset, self.h_offset, self.width, self.height))
|
|
|
|
def draw_image(self, x, y, sprite, tag=None, anchor="nw", source_rect=None, dest_size=None):
|
|
"""Draw an image sprite at specified coordinates"""
|
|
if not self.is_in_visible_area(x, y):
|
|
return
|
|
if source_rect is not None:
|
|
src_x, src_y, src_w, src_h = (int(value) for value in source_rect)
|
|
dst_w, dst_h = dest_size or (src_w, src_h)
|
|
dstrect = sdl2.SDL_Rect(
|
|
int(x + self.w_offset),
|
|
int(y + self.h_offset),
|
|
int(dst_w),
|
|
int(dst_h),
|
|
)
|
|
srcrect = sdl2.SDL_Rect(src_x, src_y, src_w, src_h)
|
|
self.renderer.copy(sprite, srcrect=srcrect, dstrect=dstrect)
|
|
return
|
|
|
|
sprite.position = (x + self.w_offset, y + self.h_offset)
|
|
self.renderer.copy(sprite, dstrect=sprite.position)
|
|
|
|
def draw_rectangle(self, x, y, width, height, tag, outline="red", filling=None):
|
|
"""Draw a rectangle with optional fill and outline"""
|
|
if filling:
|
|
self.renderer.fill((x, y, width, height), sdl2.ext.Color(*filling))
|
|
else:
|
|
self.renderer.draw_rect((x, y, width, height), sdl2.ext.Color(*outline))
|
|
|
|
def draw_pointer(self, x, y):
|
|
"""Draw a red pointer rectangle at specified coordinates"""
|
|
x = x + self.w_offset
|
|
y = y + self.h_offset
|
|
for i in range(3):
|
|
self.renderer.draw_rect((x + i, y + i, self.cell_size - 2*i, self.cell_size - 2*i),
|
|
color=sdl2.ext.Color(255, 0, 0))
|
|
|
|
def delete_tag(self, tag):
|
|
"""Placeholder for tag deletion (not implemented)"""
|
|
pass
|
|
|
|
# ======================
|
|
# UI METHODS
|
|
# ======================
|
|
|
|
def dialog(self, text, **kwargs):
|
|
"""Display a dialog box with text and optional extras"""
|
|
# Draw dialog background
|
|
self.draw_rectangle(50, 50,
|
|
self.target_size[0] - 100, self.target_size[1] - 100,
|
|
"win", filling=(255, 255, 255))
|
|
|
|
# Calculate layout positions to avoid overlaps
|
|
title_y = self.target_size[1] // 4 # Title at 1/4 of screen height
|
|
|
|
# Draw main text (title)
|
|
self.draw_text(text, self.fonts[self.target_size[1]//20],
|
|
("center", title_y), sdl2.ext.Color(0, 0, 0))
|
|
|
|
# Draw image if provided - position it below title
|
|
image_bottom_y = title_y + 60 # Default position if no image
|
|
if image := kwargs.get("image"):
|
|
image_size = self.get_image_size(image)
|
|
image_y = title_y + 50
|
|
self.draw_image(self.target_size[0] // 2 - image_size[0] // 2 - self.w_offset,
|
|
image_y - self.h_offset,
|
|
image, "win")
|
|
image_bottom_y = image_y + image_size[1] + 20
|
|
|
|
# Draw subtitle if provided - handle multi-line text, position below image
|
|
if subtitle := kwargs.get("subtitle"):
|
|
subtitle_lines = subtitle.split('\n')
|
|
base_y = image_bottom_y + 20
|
|
line_height = 25 # Fixed line height for consistent spacing
|
|
|
|
for i, line in enumerate(subtitle_lines):
|
|
if line.strip(): # Only draw non-empty lines
|
|
self.draw_text(line.strip(), self.fonts[self.target_size[1]//35],
|
|
("center", base_y + i * line_height), sdl2.ext.Color(0, 0, 0))
|
|
image_bottom_y = base_y + len(subtitle_lines) * line_height
|
|
|
|
# Draw scores if provided - position at bottom
|
|
if scores := kwargs.get("scores"):
|
|
scores_start_y = min(image_bottom_y + 25, self.target_size[1] - 160)
|
|
sprite = self.factory.from_text("High Scores:", color=sdl2.ext.Color(0, 0, 0),
|
|
fontmanager=self.fonts[self.target_size[1]//25])
|
|
sprite.position = (self.target_size[0] // 2 - sprite.size[0] // 2, scores_start_y)
|
|
self.renderer.copy(sprite, dstrect=sprite.position)
|
|
|
|
for i, score in enumerate(scores[:5]):
|
|
if isinstance(score, dict):
|
|
score_text = f"{score.get('user_id', 'Guest')}: {score.get('best_score', 0)} pts"
|
|
elif len(score) >= 4: # New format: date, score, name, device
|
|
score_text = f"{score[2]}: {score[1]} pts ({score[3]})"
|
|
elif len(score) >= 3: # Medium format: date, score, name
|
|
score_text = f"{score[2]}: {score[1]} pts"
|
|
else: # Old format: date, score
|
|
score_text = f"Guest: {score[1]} pts"
|
|
|
|
self.draw_text(score_text, self.fonts[self.target_size[1]//45],
|
|
("center", scores_start_y + 30 + 25 * (i + 1)),
|
|
sdl2.ext.Color(0, 0, 0))
|
|
|
|
def start_dialog(self, **kwargs):
|
|
"""Display the welcome dialog"""
|
|
self.dialog("Welcome to the Mice!", subtitle="A game by Matteo because was bored", **kwargs)
|
|
|
|
def draw_button(self, x, y, text, width, height, coords):
|
|
"""Draw a button with text"""
|
|
# TODO: Fix outline parameter usage
|
|
color = (0, 0, 255) if self.button_cursor == list(coords) else (0, 0, 0)
|
|
self.draw_rectangle(x, y, width, height, "button", outline=color)
|
|
#self.draw_text(text, self.fonts[20], (x + 10, y + 10), (0, 0, 0))
|
|
|
|
def update_status(self, text):
|
|
"""Update and display the status bar with FPS information"""
|
|
fps = int(1000 / self.performance) if self.performance != 0 else 0
|
|
# at 10% of probability print fps
|
|
if len(self.fpss) > 20:
|
|
self.mean_fps = round(sum(self.fpss) / len(self.fpss)) if self.fpss else fps
|
|
#print(f"FPS: {self.mean_fps}")
|
|
self.fpss.clear()
|
|
else:
|
|
self.fpss.append(fps)
|
|
|
|
status_text = f"FPS: {self.mean_fps} - {text}"
|
|
if status_text != self.last_status_text:
|
|
self.last_status_text = status_text
|
|
font = self.fonts[20]
|
|
self.stats_sprite = self.factory.from_text(status_text, color=sdl2.ext.Color(0, 0, 0), fontmanager=font)
|
|
if self.text_width != self.stats_sprite.size[0] or self.text_height != self.stats_sprite.size[1]:
|
|
self.text_width, self.text_height = self.stats_sprite.size
|
|
# create a background for the status text using texture
|
|
self.stats_background = self.factory.from_color(sdl2.ext.Color(255, 255, 255), (self.text_width + 10, self.text_height + 4))
|
|
|
|
# self.renderer.fill((3, 3, self.text_width + 10, self.text_height + 4), sdl2.ext.Color(255, 255, 255))
|
|
self.renderer.copy(self.stats_background, dstrect=sdl2.SDL_Rect(3, 3, self.text_width + 10, self.text_height + 4))
|
|
self.renderer.copy(self.stats_sprite, dstrect=sdl2.SDL_Rect(8, 5, self.text_width, self.text_height))
|
|
|
|
def update_ammo(self, ammo, assets):
|
|
"""Update and display the ammo count"""
|
|
ammo_text = f"{ammo['bomb']['count']}/{ammo['bomb']['max']} {ammo['mine']['count']}/{ammo['mine']['max']} {ammo['gas']['count']}/{ammo['gas']['max']} "
|
|
if self.ammo_text != ammo_text:
|
|
self.ammo_text = ammo_text
|
|
font = self.fonts[20]
|
|
self.ammo_sprite = self.factory.from_text(ammo_text, color=sdl2.ext.Color(0, 0, 0), fontmanager=font)
|
|
text_width, text_height = self.ammo_sprite.size
|
|
self.ammo_background = self.factory.from_color(sdl2.ext.Color(255, 255, 255), (text_width + 10, text_height + 4))
|
|
text_width, text_height = self.ammo_sprite.size
|
|
position = (self.target_size[0] - text_width - 10, self.target_size[1] - text_height - 5)
|
|
#self.renderer.fill((position[0] - 5, position[1] - 2, text_width + 10, text_height + 4), sdl2.ext.Color(255, 255, 255))
|
|
self.renderer.copy(self.ammo_background, dstrect=sdl2.SDL_Rect(position[0] - 5, position[1] - 2, text_width + 10, text_height + 4))
|
|
self.renderer.copy(self.ammo_sprite, dstrect=sdl2.SDL_Rect(position[0], position[1], text_width, text_height))
|
|
self.renderer.copy(assets["BMP_BOMB0"], dstrect=sdl2.SDL_Rect(position[0]+25, position[1], 20, 20))
|
|
self.renderer.copy(assets["BMP_POISON"], dstrect=sdl2.SDL_Rect(position[0]+85, position[1], 20, 20))
|
|
self.renderer.copy(assets["BMP_GAS"], dstrect=sdl2.SDL_Rect(position[0]+140, position[1], 20, 20))
|
|
# ======================
|
|
# VIEW & NAVIGATION
|
|
# ======================
|
|
|
|
def _update_viewport_bounds(self):
|
|
"""Update cached viewport bounds for fast visibility checks"""
|
|
self.visible_x_min = -self.w_offset - self.cell_size
|
|
self.visible_x_max = self.width - self.w_offset
|
|
self.visible_y_min = -self.h_offset - self.cell_size
|
|
self.visible_y_max = self.height - self.h_offset
|
|
|
|
def scroll_view(self, pointer):
|
|
"""Adjust the view offset based on pointer coordinates"""
|
|
cell_x, cell_y = pointer
|
|
|
|
pointer_x = cell_x * self.cell_size
|
|
pointer_y = cell_y * self.cell_size
|
|
|
|
desired_w_offset = (self.target_size[0] - self.cell_size) // 2 - pointer_x
|
|
desired_h_offset = (self.target_size[1] - self.cell_size) // 2 - pointer_y
|
|
|
|
self.w_offset = max(self.max_w_offset, min(0, desired_w_offset))
|
|
self.h_offset = max(self.max_h_offset, min(0, desired_h_offset))
|
|
|
|
# Update cached bounds when viewport changes
|
|
self._update_viewport_bounds()
|
|
|
|
def is_in_visible_area(self, x, y):
|
|
"""Check if coordinates are within the visible area (optimized with cached bounds)"""
|
|
return (self.visible_x_min <= x <= self.visible_x_max and
|
|
self.visible_y_min <= y <= self.visible_y_max)
|
|
|
|
def get_view_center(self):
|
|
"""Get the center coordinates of the current view"""
|
|
return self.w_offset + self.width // 2, self.h_offset + self.height // 2
|
|
|
|
# ======================
|
|
# AUDIO METHODS
|
|
# ======================
|
|
|
|
def play_sound(self, sound_file, tag="base"):
|
|
"""Play a sound file on the specified audio channel"""
|
|
if not self.audio:
|
|
return
|
|
sound_path = str(resolve_bundle_path(os.path.join("sound", sound_file)))
|
|
rw = sdl2.SDL_RWFromFile(byteify(sound_path, "utf-8"), b"rb")
|
|
if not rw:
|
|
raise RuntimeError("Failed to open sound file")
|
|
|
|
_buf = POINTER(sdl2.Uint8)()
|
|
_length = sdl2.Uint32()
|
|
|
|
spec = SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048)
|
|
if sdl2.SDL_LoadWAV_RW(rw, 1, byref(spec), byref(_buf), byref(_length)) == None:
|
|
raise RuntimeError("Failed to load WAV")
|
|
|
|
devid = self.audio_devs[tag]
|
|
# Clear any queued audio
|
|
sdl2.SDL_ClearQueuedAudio(devid)
|
|
# Start playing audio
|
|
sdl2.SDL_QueueAudio(devid, _buf, _length)
|
|
sdl2.SDL_PauseAudioDevice(devid, 0)
|
|
|
|
def stop_sound(self):
|
|
"""Stop all audio playback"""
|
|
for dev in self.audio_devs.values():
|
|
sdl2.SDL_PauseAudioDevice(dev, 1)
|
|
sdl2.SDL_ClearQueuedAudio(dev)
|
|
|
|
# ======================
|
|
# INPUT METHODS
|
|
# ======================
|
|
|
|
def load_joystick(self):
|
|
"""Initialize joystick support"""
|
|
sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
|
|
sdl2.SDL_JoystickOpen(0)
|
|
|
|
# ======================
|
|
# MAIN GAME LOOP
|
|
# ======================
|
|
|
|
def mainloop(self, **kwargs):
|
|
"""Main game loop handling events and rendering"""
|
|
while self.running:
|
|
performance_start = sdl2.SDL_GetPerformanceCounter()
|
|
self.renderer.clear()
|
|
|
|
# Execute background update if provided
|
|
if "bg_update" in kwargs:
|
|
kwargs["bg_update"]()
|
|
|
|
# Execute main update
|
|
kwargs["update"]()
|
|
|
|
# Update and draw white flash effect
|
|
if self.update_white_flash():
|
|
self.draw_white_flash()
|
|
|
|
# Handle SDL events
|
|
events = sdl2.ext.get_events()
|
|
for event in events:
|
|
if event.type == sdl2.SDL_QUIT:
|
|
self.running = False
|
|
elif event.type == sdl2.SDL_KEYDOWN:
|
|
# print in file keycode
|
|
keycode = event.key.keysym.sym
|
|
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
|
|
key = key.replace(" ", "_")
|
|
# Check for Right Ctrl key to trigger white flash
|
|
self.trigger(f"keydown_{key}")
|
|
elif event.type == sdl2.SDL_KEYUP:
|
|
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
|
|
key = key.replace(" ", "_")
|
|
self.trigger(f"keyup_{key}")
|
|
elif event.type == sdl2.SDL_MOUSEMOTION:
|
|
self.trigger(f"mousemove_{event.motion.x}, {event.motion.y}")
|
|
elif event.type == sdl2.SDL_JOYBUTTONDOWN:
|
|
key = event.jbutton.button
|
|
self.trigger(f"joybuttondown_{key}")
|
|
elif event.type == sdl2.SDL_JOYBUTTONUP:
|
|
key = event.jbutton.button
|
|
self.trigger(f"joybuttonup_{key}")
|
|
elif event.type == sdl2.SDL_JOYHATMOTION:
|
|
hat = event.jhat.hat
|
|
value = event.jhat.value
|
|
self.trigger(f"joyhatmotion_{hat}_{value}")
|
|
|
|
|
|
|
|
# Present the rendered frame
|
|
self.renderer.present()
|
|
|
|
# Calculate performance and delay
|
|
self.performance = ((sdl2.SDL_GetPerformanceCounter() - performance_start) /
|
|
sdl2.SDL_GetPerformanceFrequency() * 1000)
|
|
|
|
delay = max(0, self.delay - round(self.performance))
|
|
sdl2.SDL_Delay(delay)
|
|
|
|
# ======================
|
|
# INTRO SCREEN
|
|
# ======================
|
|
|
|
def show_intro(self, path, duration_ms=2000, fade_ms=500, crop_center_y=520):
|
|
"""Show a fullscreen intro image with fade-in/out from black. Skip on keypress.
|
|
|
|
SDL scales the image to target width (proportional height), then crops
|
|
vertically around crop_center_y. No PIL resize — scaling is done by SDL
|
|
via srcrect/dstrect.
|
|
"""
|
|
img = Image.open(resolve_bundle_path(path)).convert("RGBA")
|
|
tw, th = self.target_size
|
|
iw, ih = img.size
|
|
|
|
# Compute scaled height at target width (aspect-correct), keep in source space
|
|
scaled_h = int(ih * tw / iw)
|
|
|
|
# Crop window in scaled coords, mapped back to source coords for srcrect
|
|
crop_y_scaled = max(0, min(crop_center_y - th // 2, scaled_h - th))
|
|
src_y = int(crop_y_scaled * ih / scaled_h)
|
|
src_h = max(1, int(th * ih / scaled_h))
|
|
|
|
surface = sdl2.ext.pillow_to_surface(img)
|
|
texture = self.factory.from_surface(surface)
|
|
sdl2.SDL_FreeSurface(surface)
|
|
srcrect = sdl2.SDL_Rect(0, src_y, iw, src_h)
|
|
dstrect = sdl2.SDL_Rect(0, 0, tw, th)
|
|
|
|
start = sdl2.SDL_GetTicks()
|
|
skipped = False
|
|
while True:
|
|
elapsed = sdl2.SDL_GetTicks() - start
|
|
if elapsed >= duration_ms:
|
|
break
|
|
|
|
# Compute black overlay alpha for fade-in / fade-out
|
|
if elapsed < fade_ms:
|
|
overlay_alpha = int(255 * (1.0 - elapsed / fade_ms))
|
|
elif elapsed > duration_ms - fade_ms:
|
|
overlay_alpha = int(255 * (elapsed - (duration_ms - fade_ms)) / fade_ms)
|
|
else:
|
|
overlay_alpha = 0
|
|
|
|
self.renderer.clear()
|
|
self.renderer.copy(texture, srcrect=srcrect, dstrect=dstrect)
|
|
if overlay_alpha > 0:
|
|
sdl2.SDL_SetRenderDrawBlendMode(
|
|
self.renderer.sdlrenderer, sdl2.SDL_BLENDMODE_BLEND)
|
|
sdl2.SDL_SetRenderDrawColor(
|
|
self.renderer.sdlrenderer, 0, 0, 0, overlay_alpha)
|
|
sdl2.SDL_RenderFillRect(self.renderer.sdlrenderer, None)
|
|
self.renderer.present()
|
|
sdl2.SDL_Delay(16)
|
|
|
|
for event in sdl2.ext.get_events():
|
|
if event.type == sdl2.SDL_QUIT:
|
|
self.running = False
|
|
return
|
|
elif event.type in (sdl2.SDL_KEYDOWN, sdl2.SDL_JOYBUTTONDOWN):
|
|
skipped = True
|
|
|
|
if skipped:
|
|
break
|
|
|
|
# Fade to black before returning (fast if skipped, already there if natural end)
|
|
fade_out_ms = fade_ms // 2 if skipped else 0
|
|
if fade_out_ms > 0:
|
|
fade_start = sdl2.SDL_GetTicks()
|
|
while True:
|
|
elapsed = sdl2.SDL_GetTicks() - fade_start
|
|
if elapsed >= fade_out_ms:
|
|
break
|
|
alpha = int(255 * elapsed / fade_out_ms)
|
|
self.renderer.clear()
|
|
self.renderer.copy(texture, srcrect=srcrect, dstrect=dstrect)
|
|
sdl2.SDL_SetRenderDrawBlendMode(
|
|
self.renderer.sdlrenderer, sdl2.SDL_BLENDMODE_BLEND)
|
|
sdl2.SDL_SetRenderDrawColor(self.renderer.sdlrenderer, 0, 0, 0, alpha)
|
|
sdl2.SDL_RenderFillRect(self.renderer.sdlrenderer, None)
|
|
self.renderer.present()
|
|
sdl2.SDL_Delay(16)
|
|
|
|
# Final black frame
|
|
self.renderer.clear()
|
|
self.renderer.present()
|
|
|
|
# ======================
|
|
# SPECIAL EFFECTS
|
|
# ======================
|
|
|
|
def trigger_white_flash(self):
|
|
"""Trigger the white flash effect"""
|
|
self.white_flash_active = True
|
|
self.white_flash_start_time = sdl2.SDL_GetTicks()
|
|
self.white_flash_opacity = 255
|
|
|
|
def update_white_flash(self):
|
|
"""Update the white flash effect and return True if it should be drawn"""
|
|
if not self.white_flash_active:
|
|
return False
|
|
|
|
current_time = sdl2.SDL_GetTicks()
|
|
elapsed_time = current_time - self.white_flash_start_time
|
|
|
|
if elapsed_time < 500: # First 500ms : full white
|
|
self.white_flash_opacity = 255
|
|
return True
|
|
elif elapsed_time < 2000: # Next 2 seconds: fade out
|
|
# Calculate fade based on remaining time (1000ms fade duration)
|
|
fade_progress = (elapsed_time - 500) / 1000.0 # 0.0 to 1.0
|
|
self.white_flash_opacity = int(255 * (1.0 - fade_progress))
|
|
return True
|
|
else: # Effect is complete
|
|
self.white_flash_active = False
|
|
self.white_flash_opacity = 0
|
|
return False
|
|
|
|
def draw_white_flash(self):
|
|
"""Draw the white flash overlay"""
|
|
if self.white_flash_opacity > 0:
|
|
# Create a white surface with the current opacity
|
|
white_surface = sdl2.SDL_CreateRGBSurface(
|
|
0, self.target_size[0], self.target_size[1], 32,
|
|
0x000000FF, # R mask
|
|
0x0000FF00, # G mask
|
|
0x00FF0000, # B mask
|
|
0xFF000000 # A mask
|
|
)
|
|
|
|
if white_surface:
|
|
# Fill surface with white
|
|
sdl2.SDL_FillRect(white_surface, None,
|
|
sdl2.SDL_MapRGBA(white_surface.contents.format,
|
|
255, 255, 255, self.white_flash_opacity))
|
|
|
|
# Convert to texture and draw
|
|
white_texture = self.factory.from_surface(white_surface)
|
|
white_texture.position = (0, 0)
|
|
|
|
# Enable alpha blending for the texture
|
|
sdl2.SDL_SetTextureBlendMode(white_texture.texture, sdl2.SDL_BLENDMODE_BLEND)
|
|
|
|
# Draw the white overlay
|
|
self.renderer.copy(white_texture, dstrect=sdl2.SDL_Rect(0, 0, self.target_size[0], self.target_size[1]))
|
|
|
|
# Clean up
|
|
sdl2.SDL_FreeSurface(white_surface)
|
|
|
|
# ======================
|
|
# UTILITY METHODS
|
|
# ======================
|
|
|
|
def new_cycle(self, delay, callback):
|
|
"""Placeholder for cycle management (not implemented)"""
|
|
pass
|
|
|
|
def full_screen(self, flag):
|
|
"""Toggle fullscreen mode"""
|
|
sdl2.SDL_SetWindowFullscreen(self.window.window, flag)
|
|
|
|
def get_perf_counter(self):
|
|
"""Get performance counter for timing"""
|
|
return sdl2.SDL_GetPerformanceCounter()
|
|
|
|
def close(self):
|
|
"""Close the game window and cleanup"""
|
|
self.running = False
|
|
sdl2.ext.quit()
|
|
|
|
# ======================
|
|
# MAIN GAME LOOP
|
|
# ======================
|
|
|
|
|
|
|
|
# ======================
|
|
# SPECIAL EFFECTS
|
|
# ======================
|
|
|
|
def generate_blood_surface(self):
|
|
"""Generate a dynamic blood splatter surface using SDL2 with transparency"""
|
|
size = self.cell_size
|
|
|
|
# Create RGBA surface for blood splatter with proper alpha channel
|
|
blood_surface = sdl2.SDL_CreateRGBSurface(
|
|
0, size, size, 32,
|
|
0x000000FF, # R mask
|
|
0x0000FF00, # G mask
|
|
0x00FF0000, # B mask
|
|
0xFF000000 # A mask
|
|
)
|
|
|
|
if not blood_surface:
|
|
return None
|
|
|
|
# Enable alpha blending for the surface
|
|
sdl2.SDL_SetSurfaceBlendMode(blood_surface, sdl2.SDL_BLENDMODE_BLEND)
|
|
|
|
# Fill with transparent color first
|
|
sdl2.SDL_FillRect(blood_surface, None,
|
|
sdl2.SDL_MapRGBA(blood_surface.contents.format, 0, 0, 0, 0))
|
|
|
|
# Lock surface for pixel manipulation
|
|
sdl2.SDL_LockSurface(blood_surface)
|
|
|
|
# Get pixel data
|
|
pixels = cast(blood_surface.contents.pixels, POINTER(c_uint32))
|
|
pitch = blood_surface.contents.pitch // 4 # Convert pitch to pixels (32-bit)
|
|
|
|
# Blood color variations (RGBA format for proper alpha)
|
|
blood_colors = [
|
|
(139, 0, 0), # Dark red
|
|
(178, 34, 34), # Firebrick
|
|
(160, 0, 0), # Dark red
|
|
(200, 0, 0), # Red
|
|
(128, 0, 0), # Maroon
|
|
]
|
|
|
|
# Generate splatter with diffusion algorithm
|
|
center_x, center_y = size // 2, size // 2
|
|
max_radius = size // 3 + random.randint(-3, 5)
|
|
|
|
for y in range(size):
|
|
for x in range(size):
|
|
# Calculate distance from center
|
|
distance = ((x - center_x) ** 2 + (y - center_y) ** 2) ** 0.5
|
|
|
|
# Calculate blood probability based on distance
|
|
if distance <= max_radius:
|
|
# Closer to center = higher probability
|
|
probability = max(0, 1 - (distance / max_radius))
|
|
|
|
# Add noise for irregular shape
|
|
noise = random.random() * 0.7
|
|
|
|
if random.random() < probability * noise:
|
|
# Choose random blood color
|
|
r, g, b = random.choice(blood_colors)
|
|
|
|
# Add alpha variation for transparency
|
|
alpha = int(255 * probability * random.uniform(0.6, 1.0))
|
|
|
|
# Pack RGBA into uint32 (ABGR format for SDL)
|
|
pixel_color = (alpha << 24) | (b << 16) | (g << 8) | r
|
|
pixels[y * pitch + x] = pixel_color
|
|
else:
|
|
# Transparent pixel
|
|
pixels[y * pitch + x] = 0x00000000
|
|
else:
|
|
# Outside radius, transparent
|
|
pixels[y * pitch + x] = 0x00000000
|
|
|
|
# Add scattered droplets around main splatter
|
|
for _ in range(random.randint(3, 8)):
|
|
drop_x = center_x + random.randint(-max_radius - 5, max_radius + 5)
|
|
drop_y = center_y + random.randint(-max_radius - 5, max_radius + 5)
|
|
|
|
if 0 <= drop_x < size and 0 <= drop_y < size:
|
|
drop_size = random.randint(1, 3)
|
|
for dy in range(-drop_size, drop_size + 1):
|
|
for dx in range(-drop_size, drop_size + 1):
|
|
nx, ny = drop_x + dx, drop_y + dy
|
|
if 0 <= nx < size and 0 <= ny < size:
|
|
if random.random() < 0.6:
|
|
r, g, b = random.choice(blood_colors[:3]) # Darker colors for drops
|
|
alpha = random.randint(100, 200)
|
|
|
|
# Pack RGBA into uint32 (ABGR format for SDL)
|
|
pixel_color = (alpha << 24) | (b << 16) | (g << 8) | r
|
|
pixels[ny * pitch + nx] = pixel_color
|
|
|
|
# Unlock surface
|
|
sdl2.SDL_UnlockSurface(blood_surface)
|
|
|
|
return blood_surface
|
|
|
|
def draw_blood_surface(self, blood_surface, position):
|
|
"""Convert blood surface to texture with proper alpha blending"""
|
|
# Create texture directly from renderer
|
|
texture_ptr = sdl2.SDL_CreateTextureFromSurface(self.renderer.renderer, blood_surface)
|
|
|
|
if texture_ptr:
|
|
# Enable alpha blending
|
|
sdl2.SDL_SetTextureBlendMode(texture_ptr, sdl2.SDL_BLENDMODE_BLEND)
|
|
|
|
# Wrap in sprite for compatibility
|
|
sprite = sdl2.ext.TextureSprite(texture_ptr)
|
|
|
|
# Free the surface
|
|
sdl2.SDL_FreeSurface(blood_surface)
|
|
|
|
return sprite
|
|
|
|
sdl2.SDL_FreeSurface(blood_surface)
|
|
return None
|
|
|
|
def combine_blood_surfaces(self, existing_surface, new_surface):
|
|
"""Combine two blood surfaces by blending them together"""
|
|
# Create combined surface
|
|
combined_surface = sdl2.SDL_CreateRGBSurface(
|
|
0, self.cell_size, self.cell_size, 32,
|
|
0x000000FF, # R mask
|
|
0x0000FF00, # G mask
|
|
0x00FF0000, # B mask
|
|
0xFF000000 # A mask
|
|
)
|
|
|
|
if combined_surface is None:
|
|
return existing_surface
|
|
|
|
# Lock surfaces for pixel manipulation
|
|
sdl2.SDL_LockSurface(existing_surface)
|
|
sdl2.SDL_LockSurface(new_surface)
|
|
sdl2.SDL_LockSurface(combined_surface)
|
|
|
|
# Get pixel data
|
|
existing_pixels = cast(existing_surface.contents.pixels, POINTER(c_uint32))
|
|
new_pixels = cast(new_surface.contents.pixels, POINTER(c_uint32))
|
|
combined_pixels = cast(combined_surface.contents.pixels, POINTER(c_uint32))
|
|
|
|
pitch = combined_surface.contents.pitch // 4 # Convert pitch to pixels (32-bit)
|
|
|
|
# Combine pixels with additive blending
|
|
for y in range(self.cell_size):
|
|
for x in range(self.cell_size):
|
|
idx = y * pitch + x
|
|
|
|
existing_pixel = existing_pixels[idx]
|
|
new_pixel = new_pixels[idx]
|
|
|
|
# Extract RGBA components
|
|
existing_a = (existing_pixel >> 24) & 0xFF
|
|
existing_r = (existing_pixel >> 16) & 0xFF
|
|
existing_g = (existing_pixel >> 8) & 0xFF
|
|
existing_b = existing_pixel & 0xFF
|
|
|
|
new_a = (new_pixel >> 24) & 0xFF
|
|
new_r = (new_pixel >> 16) & 0xFF
|
|
new_g = (new_pixel >> 8) & 0xFF
|
|
new_b = new_pixel & 0xFF
|
|
|
|
# Blend colors (additive blending for blood accumulation)
|
|
if new_a > 0: # If new pixel has color
|
|
if existing_a > 0: # If existing pixel has color
|
|
# Combine both colors, making it darker/more opaque
|
|
final_r = min(255, existing_r + (new_r // 2))
|
|
final_g = min(255, existing_g + (new_g // 2))
|
|
final_b = min(255, existing_b + (new_b // 2))
|
|
final_a = min(255, existing_a + (new_a // 2))
|
|
else:
|
|
# Use new pixel color
|
|
final_r = new_r
|
|
final_g = new_g
|
|
final_b = new_b
|
|
final_a = new_a
|
|
else:
|
|
# Use existing pixel color
|
|
final_r = existing_r
|
|
final_g = existing_g
|
|
final_b = existing_b
|
|
final_a = existing_a
|
|
|
|
# Pack the final pixel
|
|
combined_pixels[idx] = (final_a << 24) | (final_r << 16) | (final_g << 8) | final_b
|
|
|
|
# Unlock surfaces
|
|
sdl2.SDL_UnlockSurface(existing_surface)
|
|
sdl2.SDL_UnlockSurface(new_surface)
|
|
sdl2.SDL_UnlockSurface(combined_surface)
|
|
|
|
return combined_surface
|
|
|
|
def free_surface(self, surface):
|
|
"""Safely free an SDL surface"""
|
|
if surface is not None:
|
|
sdl2.SDL_FreeSurface(surface) |