Skip to content

Commit 7fe7202

Browse files
committed
support float input, add test
1 parent 83d5e02 commit 7fe7202

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src_c/mixer.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,22 @@ chan_set_source_location(PyObject *self, PyObject *args)
11651165
{
11661166
int channelnum = pgChannel_AsInt(self);
11671167
Sint16 angle;
1168+
float angle_f;
11681169
Uint8 distance;
1170+
float distance_f;
11691171
PyThreadState *_save;
11701172

1171-
if (!PyArg_ParseTuple(args, "hb", &angle, &distance))
1173+
if (!PyArg_ParseTuple(args, "ff", &angle_f, &distance_f))
11721174
return NULL;
11731175

1176+
angle = (Sint16)roundf(fmodf(angle_f, 360));
1177+
distance_f = roundf(distance_f);
1178+
if (0 > distance_f || 256 <= distance_f) {
1179+
return RAISE(PyExc_ValueError,
1180+
"distance out of range, expected (0, 255)");
1181+
}
1182+
distance = (Uint8)distance_f;
1183+
11741184
MIXER_INIT_CHECK();
11751185
_save = PyEval_SaveThread();
11761186
if (!Mix_SetPosition(channelnum, angle, distance)) {

test/mixer_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,13 @@ def todo_test_unpause(self):
898898

899899
self.fail()
900900

901+
def test_set_source_location(self):
902+
ch = mixer.Channel(0)
903+
ch.set_source_location(-3.14, 6.25)
904+
self.assertRaises(ValueError, lambda: ch.set_source_location(0, -1))
905+
self.assertRaises(ValueError, lambda: ch.set_source_location(0, 256.0))
906+
self.assertRaises(TypeError, lambda: ch.set_source_location("", 6.25))
907+
901908

902909
class ChannelInteractiveTest(unittest.TestCase):
903910
__tags__ = ["interactive"]

0 commit comments

Comments
 (0)