summaryrefslogtreecommitdiff
path: root/src/honey.c
blob: c55ddc2937217017ec24c0fd03e984aa8ef7f314 (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
#include "include/honey.h"

static void default_honey_update_callback(float dt) {}
static void default_honey_draw_callback() {}

honey_window honey_setup(int screen_width, int screen_height, char* window_title) {
  honey_update_callback = &default_honey_update_callback;
  honey_draw_callback = &default_honey_draw_callback;
  
  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  honey_window window = glfwCreateWindow(screen_width, screen_height, window_title, NULL, NULL);
  if (window == NULL) {
    fprintf(stderr, "ERROR: failed to create window!\n");
    glfwTerminate();
    return NULL;
  }

  glfwMakeContextCurrent(window);
  glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

  if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
    fprintf(stderr, "ERROR: failed to initialize GLAD!\n");
    glfwTerminate();
    return NULL;
  }

  return window;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void honey_set_update_callback(void (*update_callback)(float)) {
  honey_update_callback = update_callback;
}

void honey_set_draw_callback(void (*draw_callback)()) {
  honey_draw_callback = draw_callback;
}

void honey_set_fps(unsigned int fps) {
  honey_draw_dt = 1.0f / fps;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void honey_run(honey_window window) {
  float prevTime = 0;
  float currentTime = 0;
  float dt;
  float draw_dt = 0;

  while(!glfwWindowShouldClose(window)) {
    currentTime = (float) glfwGetTime();
    dt = currentTime - prevTime;
    prevTime = currentTime;

    honey_update_callback(dt);
    honey_draw_callback();
  }
}