Skip to content

Commit b00d061

Browse files
committed
Fix check entities and add perfs
1 parent 5ddd362 commit b00d061

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/zimpact/engine.zig

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ var background_maps_len: u32 = 0;
6868
pub var viewport: Vec2 = .{ .x = 0, .y = 0 };
6969

7070
// Various infos about the last frame
71-
// struct {
72-
// int entities;
73-
// int checks;
74-
// int draw_calls;
75-
// float update;
76-
// float draw;
77-
// float total;
78-
// } perf;
71+
const Perf = struct {
72+
entities: usize,
73+
checks: usize,
74+
draw_calls: usize,
75+
update: f32,
76+
draw: f32,
77+
total: f32,
78+
};
79+
pub var perf: Perf = undefined;
7980

8081
var scene: ?*Scene = null;
8182
var scene_next: ?*Scene = null;
@@ -125,7 +126,7 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
125126
}
126127

127128
pub fn update() void {
128-
// const time_frame_start = platform.now();
129+
const time_frame_start = platform.now();
129130

130131
// Do we want to switch scenes?
131132
if (scene_next) |scene_n| {
@@ -174,7 +175,7 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
174175
sceneBaseUpdate();
175176
}
176177

177-
// perf.update = platform_now() - time_real_now;
178+
perf.update = @floatCast(platform.now() - time_real_now);
178179

179180
render.framePrepare();
180181

@@ -186,16 +187,16 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
186187
}
187188

188189
render.frameEnd();
189-
// engine.perf.draw = (platform_now() - time_real_now) - engine.perf.update;
190+
perf.draw = @as(f32, @floatCast(platform.now() - time_real_now)) - perf.update;
190191
alloc.bumpReset(mark);
191192
mark.index = 0xFFFFFFFF;
192193
}
193194

194195
input.clear();
195196
// temp.alloc_check();
196197

197-
// engine.perf.draw_calls = render_draw_calls();
198-
// engine.perf.total = platform_now() - time_frame_start;
198+
// perf.draw_calls = render.drawCalls();
199+
perf.total = @floatCast(platform.now() - time_frame_start);
199200
}
200201

201202
pub fn cleanup() void {
@@ -347,19 +348,17 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
347348
const len: usize = entities_len;
348349

349350
// Sweep touches
350-
// engine.perf.checks = 0;
351+
perf.checks = 0;
351352
i = 0;
352-
for (entities) |e1| {
353-
if (i == len) break;
354-
353+
for (entities[0..len]) |e1| {
355354
if (e1.base.check_against != ett.ENTITY_GROUP_NONE or
356355
e1.base.group != ett.ENTITY_GROUP_NONE or (e1.base.physics > ett.ENTITY_COLLIDES_LITE))
357356
{
358357
const max_pos = e1.base.pos.x + e1.base.size.x;
359358
var j: usize = i + 1;
360359
while (j < len and entities[j].base.pos.x < max_pos) {
361360
const e2 = entities[j];
362-
// engine.perf.checks += 1;
361+
perf.checks += 1;
363362

364363
if (entityIsTouching(e1, e2)) {
365364
if (contains(e1.base.check_against, e2.base.group)) {
@@ -383,15 +382,16 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
383382
i += 1;
384383
}
385384

386-
//engine.perf.entities = entities_len;
385+
perf.entities = entities_len;
387386
}
387+
388388
fn contains(g1: u8, g2: u8) bool {
389389
return (g1 & g2) != 0;
390390
}
391391

392392
fn cmpEntityPos(context: void, a: *T, b: *T) bool {
393393
_ = context;
394-
return a.base.pos.x > b.base.pos.x;
394+
return a.base.pos.x <= b.base.pos.x;
395395
}
396396

397397
fn initEntity(entity: *T) void {
@@ -631,7 +631,18 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
631631
.base = .{
632632
.id = entity_unique_id,
633633
.is_alive = true,
634+
.on_ground = false,
635+
.draw_order = 0,
636+
.physics = ett.ENTITY_GROUP_NONE,
637+
.group = ett.ENTITY_GROUP_NONE,
638+
.check_against = ett.ENTITY_GROUP_NONE,
634639
.pos = pos,
640+
.vel = vec2(0, 0),
641+
.accel = vec2(0, 0),
642+
.friction = vec2(0, 0),
643+
.offset = vec2(0, 0),
644+
.health = 0,
645+
.restitution = 0,
635646
.max_ground_normal = 0.69, // cosf(to_radians(46)),
636647
.min_slide_normal = 1, // cosf(to_radians(0)),
637648
.gravity = 1,
@@ -725,7 +736,7 @@ pub fn Engine(comptime T: type, comptime TKind: type) type {
725736

726737
fn cmpEntity(context: void, lhs: *T, rhs: *T) bool {
727738
_ = context;
728-
return lhs.base.draw_order > rhs.base.draw_order;
739+
return lhs.base.draw_order <= rhs.base.draw_order;
729740
}
730741

731742
const EntitySettings = struct {

0 commit comments

Comments
 (0)