gosu 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/src/Color.cpp CHANGED
@@ -9,9 +9,9 @@ namespace
9
9
 
10
10
  HSV color_to_hsv(const Gosu::Color& c)
11
11
  {
12
- double r = c.red() / 255.0;
13
- double g = c.green() / 255.0;
14
- double b = c.blue() / 255.0;
12
+ double r = c.red / 255.0;
13
+ double g = c.green / 255.0;
14
+ double b = c.blue / 255.0;
15
15
 
16
16
  double min = std::min(std::min(r, g), b);
17
17
  double max = std::max(std::max(r, g), b);
@@ -22,7 +22,7 @@ namespace
22
22
  return hsv;
23
23
  }
24
24
 
25
- HSV hsv;
25
+ HSV hsv{};
26
26
 
27
27
  // Value.
28
28
  hsv.v = max;
@@ -50,38 +50,33 @@ namespace
50
50
  }
51
51
 
52
52
  Gosu::Color Gosu::Color::from_hsv(double h, double s, double v)
53
- {
54
- return from_ahsv(255, h, s, v);
55
- }
56
-
57
- Gosu::Color Gosu::Color::from_ahsv(Channel alpha, double h, double s, double v)
58
53
  {
59
54
  // Normalize hue so that is always in the [0, 360) range and wraps around.
60
55
  h = normalize_angle(h);
61
56
  // Clamp s and v for consistency with the Ruby/Gosu ARGB getters/setters.
62
- s = clamp(s, 0.0, 1.0);
63
- v = clamp(v, 0.0, 1.0);
64
-
57
+ s = std::clamp(s, 0.0, 1.0);
58
+ v = std::clamp(v, 0.0, 1.0);
59
+
65
60
  int sector = static_cast<int>(h / 60);
66
61
  double factorial = h / 60 - sector;
67
-
68
- double p = v * (1 - s);
69
- double q = v * (1 - s * factorial);
70
- double t = v * (1 - s * (1 - factorial));
71
-
62
+
63
+ Channel p = static_cast<Channel>(255 * v * (1 - s));
64
+ Channel q = static_cast<Channel>(255 * v * (1 - s * factorial));
65
+ Channel t = static_cast<Channel>(255 * v * (1 - s * (1 - factorial)));
66
+
72
67
  switch (sector) {
73
68
  case 0:
74
- return Color(alpha, v * 255, t * 255, p * 255);
69
+ return Color{static_cast<Channel>(255 * v), t, p};
75
70
  case 1:
76
- return Color(alpha, q * 255, v * 255, p * 255);
71
+ return Color{q, static_cast<Channel>(255 * v), p};
77
72
  case 2:
78
- return Color(alpha, p * 255, v * 255, t * 255);
73
+ return Color{p, static_cast<Channel>(255 * v), t};
79
74
  case 3:
80
- return Color(alpha, p * 255, q * 255, v * 255);
75
+ return Color{p, q, static_cast<Channel>(255 * v)};
81
76
  case 4:
82
- return Color(alpha, t * 255, p * 255, v * 255);
77
+ return Color{t, p, static_cast<Channel>(255 * v)};
83
78
  default: // sector 5
84
- return Color(alpha, v * 255, p * 255, q * 255);
79
+ return Color{static_cast<Channel>(255 * v), p, q};
85
80
  }
86
81
  }
87
82
 
@@ -92,7 +87,7 @@ double Gosu::Color::hue() const
92
87
 
93
88
  void Gosu::Color::set_hue(double h)
94
89
  {
95
- *this = from_ahsv(alpha(), h, saturation(), value());
90
+ *this = from_hsv(h, saturation(), value()).with_alpha(alpha);
96
91
  }
97
92
 
98
93
  double Gosu::Color::saturation() const
@@ -102,7 +97,7 @@ double Gosu::Color::saturation() const
102
97
 
103
98
  void Gosu::Color::set_saturation(double s)
104
99
  {
105
- *this = from_ahsv(alpha(), hue(), s, value());
100
+ *this = from_hsv(hue(), s, value()).with_alpha(alpha);
106
101
  }
107
102
 
108
103
  double Gosu::Color::value() const
@@ -112,33 +107,42 @@ double Gosu::Color::value() const
112
107
 
113
108
  void Gosu::Color::set_value(double v)
114
109
  {
115
- *this = from_ahsv(alpha(), hue(), saturation(), v);
110
+ *this = from_hsv(hue(), saturation(), v).with_alpha(alpha);
116
111
  }
117
112
 
118
- Gosu::Color Gosu::interpolate(Color a, Color b, double weight)
113
+ Gosu::Color Gosu::lerp(Color a, Color b, double t)
119
114
  {
120
- return Color(clamp<long>(round(interpolate(a.alpha(), b.alpha(), weight)), 0, 255),
121
- clamp<long>(round(interpolate(a.red(), b.red(), weight)), 0, 255),
122
- clamp<long>(round(interpolate(a.green(), b.green(), weight)), 0, 255),
123
- clamp<long>(round(interpolate(a.blue(), b.blue(), weight)), 0, 255));
115
+ const auto lerp_channel = [](Color::Channel a, Color::Channel b, double t) {
116
+ return static_cast<Color::Channel>(std::clamp(std::round(lerp(a, b, t)), 0.0, 255.0));
117
+ };
118
+
119
+ Color result;
120
+ result.red = lerp_channel(a.red, b.red, t);
121
+ result.green = lerp_channel(a.green, b.green, t);
122
+ result.blue = lerp_channel(a.blue, b.blue, t);
123
+ result.alpha = lerp_channel(a.alpha, b.alpha, t);
124
+ return result;
124
125
  }
125
126
 
126
127
  Gosu::Color Gosu::multiply(Color a, Color b)
127
128
  {
128
- return Color(round(a.alpha() * b.alpha() / 255.0),
129
- round(a.red() * b.red() / 255.0),
130
- round(a.green() * b.green() / 255.0),
131
- round(a.blue() * b.blue() / 255.0));
129
+ Color result;
130
+ result.red = static_cast<Color::Channel>(std::round(a.red * b.red / 255.0));
131
+ result.green = static_cast<Color::Channel>(std::round(a.green * b.green / 255.0));
132
+ result.blue = static_cast<Color::Channel>(std::round(a.blue * b.blue / 255.0));
133
+ result.alpha = static_cast<Color::Channel>(std::round(a.alpha * b.alpha / 255.0));
134
+ return result;
132
135
  }
133
136
 
134
- const Gosu::Color Gosu::Color::NONE (0x00000000);
135
- const Gosu::Color Gosu::Color::BLACK (0xff000000);
136
- const Gosu::Color Gosu::Color::GRAY (0xff808080);
137
- const Gosu::Color Gosu::Color::WHITE (0xffffffff);
138
- const Gosu::Color Gosu::Color::AQUA (0xff00ffff);
139
- const Gosu::Color Gosu::Color::RED (0xffff0000);
140
- const Gosu::Color Gosu::Color::GREEN (0xff00ff00);
141
- const Gosu::Color Gosu::Color::BLUE (0xff0000ff);
142
- const Gosu::Color Gosu::Color::YELLOW (0xffffff00);
143
- const Gosu::Color Gosu::Color::FUCHSIA (0xffff00ff);
144
- const Gosu::Color Gosu::Color::CYAN (0xff00ffff);
137
+ const Gosu::Color Gosu::Color::NONE{0x00'000000};
138
+ const Gosu::Color Gosu::Color::BLACK{0, 0, 0};
139
+ const Gosu::Color Gosu::Color::GRAY{128, 128, 128};
140
+ const Gosu::Color Gosu::Color::WHITE{255, 255, 255};
141
+
142
+ const Gosu::Color Gosu::Color::AQUA{0, 255, 255};
143
+ const Gosu::Color Gosu::Color::RED{255, 0, 0};
144
+ const Gosu::Color Gosu::Color::GREEN{0, 255, 0};
145
+ const Gosu::Color Gosu::Color::BLUE{0, 0, 255};
146
+ const Gosu::Color Gosu::Color::YELLOW{255, 255, 0};
147
+ const Gosu::Color Gosu::Color::FUCHSIA{255, 0, 255};
148
+ const Gosu::Color Gosu::Color::CYAN{0, 255, 255};
@@ -121,6 +121,8 @@ static void handle_audio_interruption(void* unused, UInt32 inInterruptionState)
121
121
  - (void)applicationWillEnterForeground:(NSNotification*)notification
122
122
  {
123
123
  [self setupTimerOrDisplayLink];
124
+
125
+ self.gosuWindowReference.gain_focus();
124
126
  }
125
127
 
126
128
  - (void)applicationDidEnterBackground:(NSNotification*)notification
@@ -84,25 +84,25 @@ void Gosu::LargeImageData::draw(double x1, double y1, Color c1,
84
84
  double rel_y_t = y / h;
85
85
  double rel_y_b = (y + tile.height()) / h;
86
86
 
87
- #define INTERPOLATE(what, x_weight, y_weight) \
88
- interpolate(interpolate(what##1, what##3, y_weight), \
89
- interpolate(what##2, what##4, y_weight), \
90
- x_weight);
91
-
92
- double x_t_l = INTERPOLATE(x, rel_x_l, rel_y_t);
93
- double x_t_r = INTERPOLATE(x, rel_x_r, rel_y_t);
94
- double x_b_l = INTERPOLATE(x, rel_x_l, rel_y_b);
95
- double x_b_r = INTERPOLATE(x, rel_x_r, rel_y_b);
96
-
97
- double y_t_l = INTERPOLATE(y, rel_x_l, rel_y_t);
98
- double y_t_r = INTERPOLATE(y, rel_x_r, rel_y_t);
99
- double y_b_l = INTERPOLATE(y, rel_x_l, rel_y_b);
100
- double y_b_r = INTERPOLATE(y, rel_x_r, rel_y_b);
101
-
102
- Color c_t_l = INTERPOLATE(c, rel_x_l, rel_y_t);
103
- Color c_t_r = INTERPOLATE(c, rel_x_r, rel_y_t);
104
- Color c_b_l = INTERPOLATE(c, rel_x_l, rel_y_b);
105
- Color c_b_r = INTERPOLATE(c, rel_x_r, rel_y_b);
87
+ #define LERP2D(what, x_weight, y_weight) \
88
+ lerp(lerp(what##1, what##3, y_weight), \
89
+ lerp(what##2, what##4, y_weight), \
90
+ x_weight);
91
+
92
+ double x_t_l = LERP2D(x, rel_x_l, rel_y_t);
93
+ double x_t_r = LERP2D(x, rel_x_r, rel_y_t);
94
+ double x_b_l = LERP2D(x, rel_x_l, rel_y_b);
95
+ double x_b_r = LERP2D(x, rel_x_r, rel_y_b);
96
+
97
+ double y_t_l = LERP2D(y, rel_x_l, rel_y_t);
98
+ double y_t_r = LERP2D(y, rel_x_r, rel_y_t);
99
+ double y_b_l = LERP2D(y, rel_x_l, rel_y_b);
100
+ double y_b_r = LERP2D(y, rel_x_r, rel_y_b);
101
+
102
+ Color c_t_l = LERP2D(c, rel_x_l, rel_y_t);
103
+ Color c_t_r = LERP2D(c, rel_x_r, rel_y_t);
104
+ Color c_b_l = LERP2D(c, rel_x_l, rel_y_b);
105
+ Color c_b_r = LERP2D(c, rel_x_r, rel_y_b);
106
106
 
107
107
  tile.draw(x_t_l, y_t_l, c_t_l,
108
108
  x_t_r, y_t_r, c_t_r,