Source

pygame / src / rect.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
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
/*
  pygame - Python Game Library
  Copyright (C) 2000-2001  Pete Shinners

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Pete Shinners
  pete@shinners.org
*/

/*
 *  Python Rect Object -- useful 2d rectangle class
 */
#define PYGAMEAPI_RECT_INTERNAL
#include "pygame.h"
#include "pygamedocs.h"
#include "structmember.h"
#include "pgcompat.h"

static PyTypeObject PyRect_Type;
#define PyRect_Check(x) ((x)->ob_type == &PyRect_Type)

static PyObject* rect_new (PyTypeObject *type, PyObject *args, PyObject *kwds);
static int rect_init (PyRectObject *self, PyObject *args, PyObject *kwds);

GAME_Rect*
GameRect_FromObject (PyObject* obj, GAME_Rect* temp)
{
    int val;
    int length;

    if (PyRect_Check (obj))
        return &((PyRectObject*) obj)->r;
    if (PySequence_Check (obj) && (length = PySequence_Length (obj)) > 0)
    {
        if (length == 4)
        {
            if (!IntFromObjIndex (obj, 0, &val))
                return NULL;
            temp->x = val;
            if (!IntFromObjIndex (obj, 1, &val))
                return NULL;
            temp->y = val;
            if (!IntFromObjIndex (obj, 2, &val))
                return NULL;
            temp->w = val;
            if (!IntFromObjIndex (obj, 3, &val))
                return NULL;
            temp->h = val;
            return temp;
        }
        if (length == 2)
        {
            PyObject* sub = PySequence_GetItem (obj, 0);
            if (!sub || !PySequence_Check (sub) || PySequence_Length (sub) != 2)
            {
                Py_XDECREF (sub);
                return NULL;
            }
            if (!IntFromObjIndex (sub, 0, &val))
            {
                Py_DECREF (sub);
                return NULL;
            }
            temp->x = val;
            if (!IntFromObjIndex (sub, 1, &val))
            {
                Py_DECREF (sub);
                return NULL;
            }
            temp->y = val;
            Py_DECREF (sub);
            
            sub = PySequence_GetItem (obj, 1);
            if (!sub || !PySequence_Check (sub) || PySequence_Length (sub) != 2)
            {
                Py_XDECREF (sub);
                return NULL;
            }
            if (!IntFromObjIndex (sub, 0, &val))
            {
                Py_DECREF (sub);
                return NULL;
            }
            temp->w = val;
            if (!IntFromObjIndex (sub, 1, &val))
            {
                Py_DECREF (sub);
                return NULL;
            }
            temp->h = val;
            Py_DECREF (sub);
            return temp;
        }
        if (PyTuple_Check (obj) && length == 1) /*looks like an arg?*/
        {
            PyObject* sub = PyTuple_GET_ITEM (obj, 0);
            if (sub)
                return GameRect_FromObject (sub, temp);
        }
    }
    if (PyObject_HasAttrString (obj, "rect"))
    {
        PyObject *rectattr;
        GAME_Rect *returnrect;
        rectattr = PyObject_GetAttrString (obj, "rect");
        if (PyCallable_Check (rectattr)) /*call if it's a method*/
        {
            PyObject *rectresult = PyObject_CallObject (rectattr, NULL);
            Py_DECREF (rectattr);
            if (!rectresult)
                return NULL;
            rectattr = rectresult;
        }
        returnrect = GameRect_FromObject (rectattr, temp);
        Py_DECREF (rectattr);
        return returnrect;
    }
    return NULL;
}

PyObject*
PyRect_New (SDL_Rect* r)
{
    PyRectObject* rect;
    rect = (PyRectObject *) PyRect_Type.tp_new (&PyRect_Type, NULL, NULL);
    if (rect)
    {
        rect->r.x = r->x;
        rect->r.y = r->y;
        rect->r.w = r->w;
        rect->r.h = r->h;
    }
    return (PyObject*)rect;
}

PyObject*
PyRect_New4 (int x, int y, int w, int h)
{
    PyRectObject* rect;
    rect = (PyRectObject *) PyRect_Type.tp_new (&PyRect_Type, NULL, NULL);
    if (rect)
    {
        rect->r.x = x;
        rect->r.y = y;
        rect->r.w = w;
        rect->r.h = h;
    }
    return (PyObject*)rect;
}

static int
DoRectsIntersect (GAME_Rect *A, GAME_Rect *B)
{
    return ((A->x >= B->x && A->x < B->x + B->w)  ||
            (B->x >= A->x && B->x < A->x + A->w)) &&
        ((A->y >= B->y && A->y < B->y + B->h)	||
         (B->y >= A->y && B->y < A->y + A->h));
}

static PyObject*
rect_normalize (PyObject* oself)
{
    PyRectObject* self = (PyRectObject*)oself;

    if (self->r.w < 0)
    {
        self->r.x += self->r.w;
        self->r.w = -self->r.w;
    }
    if (self->r.h < 0)
    {
        self->r.y += self->r.h;
        self->r.h = -self->r.h;
    }

    Py_RETURN_NONE;
}

static PyObject*
rect_move (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    int x, y;

    if (!TwoIntsFromObj (args, &x, &y))
        return RAISE (PyExc_TypeError, "argument must contain two numbers");

    return PyRect_New4 (self->r.x + x, self->r.y + y, self->r.w, self->r.h);
}

static PyObject*
rect_move_ip (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    int x, y;

    if (!TwoIntsFromObj (args, &x, &y))
        return RAISE (PyExc_TypeError, "argument must contain two numbers");

    self->r.x += x;
    self->r.y += y;
    Py_RETURN_NONE;
}

static PyObject*
rect_inflate (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    int x, y;

    if (!TwoIntsFromObj (args, &x, &y))
        return RAISE (PyExc_TypeError, "argument must contain two numbers");

    return PyRect_New4 (self->r.x - x / 2, self->r.y - y / 2,
                        self->r.w + x, self->r.h + y);
}

static PyObject*
rect_inflate_ip (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    int x, y;

    if (!TwoIntsFromObj (args, &x, &y))
        return RAISE (PyExc_TypeError, "argument must contain two numbers");

    self->r.x -= x / 2;
    self->r.y -= y / 2;
    self->r.w += x;
    self->r.h += y;
    Py_RETURN_NONE;
}

static PyObject*
rect_union (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int x, y, w, h;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    x = MIN (self->r.x, argrect->x);
    y = MIN (self->r.y, argrect->y);
    w = MAX (self->r.x + self->r.w, argrect->x + argrect->w) - x;
    h = MAX (self->r.y + self->r.h, argrect->y + argrect->h) - y;
    return PyRect_New4 (x, y, w, h);
}

static PyObject*
rect_union_ip (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int x, y, w, h;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    x = MIN (self->r.x, argrect->x);
    y = MIN (self->r.y, argrect->y);
    w = MAX (self->r.x + self->r.w, argrect->x + argrect->w) - x;
    h = MAX (self->r.y + self->r.h, argrect->y + argrect->h) - y;
    self->r.x = x;
    self->r.y = y;
    self->r.w = w;
    self->r.h = h;
    Py_RETURN_NONE;
}

static PyObject*
rect_unionall (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int loop, size;
    PyObject* list, *obj;
    int t, l, b, r;

    if (!PyArg_ParseTuple (args, "O", &list))
        return NULL;
    if (!PySequence_Check (list))
        return RAISE (PyExc_TypeError,
                      "Argument must be a sequence of rectstyle objects.");

    l = self->r.x;
    t = self->r.y;
    r = self->r.x + self->r.w;
    b = self->r.y + self->r.h;
    size = PySequence_Length (list); /*warning, size could be -1 on error?*/
    if (size < 1)
        return PyRect_New4 (l, t, r-l, b-t);

    for (loop = 0; loop < size; ++loop)
    {
        obj = PySequence_GetItem (list, loop);
        if(!obj || !(argrect = GameRect_FromObject (obj, &temp)))
        {
            RAISE (PyExc_TypeError,
                   "Argument must be a sequence of rectstyle objects.");
            Py_XDECREF (obj);
            break;
        }
        l = MIN (l, argrect->x);
        t = MIN (t, argrect->y);
        r = MAX (r, argrect->x + argrect->w);
        b = MAX (b, argrect->y + argrect->h);
        Py_DECREF (obj);
    }
    return PyRect_New4 (l, t, r-l, b-t);
}

static PyObject*
rect_unionall_ip (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int loop, size;
    PyObject* list, *obj;
    int t, l, b, r;

    if (!PyArg_ParseTuple (args, "O", &list))
        return NULL;
    if (!PySequence_Check (list))
        return RAISE (PyExc_TypeError,
                      "Argument must be a sequence of rectstyle objects.");

    l = self->r.x;
    t = self->r.y;
    r = self->r.x + self->r.w;
    b = self->r.y + self->r.h;

    size = PySequence_Length (list); /*warning, size could be -1 on error?*/
    if (size < 1)
        return PyRect_New4 (l, t, r-l, b-t);

    for (loop = 0; loop < size; ++loop)
    {
        obj = PySequence_GetItem (list, loop);
        if (!obj || !(argrect = GameRect_FromObject (obj, &temp)))
        {
            RAISE (PyExc_TypeError,
                   "Argument must be a sequence of rectstyle objects.");
            Py_XDECREF (obj);
            break;
        }
        l = MIN (l, argrect->x);
        t = MIN (t, argrect->y);
        r = MAX (r, argrect->x + argrect->w);
        b = MAX (b, argrect->y + argrect->h);
        Py_DECREF (obj);
    }

    self->r.x = l;
    self->r.y = t;
    self->r.w = r - l;
    self->r.h = b - t;
    Py_RETURN_NONE;
}

static PyObject*
rect_collidepoint (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    int x, y;
    int inside;

    if (!TwoIntsFromObj (args, &x, &y))
        return RAISE (PyExc_TypeError, "argument must contain two numbers");

    inside = x >= self->r.x && x < self->r.x + self->r.w &&
        y >= self->r.y && y < self->r.y + self->r.h;

    return PyInt_FromLong (inside);
}

static PyObject*
rect_colliderect (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    return PyInt_FromLong (DoRectsIntersect (&self->r, argrect));
}

static PyObject*
rect_collidelist (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int loop, size;
    PyObject* list, *obj;
    PyObject* ret = NULL;

    if (!PyArg_ParseTuple (args, "O", &list))
        return NULL;

    if (!PySequence_Check (list))
        return RAISE (PyExc_TypeError,
                      "Argument must be a sequence of rectstyle objects.");

    size = PySequence_Length (list); /*warning, size could be -1 on error?*/
    for (loop = 0; loop < size; ++loop)
    {
        obj = PySequence_GetItem (list, loop);
        if (!obj || !(argrect = GameRect_FromObject (obj, &temp)))
        {
            RAISE (PyExc_TypeError,
                   "Argument must be a sequence of rectstyle objects.");
            Py_XDECREF (obj);
            break;
        }
        if (DoRectsIntersect (&self->r, argrect))
        {
            ret = PyInt_FromLong (loop);
            Py_DECREF (obj);
            break;
        }
        Py_DECREF (obj);
    }
    if (loop == size)
        ret = PyInt_FromLong (-1);

    return ret;
}

static PyObject*
rect_collidelistall (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int loop, size;
    PyObject* list, *obj;
    PyObject* ret = NULL;

    if (!PyArg_ParseTuple (args, "O", &list))
        return NULL;

    if (!PySequence_Check (list))
        return RAISE (PyExc_TypeError,
                      "Argument must be a sequence of rectstyle objects.");

    ret = PyList_New (0);
    if (!ret)
        return NULL;

    size = PySequence_Length (list); /*warning, size could be -1?*/
    for (loop = 0; loop < size; ++loop)
    {
        obj = PySequence_GetItem (list, loop);

        if(!obj || !(argrect = GameRect_FromObject (obj, &temp)))
        {
            Py_XDECREF (obj);
            Py_DECREF (ret);
            return RAISE (PyExc_TypeError,
                          "Argument must be a sequence of rectstyle objects.");
        }

        if (DoRectsIntersect (&self->r, argrect))
        {
            PyObject* num = PyInt_FromLong (loop);
            if (!num)
            {
                Py_DECREF (obj);
                return NULL;
            }
            PyList_Append (ret, num);
            Py_DECREF (num);
        }
        Py_DECREF (obj);
    }

    return ret;
}

static PyObject*
rect_collidedict (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    Py_ssize_t loop=0;
    Py_ssize_t values=0;
    PyObject* dict, *key, *val;
    PyObject* ret = NULL;

    if (!PyArg_ParseTuple (args, "O|i", &dict, &values))
        return NULL;
    if (!PyDict_Check (dict))
        return RAISE (PyExc_TypeError,
                      "Argument must be a dict with rectstyle keys.");

    while (PyDict_Next (dict, &loop, &key, &val))
    {
        if(values) {
            if (!(argrect = GameRect_FromObject (val, &temp)))
            {
                RAISE (PyExc_TypeError,
                       "Argument must be a dict with rectstyle values.");
                break;
            }
        } else {
            if (!(argrect = GameRect_FromObject (key, &temp)))
            {
                RAISE (PyExc_TypeError,
                       "Argument must be a dict with rectstyle keys.");
                break;
            }
        }


        if (DoRectsIntersect (&self->r, argrect))
        {
            ret = Py_BuildValue ("(OO)", key, val);
            break;
        }
    }

    if (!ret)
        Py_RETURN_NONE;
    return ret;
}

static PyObject*
rect_collidedictall (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    Py_ssize_t loop=0;
    /* should we use values or keys? */
    Py_ssize_t values=0;

    PyObject* dict, *key, *val;
    PyObject* ret = NULL;

    if (!PyArg_ParseTuple (args, "O|i", &dict, &values))
        return NULL;
    if (!PyDict_Check (dict))
        return RAISE (PyExc_TypeError,
                      "Argument must be a dict with rectstyle keys.");

    ret = PyList_New (0);
    if(!ret)
        return NULL;

    while (PyDict_Next (dict, &loop, &key, &val))
    {
        if (values) {
            if (!(argrect = GameRect_FromObject (val, &temp)))
            {
                Py_DECREF (ret);
                return RAISE (PyExc_TypeError,
                              "Argument must be a dict with rectstyle values.");
            }
        } else {
            if (!(argrect = GameRect_FromObject (key, &temp)))
            {
                Py_DECREF (ret);
                return RAISE (PyExc_TypeError,
                              "Argument must be a dict with rectstyle keys.");
            }
        }

        if (DoRectsIntersect (&self->r, argrect))
        {
            PyObject* num = Py_BuildValue ("(OO)", key, val);
            if(!num)
                return NULL;
            PyList_Append (ret, num);
            Py_DECREF (num);
        }
    }

    return ret;
}

static PyObject*
rect_clip (PyObject* self, PyObject* args)
{
    GAME_Rect *A, *B, temp;
    int x, y, w, h;

    A = &((PyRectObject*) self)->r;
    if (!(B = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    /* Left */
    if ((A->x >= B->x) && (A->x < (B->x + B->w)))
        x = A->x;
    else if ((B->x >= A->x) && (B->x < (A->x + A->w)))
        x = B->x;
    else
        goto nointersect;

    /* Right */
    if (((A->x + A->w) > B->x) && ((A->x + A->w) <= (B->x + B->w)))
        w = (A->x + A->w) - x;
    else if (((B->x + B->w) > A->x) && ((B->x + B->w) <= (A->x + A->w)))
        w = (B->x + B->w) - x;
    else
        goto nointersect;

    /* Top */
    if ((A->y >= B->y) && (A->y < (B->y + B->h)))
        y = A->y;
    else if ((B->y >= A->y) && (B->y < (A->y + A->h)))
        y = B->y;
    else
        goto nointersect;

    /* Bottom */
    if (((A->y + A->h) > B->y) && ((A->y + A->h) <= (B->y + B->h)))
        h = (A->y + A->h) - y;
    else if (((B->y + B->h) > A->y) && ((B->y + B->h) <= (A->y + A->h)))
        h = (B->y + B->h) - y;
    else
        goto nointersect;

    return PyRect_New4 (x, y, w, h);

nointersect:
    return PyRect_New4 (A->x, A->y, 0, 0);
}

static PyObject*
rect_contains (PyObject* oself, PyObject* args)
{
    int contained;
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    contained = (self->r.x <= argrect->x) && (self->r.y <= argrect->y) &&
        (self->r.x + self->r.w >= argrect->x + argrect->w) &&
        (self->r.y + self->r.h >= argrect->y + argrect->h) &&
        (self->r.x + self->r.w > argrect->x) &&
        (self->r.y + self->r.h > argrect->y);

    return PyInt_FromLong (contained);
}

static PyObject*
rect_clamp (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int x, y;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    if (self->r.w >= argrect->w)
        x = argrect->x + argrect->w / 2 - self->r.w / 2;
    else if (self->r.x < argrect->x)
        x = argrect->x;
    else if (self->r.x + self->r.w > argrect->x + argrect->w)
        x = argrect->x + argrect->w - self->r.w;
    else
        x = self->r.x;

    if (self->r.h >= argrect->h)
        y = argrect->y + argrect->h / 2 - self->r.h / 2;
    else if (self->r.y < argrect->y)
        y = argrect->y;
    else if (self->r.y + self->r.h > argrect->y + argrect->h)
        y = argrect->y + argrect->h - self->r.h;
    else
        y = self->r.y;

    return PyRect_New4 (x, y, self->r.w, self->r.h);
}

static PyObject*
rect_fit (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int w, h, x, y;
    float xratio, yratio, maxratio;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    xratio = (float) self->r.w / (float) argrect->w;
    yratio = (float) self->r.h / (float) argrect->h;
    maxratio = (xratio > yratio) ? xratio : yratio;

    w = (int) (self->r.w / maxratio);
    h = (int) (self->r.h / maxratio);

    x = argrect->x + (argrect->w - w)/2;
    y = argrect->y + (argrect->h - h)/2;

    return PyRect_New4 (x, y, w, h);
}

static PyObject*
rect_clamp_ip (PyObject* oself, PyObject* args)
{
    PyRectObject* self = (PyRectObject*)oself;
    GAME_Rect *argrect, temp;
    int x, y;
    if (!(argrect = GameRect_FromObject (args, &temp)))
        return RAISE (PyExc_TypeError, "Argument must be rect style object");

    if (self->r.w >= argrect->w)
        x = argrect->x + argrect->w / 2 - self->r.w / 2;
    else if (self->r.x < argrect->x)
        x = argrect->x;
    else if (self->r.x + self->r.w > argrect->x + argrect->w)
        x = argrect->x + argrect->w - self->r.w;
    else
        x = self->r.x;

    if (self->r.h >= argrect->h)
        y = argrect->y + argrect->h / 2 - self->r.h / 2;
    else if (self->r.y < argrect->y)
        y = argrect->y;
    else if (self->r.y + self->r.h > argrect->y + argrect->h)
        y = argrect->y + argrect->h - self->r.h;
    else
        y = self->r.y;

    self->r.x = x;
    self->r.y = y;
    Py_RETURN_NONE;
}

/* for pickling */
static PyObject*
rect_reduce (PyObject* oself)
{
    PyRectObject* self = (PyRectObject*)oself;
    return Py_BuildValue ("(O(iiii))", oself->ob_type,
                          (int)self->r.x, (int)self->r.y,
                          (int)self->r.w, (int)self->r.h);
}

/* for copy module */
static PyObject*
rect_copy (PyObject* oself)
{
    PyRectObject* self = (PyRectObject*)oself;
    return PyRect_New4 (self->r.x, self->r.y, self->r.w, self->r.h);
}

static struct PyMethodDef rect_methods[] =
{
    { "normalize", (PyCFunction) rect_normalize, METH_NOARGS,
      DOC_RECTNORMALIZE },
    { "clip", rect_clip, METH_VARARGS, DOC_RECTCLIP},
    { "clamp", rect_clamp, METH_VARARGS, DOC_RECTCLAMP},
    { "clamp_ip", rect_clamp_ip, METH_VARARGS, DOC_RECTCLAMPIP},
    { "copy", (PyCFunction) rect_copy, METH_NOARGS, DOC_RECTCOPY},
    { "fit", rect_fit, METH_VARARGS, DOC_RECTFIT},
    { "move", rect_move, METH_VARARGS, DOC_RECTMOVE},
    { "inflate",  rect_inflate, METH_VARARGS, DOC_RECTINFLATE},
    { "union",  rect_union, METH_VARARGS, DOC_RECTUNION},
    { "unionall",  rect_unionall, METH_VARARGS, DOC_RECTUNIONALL},
    { "move_ip",  rect_move_ip, METH_VARARGS, DOC_RECTMOVEIP},
    { "inflate_ip", rect_inflate_ip, METH_VARARGS, DOC_RECTINFLATEIP},
    { "union_ip", rect_union_ip, METH_VARARGS, DOC_RECTUNIONIP},
    { "unionall_ip", rect_unionall_ip, METH_VARARGS, DOC_RECTUNIONALLIP},
    { "collidepoint", rect_collidepoint, METH_VARARGS, DOC_RECTCOLLIDEPOINT},
    { "colliderect", rect_colliderect, METH_VARARGS, DOC_RECTCOLLIDERECT},
    { "collidelist", rect_collidelist, METH_VARARGS, DOC_RECTCOLLIDELIST},
    { "collidelistall", rect_collidelistall, METH_VARARGS,
      DOC_RECTCOLLIDELISTALL},
    { "collidedict", rect_collidedict, METH_VARARGS, DOC_RECTCOLLIDEDICT},
    { "collidedictall", rect_collidedictall, METH_VARARGS,
      DOC_RECTCOLLIDEDICTALL},
    { "contains", rect_contains, METH_VARARGS, DOC_RECTCONTAINS},
    { "__reduce__", (PyCFunction) rect_reduce, METH_NOARGS, NULL},
    { "__copy__", (PyCFunction) rect_copy, METH_NOARGS, NULL},
    { NULL, NULL, 0, NULL }
};

/* sequence functions */

static Py_ssize_t
rect_length (PyObject *_self)
{
    return 4;
}

static PyObject*
rect_item (PyObject *_self, Py_ssize_t i)
{
    PyRectObject *self = (PyRectObject *) _self;
    int* data = (int*)&self->r;
    if (i < 0 || i > 3)
        return RAISE (PyExc_IndexError, "Invalid rect Index");

    return PyInt_FromLong (data[i]);
}

static int
rect_ass_item (PyObject *_self, Py_ssize_t i, PyObject *v)
{
    PyRectObject *self = (PyRectObject *) _self;
    int val;
    int* data = (int*)&self->r;
    if (i < 0 || i > 3)
    {
        RAISE (PyExc_IndexError, "Invalid rect Index");
        return -1;
    }
    if (!IntFromObj (v, &val))
    {
        RAISE (PyExc_TypeError, "Must assign numeric values");
        return -1;
    }
    data[i] = val;
    return 0;
}

static PyObject*
rect_slice (PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh)
{
    PyRectObject *self = (PyRectObject *) _self;
    PyObject *list;
    int* data = (int*)&self->r;
    int numitems, loop, l = 4;
    
    if (ihigh < 0)
        ihigh += l;
    if (ilow < 0)
        ilow += l;

    if (ilow < 0)
        ilow = 0;
    else if (ilow > l)
        ilow = l;

    if (ihigh < 0)
        ihigh = 0;
    else if (ihigh > l)
        ihigh = l;

    if (ihigh < ilow)
        ihigh = ilow;
    
    numitems = ihigh - ilow;
    list = PyList_New (numitems);
    for (loop = 0; loop < numitems; ++loop)
        PyList_SET_ITEM (list, loop, PyInt_FromLong (data[loop + ilow]));
    
    return list;
}

static int
rect_ass_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
    PyRectObject *self = (PyRectObject *) _self;
    int* data = (int*)&self->r;
    int numitems, loop, l = 4;
    int val;
    
    if (!PySequence_Check (v))
    {
        RAISE (PyExc_TypeError, "Assigned slice must be a sequence");
        return -1;
    }
    
    if (ihigh < 0)
        ihigh += l;
    if (ilow  < 0)
        ilow  += l;
    if (ilow < 0)
        ilow = 0;
    else if (ilow > l)
        ilow = l;

    if (ihigh < 0)
        ihigh = 0;
    else if (ihigh > l)
        ihigh = l;

    if (ihigh < ilow)
        ihigh = ilow;
    
    numitems = ihigh - ilow;
    if (numitems != PySequence_Length (v))
    {
        RAISE (PyExc_ValueError, "Assigned slice must be same length");
        return -1;
    }
    
    for (loop = 0; loop < numitems; ++loop)
    {
        if (!IntFromObjIndex (v, loop, &val))
            return -1;
        data[loop + ilow] = val;
    }
    
    return 0;
}

static PySequenceMethods rect_as_sequence =
{
    rect_length,    /*length*/
    NULL,           /*concat*/
    NULL,           /*repeat*/
    rect_item,      /*item*/
    rect_slice,     /*slice*/
    rect_ass_item,  /*ass_item*/
    rect_ass_slice, /*ass_slice*/
};

/* numeric functions */
static int
rect_nonzero (PyRectObject *self)
{
    return self->r.w != 0 && self->r.h != 0;
}

#if !PY3
static int
rect_coerce (PyObject** o1, PyObject** o2)
{
    PyObject* new1;
    PyObject* new2;
    GAME_Rect* r, temp;

    if (PyRect_Check (*o1))
    {
        new1 = *o1;
        Py_INCREF (new1);
    }
    else if ((r = GameRect_FromObject (*o1, &temp)))
        new1 = PyRect_New4 (r->x, r->y, r->w, r->h);
    else
        return 1;

    if (PyRect_Check (*o2))
    {
        new2 = *o2;
        Py_INCREF (new2);
    }
    else if ((r = GameRect_FromObject (*o2, &temp)))
        new2 = PyRect_New4 (r->x, r->y, r->w, r->h);
    else
    {
        Py_DECREF (new1);
        return 1;
    }

    *o1 = new1;
    *o2 = new2;
    return 0;
}
#endif

static PyNumberMethods rect_as_number =
{
    (binaryfunc)NULL,		/*add*/
    (binaryfunc)NULL,		/*subtract*/
    (binaryfunc)NULL,		/*multiply*/
#if !PY3
    (binaryfunc)NULL,		/*divide*/
#endif
    (binaryfunc)NULL,		/*remainder*/
    (binaryfunc)NULL,		/*divmod*/
    (ternaryfunc)NULL,		/*power*/
    (unaryfunc)NULL,		/*negative*/
    (unaryfunc)NULL,		/*pos*/
    (unaryfunc)NULL,		/*abs*/
    (inquiry)rect_nonzero,	/*nonzero / bool*/
    (unaryfunc)NULL,		/*invert*/
    (binaryfunc)NULL,		/*lshift*/
    (binaryfunc)NULL,		/*rshift*/
    (binaryfunc)NULL,		/*and*/
    (binaryfunc)NULL,		/*xor*/
    (binaryfunc)NULL,		/*or*/
#if !PY3
    (coercion)rect_coerce,	/*coerce*/
#endif
    (unaryfunc)NULL,		/*int*/
#if !PY3
    (unaryfunc)NULL,		/*long*/
#endif
    (unaryfunc)NULL,		/*float*/
};

/* object type functions */
static void
rect_dealloc (PyRectObject *self)
{
    if (self->weakreflist)
        PyObject_ClearWeakRefs ((PyObject*)self);
    Py_TYPE(self)->tp_free ((PyObject*)self);
}

static PyObject*
rect_repr (PyRectObject *self)
{
    char string[256];
    sprintf (string, "<rect(%d, %d, %d, %d)>", self->r.x, self->r.y,
             self->r.w, self->r.h);
    return Text_FromUTF8 (string);
}

static PyObject*
rect_str (PyRectObject *self)
{
    return rect_repr (self);
}

static PyObject*
rect_richcompare(PyObject *o1, PyObject *o2, int opid)
{
    GAME_Rect *o1rect, *o2rect, temp1, temp2;
    int cmp;

    o1rect = GameRect_FromObject (o1, &temp1);
    if (!o1rect) {
        goto Unimplemented;
    }
    o2rect = GameRect_FromObject (o2, &temp2);
    if (!o2rect)
    {
        goto Unimplemented;
    }

    if (o1rect->x != o2rect->x) {
        cmp = o1rect->x < o2rect->x ? -1 : 1;
    }
    else if (o1rect->y != o2rect->y) {
        cmp = o1rect->y < o2rect->y ? -1 : 1;
    }
    else if (o1rect->w != o2rect->w) {
        cmp = o1rect->w < o2rect->w ? -1 : 1;
    }
    else if (o1rect->h != o2rect->h) {
        cmp = o1rect->h < o2rect->h ? -1 : 1;
    }
    else {
        cmp = 0;
    }

    switch (opid)
    {
    case Py_LT:
        return PyBool_FromLong (cmp < 0);
    case Py_LE:
        return PyBool_FromLong (cmp <= 0);
    case Py_EQ:
        return PyBool_FromLong (cmp == 0);
    case Py_NE:
        return PyBool_FromLong (cmp != 0);
    case Py_GT:
        return PyBool_FromLong (cmp > 0);
    case Py_GE:
        return PyBool_FromLong (cmp >= 0);
    default:
        break;
    }

Unimplemented:
    Py_INCREF (Py_NotImplemented);
    return Py_NotImplemented;
}

/*width*/
static PyObject*
rect_getwidth (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.w);
}

static int
rect_setwidth (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
        return -1;
    self->r.w = val1;
    return 0;
}

/*height*/
static PyObject*
rect_getheight (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.h);
}

static int
rect_setheight (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.h = val1;
    return 0;
}

/*top*/
static PyObject*
rect_gettop (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.y);
}

static int
rect_settop (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.y = val1;
    return 0;
}

/*left*/
static PyObject*
rect_getleft (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.x);
}

static int
rect_setleft (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1;
    return 0;
}

/*right*/
static PyObject*
rect_getright (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.x + self->r.w);
}

static int
rect_setright (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1-self->r.w;
    return 0;
}

/*bottom*/
static PyObject*
rect_getbottom (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.y + self->r.h);
}

static int
rect_setbottom (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.y = val1-self->r.h;
    return 0;
}

/*centerx*/
static PyObject*
rect_getcenterx (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.x + (self->r.w >> 1));
}

static int
rect_setcenterx (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1 - (self->r.w >> 1);
    return 0;
}

/*centery*/
static PyObject*
rect_getcentery (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.y + (self->r.h >> 1));
}

static int
rect_setcentery (PyRectObject *self, PyObject* value, void *closure)
{
    int val1;
    if (!IntFromObj (value, &val1))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.y = val1 - (self->r.h >> 1);
    return 0;
}

/*topleft*/
static PyObject*
rect_gettopleft (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x, self->r.y);
}

static int
rect_settopleft (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1;
    self->r.y = val2;
    return 0;
}

/*topright*/
static PyObject*
rect_gettopright (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x+self->r.w, self->r.y);
}

static int
rect_settopright (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1-self->r.w;
    self->r.y = val2;
    return 0;
}

/*bottomleft*/
static PyObject*
rect_getbottomleft (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x, self->r.y+self->r.h);
}

static int
rect_setbottomleft (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1;
    self->r.y = val2-self->r.h;
    return 0;
}

/*bottomright*/
static PyObject*
rect_getbottomright (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x+self->r.w, self->r.y+self->r.h);
}

static int
rect_setbottomright (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1-self->r.w;
    self->r.y = val2-self->r.h;
    return 0;
}

/*midtop*/
static PyObject*
rect_getmidtop (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x + (self->r.w >> 1), self->r.y);
}

static int
rect_setmidtop (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x += val1 - (self->r.x + (self->r.w >> 1));
    self->r.y = val2;
    return 0;
}

/*midleft*/
static PyObject*
rect_getmidleft (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x, self->r.y+(self->r.h>>1));
}

static int
rect_setmidleft (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1;
    self->r.y += val2 - (self->r.y + (self->r.h >> 1));
    return 0;
}

/*midbottom*/
static PyObject*
rect_getmidbottom (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x + (self->r.w >> 1),
                          self->r.y + self->r.h);
}

static int
rect_setmidbottom (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x += val1 - (self->r.x + (self->r.w >> 1));
    self->r.y = val2 - self->r.h;
    return 0;
}

/*midright*/
static PyObject*
rect_getmidright (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x + self->r.w,
                          self->r.y + (self->r.h >> 1));
}

static int
rect_setmidright (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x = val1 - self->r.w;
    self->r.y += val2 -(self->r.y + (self->r.h >> 1));
    return 0;
}

/*center*/
static PyObject*
rect_getcenter (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.x + (self->r.w >> 1),
                          self->r.y + (self->r.h >> 1));
}

static int
rect_setcenter (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.x += val1 - (self->r.x + (self->r.w >> 1));
    self->r.y += val2 - (self->r.y + (self->r.h >> 1));
    return 0;
}

/*size*/
static PyObject*
rect_getsize (PyRectObject *self, void *closure)
{
    return Py_BuildValue ("(ii)", self->r.w, self->r.h);
}

static int
rect_setsize (PyRectObject *self, PyObject* value, void *closure)
{
    int val1, val2;
    if (!TwoIntsFromObj (value, &val1, &val2))
    {
        RAISE (PyExc_TypeError, "invalid rect assignment");
        return -1;
    }
    self->r.w = val1;
    self->r.h = val2;
    return 0;
}

static PyObject*
rect_getsafepickle (PyRectObject *self, void *closure)
{
    Py_RETURN_TRUE;
}

static PyGetSetDef rect_getsets[] = {
    { "x", (getter)rect_getleft, (setter)rect_setleft, NULL, NULL },
    { "y", (getter)rect_gettop, (setter)rect_settop, NULL, NULL },
    { "w", (getter)rect_getwidth, (setter)rect_setwidth, NULL, NULL },
    { "h", (getter)rect_getheight, (setter)rect_setheight, NULL, NULL },
    { "width", (getter)rect_getwidth, (setter)rect_setwidth, NULL, NULL },
    { "height", (getter)rect_getheight, (setter)rect_setheight, NULL, NULL },
    { "top", (getter)rect_gettop, (setter)rect_settop, NULL, NULL },
    { "left", (getter)rect_getleft, (setter)rect_setleft, NULL, NULL },
    { "bottom", (getter)rect_getbottom, (setter)rect_setbottom, NULL, NULL },
    { "right", (getter)rect_getright, (setter)rect_setright, NULL, NULL },
    { "centerx", (getter)rect_getcenterx, (setter)rect_setcenterx, NULL, NULL },
    { "centery", (getter)rect_getcentery, (setter)rect_setcentery, NULL, NULL },
    { "topleft", (getter)rect_gettopleft, (setter)rect_settopleft, NULL, NULL },
    { "topright", (getter)rect_gettopright, (setter)rect_settopright, NULL,
     NULL },
    { "bottomleft", (getter)rect_getbottomleft, (setter)rect_setbottomleft,
      NULL, NULL },
    { "bottomright", (getter)rect_getbottomright, (setter)rect_setbottomright,
      NULL, NULL },
    { "midtop", (getter)rect_getmidtop, (setter)rect_setmidtop, NULL, NULL },
    { "midleft", (getter)rect_getmidleft, (setter)rect_setmidleft, NULL, NULL },
    { "midbottom", (getter)rect_getmidbottom, (setter)rect_setmidbottom, NULL,
      NULL },
    { "midright", (getter)rect_getmidright, (setter)rect_setmidright, NULL,
      NULL },
    { "size", (getter)rect_getsize, (setter)rect_setsize, NULL, NULL },
    { "center", (getter)rect_getcenter, (setter)rect_setcenter, NULL, NULL },

    { "__safe_for_unpickling__", (getter)rect_getsafepickle, NULL, NULL, NULL },
    { NULL, 0, NULL, NULL, NULL }  /* Sentinel */
};

static PyTypeObject PyRect_Type =
{
    TYPE_HEAD (NULL, 0)
    "pygame.Rect", 				/*name*/
    sizeof(PyRectObject),		        /*basicsize*/
    0,					/*itemsize*/
    /* methods */
    (destructor)rect_dealloc,	        /*dealloc*/
    (printfunc)NULL,			/*print*/
    NULL,	                                /*getattr*/
    NULL,	                                /*setattr*/
    NULL,                               /*compare/reserved*/
    (reprfunc)rect_repr,		        /*repr*/
    &rect_as_number,			/*as_number*/
    &rect_as_sequence,			/*as_sequence*/
    NULL,					/*as_mapping*/
    (hashfunc)NULL, 			/*hash*/
    (ternaryfunc)NULL,			/*call*/
    (reprfunc)rect_str,			/*str*/

    /* Space for future expansion */
    0L,0L,0L,
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    DOC_PYGAMERECT,                        /* Documentation string */
    0,					/* tp_traverse */
    0,					/* tp_clear */
    (richcmpfunc)rect_richcompare,			/* tp_richcompare */
    offsetof(PyRectObject, weakreflist),    /* tp_weaklistoffset */
    0,					/* tp_iter */
    0,					/* tp_iternext */
    rect_methods,			        /* tp_methods */
    0,				        /* tp_members */
    rect_getsets,				/* tp_getset */
    0,					/* tp_base */
    0,					/* tp_dict */
    0,					/* tp_descr_get */
    0,					/* tp_descr_set */
    0,					/* tp_dictoffset */
    (initproc)rect_init,			/* tp_init */
    0,					/* tp_alloc */
    rect_new,			        /* tp_new */
};

static PyObject*
rect_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyRectObject *self;
    self = (PyRectObject *)type->tp_alloc (type, 0);
    if (self)
    {
        self->r.x = self->r.y = 0;
        self->r.w = self->r.h = 0;
        self->weakreflist = NULL;
    }
    return (PyObject*)self;
}

static int
rect_init (PyRectObject *self, PyObject *args, PyObject *kwds)
{
    GAME_Rect *argrect, temp;
    if (!(argrect = GameRect_FromObject (args, &temp)))
    {
        RAISE (PyExc_TypeError, "Argument must be rect style object");
        return -1;
    }

    self->r.x = argrect->x;
    self->r.y = argrect->y;
    self->r.w = argrect->w;
    self->r.h = argrect->h;
    return 0;
}

static PyMethodDef _rect_methods[] =
{
    {NULL, NULL, 0, NULL}
};

/*DOC*/ static char _rectangle_doc[] =
/*DOC*/    "Module for the rectangle object\n";

MODINIT_DEFINE (rect)
{
    PyObject *module, *dict, *apiobj;
    int ecode;
    static void* c_api[PYGAMEAPI_RECT_NUMSLOTS];
    
#if PY3
    static struct PyModuleDef _module = {
        PyModuleDef_HEAD_INIT,
        "rect",
        _rectangle_doc,
        -1,
        _rect_methods,
        NULL, NULL, NULL, NULL
    };
#endif

    /* imported needed apis; Do this first so if there is an error
       the module is not loaded.
    */
    import_pygame_base ();
    if (PyErr_Occurred ()) {
        MODINIT_ERROR;
    }

    /* Create the module and add the functions */
    if (PyType_Ready (&PyRect_Type) < 0) {
        MODINIT_ERROR;
    }

#if PY3
    module = PyModule_Create (&_module);
#else
    module = Py_InitModule3 (MODPREFIX "rect", _rect_methods, _rectangle_doc);
#endif
    if (module == NULL) {
        MODINIT_ERROR;
    }
    dict = PyModule_GetDict (module);

    if (PyDict_SetItemString (dict, "RectType", (PyObject *)&PyRect_Type)) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    if (PyDict_SetItemString (dict, "Rect", (PyObject *)&PyRect_Type)) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }

    /* export the c api */
    c_api[0] = &PyRect_Type;
    c_api[1] = PyRect_New;
    c_api[2] = PyRect_New4;
    c_api[3] = GameRect_FromObject;
    apiobj = PyCObject_FromVoidPtr (c_api, NULL);
    if (apiobj == NULL) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
    Py_DECREF (apiobj);
    if (ecode) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    MODINIT_RETURN (module);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.