⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 puzzle.c

📁 VLC Player Source Code
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** * 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 *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_plugin.h>#include <vlc_vout.h>#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 * );static int PuzzleCallback( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );/***************************************************************************** * 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")#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.")#define CFG_PREFIX "puzzle-"vlc_module_begin();    set_description( N_("Puzzle interactive game video filter") );    set_shortname( N_( "Puzzle" ));    set_capability( "video filter", 0 );    set_category( CAT_VIDEO );    set_subcategory( SUBCAT_VIDEO_VFILTER );    add_integer_with_range( CFG_PREFIX "rows", 4, 1, 128, NULL,                            ROWS_TEXT, ROWS_LONGTEXT, false );    add_integer_with_range( CFG_PREFIX "cols", 4, 1, 128, NULL,                            COLS_TEXT, COLS_LONGTEXT, false );    add_bool( CFG_PREFIX "black-slot", 0, NULL,              BLACKSLOT_TEXT, BLACKSLOT_LONGTEXT, false );    set_callbacks( Create, Destroy );vlc_module_end();static const char *const ppsz_filter_options[] = {    "rows", "cols", "black-slot", NULL};/***************************************************************************** * 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;    bool b_finished;    bool b_blackslot;};/***************************************************************************** * 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... *****************************************************************************/static bool finished( vout_sys_t *p_sys ){    int i;    for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )    {        if( i != p_sys->pi_order[i] ) return false;    }    return true;}static bool is_valid( vout_sys_t *p_sys ){    int i, j, d=0;    if( p_sys->b_blackslot == false ) return true;    for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )    {        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++;        }    }    if( d%2!=0 ) return false;    else return true;}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 );    } while(    p_sys->b_finished == true             || is_valid( p_sys ) == false );    if( p_sys->b_blackslot == true )    {        for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )        {            if( p_sys->pi_order[i] ==                p_sys->i_cols * p_sys->i_rows - 1 )            {                p_sys->i_selected = i;                break;            }        }    }    else    {        p_sys->i_selected = -1;    }}/***************************************************************************** * 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 );    config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,                       p_vout->p_cfg );    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" );    p_vout->p_sys->b_blackslot =        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 );    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;    video_format_t fmt;    memset( &fmt, 0, sizeof( video_format_t ) );    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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -