From cf2abfcfe34a9b3e4cf5af193d1f320374416267 Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 18 Jan 2025 16:19:41 +1100 Subject: convert models and editor to use ecs --- c2.cpp | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'c2.cpp') diff --git a/c2.cpp b/c2.cpp index ff4196f..d686765 100644 --- a/c2.cpp +++ b/c2.cpp @@ -3,8 +3,10 @@ #include "debugdraw.hpp" #include "editor.hpp" #include "model.hpp" +#include "scene.hpp" #include "ui.hpp" #include "video.hpp" +#include "world.hpp" extern "C" { #include "plat.h" #include "memory.h" @@ -571,7 +573,7 @@ struct C2 : public App { Texture* texture; Texture* texture2; Texture_Id default_texture; - Model_Instance* monkey, * monkey2; + Entity_Id monkey, monkey2; Model_Scene scene; Orbit_Cam camera; Fullscreen_Quad quad; @@ -580,6 +582,7 @@ struct C2 : public App { Buffer_Id vbo, cbuf; Sampler_Id clamped_linear; Line_Renderer lr; + World* world; UI* ui; UI::Label* fps_label; void* per_frame; @@ -643,17 +646,27 @@ struct C2 : public App { ui->layout(w, h); fps_label = ui->create_element(ui->root, ""); scene.init(&scene_arena, 32, clamped_linear); - monkey = scene.instantiate( - dev, - (Model*)assets.load("monkey.mdl") - ); - monkey2 = scene.instantiate( - dev, - (Model*)assets.load("monkey.mdl") - ); sky.init(dev, &assets); eprobe.init(dev, &assets, 256); camera.init(); + world = (World*)arena_alloc(arena, sizeof *world); + world->init(arena); + { + monkey = world->create_entity(); + auto [_, mod] = world->add(monkey); + mod.i = scene.instantiate( + dev, + (Model*)assets.load("monkey.mdl") + ); + } + { + monkey2 = world->create_entity(); + auto [_, mod] = world->add(monkey2); + mod.i = scene.instantiate( + dev, + (Model*)assets.load("monkey.mdl") + ); + } } void on_update() override { @@ -738,7 +751,7 @@ struct C2 : public App { ctx.debug_pop(); Texture& bb = dev->get_texture(dev->get_backbuffer()); - monkey->transform = m4f::translate( + world->get(monkey).mat = m4f::translate( m4f::identity(), v3f(0.0f, 0.0f, 0.0f) ) * m4f::rotate( @@ -746,7 +759,7 @@ struct C2 : public App { rot, raxis ); - monkey2->transform = m4f::translate( + world->get(monkey2).mat = m4f::translate( m4f::identity(), v3f(2.0f, 0.0f, 2.0f) ) * m4f::rotate( @@ -757,7 +770,7 @@ struct C2 : public App { camera.asp = (float)bb.w / (float)bb.h; camera.update_orbit(*this); camera.update(); - scene.update(camera, dev); + update_scene(scene, dev, camera, *world); ctx.debug_push("scene"); ctx.debug_push("depth prepass"); scene.render(dev, &frame_arena, depth_prepass, 0, clamped_linear); @@ -783,8 +796,15 @@ struct C2 : public App { ctx.debug_pop(); if (mjp(mbtn_left)) { - auto [a, b] = scene.pick(camera, w, h, mx, my); - editor_on_select(a, b); + auto [a, b] = scene_pick( + *world, + camera, + w, + h, + mx, + my + ); + editor_on_select(*world, a, b); } lr.colour(v3f(1.0f, 0.0f, 0.0f)); @@ -830,7 +850,6 @@ struct C2 : public App { } }; - extern "C" App* entrypoint(Arena* mem) { C2* a = (C2*)arena_alloc(mem, sizeof *a); new(a) C2(); -- cgit v1.2.3-54-g00ecf