forked from sam-itt/sofis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ladder-gauge.c
248 lines (224 loc) · 8.75 KB
/
ladder-gauge.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <samuel.cuella@gmail.com>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include <stdlib.h>
#include "ladder-gauge.h"
#include "ladder-page-factory.h"
#include "generic-layer.h"
#include "sdl-colors.h"
static void ladder_gauge_update_state(LadderGauge *self, Uint32 dt);
static void ladder_gauge_render(LadderGauge *self, Uint32 dt, RenderContext *ctx);
static void *ladder_gauge_dispose(LadderGauge *self);
static BaseGaugeOps ladder_gauge_ops = {
.render = (RenderFunc)ladder_gauge_render,
.update_state = (StateUpdateFunc)ladder_gauge_update_state,
.dispose = (DisposeFunc)ladder_gauge_dispose
};
LadderGauge *ladder_gauge_new(LadderPageDescriptor *descriptor, int rubis)
{
LadderGauge *self;
self = calloc(1, sizeof(LadderGauge));
if(self){
if(!ladder_gauge_init(self, descriptor,rubis)){
return base_gauge_free(BASE_GAUGE(self));
}
}
return self;
}
LadderGauge *ladder_gauge_init(LadderGauge *self, LadderPageDescriptor *descriptor, int rubis)
{
base_gauge_init(BASE_GAUGE(self), &ladder_gauge_ops, 68, 240);
self->descriptor = descriptor;
if(rubis > 0)
self->rubis = rubis;
else
self->rubis = round(base_gauge_h(BASE_GAUGE(self))/2.0);
return self;
}
static void *ladder_gauge_dispose(LadderGauge *self)
{
for(int i = 0; i < N_PAGES; i++){
if(self->pages[i]){
ladder_page_free(self->pages[i]);
self->pages[i] = NULL;
}
}
if(self->descriptor)
free(self->descriptor); /*No need for virtual dispose ATM*/
return self;
}
bool ladder_gauge_set_value(LadderGauge *self, float value, bool animated)
{
return sfv_gauge_set_value(SFV_GAUGE(self), value, animated);
}
/**
*
* @param idx: the page number, computed from page range.
* If range is 0-699, then page 0 would be 0 -> 699, page 1
* 700-1399, etc.
*/
LadderPage *ladder_gauge_get_page(LadderGauge *self, uintf8_t idx)
{
int a_idx;
uintf8_t cmp; /*current max page*/
uintf8_t offset; /*Number of pages to move (advance of back up)*/
int j;
cmp = self->base + (N_PAGES-1);
if(idx > cmp){
offset = idx - cmp;
for(int i = 0; i < N_PAGES; i++){
j = i - offset;
// printf("j = %d - %d = %d\n",i,offset,j);
if( j < 0 ){
if(self->pages[i]){
ladder_page_free(self->pages[i]);
self->pages[i] = NULL;
}
}else{
self->pages[j] = self->pages[i];
self->pages[i] = NULL;
}
}
self->base += offset;
}else if(idx < self->base){
offset = self->base - idx;
for(int i = N_PAGES-1; i >= 0; i--){
j = i + offset;
if( j > N_PAGES-1){
if(self->pages[i]){
ladder_page_free(self->pages[i]);
self->pages[i] = NULL;
}
}else{
self->pages[j] = self->pages[i];
self->pages[i] = NULL;
}
}
self->base -= offset;
}
a_idx = idx - self->base;
if(!self->pages[a_idx])
self->pages[a_idx] = ladder_page_factory_create(idx, self->descriptor);
return self->pages[a_idx];
}
LadderPage *ladder_gauge_get_page_for(LadderGauge *self, float value)
{
int page_idx;
page_idx = (int)value/(self->descriptor->page_size);
return ladder_gauge_get_page(self, page_idx);
}
static void ladder_gauge_update_state(LadderGauge *self, Uint32 dt)
{
float y;
float rubis;
LadderPage *page, *page2;
SDL_Rect dst_region = {0,0,base_gauge_w(BASE_GAUGE(self)),base_gauge_h(BASE_GAUGE(self))};
memset(&self->state, 0, sizeof(LadderGaugeState));
SFV_GAUGE(self)->value = SFV_GAUGE(self)->value >= 0 ? SFV_GAUGE(self)->value : 0.0f;
page = ladder_gauge_get_page_for(self, SFV_GAUGE(self)->value);
y = ladder_page_resolve_value(page, SFV_GAUGE(self)->value);
// printf("y = %f for value = %f\n",y,value);
rubis = (self->rubis < 0) ? base_gauge_h(BASE_GAUGE(self)) / 2.0 : self->rubis;
SDL_Rect portion = {
.x = 0,
.y = round(y - rubis),
.w = generic_layer_w(GENERIC_LAYER(page)),
.h = base_gauge_h(BASE_GAUGE(self))
};
/* Ensures that portion.y + portion.h doesn't got past image bounds:
* w/h are ignored by SDL_BlitSurface, but when using SDL_Renderers wrong
* values will stretch the image.
* */
if(portion.y + portion.h > generic_layer_h(GENERIC_LAYER(page)))
portion.h = generic_layer_h(GENERIC_LAYER(page)) - portion.y;
/*All pages must have the same size*/
if(portion.y < 0){ //Fill top
SDL_Rect patch = {
.x = 0,
.y = generic_layer_h(GENERIC_LAYER(page)) + portion.y, //means - portion.y as portion.y < 0 here
.w = generic_layer_w(GENERIC_LAYER(page)),
.h = generic_layer_h(GENERIC_LAYER(page)) - patch.y
};
if(self->descriptor->direction == TOP_DOWN){
/* 0 is on top, 100 is downwards. We need to fill the top with values before the begining
* of the current page, i.e get the previous page */
int pidx = ladder_page_get_index(page);
if(pidx > 0){
page2 = ladder_gauge_get_page(self, pidx - 1);
self->state.patches[self->state.npatches].layer = GENERIC_LAYER(page2);
self->state.patches[self->state.npatches].src = patch;
self->state.patches[self->state.npatches].dst = dst_region;
self->state.npatches++;
}
}else{
/* 0 at the bottom, 100 is upwards. We need to fill the top with values after the end
* of the current page, i.e get the next page */
int pidx = ladder_page_get_index(page);
page2 = ladder_gauge_get_page(self, pidx + 1);
self->state.patches[self->state.npatches].layer = GENERIC_LAYER(page2);
self->state.patches[self->state.npatches].src = patch;
self->state.patches[self->state.npatches].dst = dst_region;
self->state.npatches++;
}
dst_region.y = patch.h;
portion.y = 0;
portion.h -= patch.h;
}
self->state.patches[self->state.npatches].layer = GENERIC_LAYER(page);
self->state.patches[self->state.npatches].src = portion;
self->state.patches[self->state.npatches].dst = dst_region;
self->state.npatches++;
if(portion.y + base_gauge_h(BASE_GAUGE(self)) > generic_layer_h(GENERIC_LAYER(page))){// fill bottom
float taken = generic_layer_h(GENERIC_LAYER(page)) - portion.y; //number of pixels taken from the bottom of values pict
float delta = base_gauge_h(BASE_GAUGE(self)) - taken;
dst_region.y += taken;
SDL_Rect patch = {
.x = 0,
.y = 0,
.w = generic_layer_w(GENERIC_LAYER(page)),
.h = delta
};
if(self->descriptor->direction == TOP_DOWN){
/* 0 is on top, 100 is downwards. We need to fill the bottom with values that are after the end
* of the current page, i.e get the next page */
int pidx = ladder_page_get_index(page);
page2 = ladder_gauge_get_page(self, pidx + 1);
self->state.patches[self->state.npatches].layer = GENERIC_LAYER(page2);
self->state.patches[self->state.npatches].src = patch;
self->state.patches[self->state.npatches].dst = dst_region;
self->state.npatches++;
}else{
/* 0 at the bottom, 100 is upwards. We need to fill the bottom with values that are before the begining
* of the current page, i.e get the previous page */
int pidx = ladder_page_get_index(page);
if(pidx > 0){
page2 = ladder_gauge_get_page(self, pidx - 1);
self->state.patches[self->state.npatches].layer = GENERIC_LAYER(page2);
self->state.patches[self->state.npatches].src = patch;
self->state.patches[self->state.npatches].dst = dst_region;
self->state.npatches++;
}
}
}
self->state.pskip = round(base_gauge_w(BASE_GAUGE(self))/2.0);
}
static void ladder_gauge_render(LadderGauge *self, Uint32 dt, RenderContext *ctx)
{
for(int i = 0; i < self->state.npatches; i++){
base_gauge_blit_layer(BASE_GAUGE(self), ctx,
self->state.patches[i].layer,
&self->state.patches[i].src,
&self->state.patches[i].dst
);
}
base_gauge_draw_rubis(BASE_GAUGE(self),
ctx, self->rubis,
&SDL_RED, self->state.pskip
);
base_gauge_draw_outline(BASE_GAUGE(self), ctx, &SDL_WHITE, NULL);
}