diff options
Diffstat (limited to 'gfx-iso/demo')
| -rw-r--r-- | gfx-iso/demo/isogfx-demo.c | 213 |
1 files changed, 0 insertions, 213 deletions
diff --git a/gfx-iso/demo/isogfx-demo.c b/gfx-iso/demo/isogfx-demo.c deleted file mode 100644 index d6c1ab0..0000000 --- a/gfx-iso/demo/isogfx-demo.c +++ /dev/null | |||
| @@ -1,213 +0,0 @@ | |||
| 1 | #include <isogfx/isogfx.h> | ||
| 2 | |||
| 3 | #include <gfx/gfx.h> | ||
| 4 | #include <gfx/gfx_app.h> | ||
| 5 | #include <gfx/render_backend.h> | ||
| 6 | #include <gfx/renderer.h> | ||
| 7 | #include <gfx/scene.h> | ||
| 8 | #include <gfx/util/geometry.h> | ||
| 9 | #include <gfx/util/shader.h> | ||
| 10 | |||
| 11 | #include <assert.h> | ||
| 12 | #include <stdbool.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | |||
| 16 | static const int SCREEN_WIDTH = 1408; | ||
| 17 | static const int SCREEN_HEIGHT = 960; | ||
| 18 | static const int TILE_WIDTH = 64; | ||
| 19 | static const int TILE_HEIGHT = TILE_WIDTH / 2; | ||
| 20 | static const int WORLD_WIDTH = 20; | ||
| 21 | static const int WORLD_HEIGHT = 20; | ||
| 22 | |||
| 23 | static const Pixel BLACK = (Pixel){.r = 0x38, .g = 0x3b, .b = 0x46}; | ||
| 24 | static const Pixel WHITE = (Pixel){.r = 0xA5, .g = 0xb3, .b = 0xc0}; | ||
| 25 | static const Pixel RED = (Pixel){.r = 0xdc, .g = 0x76, .b = 0x84}; | ||
| 26 | |||
| 27 | typedef struct State { | ||
| 28 | Gfx* gfx; | ||
| 29 | IsoGfx* iso; | ||
| 30 | Tile red; | ||
| 31 | int xpick; | ||
| 32 | int ypick; | ||
| 33 | Texture* screen_texture; | ||
| 34 | Scene* scene; | ||
| 35 | } State; | ||
| 36 | |||
| 37 | static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) { | ||
| 38 | assert(iso); | ||
| 39 | for (int y = 0; y < isogfx_world_height(iso); ++y) { | ||
| 40 | for (int x = 0; x < isogfx_world_width(iso); ++x) { | ||
| 41 | const int odd_col = x & 1; | ||
| 42 | const int odd_row = y & 1; | ||
| 43 | const Tile value = (odd_row ^ odd_col) == 0 ? black : white; | ||
| 44 | isogfx_set_tile(iso, x, y, value); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | static bool init(const GfxAppDesc* desc, void** app_state) { | ||
| 50 | State* state = calloc(1, sizeof(State)); | ||
| 51 | if (!state) { | ||
| 52 | return false; | ||
| 53 | } | ||
| 54 | |||
| 55 | if (!(state->iso = isogfx_new(&(IsoGfxDesc){ | ||
| 56 | .screen_width = SCREEN_WIDTH, | ||
| 57 | .screen_height = SCREEN_HEIGHT, | ||
| 58 | .tile_width = TILE_WIDTH, | ||
| 59 | .tile_height = TILE_HEIGHT, | ||
| 60 | .world_width = WORLD_WIDTH, | ||
| 61 | .world_height = WORLD_HEIGHT}))) { | ||
| 62 | goto cleanup; | ||
| 63 | } | ||
| 64 | if (!(state->gfx = gfx_init())) { | ||
| 65 | goto cleanup; | ||
| 66 | } | ||
| 67 | RenderBackend* render_backend = gfx_get_render_backend(state->gfx); | ||
| 68 | |||
| 69 | if (!(state->screen_texture = gfx_make_texture( | ||
| 70 | render_backend, &(TextureDesc){ | ||
| 71 | .width = SCREEN_WIDTH, | ||
| 72 | .height = SCREEN_HEIGHT, | ||
| 73 | .dimension = Texture2D, | ||
| 74 | .format = TextureRGB8, | ||
| 75 | .filtering = NearestFiltering, | ||
| 76 | .wrap = ClampToEdge, | ||
| 77 | .mipmaps = false}))) { | ||
| 78 | goto cleanup; | ||
| 79 | } | ||
| 80 | |||
| 81 | ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); | ||
| 82 | if (!shader) { | ||
| 83 | goto cleanup; | ||
| 84 | } | ||
| 85 | |||
| 86 | Geometry* geometry = gfx_make_quad_11(render_backend); | ||
| 87 | if (!geometry) { | ||
| 88 | goto cleanup; | ||
| 89 | } | ||
| 90 | |||
| 91 | MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1}; | ||
| 92 | material_desc.uniforms[0] = (ShaderUniform){ | ||
| 93 | .type = UniformTexture, | ||
| 94 | .value.texture = state->screen_texture, | ||
| 95 | .name = sstring_make("Texture")}; | ||
| 96 | Material* material = gfx_make_material(&material_desc); | ||
| 97 | if (!material) { | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | |||
| 101 | const MeshDesc mesh_desc = | ||
| 102 | (MeshDesc){.geometry = geometry, .material = material, .shader = shader}; | ||
| 103 | Mesh* mesh = gfx_make_mesh(&mesh_desc); | ||
| 104 | if (!mesh) { | ||
| 105 | goto cleanup; | ||
| 106 | } | ||
| 107 | |||
| 108 | SceneObject* object = gfx_make_object(); | ||
| 109 | if (!object) { | ||
| 110 | goto cleanup; | ||
| 111 | } | ||
| 112 | gfx_add_object_mesh(object, mesh); | ||
| 113 | |||
| 114 | state->scene = gfx_make_scene(); | ||
| 115 | SceneNode* node = gfx_make_object_node(object); | ||
| 116 | SceneNode* root = gfx_get_scene_root(state->scene); | ||
| 117 | gfx_set_node_parent(node, root); | ||
| 118 | |||
| 119 | const Tile black = isogfx_make_tile( | ||
| 120 | state->iso, &(TileDesc){.type = TileFromColour, .colour = BLACK}); | ||
| 121 | const Tile white = isogfx_make_tile( | ||
| 122 | state->iso, &(TileDesc){.type = TileFromColour, .colour = WHITE}); | ||
| 123 | state->red = isogfx_make_tile( | ||
| 124 | state->iso, &(TileDesc){.type = TileFromColour, .colour = RED}); | ||
| 125 | make_checkerboard(state->iso, black, white); | ||
| 126 | isogfx_render(state->iso); | ||
| 127 | |||
| 128 | *app_state = state; | ||
| 129 | return true; | ||
| 130 | |||
| 131 | cleanup: | ||
| 132 | if (state->gfx) { | ||
| 133 | gfx_destroy(&state->gfx); | ||
| 134 | } | ||
| 135 | free(state); | ||
| 136 | return false; | ||
| 137 | } | ||
| 138 | |||
| 139 | static void shutdown(void* app_state) { | ||
| 140 | assert(app_state); | ||
| 141 | State* state = (State*)(app_state); | ||
| 142 | isogfx_del(&state->iso); | ||
| 143 | gfx_destroy(&state->gfx); | ||
| 144 | free(app_state); | ||
| 145 | } | ||
| 146 | |||
| 147 | static void update(void* app_state, double t, double dt) { | ||
| 148 | assert(app_state); | ||
| 149 | State* state = (State*)(app_state); | ||
| 150 | |||
| 151 | double mouse_x, mouse_y; | ||
| 152 | gfx_app_get_mouse_position(&mouse_x, &mouse_y); | ||
| 153 | |||
| 154 | isogfx_pick_tile(state->iso, mouse_x, mouse_y, &state->xpick, &state->ypick); | ||
| 155 | |||
| 156 | printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick); | ||
| 157 | } | ||
| 158 | |||
| 159 | static void render(void* app_state) { | ||
| 160 | assert(app_state); | ||
| 161 | State* state = (State*)(app_state); | ||
| 162 | |||
| 163 | isogfx_render(state->iso); | ||
| 164 | if ((state->xpick != -1) && (state->ypick != -1)) { | ||
| 165 | isogfx_draw_tile(state->iso, state->xpick, state->ypick, state->red); | ||
| 166 | } | ||
| 167 | |||
| 168 | const Pixel* screen = isogfx_get_screen_buffer(state->iso); | ||
| 169 | assert(screen); | ||
| 170 | gfx_update_texture( | ||
| 171 | state->screen_texture, &(TextureDataDesc){.pixels = screen}); | ||
| 172 | |||
| 173 | RenderBackend* render_backend = gfx_get_render_backend(state->gfx); | ||
| 174 | Renderer* renderer = gfx_get_renderer(state->gfx); | ||
| 175 | |||
| 176 | gfx_start_frame(render_backend); | ||
| 177 | gfx_render_scene( | ||
| 178 | renderer, &(RenderSceneParams){ | ||
| 179 | .mode = RenderDefault, .scene = state->scene, .camera = 0}); | ||
| 180 | gfx_end_frame(render_backend); | ||
| 181 | } | ||
| 182 | |||
| 183 | static void resize(void* app_state, int width, int height) { | ||
| 184 | assert(app_state); | ||
| 185 | State* state = (State*)(app_state); | ||
| 186 | |||
| 187 | RenderBackend* render_backend = gfx_get_render_backend(state->gfx); | ||
| 188 | gfx_set_viewport(render_backend, width, height); | ||
| 189 | } | ||
| 190 | |||
| 191 | int main(int argc, const char** argv) { | ||
| 192 | const int initial_width = SCREEN_WIDTH; | ||
| 193 | const int initial_height = SCREEN_HEIGHT; | ||
| 194 | const int max_fps = 60; | ||
| 195 | |||
| 196 | gfx_app_run( | ||
| 197 | &(GfxAppDesc){ | ||
| 198 | .argc = argc, | ||
| 199 | .argv = argv, | ||
| 200 | .width = initial_width, | ||
| 201 | .height = initial_height, | ||
| 202 | .max_fps = max_fps, | ||
| 203 | .update_delta_time = max_fps > 0 ? 1.0 / (double)max_fps : 0.0, | ||
| 204 | .title = "Isometric Renderer"}, | ||
| 205 | &(GfxAppCallbacks){ | ||
| 206 | .init = init, | ||
| 207 | .update = update, | ||
| 208 | .render = render, | ||
| 209 | .resize = resize, | ||
| 210 | .shutdown = shutdown}); | ||
| 211 | |||
| 212 | return 0; | ||
| 213 | } | ||
