summaryrefslogtreecommitdiff
path: root/src/window.c
blob: fc688b7da9cc80e123cb14dc7f33dffc1b0ad200 (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
84
85
86
#include <time.h>

/* TODO: REMOVE THIS INCLUSION */
#include <daw/daw.h>

#include <daw/types.h>
#include <daw/logging.h>

#define ENGINE_RENDERING_WINDOW_H_EXCLUDE_EXTERNS
#include <daw/window.h>
#undef ENGINE_RENDERING_WINDOW_H_EXCLUDE_EXTERNS

#include <glad/gl.h>

#undef GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <cglm/ivec2.h>

extern Instance* GLOBAL_PLATFORM;

static inline u64 platform_get_time_usec(void) {
  struct timespec t;
  int res = clock_gettime(CLOCK_MONOTONIC, &t);
  if (res != 0) {
    // TODO: Check errno
    WARN("Failed to get system time");
  }
  return (u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000);
}

/* wrapper to get time in ms */
u64 (*get_time)(void) = platform_get_time_usec;


#define DAW_WINDOW_VSYNC      (1 << 0)
#define DAW_WINDOW_FULLSCREEN (1 << 1)
#define DAW_WINDOW_RESIZEABLE (1 << 2)

// Wrapper to get a specific window and set up related structures.
// What the fuck is this doing here.
Window* Window_new(const struct Platform* p, const char *restrict title, Window_framework framework, Window_renderer renderer, ivec2 size, u32 flags) {
  Window* w = NULL;

  switch (framework) {
    case WINDOW_FRAMEWORK_GLFW:
      switch (renderer) {
        case WINDOW_RENDERER_OPENGL:
          /* For now, pass instance as NULL, fix it once all the necessary bs is
           * out of struct Instance */
          w = p->window_init(title, size, flags);

          w->bindings = NULL;
          w->bindings_sz = 0;
          w->bindings_len = 0;

          /// TODO
          //w->cam = &default_camera;
          return w;
          break;
        default:
          ERROR("Unsupported renderer.");
      }
      break;
    default:
      ERROR("Unsupported framework.");
  }
  return NULL;
}

void get_mousepos(double *x, double *y) {
  Window* w = GLOBAL_PLATFORM->window;

  switch(w->framework) {
    case WINDOW_FRAMEWORK_GLFW:
      glfwGetCursorPos(GLOBAL_PLATFORM->window->window, x, y);
      break;
    default:
      ERROR("get_mouse_pos not implemented for chosen framework.");
  }

}

void window_get_size(ivec2* dst) {
  glm_ivec2_copy(GLOBAL_PLATFORM->window->windowsize, *dst);
}