summaryrefslogtreecommitdiff
path: root/src/render.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/render.c')
-rw-r--r--src/render.c61
1 files changed, 43 insertions, 18 deletions
diff --git a/src/render.c b/src/render.c
index 51112a9..2fcade6 100644
--- a/src/render.c
+++ b/src/render.c
@@ -19,9 +19,9 @@ static const uiPixel uiPink = {128, 0, 128, 255};
19/// We store a subsurface separate from the surface so that we can always check 19/// We store a subsurface separate from the surface so that we can always check
20/// whether a given coordinate is within the bounds of the physical surface. 20/// whether a given coordinate is within the bounds of the physical surface.
21typedef struct RenderState { 21typedef struct RenderState {
22 uiSurface surface; /// Surface of pixels on which the UI is rendered. 22 uiSurface surface; ///< Surface of pixels on which the UI is rendered.
23 uiRect subsurface; /// Subregion where the current UI widget is rendered. 23 uiRect subsurface; ///< Subregion where the current UI widget is rendered.
24 uiPoint pen; /// Current pen position relative to subsurface. 24 uiPoint pen; ///< Current pen position relative to subsurface.
25} RenderState; 25} RenderState;
26 26
27static void RenderWidget(RenderState* state, const uiWidget* widget); 27static void RenderWidget(RenderState* state, const uiWidget* widget);
@@ -165,6 +165,13 @@ static void RenderFrame(const uiFrame* frame, RenderState* state) {
165 FillRect(&frame->widget.rect, uiBlack, state); 165 FillRect(&frame->widget.rect, uiBlack, state);
166} 166}
167 167
168/// Render a button.
169static void RenderButton(const uiButton* button, RenderState* state) {
170 assert(button);
171 assert(state);
172 RenderText(string_data(button->text), string_length(button->text), state);
173}
174
168/// Render a label. 175/// Render a label.
169static void RenderLabel(const uiLabel* label, RenderState* state) { 176static void RenderLabel(const uiLabel* label, RenderState* state) {
170 assert(label); 177 assert(label);
@@ -253,14 +260,43 @@ static void RenderTable(const uiTable* table, RenderState* state) {
253 } 260 }
254} 261}
255 262
263void uiRender(const uiFrame* frame, uiSurface* surface) {
264 assert(frame);
265 assert(surface);
266
267 RenderWidget(
268 &(RenderState){
269 .surface = *surface,
270 .subsurface = (uiRect){.x = 0,
271 .y = 0,
272 .width = surface->width,
273 .height = surface->height},
274 .pen = {.x = 0, .y = 0},
275 },
276 (const uiWidget*)frame);
277}
278
256/// Render a widget. 279/// Render a widget.
257static void RenderWidget(RenderState* state, const uiWidget* widget) { 280static void RenderWidget(RenderState* state, const uiWidget* widget) {
258 assert(state); 281 assert(state);
259 assert(widget); 282 assert(widget);
260 283
284 // A widget's position is relative to its parent's position.
285 // The pen currently points at the parent. Move it to this widget's position
286 // before rendering it, then render the widget's children using this new
287 // position.
288 // The pen's original position must be restored before returning, so save a
289 // copy here.
290 const uiPoint pen = state->pen;
291 state->pen =
292 (uiPoint){state->pen.x + widget->rect.x, state->pen.y + widget->rect.y};
293
261 // Render this widget. 294 // Render this widget.
262 switch (widget->type) { 295 switch (widget->type) {
296 case uiTypeLayout:
297 break;
263 case uiTypeButton: 298 case uiTypeButton:
299 RenderButton((const uiButton*)widget, state);
264 break; 300 break;
265 case uiTypeFrame: 301 case uiTypeFrame:
266 RenderFrame((const uiFrame*)widget, state); 302 RenderFrame((const uiFrame*)widget, state);
@@ -268,6 +304,8 @@ static void RenderWidget(RenderState* state, const uiWidget* widget) {
268 case uiTypeLabel: 304 case uiTypeLabel:
269 RenderLabel((const uiLabel*)widget, state); 305 RenderLabel((const uiLabel*)widget, state);
270 break; 306 break;
307 case uiTypeEdit:
308 break;
271 case uiTypeTable: 309 case uiTypeTable:
272 RenderTable((const uiTable*)widget, state); 310 RenderTable((const uiTable*)widget, state);
273 break; 311 break;
@@ -278,20 +316,7 @@ static void RenderWidget(RenderState* state, const uiWidget* widget) {
278 316
279 // Render children. 317 // Render children.
280 list_foreach(widget->children, child, { RenderWidget(state, child); }); 318 list_foreach(widget->children, child, { RenderWidget(state, child); });
281}
282 319
283void uiRender(const uiFrame* frame, uiSurface* surface) { 320 // Restore the pen.
284 assert(frame); 321 state->pen = pen;
285 assert(surface);
286
287 RenderWidget(
288 &(RenderState){
289 .surface = *surface,
290 .subsurface = (uiRect){.x = 0,
291 .y = 0,
292 .width = surface->width,
293 .height = surface->height},
294 .pen = {.x = 0, .y = 0},
295 },
296 (const uiWidget*)frame);
297} 322}