import pygame import numpy as np from flask import Flask, Response, render_template_string, request import time import os # Initialize Pygame headlessly os.environ['SDL_VIDEODRIVER'] = 'dummy' pygame.init() pygame.mixer.init(frequency=44100, size=-16, channels=2) app = Flask(__name__) class ShaderRenderer: def __init__(self, width=640, height=480): self.width = width self.height = height self.mouse_x = width // 2 self.mouse_y = height // 2 self.start_time = time.time() self.surface = pygame.Surface((width, height)) # Sound sources self.sound_source = 'none' # 'none', 'pygame', 'browser' self.pygame_sound = None self.pygame_playing = False self.sound_amp = 0.0 # Load pygame sound if available if os.path.exists('sound.mp3'): try: self.pygame_sound = pygame.mixer.Sound('sound.mp3') print("ā Pygame sound loaded") except: print("ā ļø Could not load sound.mp3") def set_mouse(self, x, y): self.mouse_x = max(0, min(self.width, x)) self.mouse_y = max(0, min(self.height, y)) def set_sound_source(self, source): """Change sound source: none, pygame, browser""" self.sound_source = source # Handle pygame sound if source == 'pygame': if self.pygame_sound and not self.pygame_playing: self.pygame_sound.play(loops=-1) self.pygame_playing = True self.sound_amp = 0.5 else: if self.pygame_playing: pygame.mixer.stop() self.pygame_playing = False self.sound_amp = 0.0 def render_frame(self): """Render a simple pattern that shader will transform""" t = time.time() - self.start_time # Clear self.surface.fill((20, 20, 30)) # Draw TOP marker (should be at top of canvas) pygame.draw.rect(self.surface, (255, 100, 100), (10, 10, 100, 30)) font = pygame.font.Font(None, 24) text = font.render("TOP", True, (255, 255, 255)) self.surface.blit(text, (20, 15)) # Draw BOTTOM marker pygame.draw.rect(self.surface, (100, 255, 100), (10, self.height-40, 100, 30)) text = font.render("BOTTOM", True, (0, 0, 0)) self.surface.blit(text, (20, self.height-35)) # Draw moving circle circle_size = 30 + int(20 * np.sin(t * 2)) # Color code based on sound source if self.sound_source == 'pygame': color = (100, 255, 100) # Green for pygame sound elif self.sound_source == 'browser': color = (100, 100, 255) # Blue for browser sound else: color = (255, 100, 100) # Red for no sound pygame.draw.circle(self.surface, color, (self.mouse_x, self.mouse_y), circle_size) # Draw grid for x in range(0, self.width, 50): alpha = int(40 + 20 * np.sin(x * 0.1 + t)) pygame.draw.line(self.surface, (alpha, alpha, 50), (x, 0), (x, self.height)) for y in range(0, self.height, 50): alpha = int(40 + 20 * np.cos(y * 0.1 + t)) pygame.draw.line(self.surface, (alpha, alpha, 50), (0, y), (self.width, y)) # Sound meter meter_width = int(200 * self.sound_amp) pygame.draw.rect(self.surface, (60, 60, 60), (self.width-220, 10, 200, 20)) pygame.draw.rect(self.surface, (100, 255, 100), (self.width-220, 10, meter_width, 20)) # Shader indicator shader_text = font.render("SHADER ON", True, (255, 255, 0)) self.surface.blit(shader_text, (self.width-150, self.height-30)) return pygame.image.tostring(self.surface, 'RGB') def get_frame(self): return self.render_frame() renderer = ShaderRenderer() @app.route('/') def index(): return render_template_string('''