This commit is contained in:
agra
2026-03-04 17:17:29 +02:00
parent 343ea4bf08
commit 0782353ffa
12 changed files with 1288 additions and 131 deletions

227
main.sx
View File

@@ -20,6 +20,58 @@ g_height : s32 = 600;
g_pixel_w : s32 = 800; // physical pixel size
g_pixel_h : s32 = 600;
// --- FPS / delta time tracking ---
g_delta_time : f32 = 0.008;
g_last_perf : u64 = 0;
g_frame_count : u64 = 0;
g_total_time : f64 = 0.0;
g_min_fps : f32 = 999999.0;
g_max_fps : f32 = 0.0;
// --- Persistent UI state (survives arena resets) ---
g_scroll_state : ScrollState = ---;
g_dock_interaction : *DockInteraction = xx 0;
FPS_REGRESSION_THRESHOLD :f32: 1400.0;
update_delta_time :: () {
current := SDL_GetPerformanceCounter();
freq := SDL_GetPerformanceFrequency();
if freq > 0 and g_last_perf > 0 {
g_delta_time = xx (current - g_last_perf) / xx freq;
}
g_last_perf = current;
// Track FPS stats (skip first 10 frames for warmup)
if g_frame_count > 10 {
g_total_time += xx g_delta_time;
fps : f32 = 1.0 / g_delta_time;
if fps < g_min_fps { g_min_fps = fps; }
if fps > g_max_fps { g_max_fps = fps; }
}
g_frame_count += 1;
}
print_fps_summary :: () {
if g_frame_count <= 11 or g_total_time <= 0.0 { return; }
measured : u64 = g_frame_count - 11;
if measured > 0 {
avg_fps : f32 = xx measured / xx g_total_time;
passed := avg_fps >= FPS_REGRESSION_THRESHOLD;
status := if passed then "PASS" else "FAIL";
out("\n=== FPS Summary ===\n");
print("Frames: {}\n", measured);
print("Time: {}s\n", g_total_time);
print("Avg: {} FPS\n", xx avg_fps);
print("Min: {} FPS\n", xx g_min_fps);
print("Max: {} FPS\n", xx g_max_fps);
out("-------------------\n");
print("Threshold: {} FPS\n", xx FPS_REGRESSION_THRESHOLD);
print("Status: {}\n", status);
out("===================\n");
}
}
load_texture :: (path: [:0]u8) -> u32 {
w : s32 = 0;
h : s32 = 0;
@@ -62,6 +114,87 @@ save_snapshot :: (path: [:0]u8, w: s32, h: s32) {
out("\n");
}
run_dock_drag_test :: (pipeline: *UIPipeline) {
out("=== Dock Drag Test: move Statistics panel to left zone ===\n");
// Initial layout pass
glClearColor(0.12, 0.12, 0.15, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
pipeline.tick();
// Print the initial interaction state
print("BEFORE drag: has_override[1]={}, is_floating[1]={}\n",
g_dock_interaction.has_alignment_override.items[1],
g_dock_interaction.is_floating.items[1]);
print("BEFORE drag: child_bounds[1]=({},{} {}x{})\n",
g_dock_interaction.child_bounds.items[1].origin.x,
g_dock_interaction.child_bounds.items[1].origin.y,
g_dock_interaction.child_bounds.items[1].size.width,
g_dock_interaction.child_bounds.items[1].size.height);
// Statistics panel (index 1) is at ALIGN_TOP_TRAILING.
// Its header is the top 28px of the panel frame.
// Use the actual child_bounds to find the header center.
panel_frame := g_dock_interaction.child_bounds.items[1];
header_x := panel_frame.origin.x + panel_frame.size.width * 0.5;
header_y := panel_frame.origin.y + 14.0; // middle of 28px header
header_pos := Point.{ x = header_x, y = header_y };
print("clicking header at ({}, {})\n", header_x, header_y);
// Step 1: Mouse down on the Statistics panel header
e : Event = .mouse_down(MouseButtonData.{ position = header_pos, button = .left });
pipeline.dispatch_event(@e);
print("after mouse_down: dragging_child={}\n", g_dock_interaction.dragging_child);
// Step 2: Drag to the "left" zone.
// Left zone hint: (8, cy, 40, 40) where cy = (height - 40) / 2
// Use actual screen size from pipeline
screen_w := pipeline.screen_width;
screen_h := pipeline.screen_height;
zone_cx := 8.0 + 20.0; // center of left zone hint
zone_cy := screen_h * 0.5;
print("screen={}x{}, left zone center=({}, {})\n", xx screen_w, xx screen_h, zone_cx, zone_cy);
target := Point.{ x = zone_cx, y = zone_cy };
steps : s64 = 20;
i : s64 = 1;
while i <= steps {
t : f32 = xx i / xx steps;
cur_x := header_pos.x + (target.x - header_pos.x) * t;
cur_y := header_pos.y + (target.y - header_pos.y) * t;
e = .mouse_moved(MouseMotionData.{
position = Point.{ x = cur_x, y = cur_y },
delta = Point.{ x = (target.x - header_pos.x) / xx steps, y = (target.y - header_pos.y) / xx steps }
});
pipeline.dispatch_event(@e);
i += 1;
}
print("after drag: hovered_zone={}\n", g_dock_interaction.hovered_zone);
// Step 3: Mouse up at the target zone
e = .mouse_up(MouseButtonData.{ position = target, button = .left });
pipeline.dispatch_event(@e);
// Check the result
print("AFTER drop: has_override[1]={}, is_floating[1]={}, is_fill[1]={}\n",
g_dock_interaction.has_alignment_override.items[1],
g_dock_interaction.is_floating.items[1],
g_dock_interaction.is_fill.items[1]);
print("AFTER drop: dragging_child={}\n", g_dock_interaction.dragging_child);
// Render with new layout and save snapshot
glClear(GL_COLOR_BUFFER_BIT);
pipeline.tick();
save_snapshot("goldens/test_dock_drag.png", g_pixel_w, g_pixel_h);
// Print final child_bounds to see where the panel ended up
print("FINAL: child_bounds[1]=({},{} {}x{})\n",
g_dock_interaction.child_bounds.items[1].origin.x,
g_dock_interaction.child_bounds.items[1].origin.y,
g_dock_interaction.child_bounds.items[1].size.width,
g_dock_interaction.child_bounds.items[1].size.height);
out("=== end dock drag test ===\n");
}
run_ui_tests :: (pipeline: *UIPipeline) {
// Do a layout pass first so frames are computed
glClearColor(0.12, 0.12, 0.15, 1.0);
@@ -130,10 +263,10 @@ run_ui_tests :: (pipeline: *UIPipeline) {
// One frame of the main loop — called repeatedly by emscripten or desktop while-loop
frame :: () {
update_delta_time();
sdl_event : SDL_Event = .none;
while SDL_PollEvent(@sdl_event) {
print("SDL event: {}\n", sdl_event.tag);
if sdl_event == {
case .quit: { g_running = false; }
case .key_up: (e) {
@@ -149,10 +282,7 @@ frame :: () {
ui_event := translate_sdl_event(@sdl_event);
if ui_event != .none {
print(" ui event dispatched\n");
g_pipeline.dispatch_event(@ui_event);
} else {
print(" -> .none\n");
}
}
@@ -161,8 +291,50 @@ frame :: () {
glClear(GL_COLOR_BUFFER_BIT);
g_pipeline.*.tick();
SDL_GL_SwapWindow(g_window);
// Auto-quit after 300 frames for benchmarking
if g_frame_count > 300 { g_running = false; }
}
// Body function — rebuilds the entire view tree each frame (arena-allocated)
build_ui :: () -> View {
scroll_content := VStack.{ spacing = 10.0, alignment = .center } {
self.add(
Label.{ text = "Hello, SX!", font_size = 24.0, color = COLOR_WHITE }
|> padding(EdgeInsets.all(8.0))
);
self.add(
RectView.{ color = COLOR_YELLOW, preferred_height = 80.0, corner_radius = 8.0 }
|> padding(EdgeInsets.all(8.0))
|> on_tap(closure(() { out("Yellow tapped!\n"); }))
);
self.add(
Button.{ label = "Click Me", font_size = 14.0, style = ButtonStyle.default(), on_tap = closure(() { out("Button tapped!\n"); }) }
);
self.add(HStack.{ spacing = 10.0, alignment = .center } {
self.add(RectView.{ color = COLOR_RED, preferred_width = 200.0, preferred_height = 300.0, corner_radius = 4.0 });
self.add(RectView.{ color = COLOR_GREEN, preferred_width = 200.0, preferred_height = 300.0, corner_radius = 4.0 });
});
self.add(
RectView.{ color = COLOR_DARK_GRAY, preferred_height = 60.0 }
|> padding(.symmetric(16.0, 8.0))
|> background(COLOR_BLUE, 8.0)
);
self.add(RectView.{ color = COLOR_ORANGE, preferred_height = 120.0, corner_radius = 12.0 });
self.add(RectView.{ color = COLOR_GRAY, preferred_height = 200.0, corner_radius = 8.0 });
};
scroll := ScrollView.{ child = ViewChild.{ view = scroll_content }, state = @g_scroll_state, axes = .vertical };
stats := StatsPanel.{ delta_time = @g_delta_time, font_size = 12.0 };
dock := Dock.make(g_dock_interaction);
content_panel := DockPanel.make("Content", ALIGN_CENTER, scroll);
content_panel.fill = true;
dock.add_panel(content_panel);
dock.add_panel(DockPanel.make("Statistics", ALIGN_TOP_TRAILING, stats));
xx dock;
}
main :: () -> void {
@@ -198,7 +370,7 @@ main :: () -> void {
window := SDL_CreateWindow("SX UI Demo", init_w, init_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
gl_ctx := SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_ctx);
SDL_GL_SetSwapInterval(1);
SDL_GL_SetSwapInterval(0);
load_gl(xx SDL_GL_GetProcAddress);
@@ -213,42 +385,24 @@ main :: () -> void {
glViewport(0, 0, g_pixel_w, g_pixel_h);
// --- Build UI ---
pipeline : UIPipeline = ---;
pipeline : *UIPipeline = xx context.allocator.alloc(size_of(UIPipeline));
pipeline.init(width_f, height_f);
pipeline.init_font("assets/fonts/default.ttf", 32.0, dpi_scale);
scroll_content := VStack.{ spacing = 10.0, alignment = .center } {
self.add(
Label.{ text = "Hello, SX!", font_size = 24.0, color = COLOR_WHITE }
|> padding(EdgeInsets.all(8.0))
);
self.add(
RectView.{ color = COLOR_YELLOW, preferred_height = 80.0, corner_radius = 8.0 }
|> padding(EdgeInsets.all(8.0))
|> on_tap(closure(() { out("Yellow tapped!\n"); }))
);
self.add(
Button.{ label = "Click Me", font_size = 14.0, style = ButtonStyle.default(), on_tap = closure(() { out("Button tapped!\n"); }) }
);
self.add(HStack.{ spacing = 10.0, alignment = .center } {
self.add(RectView.{ color = COLOR_RED, preferred_width = 200.0, preferred_height = 300.0, corner_radius = 4.0 });
self.add(RectView.{ color = COLOR_GREEN, preferred_width = 200.0, preferred_height = 300.0, corner_radius = 4.0 });
});
self.add(
RectView.{ color = COLOR_DARK_GRAY, preferred_height = 60.0 }
|> padding(.symmetric(16.0, 8.0))
|> background(COLOR_BLUE, 8.0)
);
self.add(RectView.{ color = COLOR_ORANGE, preferred_height = 120.0, corner_radius = 12.0 });
self.add(RectView.{ color = COLOR_GRAY, preferred_height = 200.0, corner_radius = 8.0 });
};
// Initialize persistent state (on GPA, before arena is active)
g_scroll_state = ScrollState.{};
g_dock_interaction = xx context.allocator.alloc(size_of(DockInteraction));
g_dock_interaction.init();
g_dock_delta_time = @g_delta_time;
root := ScrollView.{ child = ViewChild.{ view = scroll_content }, axes = .vertical };
pipeline.set_root(root);
pipeline.set_body(closure(build_ui));
// Store state in globals for frame callback
g_window = xx window;
g_pipeline = @pipeline;
g_pipeline = pipeline;
// Reset perf counter so first frame doesn't include init time
g_last_perf = SDL_GetPerformanceCounter();
// --- Main loop ---
inline if OS == .wasm {
@@ -259,6 +413,7 @@ main :: () -> void {
}
}
print_fps_summary();
save_snapshot("goldens/last_frame.png", g_pixel_w, g_pixel_h);
SDL_GL_DestroyContext(gl_ctx);