summaryrefslogtreecommitdiff
path: root/c2.cpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2025-01-09 21:32:05 +1100
committerquou <quou@disroot.org>2025-01-09 21:32:05 +1100
commitbf29ba639dbec647fc347a401cff05ced7e2b22f (patch)
tree0e2d86f532a1096350bcd963c7fcd8546d527e1e /c2.cpp
parent78321be96d15afaccb3d0a179e91a3e5887d3376 (diff)
refactor app
Diffstat (limited to 'c2.cpp')
-rw-r--r--c2.cpp181
1 files changed, 93 insertions, 88 deletions
diff --git a/c2.cpp b/c2.cpp
index e4834ad..c9ab715 100644
--- a/c2.cpp
+++ b/c2.cpp
@@ -21,15 +21,6 @@ static float verts[] = {
0.0f, -0.5f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f
};
-struct C2 : public App {
- Device* dev;
- UI* ui;
- void on_resize() override {
- ui->layout(w, h);
- dev->on_resize();
- }
-};
-
static Buffer_Id upload_verts(Device* dev) {
Buffer_Id stage = dev->create_buffer(
"temp",
@@ -326,7 +317,7 @@ struct Env_Probe {
res,
res,
1,
- 1,
+ 0,
1,
Buffer_Id(0)
);
@@ -433,7 +424,7 @@ struct Config_Buffer2 {
m4f projection;
};
-extern "C" int entrypoint() {
+struct C2 : public App {
Arena video_arena, asset_arena, ui_arena;
Model_Loader model_loader;
Material_Loader mat_loader;
@@ -450,80 +441,81 @@ extern "C" int entrypoint() {
Env_Probe eprobe;
Buffer_Id vbo, cbuf;
Sampler_Id clamped_linear;
- C2* app = App::create<C2>("c2");
UI* ui;
+ UI::Label* fps_label;
void* per_frame;
int frame = 0;
- app->running = 1;
- init_arena(
- &video_arena,
- arena_alloc(app->arena, video_arena_size),
- video_arena_size
- );
- init_arena(
- &asset_arena,
- arena_alloc(app->arena, asset_arena_size),
- asset_arena_size
- );
- init_arena(
- &ui_arena,
- arena_alloc(app->arena, ui_arena_size),
- ui_arena_size
- );
- assets.init(&asset_arena, "pack", 128);
- dev = Device::create(&video_arena, app);
- default_texture = make_default_texture(dev);
- model_loader.init(dev, &assets);
- mat_loader.init(&assets, default_texture);
- register_asset_loader("MODL", &model_loader);
- register_asset_loader("MTRL", &mat_loader);
- app->dev = dev;
- shader = (Shader*)assets.load("triangle.csh");
- ui_shader = (Shader*)assets.load("ui.csh");
- texture = (Texture*)assets.load("22.tex");
- texture2 = (Texture*)assets.load("kita.tex");
- cbuf = dev->create_buffer(
- "config buffer",
- sizeof(Config_Buffer),
- Buffer_Flags::constant_buffer |
- Buffer_Flags::cpu_readwrite
- );
- per_frame = heap_alloc(
- dev->heap,
- per_frame_memory_size
- );
- clamped_linear = create_clamped_linear(dev);
- ui = UI::create(dev, app, &ui_arena, ui_shader->id);
- app->ui = ui;
- assert(per_frame != 0);
uint8_t r = 0;
float rot = 0.0f;
- v3f raxis(0.0f, 1.0f, 0.0);
- vbo = upload_verts(dev);
- ui->layout(app->w, app->h);
- auto fps_label = ui->create_element<UI::Label>(ui->root, "");
- scene.init(app->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();
- while (app->running) {
+ v3f raxis = v3f(0.0f, 1.0f, 0.0);
+
+ void on_init() override {
+ running = 1;
+ init_arena(
+ &video_arena,
+ arena_alloc(arena, video_arena_size),
+ video_arena_size
+ );
+ init_arena(
+ &asset_arena,
+ arena_alloc(arena, asset_arena_size),
+ asset_arena_size
+ );
+ init_arena(
+ &ui_arena,
+ arena_alloc(arena, ui_arena_size),
+ ui_arena_size
+ );
+ assets.init(&asset_arena, "pack", 128);
+ dev = Device::create(&video_arena, this);
+ default_texture = make_default_texture(dev);
+ model_loader.init(dev, &assets);
+ mat_loader.init(&assets, default_texture);
+ register_asset_loader("MODL", &model_loader);
+ register_asset_loader("MTRL", &mat_loader);
+ shader = (Shader*)assets.load("triangle.csh");
+ ui_shader = (Shader*)assets.load("ui.csh");
+ texture = (Texture*)assets.load("22.tex");
+ texture2 = (Texture*)assets.load("kita.tex");
+ cbuf = dev->create_buffer(
+ "config buffer",
+ sizeof(Config_Buffer),
+ Buffer_Flags::constant_buffer |
+ Buffer_Flags::cpu_readwrite
+ );
+ per_frame = heap_alloc(
+ dev->heap,
+ per_frame_memory_size
+ );
+ clamped_linear = create_clamped_linear(dev);
+ ui = UI::create(dev, this, &ui_arena, ui_shader->id);
+ assert(per_frame != 0);
+ vbo = upload_verts(dev);
+ ui->layout(w, h);
+ fps_label = ui->create_element<UI::Label>(ui->root, "");
+ scene.init(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();
+ }
+
+ void on_update() override {
Arena frame_arena;
init_arena(&frame_arena, per_frame, per_frame_memory_size);
- app->begin();
dev->begin_frame();
if (frame % 10 == 0) {
char buf[32];
- sprintf(buf, "FPS: %g", 1.0f / app->dt);
+ sprintf(buf, "FPS: %g", 1.0f / dt);
fps_label->set_text(buf);
}
ui->update(&frame_arena);
@@ -613,7 +605,7 @@ extern "C" int entrypoint() {
v3f(1.0f, 0.0f, 0.0f)
);
camera.asp = (float)bb.w / (float)bb.h;
- camera.update(*app);
+ camera.update(*this);
scene.update(camera, dev);
ctx.debug_push("scene");
ctx.debug_push("depth prepass");
@@ -639,21 +631,34 @@ extern "C" int entrypoint() {
ctx.debug_pop();
r += 10;
- rot += 1.0f * app->dt;
+ rot += 1.0f * dt;
frame++;
dev->present();
- app->end();
}
- scene.destroy(dev);
- sky.destroy(dev);
- eprobe.destroy(dev);
- ui->destroy();
- assets.destroy();
- dev->destroy_texture(default_texture);
- dev->destroy_sampler(clamped_linear);
- dev->destroy_buffer(vbo);
- dev->destroy_buffer(cbuf);
- dev->destroy();
- app->destroy();
- return 0;
+
+ void on_destroy() override {
+ scene.destroy(dev);
+ sky.destroy(dev);
+ eprobe.destroy(dev);
+ ui->destroy();
+ assets.destroy();
+ dev->destroy_texture(default_texture);
+ dev->destroy_sampler(clamped_linear);
+ dev->destroy_buffer(vbo);
+ dev->destroy_buffer(cbuf);
+ dev->destroy();
+ }
+
+ void on_resize() override {
+ ui->layout(w, h);
+ dev->on_resize();
+ }
+};
+
+
+extern "C" App* entrypoint(Arena* mem) {
+ C2* a = (C2*)arena_alloc(mem, sizeof *a);
+ new(a) C2();
+ a->init("C2", mem);
+ return a;
}