summaryrefslogtreecommitdiff
path: root/src/window.c
blob: 36c1612684647928a1d00abf92ae53f322fabc25 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <string.h>
#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

#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);

          // Manually reset bindings et. al.
          w->bindings = NULL;
          w->bindings_sz = 0;
          w->bindings_len = 0;

          w->render_targets = NULL;
          return w;
        default:
          ERROR("Unsupported renderer.");
      }
      break;
    default:
      ERROR("Unsupported framework.");
  }
  return NULL;
}


void window_reset_cameras(Window* w, RenderTargets* restrict targets) {
  // Question, would it be better to check both callbacks and continue early,
  // instead of resizing the texture first, then check the camera callback?
  for (usize i = 0; i < targets->framebuffer_len; i++) {
    //if (targets->framebuffer_size_callback[i] == NULL) continue;

    //ivec2 newsz;
    //targets->framebuffer_size_callback[i](&newsz, w->windowsize);

    // Destroy & Create framebuffers (and related buffers?)

    if (targets->camera_reset_callback[i] == NULL) continue;
    targets->camera_reset_callback[i](targets->cam[i], w->windowsize);
  }
}

//void window_reset_texture_sizes(Window* w, RenderTargets* restrict targets) {
//  // Question, would it be more efficient to just wipe all of them at once
//  // instead of doing this? Not all are necessarily deleted.
//  for (usize i = 0; i < targets->texture_len; i++) {
//    if (targets->framebuffer_size_callback[i] == NULL) continue;
//
//    ivec2 newsz;
//    targets->framebuffer_size_callback[i](&newsz, w->windowsize);
//
//    r_destroy_framebuffers(w->context, targets->framebuffer, 1);
//    r_create_framebuffers(w->context, &targets->framebuffer[i], &targets->framebuffer_parameters[i].framebuffer_type, &newsz, 1);
//  }
//}


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);
}


// Assume framebuffer_len and texture_len is already set
void window_init_renderstack(Window *restrict w,
    usize num_fbuf, usize num_buf,
    FramebufferParameters *restrict fb_params,
    u32 *restrict buffer_params
    ) {

  ASSERT(w != NULL);
  ASSERT(fb_params != NULL);
  ASSERT(buffer_params != NULL);

  RenderTargets *t = NULL;
  void* allocation = malloc(
      sizeof(RenderTargets) +
      sizeof(*t->framebuffer)            * num_fbuf +
      sizeof(*t->framebuffer_parameters) * num_fbuf +
      sizeof(*t->cam)                    * num_fbuf +
      sizeof(*t->camera_reset_callback)  * num_fbuf +
      sizeof(*t->buffer)                 * num_buf +
      sizeof(*t->buffer_parameters)      * num_buf
      // TODO: callbacks
  );

#define ADVANCE_PTR(target, count)\
  t->target = (void*)((u64)allocation + acc); \
  acc += sizeof(*t->target) * count

  t = allocation;

  t->framebuffer_len = num_fbuf;
  t->buffer_len = num_buf;

  u64 acc = sizeof(RenderTargets);
  ADVANCE_PTR(framebuffer, num_fbuf);
  ADVANCE_PTR(framebuffer_parameters, num_fbuf);
  ADVANCE_PTR(cam, num_fbuf);
  ADVANCE_PTR(camera_reset_callback, num_fbuf);
  ADVANCE_PTR(buffer, num_buf);
  ADVANCE_PTR(buffer_parameters, num_buf);
  // TODO: callbacks
#undef ADVANCE_PTR

  memcpy(t->framebuffer_parameters, fb_params, sizeof(*t->framebuffer_parameters) * num_fbuf);
  memcpy(t->buffer_parameters, buffer_params, sizeof(*t->buffer_parameters) * num_buf);

  r_create_framebuffers(w->context, &t->framebuffer[0], num_fbuf);

  // Iteratively set up each framebuffer and framebuffer-attached objects
  usize buffer_offset = 0;
  for (usize fb_idx = 0; fb_idx < num_fbuf; fb_idx++) {

    // Count textures & renderbuffers
    u32 num_textures = 0;
    u32 num_renderbuffers = 0;
    for (i32 i = 0; i < t->framebuffer_parameters[fb_idx].num_attached_buffers; i++) {
      switch (BUFFERPARAMETER_GET_TYPE(t->buffer_parameters[buffer_offset + i])) {
        case BufferType_frame:
          UNIMPLEMENTED;
        case BufferType_texture:
          num_textures++;
          break;
        case BufferType_render:
          num_renderbuffers++;
          break;
        case BufferType_buffer:
          UNIMPLEMENTED;
        default:
          UNIMPLEMENTED;
      }
    }

    // Create `num_attached_buffers` identical buffers
    r_create_textures(w->context, &t->buffer[buffer_offset],
          &t->buffer_parameters[buffer_offset],
          &t->framebuffer_parameters[fb_idx].dimensions,
          num_textures);

    r_create_renderbuffers(w->context, &t->buffer[buffer_offset + num_textures],
          &t->buffer_parameters[buffer_offset + num_textures],
          &t->framebuffer_parameters[fb_idx].dimensions,
          num_renderbuffers);

    r_attach_buffers(w->context,
        t->framebuffer[fb_idx],
        &t->buffer[buffer_offset],
        &t->buffer_parameters[buffer_offset],
        t->framebuffer_parameters[fb_idx].num_attached_buffers);

    //if (BUFFERPARAMETER_GET_TYPE(buffer_params[i]) != BufferType_texture) continue;
    //u32 texture_type = BUFFERPARAMETER_GET_PARAMETER(buffer_params[i]);

    //usize span = 0;
    //while (
    //    (i + span < targets->buffer_len) &&
    //    (BUFFERPARAMETER_GET_TYPE(buffer_params[i + span]) == BufferType_texture) &&
    //    (buffer_params[i + span].buffer_param & 3) == textureType
    //) {
    //  span++;
    //}
    //if (span == 0) continue;
    //r_create_textures(w->context, &targets->buffer[i], texture_type, texturesizes, span);
  }
  //r_create_renderbuffers(c, targets->renderbuffer, renderbuffertypes, renderbuffersizes, targets->renderbuffer_len);
  // Skip `texture` (??)

  w->render_targets = t;
}


void window_free_renderstack(RenderTargets *restrict t) {
  // Everything is in the same buffer anyways
  free(t->framebuffer);
}

void draw_model(Window *restrict w, u32 framebuffer_idx, RenderObject* o, vec4 pos) {
  r_draw_model(w->context, w->render_targets, framebuffer_idx, o, pos);
}