summaryrefslogtreecommitdiff
path: root/gfx-iso/src
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-08-31 18:58:39 -0700
committer3gg <3gg@shellblade.net>2024-08-31 18:58:39 -0700
commitdaf6262c029892212f6b9b4014887c2c37e9ef75 (patch)
tree40b24f4f23cc48e55c3511c28c7bef29834d61b3 /gfx-iso/src
parent0a78a9d9c1ac2090da56f395d6f2394a8a210010 (diff)
Handle resizing.
Diffstat (limited to 'gfx-iso/src')
-rw-r--r--gfx-iso/src/app.c196
-rw-r--r--gfx-iso/src/backend.c199
2 files changed, 199 insertions, 196 deletions
diff --git a/gfx-iso/src/app.c b/gfx-iso/src/app.c
deleted file mode 100644
index 2f2241f..0000000
--- a/gfx-iso/src/app.c
+++ /dev/null
@@ -1,196 +0,0 @@
1#include <isogfx/app.h>
2#include <isogfx/isogfx.h>
3
4#include <gfx/app.h>
5#include <gfx/core.h>
6#include <gfx/gfx.h>
7#include <gfx/renderer.h>
8#include <gfx/scene.h>
9#include <gfx/util/geometry.h>
10#include <gfx/util/shader.h>
11
12#include <assert.h>
13#include <stdbool.h>
14#include <stdlib.h>
15
16static const int WINDOW_WIDTH = 1408;
17static const int WINDOW_HEIGHT = 960;
18static const int MAX_FPS = 60;
19
20typedef struct AppState {
21 Gfx* gfx;
22 IsoGfx* iso;
23 IsoGfxApp* app;
24 Texture* screen_texture;
25 Scene* scene;
26} AppState;
27
28typedef struct GfxAppState {
29 AppState state;
30} GfxAppState;
31
32static bool init(GfxAppState* gfx_app_state, int argc, const char** argv) {
33 assert(gfx_app_state);
34 AppState* state = &gfx_app_state->state;
35
36 IsoGfxApp* app = state->app;
37
38 // Virtual screen dimensions.
39 const int scale = app->pixel_scale == 0 ? 1 : app->pixel_scale;
40 const int screen_width = WINDOW_WIDTH / scale;
41 const int screen_height = WINDOW_HEIGHT / scale;
42
43 if (!(state->iso = isogfx_new(&(IsoGfxDesc){
44 .screen_width = screen_width, .screen_height = screen_height}))) {
45 goto cleanup;
46 }
47
48 if (!(*app->init)(app->state, state->iso, argc, argv)) {
49 goto cleanup;
50 }
51
52 isogfx_resize(state->iso, screen_width, screen_height);
53
54 if (!(state->gfx = gfx_init())) {
55 goto cleanup;
56 }
57 GfxCore* gfxcore = gfx_get_core(state->gfx);
58
59 if (!(state->screen_texture = gfx_make_texture(
60 gfxcore, &(TextureDesc){
61 .width = screen_width,
62 .height = screen_height,
63 .dimension = Texture2D,
64 .format = TextureSRGBA8,
65 .filtering = NearestFiltering,
66 .wrap = ClampToEdge,
67 .mipmaps = false}))) {
68 goto cleanup;
69 }
70
71 ShaderProgram* shader = gfx_make_view_texture_shader(gfxcore);
72 if (!shader) {
73 goto cleanup;
74 }
75
76 Geometry* geometry = gfx_make_quad_11(gfxcore);
77 if (!geometry) {
78 goto cleanup;
79 }
80
81 MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1};
82 material_desc.uniforms[0] = (ShaderUniform){
83 .type = UniformTexture,
84 .value.texture = state->screen_texture,
85 .name = sstring_make("Texture")};
86 Material* material = gfx_make_material(&material_desc);
87 if (!material) {
88 return false;
89 }
90
91 const MeshDesc mesh_desc =
92 (MeshDesc){.geometry = geometry, .material = material, .shader = shader};
93 Mesh* mesh = gfx_make_mesh(&mesh_desc);
94 if (!mesh) {
95 goto cleanup;
96 }
97
98 SceneObject* object =
99 gfx_make_object(&(ObjectDesc){.num_meshes = 1, .meshes = {mesh}});
100 if (!object) {
101 goto cleanup;
102 }
103
104 state->scene = gfx_make_scene();
105 SceneNode* node = gfx_make_object_node(object);
106 SceneNode* root = gfx_get_scene_root(state->scene);
107 gfx_set_node_parent(node, root);
108
109 return true;
110
111cleanup:
112 if (state->gfx) {
113 gfx_destroy(&state->gfx);
114 }
115 free(state);
116 return false;
117}
118
119static void shutdown(GfxAppState* gfx_app_state) {
120 assert(gfx_app_state);
121 AppState* state = &gfx_app_state->state;
122
123 if (state->app) {
124 assert(state->iso);
125 (*state->app->shutdown)(state->app->state, state->iso);
126 }
127
128 isogfx_del(&state->iso);
129 gfx_destroy(&state->gfx);
130}
131
132static void update(GfxAppState* gfx_app_state, double t, double dt) {
133 assert(gfx_app_state);
134 AppState* state = &gfx_app_state->state;
135
136 isogfx_update(state->iso, t);
137
138 assert(state->app->update);
139 (*state->app->update)(state->app->state, state->iso, t, dt);
140}
141
142static void render(GfxAppState* gfx_app_state) {
143 assert(gfx_app_state);
144 AppState* state = &gfx_app_state->state;
145
146 assert(state->app->render);
147 (*state->app->render)(state->app->state, state->iso);
148
149 const Pixel* screen = isogfx_get_screen_buffer(state->iso);
150 assert(screen);
151 gfx_update_texture(
152 state->screen_texture, &(TextureDataDesc){.pixels = screen});
153
154 GfxCore* gfxcore = gfx_get_core(state->gfx);
155 Renderer* renderer = gfx_get_renderer(state->gfx);
156
157 // TODO: Prevent stretching of the virtual screen onto the window; preserve
158 // aspect ratio.
159 gfx_start_frame(gfxcore);
160 gfx_render_scene(
161 renderer, &(RenderSceneParams){
162 .mode = RenderDefault, .scene = state->scene, .camera = 0});
163 gfx_end_frame(gfxcore);
164}
165
166static void resize(GfxAppState* gfx_app_state, int width, int height) {
167 assert(gfx_app_state);
168 AppState* state = &gfx_app_state->state;
169
170 GfxCore* gfxcore = gfx_get_core(state->gfx);
171 gfx_set_viewport(gfxcore, width, height);
172}
173
174void iso_run(int argc, const char** argv, IsoGfxApp* app) {
175 GfxAppState app_state = {
176 .state = (AppState){
177 .app = app,
178 }
179 };
180 gfx_app_run(
181 &(GfxAppDesc){
182 .argc = argc,
183 .argv = argv,
184 .width = WINDOW_WIDTH,
185 .height = WINDOW_HEIGHT,
186 .max_fps = MAX_FPS,
187 .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0,
188 .title = "Isometric Renderer",
189 .app_state = &app_state},
190 &(GfxAppCallbacks){
191 .init = init,
192 .update = update,
193 .render = render,
194 .resize = resize,
195 .shutdown = shutdown});
196}
diff --git a/gfx-iso/src/backend.c b/gfx-iso/src/backend.c
new file mode 100644
index 0000000..db91647
--- /dev/null
+++ b/gfx-iso/src/backend.c
@@ -0,0 +1,199 @@
1#include <isogfx/backend.h>
2#include <isogfx/isogfx.h>
3
4#include <gfx/core.h>
5#include <gfx/gfx.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 <stdlib.h>
13
14typedef struct IsoBackend {
15 Gfx* gfx;
16 Scene* scene;
17 /// The screen or "iso screen" refers to the colour buffer of the iso graphics
18 /// library. This texture is used to draw the iso screen onto the graphics
19 /// window.
20 Texture* screen_texture;
21 /// Window size.
22 int window_width;
23 int window_height;
24 /// The viewport refers to the area inside the window to which screen_texture
25 /// is drawn. It is a scaled version of the iso screen, scaled while
26 /// respecting the iso screen's aspect ratio to prevent distortion.
27 int viewport_x, viewport_y, viewport_width, viewport_height;
28 double stretch; // Stretch factor from iso screen dimensions to viewport
29 // dimensions.
30} IsoBackend;
31
32IsoBackend* IsoBackendInit(const IsoGfx* iso) {
33 assert(iso);
34
35 IsoBackend* backend = calloc(1, sizeof(IsoBackend));
36 if (!backend) {
37 return 0;
38 }
39
40 if (!(backend->gfx = gfx_init())) {
41 goto cleanup;
42 }
43 GfxCore* gfxcore = gfx_get_core(backend->gfx);
44
45 int screen_width, screen_height;
46 isogfx_get_screen_size(iso, &screen_width, &screen_height);
47
48 if (!(backend->screen_texture = gfx_make_texture(
49 gfxcore, &(TextureDesc){
50 .width = screen_width,
51 .height = screen_height,
52 .dimension = Texture2D,
53 .format = TextureSRGBA8,
54 .filtering = NearestFiltering,
55 .wrap = ClampToEdge,
56 .mipmaps = false}))) {
57 goto cleanup;
58 }
59
60 ShaderProgram* shader = gfx_make_view_texture_shader(gfxcore);
61 if (!shader) {
62 goto cleanup;
63 }
64
65 Geometry* geometry = gfx_make_quad_11(gfxcore);
66 if (!geometry) {
67 goto cleanup;
68 }
69
70 MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1};
71 material_desc.uniforms[0] = (ShaderUniform){
72 .type = UniformTexture,
73 .value.texture = backend->screen_texture,
74 .name = sstring_make("Texture")};
75 Material* material = gfx_make_material(&material_desc);
76 if (!material) {
77 return false;
78 }
79
80 const MeshDesc mesh_desc =
81 (MeshDesc){.geometry = geometry, .material = material, .shader = shader};
82 Mesh* mesh = gfx_make_mesh(&mesh_desc);
83 if (!mesh) {
84 goto cleanup;
85 }
86
87 SceneObject* object =
88 gfx_make_object(&(ObjectDesc){.num_meshes = 1, .meshes = {mesh}});
89 if (!object) {
90 goto cleanup;
91 }
92
93 backend->scene = gfx_make_scene();
94 SceneNode* node = gfx_make_object_node(object);
95 SceneNode* root = gfx_get_scene_root(backend->scene);
96 gfx_set_node_parent(node, root);
97
98 return backend;
99
100cleanup:
101 if (backend->gfx) {
102 gfx_destroy(&backend->gfx);
103 }
104 free(backend);
105 return 0;
106}
107
108void IsoBackendShutdown(IsoBackend** ppApp) {
109 assert(ppApp);
110
111 IsoBackend* app = *ppApp;
112 if (!app) {
113 return;
114 }
115
116 gfx_destroy(&app->gfx);
117}
118
119void IsoBackendResizeWindow(
120 IsoBackend* app, const IsoGfx* iso, int width, int height) {
121 assert(app);
122 assert(iso);
123
124 app->window_width = width;
125 app->window_height = height;
126
127 // Virtual screen dimensions.
128 int screen_width, screen_height;
129 isogfx_get_screen_size(iso, &screen_width, &screen_height);
130
131 // Stretch the virtual screen onto the viewport while respecting the screen's
132 // aspect ratio to prevent distortion.
133 if (width > height) { // Wide screen.
134 app->stretch = (double)height / (double)screen_height;
135 app->viewport_width = (int)((double)screen_width * app->stretch);
136 app->viewport_height = height;
137 app->viewport_x = (width - app->viewport_width) / 2;
138 app->viewport_y = 0;
139 } else { // Tall screen.
140 app->stretch = (double)width / (double)screen_width;
141 app->viewport_width = width;
142 app->viewport_height = (int)((float)screen_height * app->stretch);
143 app->viewport_x = 0;
144 app->viewport_y = (height - app->viewport_height) / 2;
145 }
146}
147
148void IsoBackendRender(const IsoBackend* app, const IsoGfx* iso) {
149 assert(app);
150 assert(iso);
151
152 const Pixel* screen = isogfx_get_screen_buffer(iso);
153 assert(screen);
154 gfx_update_texture(app->screen_texture, &(TextureDataDesc){.pixels = screen});
155
156 GfxCore* gfxcore = gfx_get_core(app->gfx);
157 Renderer* renderer = gfx_get_renderer(app->gfx);
158
159 // Clear the whole window.
160 gfx_set_viewport(gfxcore, 0, 0, app->window_width, app->window_height);
161 gfx_clear(gfxcore, vec4_make(0, 0, 0, 0));
162
163 // Draw to the subregion where the virtual screen can stretch without
164 // distortion.
165 gfx_set_viewport(
166 gfxcore, app->viewport_x, app->viewport_y, app->viewport_width,
167 app->viewport_height);
168
169 // Render the iso screen.
170 gfx_start_frame(gfxcore);
171 gfx_render_scene(
172 renderer, &(RenderSceneParams){
173 .mode = RenderDefault, .scene = app->scene, .camera = 0});
174 gfx_end_frame(gfxcore);
175}
176
177bool IsoBackendGetMousePosition(
178 const IsoBackend* app, double window_x, double window_y, double* x,
179 double* y) {
180 assert(app);
181
182 // Translate from window coordinates to the subregion where the stretched
183 // iso screen is rendered.
184 const double screen_x = window_x - app->viewport_x;
185 const double screen_y = window_y - app->viewport_y;
186
187 // Position may be out of bounds.
188 if ((0 <= screen_x) && (screen_x < app->viewport_width) && (0 <= screen_y) &&
189 (screen_y < app->viewport_height)) {
190 // Scale back from the stretched subregion to the iso screen dimensions.
191 *x = screen_x / app->stretch;
192 *y = screen_y / app->stretch;
193 return true;
194 } else {
195 *x = -1;
196 *y = -1;
197 return false;
198 }
199}