summaryrefslogtreecommitdiff
path: root/src/core.c
blob: 0cbaa091f555c8349e4ce6643a65f68a784e5690 (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
// The engine "core"

#include <celeritas.h>
#include <stdlib.h>
#include "glfw3.h"

NAMESPACED_LOGGER(core);

core g_core = { 0 };

#ifdef GPU_METAL
static const char* gapi = "Metal";
#else
static const char* gapi = "Vulkan";
#endif

// forward declares
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods);
void resize_callback(GLFWwindow* win, int width, int height);

void core_bringup(const char* window_name, struct GLFWwindow* optional_window) {
  INFO("Initiate Core bringup");

  INFO("Create GLFW window");
  glfwInit();

  glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

  char* full_window_name = malloc(sizeof(char) * 100);
  int _offset = sprintf(full_window_name, "%s (%s)", window_name, gapi);

  GLFWwindow* glfw_window = glfwCreateWindow(800, 600, full_window_name, NULL, NULL);
  g_core.window = glfw_window;

  // This may move into a renderer struct
  ral_backend_init(window_name, glfw_window);

  glfwSetKeyCallback(glfw_window, key_callback);
  glfwSetFramebufferSizeCallback(glfw_window, resize_callback);
}
void core_shutdown() {
  ral_backend_shutdown();
  glfwTerminate();
}

bool app_should_exit() { return glfwWindowShouldClose(g_core.window) || g_core.should_exit; }

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
    g_core.should_exit = true;
  }
}

void resize_callback(GLFWwindow* window, int width, int height) { ral_backend_resize_framebuffer(width, height); }