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