Ограничение частоты перерисовки до 20 фпс
This commit is contained in:
@@ -18,8 +18,12 @@ scroll: dvui.ScrollInfo = .{
|
|||||||
.horizontal = .auto,
|
.horizontal = .auto,
|
||||||
},
|
},
|
||||||
native_scaling: bool = true,
|
native_scaling: bool = true,
|
||||||
|
/// Максимальная частота перерисовки при зуме. 0 = без ограничения.
|
||||||
|
redraw_throttle_ms: u32 = 40,
|
||||||
_visible_rect: ?ImageRect = null,
|
_visible_rect: ?ImageRect = null,
|
||||||
_zoom: f32 = 1,
|
_zoom: f32 = 1,
|
||||||
|
_redraw_pending: bool = false,
|
||||||
|
_last_redraw_time_ms: i64 = 0,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, document: *Document, engine: RenderEngine) Canvas {
|
pub fn init(allocator: std.mem.Allocator, document: *Document, engine: RenderEngine) Canvas {
|
||||||
return .{
|
return .{
|
||||||
@@ -60,6 +64,7 @@ pub fn redrawExample(self: *Canvas) !void {
|
|||||||
|
|
||||||
self.texture = tex;
|
self.texture = tex;
|
||||||
}
|
}
|
||||||
|
self._last_redraw_time_ms = std.time.milliTimestamp();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ресетнуть example изображение в renderEngine
|
// Ресетнуть example изображение в renderEngine
|
||||||
@@ -77,6 +82,26 @@ pub fn addZoom(self: *Canvas, value: f32) void {
|
|||||||
self._zoom = @max(self._zoom, 0.01);
|
self._zoom = @max(self._zoom, 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Запросить перерисовку (выполнится с учётом redraw_throttle_ms при вызове processPendingRedraw).
|
||||||
|
pub fn requestRedraw(self: *Canvas) void {
|
||||||
|
self._redraw_pending = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Выполнить отложенную перерисовку, если прошло не менее redraw_throttle_ms с прошлой отрисовки.
|
||||||
|
pub fn processPendingRedraw(self: *Canvas) !void {
|
||||||
|
if (!self._redraw_pending) return;
|
||||||
|
if (self.redraw_throttle_ms == 0) {
|
||||||
|
self._redraw_pending = false;
|
||||||
|
try self.redrawExample();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const now_ms = std.time.milliTimestamp();
|
||||||
|
const elapsed: i64 = if (self._last_redraw_time_ms == 0) self.redraw_throttle_ms else now_ms - self._last_redraw_time_ms;
|
||||||
|
if (elapsed < @as(i64, @intCast(self.redraw_throttle_ms))) return;
|
||||||
|
self._redraw_pending = false;
|
||||||
|
try self.redrawExample();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn getScaledImageSize(self: Canvas) ImageRect {
|
pub fn getScaledImageSize(self: Canvas) ImageRect {
|
||||||
const doc = self.document;
|
const doc = self.document;
|
||||||
return .{
|
return .{
|
||||||
@@ -103,7 +128,7 @@ pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset:
|
|||||||
std.debug.print("Visible Image Rect: {{ x: {}, y: {}, w: {}, h: {} }}\n", .{ next.x, next.y, next.w, next.h });
|
std.debug.print("Visible Image Rect: {{ x: {}, y: {}, w: {}, h: {} }}\n", .{ next.x, next.y, next.w, next.h });
|
||||||
}
|
}
|
||||||
if (changed or self.texture == null) {
|
if (changed or self.texture == null) {
|
||||||
try self.redrawExample();
|
requestRedraw(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -189,6 +189,9 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
canvas.updateVisibleImageRect(viewport_px, scroll_px) catch |err| {
|
canvas.updateVisibleImageRect(viewport_px, scroll_px) catch |err| {
|
||||||
std.debug.print("updateVisibleImageRect error: {}\n", .{err});
|
std.debug.print("updateVisibleImageRect error: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
canvas.processPendingRedraw() catch |err| {
|
||||||
|
std.debug.print("processPendingRedraw error: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
const content_w_px: u32 = img_size.x + img_size.w;
|
const content_w_px: u32 = img_size.x + img_size.w;
|
||||||
const content_h_px: u32 = img_size.y + img_size.h;
|
const content_h_px: u32 = img_size.y + img_size.h;
|
||||||
@@ -237,7 +240,7 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
switch (action) {
|
switch (action) {
|
||||||
.wheel_y => |y| {
|
.wheel_y => |y| {
|
||||||
canvas.addZoom(y / 1000);
|
canvas.addZoom(y / 1000);
|
||||||
canvas.redrawExample() catch {};
|
canvas.requestRedraw();
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user