~vladh/peony

peony/src/core.cpp -rw-r--r-- 10.3 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include <thread>
#include "../src_external/pstr.h"
#include "types.hpp"
#include "logs.hpp"
#include "util.hpp"
#include "renderer.hpp"
#include "internals.hpp"
#include "engine.hpp"
#include "state.hpp"
#include "peony_parser.hpp"
#include "constants.hpp"
#include "core.hpp"
#include "intrinsics.hpp"


namespace core {
  void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    MemoryAndState *memory_and_state = (MemoryAndState*)glfwGetWindowUserPointer(window);
    State *state = memory_and_state->state;
    MemoryPool *asset_memory_pool = memory_and_state->asset_memory_pool;
    logs::info(
      "Window is now: %d x %d", state->window_size.width, state->window_size.height
    );
    state->window_size.width = width;
    state->window_size.height = height;
    cameras::update_matrices(
      state->cameras_state.camera_active,
      state->window_size.width,
      state->window_size.height
    );
    cameras::update_ui_matrices(
      state->cameras_state.camera_active,
      state->window_size.width,
      state->window_size.height
    );
    gui::update_screen_dimensions(
      &state->gui_state, state->window_size.width, state->window_size.height
    );
    renderer::resize_renderer_buffers(
      asset_memory_pool,
      &state->materials_state.materials,
      &state->renderer_state.builtin_textures,
      width,
      height
    );
  }


  void mouse_button_callback(
    GLFWwindow *window, int button, int action, int mods
  ) {
    MemoryAndState *memory_and_state = (MemoryAndState*)glfwGetWindowUserPointer(window);
    State *state = memory_and_state->state;

    input::update_mouse_button(&state->input_state, button, action, mods);
    gui::update_mouse_button(&state->gui_state);
  }


  void mouse_callback(GLFWwindow *window, real64 x, real64 y) {
    MemoryAndState *memory_and_state = (MemoryAndState*)glfwGetWindowUserPointer(window);
    State *state = memory_and_state->state;

    v2 mouse_pos = v2(x, y);
    input::update_mouse(&state->input_state, mouse_pos);

    if (state->renderer_state.is_cursor_enabled) {
      gui::update_mouse(&state->gui_state);
    } else {
      cameras::update_mouse(
        state->cameras_state.camera_active,
        state->input_state.mouse_3d_offset
      );
    }
  }


  void key_callback(
    GLFWwindow* window,
    int key, int scancode, int action, int mods
  ) {
    MemoryAndState *memory_and_state = (MemoryAndState*)glfwGetWindowUserPointer(window);
    State *state = memory_and_state->state;
    input::update_keys(&state->input_state, key, scancode, action, mods);
  }


  void char_callback(
    GLFWwindow* window, uint32 codepoint
  ) {
    MemoryAndState *memory_and_state = (MemoryAndState*)glfwGetWindowUserPointer(window);
    State *state = memory_and_state->state;
    input::update_text_input(&state->input_state, codepoint);
  }


  GLFWwindow* init_window(WindowSize *window_size) {
    glfwInit();

    logs::info("Using OpenGL 4.1");
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    #if defined(PLATFORM_MACOS)
      // macOS requires a forward compatible context
      // This means the highest OpenGL version will be used that is at least the version
      // we specified, and that contains no breaking changes from the version we specified
      glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    #endif

    #if USE_OPENGL_DEBUG
      logs::info("Using OpenGL debug context");
      glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
    #endif

    // Remove window decorations (border etc.)
    glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);

    // For fullscreen windows, do not discard our video mode when minimised
    glfwWindowHint(GLFW_AUTO_ICONIFY, GLFW_FALSE);

    // Create the window. Right now we're working with screencoord sizes,
    // not pixels!

    #if USE_FULLSCREEN
      int32 n_monitors;
      GLFWmonitor **monitors = glfwGetMonitors(&n_monitors);
      GLFWmonitor *target_monitor = monitors[TARGET_MONITOR];
      const GLFWvidmode *video_mode = glfwGetVideoMode(target_monitor);
      glfwWindowHint(GLFW_RED_BITS, video_mode->redBits);
      glfwWindowHint(GLFW_GREEN_BITS, video_mode->greenBits);
      glfwWindowHint(GLFW_BLUE_BITS, video_mode->blueBits);
      glfwWindowHint(GLFW_REFRESH_RATE, video_mode->refreshRate);

      window_size->screencoord_width = video_mode->width;
      window_size->screencoord_height = video_mode->height;

      GLFWwindow *window = glfwCreateWindow(
        window_size->screencoord_width, window_size->screencoord_height,
        WINDOW_TITLE,
        #if USE_WINDOWED_FULLSCREEN
          nullptr, nullptr
        #else
          target_monitor, nullptr
        #endif
      );
    #else
      window_size->screencoord_width = 1920;
      window_size->screencoord_height = 1080;

      GLFWwindow *window = glfwCreateWindow(
        window_size->screencoord_width, window_size->screencoord_height,
        WINDOW_TITLE,
        nullptr, nullptr
      );

      glfwSetWindowPos(window, 200, 200);
    #endif

    if (!window) {
      logs::fatal("Failed to create GLFW window");
      return nullptr;
    }

    glfwMakeContextCurrent(window);
    #if !USE_VSYNC
      glfwSwapInterval(0);
    #endif

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
      logs::fatal("Failed to initialize GLAD");
      return nullptr;
    }

    if (!GLAD_GL_ARB_texture_cube_map_array) {
      logs::fatal("No support for GLAD_GL_ARB_texture_cube_map_array");
    }
    if (!GLAD_GL_ARB_texture_storage) {
      logs::fatal("No support for GLAD_GL_ARB_texture_storage");
    }
    if (!GLAD_GL_ARB_buffer_storage) {
      logs::warning("No support for GLAD_GL_ARB_buffer_storage");
    }

    // TODO: Remove GL_EXT_debug_marker from GLAD
    // TODO: Remove GL_EXT_debug_label from GLAD
    // TODO: Remove GL_ARB_texture_storage_multisample from GLAD

    #if USE_OPENGL_DEBUG
      if (GLAD_GL_AMD_debug_output || GLAD_GL_ARB_debug_output || GLAD_GL_KHR_debug) {
        GLint flags;
        glGetIntegerv(GL_CONTEXT_FLAGS, &flags);

        if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
          glEnable(GL_DEBUG_OUTPUT);
          glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
          glDebugMessageCallback(util::debug_message_callback, nullptr);
          glDebugMessageControl(
            GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE
          );
        } else {
          logs::fatal("Tried to initialise OpenGL debug output but couldn't");
        }
      } else {
        logs::warning(
          "Tried to initialise OpenGL debug output but none of "
          "[GL_AMD_debug_output, GL_ARB_debug_output, GL_KHR_debug] "
          "are supported on this system. Skipping."
        );
      }
    #endif

    // Enable multisampling
    glEnable(GL_MULTISAMPLE);
    glfwWindowHint(GLFW_SAMPLES, 4);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    #if !defined(PLATFORM_MACOS)
      glLineWidth(2.0f);
    #endif
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // Get the framebuffer size. This is the actual window size in pixels.
    glfwGetFramebufferSize(window, &window_size->width, &window_size->height);
    glViewport(0, 0, window_size->width, window_size->height);

    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetMouseButtonCallback(window, mouse_button_callback);
    glfwSetKeyCallback(window, key_callback);
    glfwSetCharCallback(window, char_callback);

    return window;
  }


  bool32 init_state(
    State *state,
    MemoryPool *asset_memory_pool
  ) {
    state->window = init_window(&state->window_size);
    if (!state->window) { return false; }

    engine::init(&state->engine_state, asset_memory_pool);
    materials::init(&state->materials_state, asset_memory_pool);
    renderer::init(
      &state->renderer_state,
      asset_memory_pool,
      state->window_size.width, state->window_size.height,
      state->window
    );
    internals::init(
      &state->engine_state,
      &state->renderer_state,
      &state->materials_state
    );
    gui::init(
      asset_memory_pool,
      &state->gui_state,
      &state->input_state, // NOTE: Woah! What does this mean?
      state->window_size.width, state->window_size.height
    );
    debugdraw::init(&state->debug_draw_state, asset_memory_pool);
    input::init(&state->input_state, state->window);
    lights::init(&state->lights_state);
    tasks::init(&state->tasks_state, asset_memory_pool);
    anim::init(&state->anim_state, asset_memory_pool);
    cameras::init(
      &state->cameras_state,
      state->window_size.width, state->window_size.height
    );
    behavior::init(
      &state->behavior_state,
      // NOTE: behavior needs the global state to pass to the behavior functions
      state
    );

    return true;
  }


  void destroy_state(State *state) {
    glfwTerminate();
  }
}


int core::run() {
  // Allocate memory
  State *state = (State*)calloc(1, sizeof(State));
  defer { free(state); };
  MemoryPool asset_memory_pool = {.size = util::mb_to_b(1024)};
  defer { memory::destroy_memory_pool(&asset_memory_pool); };

  // Make state
  if (!init_state(state, &asset_memory_pool)) {
    return EXIT_FAILURE;
  }
  defer { destroy_state(state); };

  // Set up globals
  MemoryAndState memory_and_state = {&asset_memory_pool, state};
  glfwSetWindowUserPointer(state->window, &memory_and_state);

  // Set up loading threads
  std::mutex loading_thread_mutex;
  std::thread loading_threads[N_LOADING_THREADS];
  range (0, N_LOADING_THREADS) {
    loading_threads[idx] = std::thread(
      tasks::run_loading_loop,
      &state->tasks_state,
      &loading_thread_mutex,
      &state->engine_state.should_stop,
      idx
    );
  }
  defer { range (0, N_LOADING_THREADS) { loading_threads[idx].join(); } };

  // Run main loop
  engine::run_main_loop(
    &state->engine_state,
    &state->renderer_state,
    &state->materials_state,
    &state->cameras_state,
    &state->gui_state,
    &state->input_state,
    &state->lights_state,
    &state->tasks_state,
    &state->anim_state,
    &state->behavior_state,
    state->window,
    &state->window_size
  );

  return EXIT_SUCCESS;
}