@@ -2169,6 +2169,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
2169
2169
int x_dest = 0 , y_dest = 0 ; /* Default destination coordinates. */
2170
2170
Uint8 dflt_setcolor [] = {255 , 255 , 255 , 255 }; /* Default set color. */
2171
2171
Uint8 dflt_unsetcolor [] = {0 , 0 , 0 , 255 }; /* Default unset color. */
2172
+ int create_area_bitmask = 0 ;
2172
2173
2173
2174
static char * keywords [] = {"surface" , "setsurface" , "unsetsurface" ,
2174
2175
"setcolor" , "unsetcolor" , "dest" ,
@@ -2210,6 +2211,28 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
2210
2211
PyErr_SetString (PyExc_TypeError , "invalid rectstyle argument" );
2211
2212
goto to_surface_error ;
2212
2213
}
2214
+
2215
+ memcpy (& temp_rect , area_rect , sizeof (temp_rect ));
2216
+ area_rect = & temp_rect ;
2217
+
2218
+ pgRect_Normalize (area_rect );
2219
+
2220
+ if (area_rect -> x < 0 ) {
2221
+ // x_dest -= area_rect->x;
2222
+ area_rect -> w += area_rect -> x ;
2223
+ area_rect -> x = 0 ;
2224
+ }
2225
+ if (area_rect -> y < 0 ) {
2226
+ // y_dest -= area_rect->y;
2227
+ area_rect -> h += area_rect -> y ;
2228
+ area_rect -> y = 0 ;
2229
+ }
2230
+
2231
+ // clamp rect width and height to not stick out of the mask
2232
+ area_rect -> w = MAX (MIN (area_rect -> w , bitmask -> w - area_rect -> x ), 0 );
2233
+ area_rect -> h = MAX (MIN (area_rect -> h , bitmask -> h - area_rect -> y ), 0 );
2234
+
2235
+ create_area_bitmask = area_rect -> w > 0 && area_rect -> h > 0 ;
2213
2236
}
2214
2237
else {
2215
2238
temp_rect .x = temp_rect .y = 0 ;
@@ -2218,22 +2241,6 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
2218
2241
area_rect = & temp_rect ;
2219
2242
}
2220
2243
2221
- if (area_rect -> x < 0 ) {
2222
- // x_dest -= area_rect->x;
2223
- area_rect -> w += area_rect -> x ;
2224
- area_rect -> x = 0 ;
2225
- }
2226
- if (area_rect -> y < 0 ) {
2227
- // y_dest -= area_rect->y;
2228
- area_rect -> h += area_rect -> y ;
2229
- area_rect -> y = 0 ;
2230
- }
2231
-
2232
- // clamp rect width and height to not stick out of the mask
2233
- area_rect -> w = MAX (MIN (area_rect -> w , bitmask -> w - area_rect -> x ), 0 );
2234
- area_rect -> h = MAX (MIN (area_rect -> h , bitmask -> h - area_rect -> y ), 0 );
2235
- // pgRect_Normalize(area_rect);
2236
-
2237
2244
if (Py_None == surfobj ) {
2238
2245
surfobj = PyObject_CallFunction ((PyObject * )& pgSurface_Type , "(ii)ii" ,
2239
2246
area_rect -> w , area_rect -> h ,
@@ -2341,8 +2348,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
2341
2348
goto to_surface_error ;
2342
2349
}
2343
2350
2344
- if (areaobj ) {
2345
- assert (area_rect -> w >= 0 && area_rect -> w >= 0 );
2351
+ if (create_area_bitmask ) {
2346
2352
area_bitmask = bitmask_create (area_rect -> w , area_rect -> h );
2347
2353
if (NULL == area_bitmask ) {
2348
2354
PyErr_Format (PyExc_MemoryError , "failed to allocate memory for a mask" );
@@ -2359,6 +2365,35 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
2359
2365
2360
2366
bitmask_overlap_mask (bitmask , overlap_bitmask , area_bitmask ,
2361
2367
area_rect -> x , area_rect -> y );
2368
+
2369
+ printf ("%i,%i\n" , area_rect -> x , area_rect -> y );
2370
+ printf ("%ix%i\n" , bitmask -> w , bitmask -> h );
2371
+ for (int y = 0 ; y < bitmask -> h ; ++ y ) {
2372
+ for (int x = 0 ; x < bitmask -> w ; ++ x ) {
2373
+ int bit_value = (bitmask -> bits [y ] >> x ) & 1 ;
2374
+ printf ("%d " , bit_value );
2375
+ }
2376
+ printf ("\n" );
2377
+ }
2378
+ printf ("\n" );
2379
+ printf ("overlap\n" );
2380
+ for (int y = 0 ; y < overlap_bitmask -> h ; ++ y ) {
2381
+ for (int x = 0 ; x < overlap_bitmask -> w ; ++ x ) {
2382
+ int bit_value = (overlap_bitmask -> bits [y ] >> x ) & 1 ;
2383
+ printf ("%d " , bit_value );
2384
+ }
2385
+ printf ("\n" );
2386
+ }
2387
+ printf ("\n%ix%i\n" , area_rect -> w , area_rect -> h );
2388
+ for (int y = 0 ; y < area_bitmask -> h ; ++ y ) {
2389
+ for (int x = 0 ; x < area_bitmask -> w ; ++ x ) {
2390
+ int bit_value = (area_bitmask -> bits [y ] >> x ) & 1 ;
2391
+ printf ("%d " , bit_value );
2392
+ }
2393
+ printf ("\n" );
2394
+ }
2395
+ printf ("\n" );
2396
+
2362
2397
bitmask_free (overlap_bitmask );
2363
2398
}
2364
2399
else {
@@ -2373,7 +2408,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
2373
2408
2374
2409
Py_END_ALLOW_THREADS ; /* Obtain the GIL. */
2375
2410
2376
- if (areaobj ) {
2411
+ if (create_area_bitmask ) {
2377
2412
bitmask_free (area_bitmask );
2378
2413
}
2379
2414
0 commit comments