84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
// modules/core/frameloop.js
|
|
import {Logger} from "./logger";
|
|
|
|
const tasks = new Map();
|
|
let running = false;
|
|
let showDebug = false;
|
|
|
|
let lastTime = performance.now();
|
|
let frameCount = 0;
|
|
let fps = 0;
|
|
|
|
|
|
const debugOverlay = document.createElement('div');
|
|
debugOverlay.style.position = 'fixed';
|
|
debugOverlay.style.bottom = '0';
|
|
debugOverlay.style.left = '0';
|
|
debugOverlay.style.font = '12px monospace';
|
|
debugOverlay.style.color = '#0f0';
|
|
debugOverlay.style.background = 'rgba(0,0,0,0.75)';
|
|
debugOverlay.style.padding = '0.25rem 0.5rem';
|
|
debugOverlay.style.zIndex = '9999';
|
|
debugOverlay.style.pointerEvents = 'none';
|
|
debugOverlay.style.display = 'none';
|
|
const barWidth = Math.min(fps * 2, 100);
|
|
const bar = `<div style="width:${barWidth}%;height:4px;background:#0f0;margin-top:4px;"></div>`;
|
|
debugOverlay.innerHTML += bar;
|
|
debugOverlay.style.lineHeight = '1.4';
|
|
document.body.appendChild(debugOverlay);
|
|
|
|
import { PerformanceMonitor } from './PerformanceMonitor.js';
|
|
export const monitor = new PerformanceMonitor();
|
|
|
|
window.addEventListener('keydown', (e) => {
|
|
if (e.key === '§') {
|
|
showDebug = !showDebug;
|
|
debugOverlay.style.display = showDebug ? 'block' : 'none';
|
|
}
|
|
});
|
|
|
|
export function registerFrameTask(id, callback, options = {}) {
|
|
tasks.set(id, callback);
|
|
if (options.autoStart && !running) startFrameLoop();
|
|
}
|
|
|
|
export function unregisterFrameTask(id) {
|
|
tasks.delete(id);
|
|
}
|
|
|
|
export function clearFrameTasks() {
|
|
tasks.clear();
|
|
}
|
|
|
|
export function startFrameLoop() {
|
|
if (running) return;
|
|
running = true;
|
|
|
|
function loop() {
|
|
for (const [id, task] of tasks) {
|
|
try {
|
|
if (showDebug) {
|
|
monitor.trackTask(id, task);
|
|
} else {
|
|
task();
|
|
}
|
|
} catch (err) {
|
|
Logger.warn(`[Frameloop] Fehler in Task:`, err);
|
|
}
|
|
}
|
|
|
|
if (showDebug) {
|
|
monitor.update(tasks);
|
|
}
|
|
|
|
requestAnimationFrame(loop);
|
|
}
|
|
|
|
requestAnimationFrame(loop);
|
|
}
|
|
|
|
export function stopFrameLoop() {
|
|
running = false;
|
|
// Achtung: Loop läuft weiter, solange nicht aktiv gestoppt
|
|
}
|