]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/puzzle.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / video_filter / puzzle.c
index 6ab8a1e31dc81482cbe4975c1795854df1233881..b50fd49d195b8ed3b0f5b64ef7f152f342793a18 100644 (file)
@@ -33,6 +33,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_filter.h>
+#include <vlc_rand.h>
 
 #include "filter_picture.h"
 
@@ -97,6 +98,12 @@ struct filter_sys_t
     /* */
     vlc_mutex_t lock;
     bool b_change;
+    struct
+    {
+        int i_cols;
+        int i_rows;
+        bool b_blackslot;
+    } change;
 };
 
 #define SHUFFLE_WIDTH 81
@@ -145,9 +152,12 @@ static int Open( vlc_object_t *p_this )
     p_sys->pi_order = NULL;
 
     vlc_mutex_init( &p_sys->lock );
-    p_sys->i_rows = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "rows" );
-    p_sys->i_cols = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "cols" );
-    p_sys->b_blackslot = var_CreateGetBoolCommand( p_filter, CFG_PREFIX "black-slot" );
+    p_sys->change.i_rows =
+        var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "rows" );
+    p_sys->change.i_cols =
+        var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "cols" );
+    p_sys->change.b_blackslot =
+        var_CreateGetBoolCommand( p_filter, CFG_PREFIX "black-slot" );
     p_sys->b_change = true;
 
     var_AddCallback( p_filter, CFG_PREFIX "rows", PuzzleCallback, p_sys );
@@ -155,7 +165,7 @@ static int Open( vlc_object_t *p_this )
     var_AddCallback( p_filter, CFG_PREFIX "black-slot", PuzzleCallback, p_sys );
 
     p_filter->pf_video_filter = Filter;
-    p_filter->pf_mouse = Mouse;
+    p_filter->pf_video_mouse = Mouse;
 
     return VLC_SUCCESS;
 }
@@ -196,7 +206,11 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     vlc_mutex_lock( &p_sys->lock );
     if( p_sys->b_change )
     {
+        p_sys->i_rows      = p_sys->change.i_rows;
+        p_sys->i_cols      = p_sys->change.i_cols;
+        p_sys->b_blackslot = p_sys->change.b_blackslot;
         p_sys->b_change = false;
+
         Shuffle( p_sys );
     }
     vlc_mutex_unlock( &p_sys->lock );
@@ -209,65 +223,53 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     for( int i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
     {
         const plane_t *p_in = &p_pic->p[i_plane];
-        const int i_pitch = p_in->i_pitch;
         plane_t *p_out = &p_outpic->p[i_plane];
 
         for( int i = 0; i < i_cols * i_rows; i++ )
         {
-            int i_col = i % i_cols;
-            int i_row = i / i_cols;
-            int i_ocol = p_sys->pi_order[i] % i_cols;
-            int i_orow = p_sys->pi_order[i] / i_cols;
-            int i_last_row = i_row + 1;
-            i_orow *= p_in->i_lines / i_rows;
-            i_row *= p_in->i_lines / i_rows;
-            i_last_row *= p_in->i_lines / i_rows;
+            int i_piece_height = p_out->i_visible_lines / i_rows;
+            int i_piece_width  = p_out->i_visible_pitch / i_cols;
+
+            int i_col = (i % i_cols) * i_piece_width;
+            int i_row = (i / i_cols) * i_piece_height;
+            int i_last_row = i_row + i_piece_height;
+
+            int i_ocol = (p_sys->pi_order[i] % i_cols) * i_piece_width;
+            int i_orow = (p_sys->pi_order[i] / i_cols) * i_piece_height;
 
             if( p_sys->b_blackslot && !p_sys->b_finished && i == p_sys->i_selected )
             {
                 uint8_t color = ( i_plane == Y_PLANE ? 0x0 : 0x80 );
-                for( ; i_row < i_last_row; i_row++, i_orow++ )
+                for( int r = i_row; r < i_last_row; r++ )
                 {
-                    memset( p_out->p_pixels + i_row * i_pitch + i_col * i_pitch / i_cols,
-                            color, i_pitch / i_cols );
+                    memset( p_out->p_pixels + r * p_out->i_pitch + i_col,
+                            color, i_piece_width );
                 }
             }
             else
             {
-                for( ; i_row < i_last_row; i_row++, i_orow++ )
+                for( int r = i_row, or = i_orow; r < i_last_row; r++, or++ )
                 {
-                    memcpy( p_out->p_pixels + i_row * i_pitch + i_col * i_pitch / i_cols,
-                            p_in->p_pixels + i_orow * i_pitch + i_ocol * i_pitch / i_cols,
-                            i_pitch / i_cols );
+                    memcpy( p_out->p_pixels + r * p_out->i_pitch + i_col,
+                            p_in->p_pixels + or * p_in->i_pitch  + i_ocol,
+                            i_piece_width );
                 }
             }
-        }
-    }
-
-    /* Draw the borders of the selected slot */
-    if( p_sys->i_selected != -1 && !p_sys->b_blackslot )
-    {
-        const plane_t *p_in = &p_pic->p[Y_PLANE];
-        const int i_pitch = p_in->i_pitch;
-        plane_t *p_out = &p_outpic->p[Y_PLANE];
 
-        int i_col = p_sys->i_selected % i_cols;
-        int i_row = p_sys->i_selected / i_cols;
-        int i_last_row = i_row + 1;
-        i_row *= p_in->i_lines / i_rows;
-        i_last_row *= p_in->i_lines / i_rows;
-        memset( p_out->p_pixels + i_row * i_pitch + i_col * i_pitch / i_cols,
-                0xff, i_pitch / i_cols );
-        for( ; i_row < i_last_row; i_row++ )
-        {
-            p_out->p_pixels[   i_row * i_pitch
-                             + i_col * i_pitch / i_cols ] = 0xff;
-            p_out->p_pixels[ i_row * i_pitch
-                             + (i_col+1) * i_pitch / i_cols - 1 ] = 0xff;
+            /* Draw the borders of the selected slot */
+            if( i_plane == 0 && !p_sys->b_blackslot && p_sys->i_selected == i )
+            {
+                memset( p_out->p_pixels + i_row * p_out->i_pitch + i_col,
+                        0xff, i_piece_width );
+                for( int r = i_row; r < i_last_row; r++ )
+                {
+                    p_out->p_pixels[r * p_out->i_pitch + i_col + 0             + 0 ] = 0xff;
+                    p_out->p_pixels[r * p_out->i_pitch + i_col + i_piece_width - 1 ] = 0xff;
+                }
+                memset( p_out->p_pixels + (i_last_row - 1) * p_out->i_pitch + i_col,
+                        0xff, i_piece_width );
+            }
         }
-        i_row--;
-        memset( p_out->p_pixels + i_row * i_pitch + i_col * i_pitch / i_cols,
-                0xff, i_pitch / i_cols );
     }
 
     /* Draw the 'Shuffle' button if the puzzle is finished */
@@ -362,15 +364,15 @@ static int PuzzleCallback( vlc_object_t *p_this, char const *psz_var,
     vlc_mutex_lock( &p_sys->lock );
     if( !strcmp( psz_var, CFG_PREFIX "rows" ) )
     {
-        p_sys->i_rows = __MAX( 1, newval.i_int );
+        p_sys->change.i_rows = __MAX( 1, newval.i_int );
     }
     else if( !strcmp( psz_var, CFG_PREFIX "cols" ) )
     {
-        p_sys->i_cols = __MAX( 1, newval.i_int );
+        p_sys->change.i_cols = __MAX( 1, newval.i_int );
     }
     else if( !strcmp( psz_var, CFG_PREFIX "black-slot" ) )
     {
-        p_sys->b_blackslot = newval.b_bool;
+        p_sys->change.b_blackslot = newval.b_bool;
     }
     p_sys->b_change = true;
     vlc_mutex_unlock( &p_sys->lock );
@@ -392,7 +394,7 @@ static bool IsValid( filter_sys_t *p_sys )
 {
     const int i_count = p_sys->i_cols * p_sys->i_rows;
 
-    if( p_sys->b_blackslot )
+    if( !p_sys->b_blackslot )
         return true;
 
     int d = 0;
@@ -416,29 +418,29 @@ static bool IsValid( filter_sys_t *p_sys )
 
 static void Shuffle( filter_sys_t *p_sys )
 {
-    const int i_count = p_sys->i_cols * p_sys->i_rows;
+    const unsigned i_count = p_sys->i_cols * p_sys->i_rows;
 
     free( p_sys->pi_order );
 
     p_sys->pi_order = calloc( i_count, sizeof(*p_sys->pi_order) );
     do
     {
-        for( int i = 0; i < i_count; i++ )
+        for( unsigned i = 0; i < i_count; i++ )
             p_sys->pi_order[i] = -1;
 
-        for( int c = 0; c < i_count; )
+        for( unsigned c = 0; c < i_count; )
         {
-            int i = rand() % i_count;
+            unsigned i = ((unsigned)vlc_mrand48()) % i_count;
             if( p_sys->pi_order[i] == -1 )
                 p_sys->pi_order[i] = c++;
         }
         p_sys->b_finished = IsFinished( p_sys );
 
-    } while( p_sys->b_finished || IsValid( p_sys ) );
+    } while( p_sys->b_finished || !IsValid( p_sys ) );
 
     if( p_sys->b_blackslot )
     {
-        for( int i = 0; i < i_count; i++ )
+        for( unsigned i = 0; i < i_count; i++ )
         {
             if( p_sys->pi_order[i] == i_count - 1 )
             {