-
Notifications
You must be signed in to change notification settings - Fork 2
/
gfx_setting_render.c
164 lines (140 loc) · 5.26 KB
/
gfx_setting_render.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* gfx_setting_renderer.c
*
* Created on: 8 Sep 2013
* Author: ntuckett
*/
#include "gfx_setting_render.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gfx_event.h"
#include "gfx_event_types.h"
#include "gfx_font.h"
#include "gfx_font_resources.h"
typedef struct setting_renderer_state_t
{
VGPaint text_paint;
int display_update;
} setting_renderer_state_t;
typedef struct setting_render_internal_t
{
setting_renderer_t definition;
setting_renderer_state_t state;
} setting_render_internal_t;
static void initialise_renderer(setting_render_internal_t* renderer)
{
renderer->state.display_update = 1;
renderer->state.text_paint = vgCreatePaint();
vgSetParameteri(renderer->state.text_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
vgSetParameterfv(renderer->state.text_paint, VG_PAINT_COLOR, 4, renderer->definition.text_colour);
}
static void deinitialise_renderer(setting_render_internal_t* renderer)
{
vgDestroyPaint(renderer->state.text_paint);
}
static void update_display(setting_render_internal_t* renderer)
{
// Render it out
vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
vgLoadIdentity();
vgSetfv(VG_CLEAR_COLOR, 4, renderer->definition.background_colour);
vgClear(renderer->definition.x, renderer->definition.y,
renderer->definition.width, renderer->definition.height);
vgSetiv(VG_SCISSOR_RECTS, 4, &renderer->definition.x);
vgSeti(VG_SCISSORING, VG_TRUE);
vgTranslate(renderer->definition.x, renderer->definition.y);
vgSeti(VG_RENDERING_QUALITY, renderer->definition.text_quality);
vgSetPaint(renderer->state.text_paint, VG_FILL_PATH);
gfx_render_text(renderer->definition.text_x_offset, renderer->definition.text_y_offset, renderer->definition.text, &gfx_font_sans, renderer->definition.text_size);
VGfloat text_width = gfx_text_width(renderer->definition.text, &gfx_font_sans, renderer->definition.text_size);
const size_t buffer_size = 512;
char setting_text_buffer[buffer_size];
setting_t* setting = renderer->definition.setting;
switch(setting->type)
{
case SETTING_TYPE_INT:
{
snprintf(setting_text_buffer, buffer_size - 1, renderer->definition.format, setting_get_value_int(setting));
break;
}
case SETTING_TYPE_FLOAT:
{
snprintf(setting_text_buffer, buffer_size - 1, renderer->definition.format, setting_get_value_float(setting));
break;
}
case SETTING_TYPE_ENUM:
{
snprintf(setting_text_buffer, buffer_size - 1, renderer->definition.format, setting_get_value_enum(setting));
break;
}
default:
snprintf(setting_text_buffer, buffer_size - 1, "ILLEGAL SETTING TYPE: %d", (int)setting->type);
break;
}
setting_text_buffer[buffer_size - 1] = 0;
gfx_render_text(text_width + 2 + renderer->definition.text_x_offset, renderer->definition.text_y_offset,
setting_text_buffer, &gfx_font_sans, renderer->definition.text_size);
vgSeti(VG_SCISSORING, VG_FALSE);
}
//--------------------------------------------------------------------------------------------------------------
// Event handlers
//
static void refresh_handler(gfx_event_t* event, gfx_object_t* receiver)
{
setting_render_internal_t* renderer = (setting_render_internal_t*)receiver;
renderer->state.display_update = 1;
}
static void swap_handler(gfx_event_t* event, gfx_object_t* receiver)
{
setting_render_internal_t* renderer = (setting_render_internal_t*)receiver;
if (renderer->state.display_update)
{
update_display(renderer);
renderer->state.display_update = 0;
}
}
static void post_init_handler(gfx_event_t* event, gfx_object_t* receiver)
{
initialise_renderer((setting_render_internal_t*)receiver);
}
//--------------------------------------------------------------------------------------------------------------
// Public interface
//
void gfx_setting_render_initialise()
{
}
void gfx_setting_render_deinitialise()
{
}
setting_renderer_t* gfx_setting_renderer_create(object_id_t id)
{
setting_render_internal_t *renderer = (setting_render_internal_t*) calloc(1, sizeof(setting_render_internal_t));
gfx_object_t *gfx_object = &renderer->definition.gfx_object;
gfx_object->id = id;
gfx_register_event_receiver_handler(GFX_EVENT_POSTINIT, post_init_handler, gfx_object);
gfx_register_event_receiver_handler(GFX_EVENT_REFRESH, refresh_handler, gfx_object);
gfx_register_event_receiver_handler(GFX_EVENT_BUFFERSWAP, swap_handler, gfx_object);
renderer->definition.text_quality = VG_RENDERING_QUALITY_FASTER;
return (setting_renderer_t*)renderer;
}
void gfx_setting_renderer_destroy(setting_renderer_t* renderer)
{
deinitialise_renderer((setting_render_internal_t*)renderer);
free(renderer);
}