summaryrefslogtreecommitdiff
path: root/demos/isomap/isomap.c
blob: a940535dd94a4c389e7d539dae4ec7bc46b099de (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
#include <isogfx/backend.h>
#include <isogfx/isogfx.h>

#include <gfx/app.h>
#include <math/vec2.h>

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>

static const int WINDOW_WIDTH  = 1408;
static const int WINDOW_HEIGHT = 960;
static const int MAX_FPS       = 60;

// Virtual screen dimensions.
static const int SCREEN_WIDTH  = 704;
static const int SCREEN_HEIGHT = 480;

static const R CAMERA_SPEED = 400;

#define MEMORY_SIZE (2 * 1024 * 1024)
uint8_t MEMORY[MEMORY_SIZE];

typedef struct GfxAppState {
  IsoBackend* backend;
  IsoGfx*     iso;
  int         xpick;
  int         ypick;
  vec2        camera;
  SpriteSheet stag_sheet;
  Sprite      stag;
} GfxAppState;

static bool init(GfxAppState* state, int argc, const char** argv) {
  assert(state);
  (void)argc;
  (void)argv;

  if (!((state->iso =
             isogfx_new(&(IsoGfxDesc){.memory        = MEMORY,
                                      .memory_size   = MEMORY_SIZE,
                                      .screen_width  = SCREEN_WIDTH,
                                      .screen_height = SCREEN_HEIGHT})))) {
    return false;
  }
  IsoGfx* iso = state->iso;

  if (!isogfx_load_world(iso, "/home/jeanne/Nextcloud/assets/maps/demo-1.tm")) {
    return false;
  }

  if (!((state->stag_sheet = isogfx_load_sprite_sheet(
             iso,
             "/home/jeanne/Nextcloud/assets/tilesets/scrabling/critters/stag/"
             "stag.ss")))) {
    return false;
  }

  state->stag = isogfx_make_sprite(iso, state->stag_sheet);
  isogfx_set_sprite_position(iso, state->stag, 5, 4);

  if (!((state->backend = iso_backend_init(iso)))) {
    return false;
  }

  return true;
}

static void shutdown(GfxAppState* state) {
  assert(state);
  //
}

static vec2 get_camera_movement(R dt) {
  vec2 offset = {0};
  if (gfx_app_is_key_pressed(KeyA)) {
    offset.x -= 1;
  }
  if (gfx_app_is_key_pressed(KeyD)) {
    offset.x += 1;
  }
  if (gfx_app_is_key_pressed(KeyW)) {
    offset.y -= 1;
  }
  if (gfx_app_is_key_pressed(KeyS)) {
    offset.y += 1;
  }
  if ((offset.x != 0) || (offset.y != 0)) {
    offset = vec2_scale(vec2_normalize(offset), dt * CAMERA_SPEED);
  }
  return offset;
}

static void update(GfxAppState* state, double t, double dt) {
  assert(state);

  state->camera = vec2_add(state->camera, get_camera_movement((R)dt));

  IsoGfx* iso = state->iso;
  isogfx_set_camera(iso, (int)state->camera.x, (int)state->camera.y);
  isogfx_update(iso, t);
}

static void render(GfxAppState* state) {
  assert(state);

  IsoGfx* iso = state->iso;
  isogfx_render(iso);
  iso_backend_render(state->backend, iso);
}

static void resize(GfxAppState* state, int width, int height) {
  assert(state);

  iso_backend_resize_window(state->backend, state->iso, width, height);
}

int main(int argc, const char** argv) {
  GfxAppState state = {0};
  gfx_app_run(
      &(GfxAppDesc){.argc    = argc,
                    .argv    = argv,
                    .width   = WINDOW_WIDTH,
                    .height  = WINDOW_HEIGHT,
                    .max_fps = MAX_FPS,
                    .update_delta_time =
                        MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0,
                    .title     = "Isometric Renderer",
                    .app_state = &state},
      &(GfxAppCallbacks){.init     = init,
                         .update   = update,
                         .render   = render,
                         .resize   = resize,
                         .shutdown = shutdown});
  return 0;
}