Skip to content

Commit 0cc85a7

Browse files
authoredApr 7, 2019
daynightratio.h: Improve codestyle, minor optimisations (#8453)
1 parent a222ff4 commit 0cc85a7

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed
 

‎src/daynightratio.h

+34-30
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,51 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
2323
{
2424
float t = time_of_day;
25-
if(t < 0)
26-
t += ((int)(-t)/24000)*24000;
27-
if(t >= 24000)
28-
t -= ((int)(t)/24000)*24000;
29-
if(t > 12000)
30-
t = 24000 - t;
31-
float values[][2] = {
32-
{4250+125, 150},
33-
{4500+125, 150},
34-
{4750+125, 250},
35-
{5000+125, 350},
36-
{5250+125, 500},
37-
{5500+125, 675},
38-
{5750+125, 875},
39-
{6000+125, 1000},
40-
{6250+125, 1000},
25+
if (t < 0.0f)
26+
t += ((int)(-t) / 24000) * 24000.0f;
27+
if (t >= 24000.0f)
28+
t -= ((int)(t) / 24000) * 24000.0f;
29+
if (t > 12000.0f)
30+
t = 24000.0f - t;
31+
32+
const float values[9][2] = {
33+
{4250.0f + 125.0f, 150.0f},
34+
{4500.0f + 125.0f, 150.0f},
35+
{4750.0f + 125.0f, 250.0f},
36+
{5000.0f + 125.0f, 350.0f},
37+
{5250.0f + 125.0f, 500.0f},
38+
{5500.0f + 125.0f, 675.0f},
39+
{5750.0f + 125.0f, 875.0f},
40+
{6000.0f + 125.0f, 1000.0f},
41+
{6250.0f + 125.0f, 1000.0f},
4142
};
42-
if(!smooth){
43+
44+
if (!smooth) {
4345
float lastt = values[0][0];
44-
for(u32 i=1; i<sizeof(values)/sizeof(*values); i++){
46+
for (u32 i = 1; i < 9; i++) {
4547
float t0 = values[i][0];
46-
float switch_t = (t0 + lastt) / 2;
48+
float switch_t = (t0 + lastt) / 2.0f;
4749
lastt = t0;
48-
if(switch_t <= t)
50+
if (switch_t <= t)
4951
continue;
52+
5053
return values[i][1];
5154
}
5255
return 1000;
5356
}
5457

55-
if (t <= 4625) // 4500 + 125
58+
if (t <= 4625.0f) // 4500 + 125
5659
return values[0][1];
57-
else if (t >= 6125) // 6000 + 125
60+
else if (t >= 6125.0f) // 6000 + 125
5861
return 1000;
59-
for (u32 i=0; i < sizeof(values) / sizeof(*values); i++) {
60-
if (values[i][0] <= t)
61-
continue;
6262

63-
float td0 = values[i][0] - values[i-1][0];
64-
float f = (t - values[i-1][0]) / td0;
65-
return f * values[i][1] + (1.0 - f) * values[i-1][1];
66-
}
67-
return 1000;
63+
for (u32 i = 0; i < 9; i++) {
64+
if (values[i][0] <= t)
65+
continue;
66+
67+
float td0 = values[i][0] - values[i - 1][0];
68+
float f = (t - values[i - 1][0]) / td0;
69+
return f * values[i][1] + (1.0f - f) * values[i - 1][1];
70+
}
71+
return 1000;
6872
}

0 commit comments

Comments
 (0)
Please sign in to comment.