summaryrefslogtreecommitdiff
path: root/game/src/game.c
blob: 425119f21d899c8d0c2523d3db9dc40dcac652ff (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
/*
 * Main game module with entry point and game loop.
 *
 * The game module sets up the window and GL context and defers the core game
 * logic to a plugin.
 */
#define _GNU_SOURCE 200112L // For readlink()

#include "game.h"

#include "plugins/plugin.h"

#include <gfx/app.h>
#include <gfx/core.h>
#include <gfx/gfx.h>
#include <gfx/renderer.h>
#include <gfx/scene/camera.h>
#include <gfx/scene/node.h>
#include <gfx/scene/object.h>
#include <gfx/scene/scene.h>

#include <error.h>
#include <log/log.h>
#include <math/camera.h>
#include <plugin.h>

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include <linux/limits.h>

#include <unistd.h>

#undef _GNU_SOURCE

static const int WIDTH   = 1350;
static const int HEIGHT  = 900;
static const int MAX_FPS = 60;

typedef struct GfxAppState {
  Game game;
} GfxAppState;

/// Initialize the game's plugin.
static bool init_plugin(Game* game) {
  assert(game);
  assert(game->plugin);
  // Plugin state is allowed to be null, either when the plugin does not
  // expose an init() or when init() does not initialize a state.
  if (plugin_resolve(game->plugin, plugin_init, "init")) {
    State* plugin_state = 0;
    if (!plugin_call(game->plugin, plugin_init, "init", game, &plugin_state)) {
      return false;
    }
    set_plugin_state(game->plugin, plugin_state);
  }
  return true; // Plugin does not need to expose an init().
}

/// Shutdown the game's plugin.
/// The game's plugin is allowed to be null in the call to this function.
static void shutdown_plugin(Game* game) {
  assert(game);
  if (game->plugin &&
      (plugin_resolve(game->plugin, plugin_shutdown, "shutdown"))) {
    void* plugin_state = get_plugin_state(game->plugin);
    plugin_call(game->plugin, plugin_shutdown, "shutdown", game, plugin_state);
    set_plugin_state(game->plugin, 0);
  }
}

/// Boot the game's plugin.
static bool boot_plugin(Game* game) {
  assert(game);
  assert(game->plugin);
  if (plugin_resolve(game->plugin, plugin_boot, "boot")) {
    void* plugin_state = get_plugin_state(game->plugin);
    return plugin_call(game->plugin, plugin_boot, "boot", game, plugin_state);
  }
  return true; // Plugin does not need to expose a boot().
}

/// Update the plugin's state.
static void update_plugin(Game* game, double t, double dt) {
  assert(game);
  assert(game->plugin);
  if (plugin_resolve(game->plugin, plugin_update, "update")) {
    void* plugin_state = get_plugin_state(game->plugin);
    plugin_call(
        game->plugin, plugin_update, "update", game, plugin_state, t, dt);
  }
}

/// Plugin render.
static void render_plugin(const Game* game) {
  assert(game);
  assert(game->plugin);
  if (plugin_resolve(game->plugin, plugin_render, "render")) {
    void* plugin_state = get_plugin_state(game->plugin);
    plugin_call(game->plugin, plugin_render, "render", game, plugin_state);
  }
}

/// Plugin resize.
static void resize_plugin(Game* game, int width, int height) {
  assert(game);
  assert(game->plugin);
  if (plugin_resolve(game->plugin, plugin_resize, "resize")) {
    void* plugin_state = get_plugin_state(game->plugin);
    plugin_call(
        game->plugin, plugin_resize, "resize", game, plugin_state, width,
        height);
  }
}

void app_end(Game* game);

bool app_init(Game* game, int argc, const char** argv) {
  assert(game);

  if (argc <= 1) {
    LOGE("Usage: %s <plugin> [plugin args]", argv[0]);
    return false;
  }

  // Syntax: game <plugin> [plugin args]
  //
  // Here we consume the <plugin> arg so that plugins receive the remainder
  // args starting from 0.
  game->argc = argc - 1;
  game->argv = argv + 1;

  char exe_path_buf[NAME_MAX] = {0};
  if (readlink("/proc/self/exe", exe_path_buf, sizeof(exe_path_buf)) == -1) {
    LOGE("readlink(/proc/self/exe) failed");
    goto cleanup;
  }

  // Replace the last / with a null terminator to remove the exe file from the
  // path. This gets the file's parent directory.
  *strrchr(exe_path_buf, '/') = 0;

  const mstring exe_dir      = mstring_make(exe_path_buf);
  const mstring plugins_path = mstring_concat_cstr(exe_dir, "/src/plugins");

  if (!(game->plugin_engine = new_plugin_engine(
            &(PluginEngineDesc){.plugins_dir = mstring_cstr(&plugins_path)}))) {
    goto cleanup;
  }

  const char* plugin = argv[1];
  if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) {
    goto cleanup;
  }

  if (!(game->gfx = gfx_init())) {
    goto cleanup;
  }

  if (!init_plugin(game)) {
    goto cleanup;
  }
  if (!boot_plugin(game)) {
    goto cleanup;
  }

  return true;

cleanup:
  LOGE("Gfx error: %s", get_error());
  app_end(game);
  return false;
}

void app_end(Game* game) {
  assert(game);
  shutdown_plugin(game);
  if (game->gfx) {
    gfx_destroy(&game->gfx);
  }
  if (game->plugin) {
    delete_plugin(&game->plugin);
  }
  if (game->plugin_engine) {
    delete_plugin_engine(&game->plugin_engine);
  }
}

void app_update(Game* game, double t, double dt) {
  plugin_engine_update(game->plugin_engine);
  if (plugin_reloaded(game->plugin)) {
    shutdown_plugin(game);
    const bool result = init_plugin(game);
    assert(result); // TODO: handle error better.

    // Trigger a resize just like the initial resize that occurs when the gfx
    // application starts.
    resize_plugin(game, game->width, game->height);
  }

  update_plugin(game, t, dt);
}

void app_render(const Game* game) {
  GfxCore* gfxcore = gfx_get_core(game->gfx);
  gfx_start_frame(gfxcore);
  render_plugin(game);
  gfx_end_frame(gfxcore);
}

void app_resize(Game* game, int width, int height) {
  game->width  = width;
  game->height = height;

  GfxCore* gfxcore = gfx_get_core(game->gfx);
  gfx_set_viewport(gfxcore, width, height);

  resize_plugin(game, width, height);
}

GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS, "Game");