summaryrefslogtreecommitdiff
path: root/src/core/core.c
blob: af7daeb830a71cda98b2d8f67a12917e6742a6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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); }