diff options
author | 3gg <3gg@shellblade.net> | 2024-08-31 18:58:39 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-08-31 18:58:39 -0700 |
commit | daf6262c029892212f6b9b4014887c2c37e9ef75 (patch) | |
tree | 40b24f4f23cc48e55c3511c28c7bef29834d61b3 /gfx-iso/src/backend.c | |
parent | 0a78a9d9c1ac2090da56f395d6f2394a8a210010 (diff) |
Handle resizing.
Diffstat (limited to 'gfx-iso/src/backend.c')
-rw-r--r-- | gfx-iso/src/backend.c | 199 |
1 files changed, 199 insertions, 0 deletions
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 | |||
14 | typedef 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 | |||
32 | IsoBackend* 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 | |||
100 | cleanup: | ||
101 | if (backend->gfx) { | ||
102 | gfx_destroy(&backend->gfx); | ||
103 | } | ||
104 | free(backend); | ||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | void 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 | |||
119 | void 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 | |||
148 | void 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 | |||
177 | bool 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 | } | ||