puzzle.c 20.2 KB
Newer Older
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
/*****************************************************************************
 * puzzle.c : Puzzle game
 *****************************************************************************
 * Copyright (C) 2005-2006 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_vout.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#include <math.h>

#include "filter_common.h"
#include "vlc_image.h"
#include "vlc_input.h"
#include "vlc_playlist.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static void Render    ( vout_thread_t *, picture_t * );

static int  SendEvents   ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static int  MouseEvent   ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );

58 59 60
static int PuzzleCallback( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );

61 62 63 64 65 66 67 68
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

#define ROWS_TEXT N_("Number of puzzle rows")
#define ROWS_LONGTEXT N_("Number of puzzle rows")
#define COLS_TEXT N_("Number of puzzle columns")
#define COLS_LONGTEXT N_("Number of puzzle columns")
69 70
#define BLACKSLOT_TEXT N_("Make one tile a black slot")
#define BLACKSLOT_LONGTEXT N_("Make one slot black. Other tiles can only be swapped with the black slot.")
71

72 73
#define CFG_PREFIX "puzzle-"

74
vlc_module_begin();
75 76
    set_description( N_("Puzzle interactive game video filter") );
    set_shortname( N_( "Puzzle" ));
77 78 79 80
    set_capability( "video filter", 0 );
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VFILTER );

81
    add_integer_with_range( CFG_PREFIX "rows", 4, 1, 128, NULL,
82
                            ROWS_TEXT, ROWS_LONGTEXT, false );
83
    add_integer_with_range( CFG_PREFIX "cols", 4, 1, 128, NULL,
84
                            COLS_TEXT, COLS_LONGTEXT, false );
85
    add_bool( CFG_PREFIX "black-slot", 0, NULL,
86
              BLACKSLOT_TEXT, BLACKSLOT_LONGTEXT, false );
87 88 89 90

    set_callbacks( Create, Destroy );
vlc_module_end();

91
static const char *const ppsz_filter_options[] = {
92 93 94
    "rows", "cols", "black-slot", NULL
};

95 96 97 98 99 100 101 102 103 104 105 106 107
/*****************************************************************************
 * vout_sys_t: Magnify video output method descriptor
 *****************************************************************************/
struct vout_sys_t
{
    vout_thread_t *p_vout;

    image_handler_t *p_image;

    int i_cols;
    int i_rows;
    int *pi_order;
    int i_selected;
108
    bool b_finished;
109

110
    bool b_blackslot;
111 112 113 114 115 116 117 118 119 120 121 122 123
};

/*****************************************************************************
 * Control: control facility for the vout (forwards to child vout)
 *****************************************************************************/
static int Control( vout_thread_t *p_vout, int i_query, va_list args )
{
    return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
}

/*****************************************************************************
 * Misc stuff...
 *****************************************************************************/
124
static bool finished( vout_sys_t *p_sys )
125 126 127 128
{
    int i;
    for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
    {
129
        if( i != p_sys->pi_order[i] ) return false;
130
    }
131
    return true;
132
}
133
static bool is_valid( vout_sys_t *p_sys )
134
{
135
    int i, j, d=0;
136
    if( p_sys->b_blackslot == false ) return true;
137 138
    for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
    {
139 140 141 142 143 144 145 146 147 148 149
        if( p_sys->pi_order[i] == p_sys->i_cols * p_sys->i_rows - 1 )
        {
            d += i / p_sys->i_cols + 1;
            continue;
        }
        for( j = i+1; j < p_sys->i_cols * p_sys->i_rows; j++ )
        {
            if( p_sys->pi_order[j] == p_sys->i_cols * p_sys->i_rows - 1 )
                continue;
            if( p_sys->pi_order[i] > p_sys->pi_order[j] ) d++;
        }
150
    }
151 152
    if( d%2!=0 ) return false;
    else return true;
153
}
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void shuffle( vout_sys_t *p_sys )
{
    int i, c;
    free( p_sys->pi_order );
    p_sys->pi_order = malloc( p_sys->i_cols * p_sys->i_rows * sizeof( int ) );
    do
    {
        for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
        {
            p_sys->pi_order[i] = -1;
        }
        i = 0;
        for( c = 0; c < p_sys->i_cols * p_sys->i_rows; )
        {
            i = rand()%( p_sys->i_cols * p_sys->i_rows );
            if( p_sys->pi_order[i] == -1 )
            {
                p_sys->pi_order[i] = c;
                c++;
            }
        }
        p_sys->b_finished = finished( p_sys );
176 177
    } while(    p_sys->b_finished == true
             || is_valid( p_sys ) == false );
178

179
    if( p_sys->b_blackslot == true )
180 181 182 183
    {
        for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
        {
            if( p_sys->pi_order[i] ==
184
                p_sys->i_cols * p_sys->i_rows - 1 )
185 186 187 188 189 190 191 192 193 194
            {
                p_sys->i_selected = i;
                break;
            }
        }
    }
    else
    {
        p_sys->i_selected = -1;
    }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
}

/*****************************************************************************
 * Create: allocates Magnify video thread output method
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
        return VLC_ENOMEM;

    p_vout->p_sys->p_image = image_HandlerCreate( p_vout );

211 212 213
    config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
                       p_vout->p_cfg );

214 215 216 217
    p_vout->p_sys->i_rows =
        var_CreateGetIntegerCommand( p_vout, CFG_PREFIX "rows" );
    p_vout->p_sys->i_cols =
        var_CreateGetIntegerCommand( p_vout, CFG_PREFIX "cols" );
218
    p_vout->p_sys->b_blackslot =
219 220 221 222 223 224 225
        var_CreateGetBoolCommand( p_vout, CFG_PREFIX "black-slot" );
    var_AddCallback( p_vout, CFG_PREFIX "rows",
                     PuzzleCallback, p_vout->p_sys );
    var_AddCallback( p_vout, CFG_PREFIX "cols",
                     PuzzleCallback, p_vout->p_sys );
    var_AddCallback( p_vout, CFG_PREFIX "black-slot",
                     PuzzleCallback, p_vout->p_sys );
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    p_vout->p_sys->pi_order = NULL;
    shuffle( p_vout->p_sys );

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = NULL;
    p_vout->pf_render = Render;
    p_vout->pf_display = NULL;
    p_vout->pf_control = Control;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Init: initialize Magnify video thread output method
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic;
247 248
    video_format_t fmt;
    memset( &fmt, 0, sizeof( video_format_t ) );
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

    I_OUTPUTPICTURES = 0;

    /* Initialize the output structure */
    p_vout->output.i_chroma = p_vout->render.i_chroma;
    p_vout->output.i_width  = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->render.i_aspect;

    p_vout->fmt_out = p_vout->fmt_in;
    fmt = p_vout->fmt_out;

    /* Try to open the real video output */
    msg_Dbg( p_vout, "spawning the real video output" );

    p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );

    /* Everything failed */
    if( p_vout->p_sys->p_vout == NULL )
    {
        msg_Err( p_vout, "cannot open vout, aborting" );
        return VLC_EGENERIC;
    }

    var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout );
    var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout );
    var_AddCallback( p_vout->p_sys->p_vout, "mouse-clicked",
                     MouseEvent, p_vout);

    ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
    ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
    ADD_PARENT_CALLBACKS( SendEventsToChild );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * End: terminate Magnify video thread output method
 *****************************************************************************/
static void End( vout_thread_t *p_vout )
{
    int i_index;

292 293 294 295
    DEL_PARENT_CALLBACKS( SendEventsToChild );

    DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );

296 297 298 299 300 301 302 303 304 305
    /* Free the fake output buffers we allocated */
    for( i_index = I_OUTPUTPICTURES ; i_index ; )
    {
        i_index--;
        free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
    }

    var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
    var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
    var_DelCallback( p_vout->p_sys->p_vout, "mouse-clicked", MouseEvent, p_vout);
306

307
    vout_CloseAndRelease( p_vout->p_sys->p_vout );
308 309
}

310 311
#define SHUFFLE_WIDTH 81
#define SHUFFLE_HEIGHT 13
312
static const char *shuffle_button[] =
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
/*****************************************************************************
 * Destroy: destroy Magnify video thread output method
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    image_HandlerDelete( p_vout->p_sys->p_image );
    free( p_vout->p_sys->pi_order );

    free( p_vout->p_sys );
}

/*****************************************************************************
 * Render: displays previously rendered output
 *****************************************************************************/
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    picture_t *p_outpic;

349 350
    //video_format_t fmt_out;
    // memset( &fmt_out, 0, sizeof(video_format_t) );
351 352 353 354 355 356 357 358 359 360 361
    //picture_t *p_converted;

    int i_plane;

    int i_rows = p_vout->p_sys->i_rows;
    int i_cols = p_vout->p_sys->i_cols;

    /* This is a new frame. Get a structure from the video_output. */
    while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
              == NULL )
    {
362
        if( !vlc_object_alive (p_vout) || p_vout->b_error )
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
        {
            return;
        }
        msleep( VOUT_OUTMEM_SLEEP );
    }

    vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );

    for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
    {
        plane_t *p_in = p_pic->p+i_plane;
        plane_t *p_out = p_outpic->p+i_plane;
        int i_pitch = p_in->i_pitch;
        int i;

        for( i = 0; i < i_cols * i_rows; i++ )
        {
            int i_col = i % i_cols;
            int i_row = i / i_cols;
            int i_ocol = p_vout->p_sys->pi_order[i] % i_cols;
            int i_orow = p_vout->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;

389 390
            if( p_vout->p_sys->b_blackslot == true
                && p_vout->p_sys->b_finished == false
391
                && i == p_vout->p_sys->i_selected )
392
            {
393 394 395
                uint8_t color = ( i_plane == Y_PLANE ? 0x0 : 0x80 );
                for( ; i_row < i_last_row; i_row++, i_orow++ )
                {
396
                    vlc_memset( p_out->p_pixels + i_row * i_pitch
397 398
                                               + i_col * i_pitch / i_cols,
                               color, i_pitch / i_cols );
399 400 401 402 403 404
                }
            }
            else
            {
                for( ; i_row < i_last_row; i_row++, i_orow++ )
                {
405
                    vlc_memcpy( p_out->p_pixels + i_row * i_pitch
406 407 408 409
                                               + i_col * i_pitch / i_cols,
                               p_in->p_pixels + i_orow * i_pitch
                                              + i_ocol * i_pitch / i_cols,
                               i_pitch / i_cols );
410
                }
411 412 413 414
            }
        }
    }

415
    if(    p_vout->p_sys->i_selected != -1
416
        && p_vout->p_sys->b_blackslot == false )
417 418 419 420 421 422 423 424 425
    {
        plane_t *p_in = p_pic->p+Y_PLANE;
        plane_t *p_out = p_outpic->p+Y_PLANE;
        int i_pitch = p_in->i_pitch;
        int i_col = p_vout->p_sys->i_selected % i_cols;
        int i_row = p_vout->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;
426
        vlc_memset( p_out->p_pixels + i_row * i_pitch
427 428
                                   + i_col * i_pitch / i_cols,
                   0xff, i_pitch / i_cols );
429 430 431 432 433 434 435 436
        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;
        }
        i_row--;
437
        vlc_memset( p_out->p_pixels + i_row * i_pitch
438 439
                                   + i_col * i_pitch / i_cols,
                   0xff, i_pitch / i_cols );
440 441
    }

442
    if( p_vout->p_sys->b_finished == true )
443 444 445 446 447 448 449 450 451 452 453 454 455 456
    {
        int i, j;
        plane_t *p_out = p_outpic->p+Y_PLANE;
        int i_pitch = p_out->i_pitch;
        for( i = 0; i < SHUFFLE_HEIGHT; i++ )
        {
            for( j = 0; j < SHUFFLE_WIDTH; j++ )
            {
                if( shuffle_button[i][j] == '.' )
                   p_out->p_pixels[ i * i_pitch + j ] = 0xff;
            }
        }
    }

457 458 459 460 461 462 463 464 465
    vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
}

/*****************************************************************************
 * SendEvents: forward mouse and keyboard events to the parent p_vout
 *****************************************************************************/
static int SendEvents( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
466 467
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);

468 469 470 471 472 473 474 475 476 477 478
    var_Set( (vlc_object_t *)p_data, psz_var, newval );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * SendEventsToChild: forward events to the child/children vout
 *****************************************************************************/
static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
479
    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
480 481 482 483 484 485 486 487 488 489 490
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    var_Set( p_vout->p_sys->p_vout, psz_var, newval );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * MouseEvent: callback for mouse events
 *****************************************************************************/
static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
491
    VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(newval);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    vout_thread_t *p_vout = (vout_thread_t*)p_data;
    int i_x, i_y;
    int i_v;

#define MOUSE_DOWN    1
#define MOUSE_CLICKED 2
#define MOUSE_MOVE_X  4
#define MOUSE_MOVE_Y  8
#define MOUSE_MOVE    12
    uint8_t mouse= 0;

    int v_h = p_vout->output.i_height;
    int v_w = p_vout->output.i_width;
    int i_pos;

    if( psz_var[6] == 'x' ) mouse |= MOUSE_MOVE_X;
    if( psz_var[6] == 'y' ) mouse |= MOUSE_MOVE_Y;
    if( psz_var[6] == 'c' ) mouse |= MOUSE_CLICKED;

    i_v = var_GetInteger( p_vout->p_sys->p_vout, "mouse-button-down" );
    if( i_v & 0x1 ) mouse |= MOUSE_DOWN;
    i_y = var_GetInteger( p_vout->p_sys->p_vout, "mouse-y" );
    i_x = var_GetInteger( p_vout->p_sys->p_vout, "mouse-x" );

516 517 518
    if( i_y < 0 || i_x < 0 || i_y >= v_h || i_x >= v_w )
        return VLC_SUCCESS;

519 520 521
    if( mouse & MOUSE_CLICKED )
    {
        i_pos = p_vout->p_sys->i_cols * ( ( p_vout->p_sys->i_rows * i_y ) / v_h ) + (p_vout->p_sys->i_cols * i_x ) / v_w;
522
        if( p_vout->p_sys->b_finished == true
523
            && i_x < SHUFFLE_WIDTH && i_y < SHUFFLE_HEIGHT )
524 525 526 527 528 529 530
        {
            shuffle( p_vout->p_sys );
        }
        else if( p_vout->p_sys->i_selected == -1 )
        {
            p_vout->p_sys->i_selected = i_pos;
        }
531
        else if( p_vout->p_sys->i_selected == i_pos
532
                 && p_vout->p_sys->b_blackslot == false )
533 534 535
        {
            p_vout->p_sys->i_selected = -1;
        }
536 537 538 539
        else if(    ( p_vout->p_sys->i_selected == i_pos + 1
                      && p_vout->p_sys->i_selected%p_vout->p_sys->i_cols != 0 )
                 || ( p_vout->p_sys->i_selected == i_pos - 1
                      && i_pos % p_vout->p_sys->i_cols != 0 )
540 541 542 543 544 545 546
                 || p_vout->p_sys->i_selected == i_pos + p_vout->p_sys->i_cols
                 || p_vout->p_sys->i_selected == i_pos - p_vout->p_sys->i_cols )
        {
            int a = p_vout->p_sys->pi_order[ p_vout->p_sys->i_selected ];
            p_vout->p_sys->pi_order[ p_vout->p_sys->i_selected ] =
                p_vout->p_sys->pi_order[ i_pos ];
            p_vout->p_sys->pi_order[ i_pos ] = a;
547
            if( p_vout->p_sys->b_blackslot == true )
548 549 550
                p_vout->p_sys->i_selected = i_pos;
            else
                p_vout->p_sys->i_selected = -1;
551 552 553 554 555 556

            p_vout->p_sys->b_finished = finished( p_vout->p_sys );
        }
    }
    return VLC_SUCCESS;
}
557 558 559 560 561

static int PuzzleCallback( vlc_object_t *p_this, char const *psz_var,
                           vlc_value_t oldval, vlc_value_t newval,
                           void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
562
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    vout_sys_t *p_sys = (vout_sys_t *)p_data;
    if( !strcmp( psz_var, CFG_PREFIX "rows" ) )
    {
        p_sys->i_rows = __MAX( 1, newval.i_int );
    }
    else if( !strcmp( psz_var, CFG_PREFIX "cols" ) )
    {
        p_sys->i_cols = __MAX( 1, newval.i_int );
    }
    else if( !strcmp( psz_var, CFG_PREFIX "black-slot" ) )
    {
        p_sys->b_blackslot = newval.b_bool;
    }
    shuffle( p_sys );
    return VLC_SUCCESS;
}