summaryrefslogtreecommitdiff
path: root/gfx-iso/demo/isogfx-demo.c
blob: d6c1ab0d169b916ff792ba49d063d7af0c0fabf5 (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
#include <isogfx/isogfx.h>

#include <gfx/gfx.h>
#include <gfx/gfx_app.h>
#include <gfx/render_backend.h>
#include <gfx/renderer.h>
#include <gfx/scene.h>
#include <gfx/util/geometry.h>
#include <gfx/util/shader.h>

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

static const int SCREEN_WIDTH  = 1408;
static const int SCREEN_HEIGHT = 960;
static const int TILE_WIDTH    = 64;
static const int TILE_HEIGHT   = TILE_WIDTH / 2;
static const int WORLD_WIDTH   = 20;
static const int WORLD_HEIGHT  = 20;

static const Pixel BLACK = (Pixel){.r = 0x38, .g = 0x3b, .b = 0x46};
static const Pixel WHITE = (Pixel){.r = 0xA5, .g = 0xb3, .b = 0xc0};
static const Pixel RED   = (Pixel){.r = 0xdc, .g = 0x76, .b = 0x84};

typedef struct State {
  Gfx*     gfx;
  IsoGfx*  iso;
  Tile     red;
  int      xpick;
  int      ypick;
  Texture* screen_texture;
  Scene*   scene;
} State;

static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) {
  assert(iso);
  for (int y = 0; y < isogfx_world_height(iso); ++y) {
    for (int x = 0; x < isogfx_world_width(iso); ++x) {
      const int  odd_col = x & 1;
      const int  odd_row = y & 1;
      const Tile value   = (odd_row ^ odd_col) == 0 ? black : white;
      isogfx_set_tile(iso, x, y, value);
    }
  }
}

static bool init(const GfxAppDesc* desc, void** app_state) {
  State* state = calloc(1, sizeof(State));
  if (!state) {
    return false;
  }

  if (!(state->iso = isogfx_new(&(IsoGfxDesc){
            .screen_width  = SCREEN_WIDTH,
            .screen_height = SCREEN_HEIGHT,
            .tile_width    = TILE_WIDTH,
            .tile_height   = TILE_HEIGHT,
            .world_width   = WORLD_WIDTH,
            .world_height  = WORLD_HEIGHT}))) {
    goto cleanup;
  }
  if (!(state->gfx = gfx_init())) {
    goto cleanup;
  }
  RenderBackend* render_backend = gfx_get_render_backend(state->gfx);

  if (!(state->screen_texture = gfx_make_texture(
            render_backend, &(TextureDesc){
                                .width     = SCREEN_WIDTH,
                                .height    = SCREEN_HEIGHT,
                                .dimension = Texture2D,
                                .format    = TextureRGB8,
                                .filtering = NearestFiltering,
                                .wrap      = ClampToEdge,
                                .mipmaps   = false}))) {
    goto cleanup;
  }

  ShaderProgram* shader = gfx_make_view_texture_shader(render_backend);
  if (!shader) {
    goto cleanup;
  }

  Geometry* geometry = gfx_make_quad_11(render_backend);
  if (!geometry) {
    goto cleanup;
  }

  MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1};
  material_desc.uniforms[0]  = (ShaderUniform){
       .type          = UniformTexture,
       .value.texture = state->screen_texture,
       .name          = sstring_make("Texture")};
  Material* material = gfx_make_material(&material_desc);
  if (!material) {
    return false;
  }

  const MeshDesc mesh_desc =
      (MeshDesc){.geometry = geometry, .material = material, .shader = shader};
  Mesh* mesh = gfx_make_mesh(&mesh_desc);
  if (!mesh) {
    goto cleanup;
  }

  SceneObject* object = gfx_make_object();
  if (!object) {
    goto cleanup;
  }
  gfx_add_object_mesh(object, mesh);

  state->scene    = gfx_make_scene();
  SceneNode* node = gfx_make_object_node(object);
  SceneNode* root = gfx_get_scene_root(state->scene);
  gfx_set_node_parent(node, root);

  const Tile black = isogfx_make_tile(
      state->iso, &(TileDesc){.type = TileFromColour, .colour = BLACK});
  const Tile white = isogfx_make_tile(
      state->iso, &(TileDesc){.type = TileFromColour, .colour = WHITE});
  state->red = isogfx_make_tile(
      state->iso, &(TileDesc){.type = TileFromColour, .colour = RED});
  make_checkerboard(state->iso, black, white);
  isogfx_render(state->iso);

  *app_state = state;
  return true;

cleanup:
  if (state->gfx) {
    gfx_destroy(&state->gfx);
  }
  free(state);
  return false;
}

static void shutdown(void* app_state) {
  assert(app_state);
  State* state = (State*)(app_state);
  isogfx_del(&state->iso);
  gfx_destroy(&state->gfx);
  free(app_state);
}

static void update(void* app_state, double t, double dt) {
  assert(app_state);
  State* state = (State*)(app_state);

  double mouse_x, mouse_y;
  gfx_app_get_mouse_position(&mouse_x, &mouse_y);

  isogfx_pick_tile(state->iso, mouse_x, mouse_y, &state->xpick, &state->ypick);

  printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick);
}

static void render(void* app_state) {
  assert(app_state);
  State* state = (State*)(app_state);

  isogfx_render(state->iso);
  if ((state->xpick != -1) && (state->ypick != -1)) {
    isogfx_draw_tile(state->iso, state->xpick, state->ypick, state->red);
  }

  const Pixel* screen = isogfx_get_screen_buffer(state->iso);
  assert(screen);
  gfx_update_texture(
      state->screen_texture, &(TextureDataDesc){.pixels = screen});

  RenderBackend* render_backend = gfx_get_render_backend(state->gfx);
  Renderer*      renderer       = gfx_get_renderer(state->gfx);

  gfx_start_frame(render_backend);
  gfx_render_scene(
      renderer, &(RenderSceneParams){
                    .mode = RenderDefault, .scene = state->scene, .camera = 0});
  gfx_end_frame(render_backend);
}

static void resize(void* app_state, int width, int height) {
  assert(app_state);
  State* state = (State*)(app_state);

  RenderBackend* render_backend = gfx_get_render_backend(state->gfx);
  gfx_set_viewport(render_backend, width, height);
}

int main(int argc, const char** argv) {
  const int initial_width  = SCREEN_WIDTH;
  const int initial_height = SCREEN_HEIGHT;
  const int max_fps        = 60;

  gfx_app_run(
      &(GfxAppDesc){
          .argc              = argc,
          .argv              = argv,
          .width             = initial_width,
          .height            = initial_height,
          .max_fps           = max_fps,
          .update_delta_time = max_fps > 0 ? 1.0 / (double)max_fps : 0.0,
          .title             = "Isometric Renderer"},
      &(GfxAppCallbacks){
          .init     = init,
          .update   = update,
          .render   = render,
          .resize   = resize,
          .shutdown = shutdown});

  return 0;
}