summaryrefslogtreecommitdiff
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
parent78321be96d15afaccb3d0a179e91a3e5887d3376 (diff)
refactor app
-rw-r--r--app.cpp30
-rw-r--r--app.hpp16
-rw-r--r--c2.cpp181
3 files changed, 116 insertions, 111 deletions
diff --git a/app.cpp b/app.cpp
index 0f57a71..ff8d7ba 100644
--- a/app.cpp
+++ b/app.cpp
@@ -4,7 +4,7 @@ extern "C" {
#include "memory.h"
#include "plat.h"
-extern int entrypoint();
+extern App* entrypoint(Arena* mem);
}
@@ -272,7 +272,22 @@ void app_destroy_vk_surface(
}
int main() {
- return entrypoint();
+ char* mem = (char*)malloc(app_memory_size);
+ Arena* arena = (Arena*)mem;
+ App* a;
+ mem += sizeof *arena;
+ init_arena(arena, mem, app_memory_size - sizeof *arena);
+ a = entrypoint(arena);
+ a->arena = arena;
+ a->on_init();
+ while (a->running) {
+ a->begin();
+ a->on_update();
+ a->end();
+ }
+ a->on_destroy();
+ a->destroy();
+ return 0;
}
#endif
@@ -514,8 +529,9 @@ int WinMain(
#endif
-void App::init(const char* name) {
+void App::init(const char* name, Arena* a) {
dt = 0.0f;
+ arena = a;
internal = (App_Internal*)arena_alloc(
arena,
sizeof *internal
@@ -523,14 +539,6 @@ void App::init(const char* name) {
internal->init(this, name);
}
-void* App::alloc(int size, Arena*& arena) {
- char* mem = (char*)malloc(app_memory_size);
- arena = (Arena*)mem;
- mem += sizeof *arena;
- init_arena(arena, mem, app_memory_size - sizeof *arena);
- return arena_alloc(arena, size);
-}
-
void App::destroy() {
free(arena);
}
diff --git a/app.hpp b/app.hpp
index 9a660cf..a1fb911 100644
--- a/app.hpp
+++ b/app.hpp
@@ -112,18 +112,7 @@ struct App {
uint64_t begin_t, end_t;
App_Internal* internal;
- template <typename T>
- static T* create(const char* name) {
- Arena* arena;
- T* app = (T*)alloc(sizeof(T), arena);
- app = new(app) T();
- app->arena = arena;
- app->running = 0;
- app->init(name);
- return app;
- }
- static void* alloc(int size, Arena*& arena);
- void init(const char* name);
+ void init(const char* name, Arena* a);
void destroy();
void begin();
@@ -140,6 +129,9 @@ struct App {
void get_vk_exts(const char** exts, int& count);
+ virtual void on_init() = 0;
+ virtual void on_update() = 0;
+ virtual void on_destroy() = 0;
virtual void on_resize() = 0;
};
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;
}