summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorOmniscient <omniscient.oce@gmail.com>2024-07-11 18:03:29 +1000
committerOmniscient <omniscient.oce@gmail.com>2024-07-11 18:03:29 +1000
commit65d74bdb26af833b5380046dec204f685f745cc1 (patch)
tree6a913e8b47787fff9f4650963074ea3f8ab5de27 /src/core
parent3103f383751a12f8a0bdb22309704f1f826d204c (diff)
changing styles plus simplifying a bit
Diffstat (limited to 'src/core')
-rw-r--r--src/core/README.md3
-rw-r--r--src/core/camera.c22
-rw-r--r--src/core/camera.h37
-rw-r--r--src/core/core.c89
-rw-r--r--src/core/core.h40
5 files changed, 191 insertions, 0 deletions
diff --git a/src/core/README.md b/src/core/README.md
new file mode 100644
index 0000000..19cc1d0
--- /dev/null
+++ b/src/core/README.md
@@ -0,0 +1,3 @@
+# Core
+
+Core engine facilities
diff --git a/src/core/camera.c b/src/core/camera.c
new file mode 100644
index 0000000..50c4054
--- /dev/null
+++ b/src/core/camera.c
@@ -0,0 +1,22 @@
+#include "camera.h"
+
+#include "maths.h"
+
+Camera Camera_Create(vec3 pos, vec3 front, vec3 up, f32 fov) {
+ Camera c = { .position = pos, .front = front, .up = up, .fov = fov };
+ return c;
+}
+
+mat4 Camera_ViewProj(Camera *c, f32 lens_height, f32 lens_width, mat4 *out_view,
+ mat4 *out_proj) {
+ mat4 proj = mat4_perspective(c->fov, lens_width / lens_height, 0.1, 100.0);
+ vec3 camera_direction = vec3_add(c->position, c->front);
+ mat4 view = mat4_look_at(c->position, camera_direction, c->up);
+ if (out_view) {
+ *out_view = view;
+ }
+ if (out_proj) {
+ *out_proj = proj;
+ }
+ return mat4_mult(view, proj);
+}
diff --git a/src/core/camera.h b/src/core/camera.h
new file mode 100644
index 0000000..233f5f3
--- /dev/null
+++ b/src/core/camera.h
@@ -0,0 +1,37 @@
+/**
+ * @file camera.h
+ * @brief
+ */
+#pragma once
+
+#include "defines.h"
+#include "maths_types.h"
+
+typedef struct Camera {
+ vec3 position;
+ vec3 front;
+ vec3 up;
+ f32 fov;
+} Camera;
+
+/** @brief create a camera */
+Camera Camera_Create(vec3 pos, vec3 front, vec3 up, f32 fov);
+
+/**
+ * @brief Get 3D camera transform matrix
+ * @param out_view optionally stores just the view matrix
+ * @param out_proj optionally stores just the projection matrix
+ * @returns the camera's view projection matrix pre-multiplied
+*/
+PUB mat4 Camera_ViewProj(Camera* c, f32 lens_height, f32 lens_width, mat4* out_view, mat4* out_proj);
+
+/** @brief Get 2D camera transform matrix */
+PUB mat4 Camera_View2D(Camera* c); // TODO: 2D cameras
+
+
+// TODO: Basic reusable camera controls
+/*
+Right click + move = pan
+Left click = orbit camera
+WASD = forward/backward/left/right
+*/
diff --git a/src/core/core.c b/src/core/core.c
new file mode 100644
index 0000000..1d8fe91
--- /dev/null
+++ b/src/core/core.c
@@ -0,0 +1,89 @@
+#include "core.h"
+
+#include <stdlib.h>
+
+#include "glfw3.h"
+#include "input.h"
+#include "keys.h"
+#include "log.h"
+#include "mem.h"
+#include "render.h"
+#include "render_types.h"
+#include "scene.h"
+// #include "threadpool.h"
+
+#define SCR_WIDTH 1000
+#define SCR_HEIGHT 1000
+
+core g_core; /** @brief global `core` that other files can use */
+
+inline core* get_global_core() { return &g_core; }
+
+void core_bringup() {
+ INFO("Initiate Core bringup");
+ renderer_config conf = { .window_name = { "Celeritas Engine Core" },
+ .scr_width = SCR_WIDTH,
+ .scr_height = SCR_HEIGHT,
+ .clear_colour = (vec3){ .08, .08, .1 } };
+ g_core.renderer.config = conf;
+ g_core.renderer.backend_context = NULL;
+
+ // threadpool_create(&c->threadpool, 6, 256);
+ // threadpool_set_ctx(&c->threadpool, c); // Gives the threadpool access to the core
+
+ // initialise all subsystems
+ if (!renderer_init(&g_core.renderer)) {
+ // FATAL("Failed to start renderer");
+ ERROR_EXIT("Failed to start renderer\n");
+ }
+ if (!input_system_init(&g_core.input, g_core.renderer.window)) {
+ // the input system needs the glfw window which is created by the renderer
+ // hence the order here is important
+ FATAL("Failed to start input system");
+ ERROR_EXIT("Failed to start input system\n");
+ }
+ /*
+ if (!text_system_init(&c->text)) {
+ // FATAL("Failed to start text system");
+ ERROR_EXIT("Failed to start text system\n");
+ }
+ if (!screenspace_2d_init(&c->screenspace)) {
+ // FATAL("Failed to start screenspace 2d plugin");
+ ERROR_EXIT("Failed to start screenspace 2d plugin\n");
+ }
+ */
+
+ size_t model_data_max = 1024 * 1024 * 1024;
+ arena model_arena = arena_create(malloc(model_data_max), model_data_max);
+
+ model_pool model_pool = model_pool_create(&model_arena, 256, sizeof(model));
+ g_core.models = model_pool;
+ INFO("Created model pool allocator");
+
+ INFO("Creating default scene");
+ scene_init(&g_core.default_scene);
+}
+
+#include <glfw3.h>
+
+/* bool should_window_close(core* core) { glfwWindowShouldClose(core->renderer.window); } */
+void core_input_update() { input_update(&g_core.input); }
+void core_frame_begin(core* core) { render_frame_begin(&core->renderer); }
+void core_frame_end(core* core) { render_frame_end(&core->renderer); }
+
+void core_shutdown() {
+ // threadpool_destroy(&core->threadpool);
+ input_system_shutdown(&g_core.input);
+ renderer_shutdown(&g_core.renderer);
+}
+
+bool should_exit() {
+ return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(g_core.renderer.window);
+}
+
+void frame_begin() {
+ glfwPollEvents();
+ render_frame_begin(&g_core.renderer);
+}
+void frame_draw() {}
+void frame_end() { render_frame_end(&g_core.renderer); }
diff --git a/src/core/core.h b/src/core/core.h
new file mode 100644
index 0000000..89702fd
--- /dev/null
+++ b/src/core/core.h
@@ -0,0 +1,40 @@
+#pragma once
+
+#include "input.h"
+#include "render_types.h"
+#include "scene.h"
+#include "screenspace.h"
+#include "terrain.h"
+#include "text.h"
+// #include "threadpool.h"
+
+typedef struct core {
+ const char* app_name;
+ // foundations
+ renderer renderer;
+ // threadpool threadpool;
+ // systems
+ input_state input;
+ text_system_state text;
+ terrain_state terrain;
+ screenspace_state screenspace;
+ // data storage
+ scene default_scene;
+ model_pool models;
+ // model_darray* models;
+} core;
+
+core* get_global_core();
+
+// --- Lifecycle
+
+/** @brief Throws error if the core cannot be instantiated */
+void core_bringup();
+void core_shutdown();
+bool should_exit();
+
+void frame_begin();
+void frame_draw();
+void frame_end();
+
+void core_input_update();