diff options
Diffstat (limited to 'archive/src/core')
-rw-r--r-- | archive/src/core/README.md | 3 | ||||
-rw-r--r-- | archive/src/core/animation.c | 0 | ||||
-rw-r--r-- | archive/src/core/camera.c | 90 | ||||
-rw-r--r-- | archive/src/core/camera.h | 39 | ||||
-rw-r--r-- | archive/src/core/core.c | 81 | ||||
-rw-r--r-- | archive/src/core/core.h | 43 | ||||
-rw-r--r-- | archive/src/core/input.c | 0 | ||||
-rw-r--r-- | archive/src/core/vfs.h | 38 |
8 files changed, 294 insertions, 0 deletions
diff --git a/archive/src/core/README.md b/archive/src/core/README.md new file mode 100644 index 0000000..19cc1d0 --- /dev/null +++ b/archive/src/core/README.md @@ -0,0 +1,3 @@ +# Core + +Core engine facilities diff --git a/archive/src/core/animation.c b/archive/src/core/animation.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/archive/src/core/animation.c diff --git a/archive/src/core/camera.c b/archive/src/core/camera.c new file mode 100644 index 0000000..77ddad6 --- /dev/null +++ b/archive/src/core/camera.c @@ -0,0 +1,90 @@ +#include "camera.h" + +#include "input.h" +#include "keys.h" +#include "maths.h" + +#define CAMERA_SPEED 0.2 +#define CAMERA_SENSITIVITY 0.5 + +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, 1000.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); +} + +void FlyCamera_Update(Camera* camera) { + static f32 yaw = 0.0; + static f32 pitch = 0.0; + + // Keyboard + f32 speed = CAMERA_SPEED; + Vec3 horizontal = vec3_cross(camera->front, camera->up); + if (key_is_pressed(KEYCODE_A) || key_is_pressed(KEYCODE_KEY_LEFT)) { + Vec3 displacement = vec3_mult(horizontal, -speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_D) || key_is_pressed(KEYCODE_KEY_RIGHT)) { + Vec3 displacement = vec3_mult(horizontal, speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_W) || key_is_pressed(KEYCODE_KEY_UP)) { + Vec3 displacement = vec3_mult(camera->front, speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_S) || key_is_pressed(KEYCODE_KEY_DOWN)) { + Vec3 displacement = vec3_mult(camera->front, -speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_Q)) { + Vec3 displacement = vec3_mult(camera->up, speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_E)) { + Vec3 displacement = vec3_mult(camera->up, -speed); + camera->position = vec3_add(camera->position, displacement); + } + + // Mouse + if (MouseBtn_Held(MOUSEBTN_LEFT)) { + mouse_state mouse = Input_GetMouseState(); + // printf("Delta x: %d Delta y %d\n",mouse.x_delta, mouse.y_delta ); + + f32 x_offset = mouse.x_delta; + f32 y_offset = -mouse.y_delta; + + f32 sensitivity = CAMERA_SENSITIVITY; // change this value to your liking + x_offset *= sensitivity; + y_offset *= sensitivity; + + yaw += x_offset; + pitch += y_offset; + + // make sure that when pitch is out of bounds, screen doesn't get flipped + if (pitch > 89.0f) pitch = 89.0f; + if (pitch < -89.0f) pitch = -89.0f; + + Vec3 front; + front.x = cos(deg_to_rad(yaw) * cos(deg_to_rad(pitch))); + front.y = sin(deg_to_rad(pitch)); + front.z = sin(deg_to_rad(yaw)) * cos(deg_to_rad(pitch)); + front = vec3_normalise(front); + camera->front.x = front.x; + camera->front.y = front.y; + camera->front.z = front.z; + } + + // TODO: Right mouse => pan in screen space +} diff --git a/archive/src/core/camera.h b/archive/src/core/camera.h new file mode 100644 index 0000000..4300f87 --- /dev/null +++ b/archive/src/core/camera.h @@ -0,0 +1,39 @@ +#pragma once + +#include "defines.h" +#include "maths_types.h" + +// TODO: swap to position + quaternion + +typedef struct Camera { + Vec3 position; + Vec3 front; + Vec3 up; + f32 fov; +} Camera; + +/** @brief create a camera */ +PUB 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 + +struct Input_State; + +PUB void FlyCamera_Update(Camera* camera); + +// TODO: (HIGH) Basic reusable camera controls +/* +Right click + move = pan +Left click = orbit camera +WASD = forward/backward/left/right +*/ diff --git a/archive/src/core/core.c b/archive/src/core/core.c new file mode 100644 index 0000000..64f59f3 --- /dev/null +++ b/archive/src/core/core.c @@ -0,0 +1,81 @@ +#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" + +// These are only the initial window dimensions +#define SCR_WIDTH 1000 +#define SCR_HEIGHT 1000 + +Core g_core; /** @brief global `Core` that other files can use */ + +/** @brief Gets the global `Core` singleton */ +inline Core* GetCore() { return &g_core; } + +void Core_Bringup(const char* window_name, struct GLFWwindow* optional_window) { + INFO("Initiate Core bringup"); + memset(&g_core, 0, sizeof(Core)); + + RendererConfig conf = { .window_name = window_name, + .scr_width = SCR_WIDTH, + .scr_height = SCR_HEIGHT, + .clear_colour = (Vec3){ .08, .08, .1 } }; + + g_core.renderer = malloc(Renderer_GetMemReqs()); + + // Initialise all subsystems + + // renderer config, renderer ptr, ptr to store a window, and optional preexisting glfw window + if (!Renderer_Init(conf, g_core.renderer, &g_core.window, optional_window)) { + // FATAL("Failed to start renderer"); + ERROR_EXIT("Failed to start renderer\n"); + } + if (optional_window != NULL) { + g_core.window = optional_window; + } + + if (!Input_Init(&g_core.input, g_core.window)) { + // the input system needs the glfw window which is created by the renderer + // hence the order here is important + ERROR_EXIT("Failed to start input system\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"); +} + +void Core_Shutdown() { + Input_Shutdown(&g_core.input); + Renderer_Shutdown(g_core.renderer); + free(g_core.renderer); +} + +bool ShouldExit() { + return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(g_core.window); +} + +void Frame_Begin() { + Input_Update(&g_core.input); + Render_FrameBegin(g_core.renderer); +} +void Frame_Draw() {} +void Frame_End() { Render_FrameEnd(g_core.renderer); } + +Core* get_global_core() { return &g_core; } + +GLFWwindow* Core_GetGlfwWindowPtr(Core* core) { return g_core.window; } + +struct Renderer* Core_GetRenderer(Core* core) { return core->renderer; } + +Model* Model_Get(ModelHandle h) { return Model_pool_get(&g_core.models, h); } diff --git a/archive/src/core/core.h b/archive/src/core/core.h new file mode 100644 index 0000000..14ba65d --- /dev/null +++ b/archive/src/core/core.h @@ -0,0 +1,43 @@ +#pragma once + +#include "input.h" +#include "mem.h" +#include "render_types.h" +#include "screenspace.h" +#include "terrain.h" +#include "text.h" + +TYPED_POOL(Model, Model) +#define MODEL_GET(h) (Model_pool_get(&g_core.models, h)) +Model* Model_Get(ModelHandle h); + +typedef struct GLFWwindow GLFWwindow; + +typedef struct Core { + const char* app_name; + GLFWwindow* window; + Renderer* renderer; + Input_State input; + // Model_pool models; +} Core; +extern Core g_core; + +struct Renderer; + +Core* get_global_core(); + +/** + @brief Throws error if the core cannot be instantiated + @param [in] optional_window - Leave NULL if you want Celeritas to instantiate its own window with + GLFW, if you want to provide the glfw window then pass it in here. +*/ +void Core_Bringup(const char* window_name, GLFWwindow* optional_window); +void Core_Shutdown(); +bool ShouldExit(); + +GLFWwindow* Core_GetGlfwWindowPtr(Core* core); +struct Renderer* Core_GetRenderer(Core* core); + +void Frame_Begin(); +void Frame_Draw(); +void Frame_End(); diff --git a/archive/src/core/input.c b/archive/src/core/input.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/archive/src/core/input.c diff --git a/archive/src/core/vfs.h b/archive/src/core/vfs.h new file mode 100644 index 0000000..41033f5 --- /dev/null +++ b/archive/src/core/vfs.h @@ -0,0 +1,38 @@ +#pragma once +#include "defines.h" + +#define MAX_VIRTUAL_FILENAME_LEN 256 + +typedef struct VFS_Pack VFS_Pack; + +const char VFS_OpenErr_DoesNotExist[] = "PATH DOES NOT EXIST"; + +typedef struct VFS_File { + size_t n_bytes; + void* data; +} VFS_File; + +// virtual file open result +typedef struct VFS_FileRes { + bool success; + const char* error_reason; + VFS_File file; +} VFS_FileRes; + +VFS_Pack* VFS_Open(const char* filepath); + +bool VFS_Close(VFS_Pack*); + +VFS_FileRes VFS_VirtualRead(VFS_Pack* vfs, const char* unique_path); + +typedef struct VFS_PackBuilder { + const char* pack_filename; +} VFS_PackBuilder; + +typedef struct VFS_FileEntry { + char filename[MAX_VIRTUAL_FILENAME_LEN]; + size_t offset; + size_t size; +} VFS_FileEntry; + +VFS_PackBuilder VFS_Pack_Create();
\ No newline at end of file |