diff options
Diffstat (limited to 'gfx-iso/app/main.c')
-rw-r--r-- | gfx-iso/app/main.c | 199 |
1 files changed, 0 insertions, 199 deletions
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 | |||
19 | static const int SCREEN_WIDTH = 1408; | ||
20 | static const int SCREEN_HEIGHT = 960; | ||
21 | |||
22 | typedef struct State { | ||
23 | Gfx* gfx; | ||
24 | IsoGfx* iso; | ||
25 | IsoGfxApp app; | ||
26 | Texture* screen_texture; | ||
27 | Scene* scene; | ||
28 | } State; | ||
29 | |||
30 | static 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 | |||
116 | cleanup: | ||
117 | if (state->gfx) { | ||
118 | gfx_destroy(&state->gfx); | ||
119 | } | ||
120 | free(state); | ||
121 | return false; | ||
122 | } | ||
123 | |||
124 | static 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 | |||
137 | static 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 | |||
147 | static 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 | |||
169 | static 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 | |||
177 | int 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 | } | ||