Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

Commit

Permalink
graphics iobject interface change structure + example update
Browse files Browse the repository at this point in the history
  • Loading branch information
xfitgd committed Dec 13, 2024
1 parent 58b764a commit 8ee2b80
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 201 deletions.
118 changes: 54 additions & 64 deletions examples/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var arena_alloc: std.mem.Allocator = undefined;
const matrix = math.matrix;
const iarea = collision.iarea;

pub var objects: ArrayList(*graphics.iobject) = undefined;
pub var objects: ArrayList(graphics.iobject) = undefined;

pub var g_proj: graphics.projection = .{};
pub var g_camera: graphics.camera = undefined;
Expand All @@ -58,10 +58,7 @@ var color_trans: graphics.color_transform = undefined;
const player = animator.player;
const animate_object = animator.animate_object;

var anim: player = .{
.target_fps = 10,
.obj = .{ .obj = undefined },
};
var anim: player = undefined;

pub const CANVAS_W: f32 = 1280;
pub const CANVAS_H: f32 = 720;
Expand All @@ -77,16 +74,15 @@ fn error_func(text: []u8, stack_trace: []u8) void {
_ = fs.write(stack_trace) catch return;
}

var g_rect_button: *components.button = undefined;
var move_callback_thread: std.Thread = undefined;

var text_shape: *graphics.iobject = undefined;
var rect_button: *graphics.iobject = undefined;
var text_shape: graphics.shape = undefined;
var rect_button: components.button = undefined;

var github_shape: *graphics.iobject = undefined;
var github_shape: graphics.shape = undefined;

var img: *graphics.iobject = undefined;
var anim_img: *graphics.iobject = undefined;
var img: graphics.image = undefined;
var anim_img: graphics.animate_image = undefined;

pub fn xfit_init() !void {
//lua test
Expand Down Expand Up @@ -117,15 +113,8 @@ pub fn xfit_init() !void {
g_camera.build();
//

//alloc and set iobjects
objects = ArrayList(*graphics.iobject).init(arena_alloc);
const iobjs = try arena_alloc.alloc(graphics.iobject, 5);

text_shape = &iobjs[0];
rect_button = &iobjs[1];
img = &iobjs[2];
anim_img = &iobjs[3];
github_shape = &iobjs[4];
//alloc objects
objects = ArrayList(graphics.iobject).init(arena_alloc);
//

//graphics.set_render_clear_color(.{ 1, 1, 1, 0 });
Expand All @@ -151,13 +140,13 @@ pub fn xfit_init() !void {
};
color_trans.build(.gpu);

img.* = .{ ._image = graphics.image.init(&image_src) };
img = graphics.image.init(&image_src);

img.*._image.color_tran = &color_trans;
img.*._image.transform.camera = &g_camera;
img.*._image.transform.projection = &g_proj;
img.*._image.transform.model = math.matrix_multiply(math.matrix_scaling(f32, 2, 2, 1.0), math.matrix_translation(f32, 0, 0, 0.7));
img.*.build();
img.color_tran = &color_trans;
img.transform.camera = &g_camera;
img.transform.projection = &g_proj;
img.transform.model = math.matrix_multiply(math.matrix_scaling(f32, 2, 2, 1.0), math.matrix_translation(f32, 0, 0, 0.7));
img.build();
//

//load and decode wasp.webp
Expand All @@ -171,14 +160,17 @@ pub fn xfit_init() !void {
img_decoder.decode(data, anim_pixels) catch |e| xfit.herr3("wasp.webp decode", e);
anim_image_src.build(img_decoder.width(), img_decoder.height(), img_decoder.frame_count(), anim_pixels);

anim_img.* = .{ ._anim_image = graphics.animate_image.init(&anim_image_src) };
anim_img = graphics.animate_image.init(&anim_image_src);

anim_img.*._anim_image.transform.camera = &g_camera;
anim_img.*._anim_image.transform.projection = &g_proj;
anim_img.*._anim_image.transform.model = math.matrix_translation(f32, 300, -200, 0);
anim_img.*.build();
anim_img.transform.camera = &g_camera;
anim_img.transform.projection = &g_proj;
anim_img.transform.model = math.matrix_translation(f32, 300, -200, 0);
anim_img.build();

anim.obj.obj = anim_img;
anim = .{
.target_fps = 10,
.obj = animator.ianimate_object.init(&anim_img),
};
anim.play();
//

Expand Down Expand Up @@ -206,13 +198,13 @@ pub fn xfit_init() !void {
},
};
shape_src = try font.render_string2("Hello World!\n안녕하세요. break;", option2, arena_alloc);
text_shape.* = .{ ._shape = graphics.shape.init(shape_src) };
text_shape = graphics.shape.init(shape_src);

text_shape.*._shape.transform.camera = &g_camera;
text_shape.*._shape.transform.projection = &g_proj;
text_shape.transform.camera = &g_camera;
text_shape.transform.projection = &g_proj;

text_shape.*._shape.transform.model = math.matrix_multiply(math.matrix_scaling(f32, 5.0, 5.0, 1.0), math.matrix_translation(f32, -200.0, 0.0, 0.5));
text_shape.*.build();
text_shape.transform.model = math.matrix_multiply(math.matrix_scaling(f32, 5.0, 5.0, 1.0), math.matrix_translation(f32, -200.0, 0.0, 0.5));
text_shape.build();
//

//build button
Expand All @@ -227,12 +219,10 @@ pub fn xfit_init() !void {
rect_button_src = graphics.shape_source.init();
try rect_button_src.build(arena_alloc, concat_button_src, .gpu, .cpu);

rect_button.* = .{ ._button = try components.button.init(&rect_button_src, .{ .rect = math.rect.calc_with_canvas(button_area_rect, CANVAS_W, CANVAS_H) }, button_src[0]) };
rect_button.*._button.shape.transform.camera = &g_camera;
rect_button.*._button.shape.transform.projection = &g_proj;
rect_button.*.build();

g_rect_button = &rect_button.*._button;
rect_button = try components.button.init(&rect_button_src, .{ .rect = math.rect.calc_with_canvas(button_area_rect, CANVAS_W, CANVAS_H) }, button_src[0]);
rect_button.shape.transform.camera = &g_camera;
rect_button.shape.transform.projection = &g_proj;
rect_button.build();

//

Expand All @@ -250,22 +240,22 @@ pub fn xfit_init() !void {
github_shape_src = graphics.shape_source.init();
try github_shape_src.build(arena_alloc, github_shape_raw, .gpu, .cpu);

github_shape.* = .{ ._shape = graphics.shape.init(&github_shape_src) };
github_shape.*._shape.transform.camera = &g_camera;
github_shape.*._shape.transform.projection = &g_proj;
github_shape.*._shape.transform.model = math.matrix_multiply(
github_shape = graphics.shape.init(&github_shape_src);
github_shape.transform.camera = &g_camera;
github_shape.transform.projection = &g_proj;
github_shape.transform.model = math.matrix_multiply(
math.matrix_scaling(f32, 2, 2, 1),
math.matrix_translation(f32, -450, 0, 0),
);
github_shape.*.build();
github_shape.build();
//

//append objects
try objects.append(img);
try objects.append(text_shape);
try objects.append(anim_img);
try objects.append(rect_button);
try objects.append(github_shape);
try objects.append(graphics.iobject.init(&img));
try objects.append(graphics.iobject.init(&text_shape));
try objects.append(graphics.iobject.init(&anim_img));
try objects.append(graphics.iobject.init(&rect_button.shape));
try objects.append(graphics.iobject.init(&github_shape));
//

cmd = render_command.init();
Expand Down Expand Up @@ -302,23 +292,23 @@ pub fn xfit_init() !void {
}

fn mouse_move(pos: math.point) void {
g_rect_button.on_mouse_move(pos, {}, .{});
rect_button.on_mouse_move(pos, {}, .{});
}
fn mouse_down(pos: math.point) void {
g_rect_button.on_mouse_down(pos, {}, .{});
rect_button.on_mouse_down(pos, {}, .{});
}
fn mouse_up(pos: math.point) void {
g_rect_button.on_mouse_up(pos, {}, .{});
rect_button.on_mouse_up(pos, {}, .{});
}

fn touch_down(touch_idx: u32, pos: math.point) void {
g_rect_button.on_touch_down(touch_idx, pos, {}, .{});
rect_button.on_touch_down(touch_idx, pos, {}, .{});
}
fn touch_up(touch_idx: u32, pos: math.point) void {
g_rect_button.on_touch_up(touch_idx, pos, {}, .{});
rect_button.on_touch_up(touch_idx, pos, {}, .{});
}
fn touch_move(touch_idx: u32, pos: math.point) void {
g_rect_button.on_touch_move(touch_idx, pos, {}, .{});
rect_button.on_touch_move(touch_idx, pos, {}, .{});
}

var image_front: bool = false;
Expand Down Expand Up @@ -384,11 +374,11 @@ pub fn xfit_update() !void {
update_mutex.lock();

shape_src.*.copy_color_update(0, &[_]math.vector{.{ 1, 1, 1, shape_alpha }});
text_shape.*._shape.transform.model = math.matrix_multiply(math.matrix_scaling(f32, 5, 5, 1.0), math.matrix_translation(f32, -200 + dx, 0, 0.5));
text_shape.transform.model = math.matrix_multiply(math.matrix_scaling(f32, 5, 5, 1.0), math.matrix_translation(f32, -200 + dx, 0, 0.5));
update_mutex.unlock();

text_shape.*._shape.transform.copy_update();
rect_button.*.update();
text_shape.transform.copy_update();
rect_button.update();

anim.update(xfit.dt());
}
Expand All @@ -398,7 +388,7 @@ pub fn xfit_size() !void {

g_proj.copy_update();

g_rect_button.*.area.rect = math.rect.calc_with_canvas(button_area_rect, CANVAS_W, CANVAS_H);
rect_button.area.rect = math.rect.calc_with_canvas(button_area_rect, CANVAS_W, CANVAS_H);
}

///before system clean
Expand All @@ -415,7 +405,7 @@ pub fn xfit_destroy() !void {
g_camera.deinit();
g_proj.deinit();

for (objects.items) |value| {
for (objects.items) |*value| {
value.*.deinit();
}

Expand Down
18 changes: 5 additions & 13 deletions src/__vulkan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ fn recordCommandBuffer(commandBuffer: **render_command, fr: u32) void {
shape_list.resize(0) catch unreachable;

commandBuffer.*.*.objs_mutex.lock();
const objs = __system.allocator.dupe(*graphics.iobject, commandBuffer.*.*.scene.?) catch unreachable;
const objs = __system.allocator.dupe(graphics.iobject, commandBuffer.*.*.scene.?) catch unreachable;
commandBuffer.*.*.objs_mutex.unlock();
defer __system.allocator.free(objs);

Expand All @@ -509,13 +509,9 @@ fn recordCommandBuffer(commandBuffer: **render_command, fr: u32) void {
cmd_executed = true;
}

for (objs) |value| {
if (!value.*.is_shape_type()) {
if (value.* == ._group) {
value.*._group.group_draw(@intFromEnum(cmd));
} else {
value.*.draw(@intFromEnum(cmd));
}
for (objs) |*value| {
if (!value.*.v.*.__xfit_is_shape_type) {
value.*.v.*.draw(value.*.target, @intFromEnum(cmd));
} else {
shape_list.append(value) catch unreachable;
}
Expand All @@ -537,11 +533,7 @@ fn recordCommandBuffer(commandBuffer: **render_command, fr: u32) void {
renderPassInfo2.render_pass = vkRenderPassSample;
vkd.?.cmdBeginRenderPass(cmd, &renderPassInfo2, .@"inline");
for (shape_list.items) |value| {
if (value.* == ._shape_group) {
value.*._group.group_draw(@intFromEnum(cmd));
} else {
value.*.draw(@intFromEnum(cmd));
}
value.*.v.*.draw(value.target, @intFromEnum(cmd));
}
vkd.?.cmdEndRenderPass(cmd);

Expand Down
82 changes: 41 additions & 41 deletions src/animator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,56 @@ const xfit = @import("xfit.zig");
const animate_image = graphics.animate_image;
const iobject = graphics.iobject;

pub const animate_object = struct {
obj: *iobject,
pub const ianimate_object = struct {
target: *anyopaque,
v: *const vtable,

pub inline fn prev_frame(self: *animate_object) void {
switch (self.obj.*) {
inline ._anim_image => |*case| case.*.prev_frame(),
else => xfit.print_error("WARN animate_object.prev_frame not support\n", .{}),
pub const vtable = struct {
v: iobject.vtable,
prev_frame: *const fn (self: *anyopaque) void,
next_frame: *const fn (self: *anyopaque) void,
set_frame: *const fn (self: *anyopaque, _frame: u32) void,
cur_frame: *const fn (self: *anyopaque) u32,
get_frame_count_build: *const fn (self: *anyopaque) u32,

pub fn make(comptime T: type) vtable {
return .{
.prev_frame = @ptrCast(&T.prev_frame),
.next_frame = @ptrCast(&T.next_frame),
.set_frame = @ptrCast(&T.set_frame),
.cur_frame = @ptrCast(&T.cur_frame),
.get_frame_count_build = @ptrCast(&T.get_frame_count_build),
.v = iobject.vtable.make(T),
};
}
pub const find = graphics.iobject.vtable.find;
};

pub fn deinit(self: ianimate_object) void {
self.v.*.v.deinit(self.target);
}
pub inline fn map_update_frame(self: *animate_object) void {
switch (self.obj.*) {
inline ._anim_image => |*case| case.*.map_update_frame(),
else => xfit.print_error("WARN animate_object.map_update_frame not support\n", .{}),
}
pub fn prev_frame(self: ianimate_object) void {
self.v.*.prev_frame(self.target);
}
pub inline fn next_frame(self: *animate_object) void {
switch (self.obj.*) {
inline ._anim_image => |*case| case.*.next_frame(),
else => xfit.print_error("WARN animate_object.next_frame not support\n", .{}),
}
pub fn next_frame(self: ianimate_object) void {
self.v.*.next_frame(self.target);
}
pub inline fn set_frame(self: *animate_object, _frame: u32) void {
switch (self.obj.*) {
inline ._anim_image => |*case| case.*.set_frame(_frame),
else => xfit.print_error("WARN animate_object.set_frame not support\n", .{}),
}
pub fn set_frame(self: ianimate_object, _frame: u32) void {
self.v.*.set_frame(self.target, _frame);
}
pub inline fn cur_frame(self: *animate_object) u32 {
switch (self.obj.*) {
inline ._anim_image => |*case| return case.*.frame,
else => xfit.print_error("WARN animate_object.cur_frame not support\n", .{}),
}
return 0;
pub fn cur_frame(self: ianimate_object) u32 {
return self.v.*.cur_frame(self.target);
}
pub inline fn get_tex_count_build(self: *animate_object) u32 {
switch (self.obj.*) {
inline ._anim_image => |*case| return case.*.src.*.get_tex_count_build(),
else => xfit.print_error("WARN animate_object.get_tex_count_build not support\n", .{}),
}
return 0;
pub fn get_frame_count_build(self: ianimate_object) u32 {
return self.v.*.get_frame_count_build(self.target);
}
pub inline fn update(self: *animate_object) void {
switch (self.obj.*) {
inline else => |*case| case.*.update(),
}
pub fn init(_obj_ptr: anytype) ianimate_object {
return iobject.__init(_obj_ptr, ianimate_object);
}
};

pub const multi_player = struct {
objs: []animate_object,
objs: []ianimate_object,
playing: bool = false,
target_fps: f32 = 30,
__playing_dt: f32 = 0,
Expand All @@ -67,7 +67,7 @@ pub const multi_player = struct {
while (self.*.__playing_dt >= 1 / self.*.target_fps) : (self.*.__playing_dt -= 1 / self.*.target_fps) {
var isp: bool = false;
for (self.*.objs) |*v| {
if (self.*.loop or v.*.cur_frame() < v.*.get_tex_count_build() - 1) {
if (self.*.loop or v.*.cur_frame() < v.*.get_frame_count_build() - 1) {
v.*.next_frame();
isp = true;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ pub const multi_player = struct {
};

pub const player = struct {
obj: animate_object,
obj: ianimate_object,
playing: bool = false,
target_fps: f64 = 30,
__playing_dt: f64 = 0,
Expand All @@ -115,7 +115,7 @@ pub const player = struct {
const dt: f64 = _dt;
self.*.__playing_dt += dt;
while (self.*.__playing_dt >= 1 / self.*.target_fps) : (self.*.__playing_dt -= 1 / self.*.target_fps) {
if (self.*.loop or self.*.obj.cur_frame() < self.*.obj.get_tex_count_build() - 1) {
if (self.*.loop or self.*.obj.cur_frame() < self.*.obj.get_frame_count_build() - 1) {
self.*.obj.next_frame();
} else {
self.*.stop();
Expand Down
Loading

0 comments on commit 8ee2b80

Please sign in to comment.