diff options
Diffstat (limited to 'demos/checkerboard')
| -rw-r--r-- | demos/checkerboard/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | demos/checkerboard/checkerboard.c | 166 |
2 files changed, 182 insertions, 0 deletions
diff --git a/demos/checkerboard/CMakeLists.txt b/demos/checkerboard/CMakeLists.txt new file mode 100644 index 0000000..d1691c6 --- /dev/null +++ b/demos/checkerboard/CMakeLists.txt | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.0) | ||
| 2 | |||
| 3 | project(checkerboard) | ||
| 4 | |||
| 5 | set(CMAKE_C_STANDARD 17) | ||
| 6 | set(CMAKE_C_STANDARD_REQUIRED On) | ||
| 7 | set(CMAKE_C_EXTENSIONS Off) | ||
| 8 | |||
| 9 | add_executable(checkerboard | ||
| 10 | checkerboard.c) | ||
| 11 | |||
| 12 | target_link_libraries(checkerboard PRIVATE | ||
| 13 | gfx-app | ||
| 14 | isogfx-backend) | ||
| 15 | |||
| 16 | target_compile_options(checkerboard PRIVATE -Wall -Wextra -Wpedantic) | ||
diff --git a/demos/checkerboard/checkerboard.c b/demos/checkerboard/checkerboard.c new file mode 100644 index 0000000..dbc817c --- /dev/null +++ b/demos/checkerboard/checkerboard.c | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | #include <isogfx/backend.h> | ||
| 2 | #include <isogfx/isogfx.h> | ||
| 3 | |||
| 4 | #include <gfx/app.h> | ||
| 5 | |||
| 6 | #include <assert.h> | ||
| 7 | #include <stdbool.h> | ||
| 8 | #include <stdio.h> | ||
| 9 | |||
| 10 | static const int WINDOW_WIDTH = 1408; | ||
| 11 | static const int WINDOW_HEIGHT = 960; | ||
| 12 | static const int MAX_FPS = 60; | ||
| 13 | |||
| 14 | // Virtual screen dimensions. | ||
| 15 | static const int SCREEN_WIDTH = 704; | ||
| 16 | static const int SCREEN_HEIGHT = 480; | ||
| 17 | |||
| 18 | static const int TILE_WIDTH = 32; | ||
| 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 TileDesc tile_set[] = { | ||
| 24 | {.type = TileFromColour, | ||
| 25 | .width = TILE_WIDTH, | ||
| 26 | .height = TILE_HEIGHT, | ||
| 27 | .colour = (Pixel){.r = 0x38, .g = 0x3b, .b = 0x46, .a = 0xff}}, | ||
| 28 | {.type = TileFromColour, | ||
| 29 | .width = TILE_WIDTH, | ||
| 30 | .height = TILE_HEIGHT, | ||
| 31 | .colour = (Pixel){.r = 0xA5, .g = 0xb3, .b = 0xc0, .a = 0xff}}, | ||
| 32 | {.type = TileFromColour, | ||
| 33 | .width = TILE_WIDTH, | ||
| 34 | .height = TILE_HEIGHT, | ||
| 35 | .colour = (Pixel){.r = 0xdc, .g = 0x76, .b = 0x84, .a = 0xff}}, | ||
| 36 | }; | ||
| 37 | |||
| 38 | typedef enum Colour { | ||
| 39 | Black, | ||
| 40 | White, | ||
| 41 | Red, | ||
| 42 | } Colour; | ||
| 43 | |||
| 44 | typedef struct GfxAppState { | ||
| 45 | IsoBackend* backend; | ||
| 46 | IsoGfx* iso; | ||
| 47 | Tile red; | ||
| 48 | int xpick; | ||
| 49 | int ypick; | ||
| 50 | } GfxAppState; | ||
| 51 | |||
| 52 | static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) { | ||
| 53 | assert(iso); | ||
| 54 | for (int y = 0; y < isogfx_world_height(iso); ++y) { | ||
| 55 | for (int x = 0; x < isogfx_world_width(iso); ++x) { | ||
| 56 | const int odd_col = x & 1; | ||
| 57 | const int odd_row = y & 1; | ||
| 58 | const Tile value = (odd_row ^ odd_col) == 0 ? black : white; | ||
| 59 | isogfx_set_tile(iso, x, y, value); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | static bool init(GfxAppState* state, int argc, const char** argv) { | ||
| 65 | assert(state); | ||
| 66 | |||
| 67 | (void)argc; | ||
| 68 | (void)argv; | ||
| 69 | |||
| 70 | if (!(state->iso = isogfx_new(&(IsoGfxDesc){ | ||
| 71 | .screen_width = SCREEN_WIDTH, .screen_height = SCREEN_HEIGHT}))) { | ||
| 72 | return false; | ||
| 73 | } | ||
| 74 | IsoGfx* iso = state->iso; | ||
| 75 | |||
| 76 | isogfx_resize(iso, SCREEN_WIDTH, SCREEN_HEIGHT); | ||
| 77 | |||
| 78 | if (!isogfx_make_world( | ||
| 79 | iso, &(WorldDesc){ | ||
| 80 | .tile_width = TILE_WIDTH, | ||
| 81 | .tile_height = TILE_HEIGHT, | ||
| 82 | .world_width = WORLD_WIDTH, | ||
| 83 | .world_height = WORLD_HEIGHT})) { | ||
| 84 | return false; | ||
| 85 | } | ||
| 86 | |||
| 87 | const Tile black = isogfx_make_tile(iso, &tile_set[Black]); | ||
| 88 | const Tile white = isogfx_make_tile(iso, &tile_set[White]); | ||
| 89 | state->red = isogfx_make_tile(iso, &tile_set[Red]); | ||
| 90 | make_checkerboard(iso, black, white); | ||
| 91 | |||
| 92 | if (!(state->backend = IsoBackendInit(iso))) { | ||
| 93 | return false; | ||
| 94 | } | ||
| 95 | |||
| 96 | return true; | ||
| 97 | } | ||
| 98 | |||
| 99 | static void shutdown(GfxAppState* state) { | ||
| 100 | assert(state); | ||
| 101 | |||
| 102 | IsoBackendShutdown(&state->backend); | ||
| 103 | isogfx_del(&state->iso); | ||
| 104 | } | ||
| 105 | |||
| 106 | static void update(GfxAppState* state, double t, double dt) { | ||
| 107 | assert(state); | ||
| 108 | (void)dt; | ||
| 109 | |||
| 110 | IsoGfx* iso = state->iso; | ||
| 111 | |||
| 112 | isogfx_update(iso, t); | ||
| 113 | |||
| 114 | // Get mouse position in window coordinates. | ||
| 115 | double mouse_x, mouse_y; | ||
| 116 | gfx_app_get_mouse_position(&mouse_x, &mouse_y); | ||
| 117 | |||
| 118 | // Map from window coordinates to virtual screen coordinates. | ||
| 119 | IsoBackendGetMousePosition( | ||
| 120 | state->backend, mouse_x, mouse_y, &mouse_x, &mouse_y); | ||
| 121 | |||
| 122 | isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick); | ||
| 123 | |||
| 124 | printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick); | ||
| 125 | } | ||
| 126 | |||
| 127 | static void render(GfxAppState* state) { | ||
| 128 | assert(state); | ||
| 129 | |||
| 130 | IsoGfx* iso = state->iso; | ||
| 131 | |||
| 132 | isogfx_render(iso); | ||
| 133 | |||
| 134 | if ((state->xpick != -1) && (state->ypick != -1)) { | ||
| 135 | isogfx_draw_tile(iso, state->xpick, state->ypick, state->red); | ||
| 136 | } | ||
| 137 | |||
| 138 | IsoBackendRender(state->backend, iso); | ||
| 139 | } | ||
| 140 | |||
| 141 | static void resize(GfxAppState* state, int width, int height) { | ||
| 142 | assert(state); | ||
| 143 | |||
| 144 | IsoBackendResizeWindow(state->backend, state->iso, width, height); | ||
| 145 | } | ||
| 146 | |||
| 147 | int main(int argc, const char** argv) { | ||
| 148 | GfxAppState state = {0}; | ||
| 149 | gfx_app_run( | ||
| 150 | &(GfxAppDesc){ | ||
| 151 | .argc = argc, | ||
| 152 | .argv = argv, | ||
| 153 | .width = WINDOW_WIDTH, | ||
| 154 | .height = WINDOW_HEIGHT, | ||
| 155 | .max_fps = MAX_FPS, | ||
| 156 | .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0, | ||
| 157 | .title = "Isometric Renderer", | ||
| 158 | .app_state = &state}, | ||
| 159 | &(GfxAppCallbacks){ | ||
| 160 | .init = init, | ||
| 161 | .update = update, | ||
| 162 | .render = render, | ||
| 163 | .resize = resize, | ||
| 164 | .shutdown = shutdown}); | ||
| 165 | return 0; | ||
| 166 | } | ||
