Skip to content

Commit

Permalink
Cleanup passing layers and textures by ref
Browse files Browse the repository at this point in the history
  • Loading branch information
foxnne committed Feb 18, 2025
1 parent 103291d commit 25d5493
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Assets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn loadTexture(assets: *Assets, path: []const u8, options: pixi.gfx.Texture.
assets.textures.lock();
defer assets.textures.unlock();

const term_path = try std.fmt.allocPrintZ(assets.allocator, "{s}", .{path});
const term_path = try assets.allocator.dupeZ(u8, path);

if (pixi.gfx.Texture.loadFromFile(term_path, options) catch null) |texture| {
const texture_id = try assets.textures.new(texture);
Expand All @@ -71,7 +71,7 @@ pub fn loadAtlas(assets: *Assets, path: []const u8) !?mach.ObjectID {
assets.atlases.lock();
defer assets.atlases.unlock();

const term_path = try std.fmt.allocPrintZ(assets.allocator, "{s}", .{path});
const term_path = try assets.allocator.dupeZ(u8, path);

if (pixi.Atlas.loadFromFile(assets.allocator, term_path) catch null) |atlas| {
const atlas_id = try assets.atlases.new(atlas);
Expand Down
14 changes: 8 additions & 6 deletions src/editor/artboard/canvas.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App, editor: *Editor)
const y = @as(f32, @floatFromInt(tile_row)) * tile_height + canvas_center_offset[1];

if (editor.explorer.pane != .pack)
file.camera.drawTexture(file.background.view_handle, file.tile_width, file.tile_height, .{ x, y }, 0x88FFFFFF);
file.camera.drawTexture(&file.background, .{ x, y }, 0x88FFFFFF);

try file.processStrokeTool(.primary, .{});
try file.processFillTool(.primary, .{});
Expand Down Expand Up @@ -218,12 +218,14 @@ pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App, editor: *Editor)
while (i > 0) {
i -= 1;

if (file.layers.items(.visible)[i])
file.camera.drawLayer(file.layers.slice().get(i), canvas_center_offset);
if (file.layers.items(.visible)[i]) {
var layer = file.layers.slice().get(i);
file.camera.drawLayer(&layer, canvas_center_offset);
}
}

// Draw the temporary layer
file.camera.drawLayer(file.temporary_layer, canvas_center_offset);
file.camera.drawLayer(&file.temporary_layer, canvas_center_offset);

// Draw grid
file.camera.drawGrid(canvas_center_offset, file_width, file_height, @as(usize, @intFromFloat(file_width / tile_width)), @as(usize, @intFromFloat(file_height / tile_height)), pixi.editor.theme.text_secondary.toU32(), false);
Expand All @@ -233,14 +235,14 @@ pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App, editor: *Editor)

if (file.heightmap.visible) {
file.camera.drawRectFilled(.{ canvas_center_offset[0], canvas_center_offset[1], file_width, file_height }, 0x60FFFFFF);
if (file.heightmap.layer) |layer| {
if (file.heightmap.layer) |*layer| {
file.camera.drawLayer(layer, canvas_center_offset);
}
}
}

if (editor.tools.current == .selection) {
file.camera.drawLayer(file.selection_layer, canvas_center_offset);
file.camera.drawLayer(&file.selection_layer, canvas_center_offset);
}

// Draw height in pixels if currently editing heightmap and zoom is sufficient
Expand Down
4 changes: 2 additions & 2 deletions src/editor/artboard/canvas_pack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn draw(mode: PackTexture, editor: *Editor, packer: *Packer) void {
if (switch (mode) {
.texture => editor.atlas.texture,
.heightmap => editor.atlas.heightmap,
}) |texture| {
}) |*texture| {
var canvas_flags: imgui.WindowFlags = 0;
canvas_flags |= imgui.WindowFlags_HorizontalScrollbar;
defer imgui.endChild();
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn draw(mode: PackTexture, editor: *Editor, packer: *Packer) void {
const height: f32 = @floatFromInt(texture.height);

const center_offset: [2]f32 = .{ -width / 2.0, -height / 2.0 };
camera.drawTexture(texture.view_handle, texture.width, texture.height, center_offset, 0xFFFFFFFF);
camera.drawTexture(texture, center_offset, 0xFFFFFFFF);
camera.drawRect(.{ center_offset[0], center_offset[1], width, height }, 2.0, pixi.editor.theme.text_secondary.toU32());
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/editor/artboard/flipbook/canvas.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn draw(file: *pixi.Internal.File, app: *App, editor: *Editor) !void {
pixi.editor.theme.background.toU32(),
);
// Draw background
file.flipbook_camera.drawTexture(file.background.view_handle, file.tile_width, file.tile_height, .{ dst_rect[0], dst_rect[1] }, 0x88FFFFFF);
file.flipbook_camera.drawTexture(&file.background, .{ dst_rect[0], dst_rect[1] }, 0x88FFFFFF);
file.selected_sprite_index = sprite_index;
if (!file.setAnimationFromSpriteIndex()) {
file.selected_animation_state = .pause;
Expand All @@ -115,8 +115,10 @@ pub fn draw(file: *pixi.Internal.File, app: *App, editor: *Editor) !void {
layer_index -= 1;
if (!file.layers.items(.visible)[layer_index]) continue;

var layer = file.layers.slice().get(layer_index);

file.flipbook_camera.drawSpriteQuad(
file.layers.slice().get(layer_index),
&layer,
src_rect,
dst_p1,
dst_p2,
Expand All @@ -131,7 +133,7 @@ pub fn draw(file: *pixi.Internal.File, app: *App, editor: *Editor) !void {
}

if (sprite_index == file.selected_sprite_index)
file.flipbook_camera.drawSprite(file.temporary_layer, src_rect, dst_rect);
file.flipbook_camera.drawSprite(&file.temporary_layer, src_rect, dst_rect);

if (file.flipbook_camera.isHovered(dst_rect) and !imgui.isAnyItemHovered()) {
if (sprite_index != file.selected_sprite_index) {
Expand Down Expand Up @@ -208,7 +210,7 @@ pub fn draw(file: *pixi.Internal.File, app: *App, editor: *Editor) !void {
const dst_rect: [4]f32 = .{ dst_x + offset_x, dst_y + offset_y, dst_width, dst_height };

// Draw background
file.flipbook_camera.drawTexture(file.background.view_handle, file.tile_width, file.tile_height, .{ dst_rect[0], dst_rect[1] }, 0x88FFFFFF);
file.flipbook_camera.drawTexture(&file.background, .{ dst_rect[0], dst_rect[1] }, 0x88FFFFFF);
file.selected_sprite_index = sprite_index;
if (!file.setAnimationFromSpriteIndex()) {
file.selected_animation_state = .pause;
Expand All @@ -220,11 +222,13 @@ pub fn draw(file: *pixi.Internal.File, app: *App, editor: *Editor) !void {
layer_index -= 1;
if (!file.layers.items(.visible)[layer_index]) continue;

file.flipbook_camera.drawSprite(file.layers.slice().get(layer_index), src_rect, dst_rect);
var layer = file.layers.slice().get(layer_index);

file.flipbook_camera.drawSprite(&layer, src_rect, dst_rect);
}

if (sprite_index == file.selected_sprite_index)
file.flipbook_camera.drawSprite(file.temporary_layer, src_rect, dst_rect);
file.flipbook_camera.drawSprite(&file.temporary_layer, src_rect, dst_rect);

if (file.flipbook_camera.isHovered(dst_rect) and !imgui.isAnyItemHovered()) {
file.flipbook_camera.drawRect(dst_rect, 1, pixi.editor.theme.text.toU32());
Expand Down
4 changes: 1 addition & 3 deletions src/editor/artboard/flipbook/timeline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ pub fn draw(file: *pixi.Internal.File, editor: *Editor) !void {
file.flipbook_camera.drawLine(.{ -l / 2.0, 0.0 }, .{ l / 2.0, 0.0 }, 0x550000FF, 1.0);

file.flipbook_camera.drawTexture(
file.keyframe_animation_texture.view_handle,
file.keyframe_animation_texture.width,
file.keyframe_animation_texture.height,
&file.keyframe_animation_texture,
file.canvasCenterOffset(.primary),
0xFFFFFFFF,
);
Expand Down
4 changes: 1 addition & 3 deletions src/editor/popups/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ pub fn draw(editor: *Editor) !void {
{ // Draw reference texture
const color = pixi.math.Color.initFloats(1.0, 1.0, 1.0, reference.opacity / 100.0);
reference.camera.drawTexture(
reference.texture.view_handle,
reference.texture.width,
reference.texture.height,
&reference.texture,
canvas_center_offset,
color.toU32(),
);
Expand Down
12 changes: 6 additions & 6 deletions src/gfx/Camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ pub fn drawAnimationRect(camera: Camera, start_rect: [4]f32, end_rect: [4]f32, t
}
}

pub fn drawTexture(camera: Camera, texture: *gpu.TextureView, width: u32, height: u32, position: [2]f32, color: u32) void {
const rect_min_max = camera.getRectMinMax(.{ position[0], position[1], @as(f32, @floatFromInt(width)), @as(f32, @floatFromInt(height)) });
pub fn drawTexture(camera: Camera, texture: *pixi.gfx.Texture, position: [2]f32, color: u32) void {
const rect_min_max = camera.getRectMinMax(.{ position[0], position[1], @as(f32, @floatFromInt(texture.width)), @as(f32, @floatFromInt(texture.height)) });

const min: imgui.Vec2 = .{ .x = rect_min_max[0][0], .y = rect_min_max[0][1] };
const max: imgui.Vec2 = .{ .x = rect_min_max[1][0], .y = rect_min_max[1][1] };

if (imgui.getWindowDrawList()) |draw_list|
draw_list.addImageEx(
texture,
texture.handle,
min,
max,
.{ .x = 0.0, .y = 0.0 },
Expand Down Expand Up @@ -314,7 +314,7 @@ pub fn drawCursor(_: Camera, sprite_index: usize, color: u32) void {
);
}

pub fn drawLayer(camera: Camera, layer: pixi.Internal.Layer, position: [2]f32) void {
pub fn drawLayer(camera: Camera, layer: *pixi.Internal.Layer, position: [2]f32) void {
const rect_min_max = camera.getRectMinMax(.{ position[0], position[1], @as(f32, @floatFromInt(layer.texture.width)), @as(f32, @floatFromInt(layer.texture.height)) });

const min: imgui.Vec2 = .{ .x = rect_min_max[0][0], .y = rect_min_max[0][1] };
Expand All @@ -331,7 +331,7 @@ pub fn drawLayer(camera: Camera, layer: pixi.Internal.Layer, position: [2]f32) v
);
}

pub fn drawSprite(camera: Camera, layer: pixi.Internal.Layer, src_rect: [4]f32, dst_rect: [4]f32) void {
pub fn drawSprite(camera: Camera, layer: *pixi.Internal.Layer, src_rect: [4]f32, dst_rect: [4]f32) void {
const rect_min_max = camera.getRectMinMax(dst_rect);

const inv_w = 1.0 / @as(f32, @floatFromInt(layer.texture.width));
Expand All @@ -354,7 +354,7 @@ pub fn drawSprite(camera: Camera, layer: pixi.Internal.Layer, src_rect: [4]f32,
);
}

pub fn drawSpriteQuad(camera: Camera, layer: pixi.Internal.Layer, src_rect: [4]f32, dst_p1: [2]f32, dst_p2: [2]f32, dst_p3: [2]f32, dst_p4: [2]f32) void {
pub fn drawSpriteQuad(camera: Camera, layer: *pixi.Internal.Layer, src_rect: [4]f32, dst_p1: [2]f32, dst_p2: [2]f32, dst_p3: [2]f32, dst_p4: [2]f32) void {
const dst = camera.getQuad(dst_p1, dst_p2, dst_p3, dst_p4);

const inv_w = 1.0 / @as(f32, @floatFromInt(layer.texture.width));
Expand Down
7 changes: 3 additions & 4 deletions src/pixi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ pub const version: std.SemanticVersion = .{
pub const paths = @import("generated/paths.zig");
pub const atlas = @import("generated/atlas.zig");

pub const shaders = @import("shaders/shaders.zig");

// Other helpers and namespaces
pub const algorithms = @import("algorithms/algorithms.zig");
pub const fa = @import("tools/font_awesome.zig");
pub const fs = @import("tools/fs.zig");
pub const gfx = @import("gfx/gfx.zig");
pub const input = @import("input/input.zig");
pub const math = @import("math/math.zig");
pub const shaders = @import("shaders/shaders.zig");

// Modules

Expand Down Expand Up @@ -83,8 +82,8 @@ pub const Atlas = @import("Atlas.zig");
/// The data that gets written to disk in a .pixi file and read back into this type
pub const File = @import("File.zig");

/// Contains information such as the name, visibility, and collapse settings of a texture layer
/// Contains information such as the name, visibility and collapse settings of a texture layer
pub const Layer = @import("Layer.zig");

/// Source location within the atlas texture, and origin location
/// Source location within the atlas texture and origin location
pub const Sprite = @import("Sprite.zig");

0 comments on commit 25d5493

Please sign in to comment.