]> git.sesse.net Git - vlc/blob - modules/video_filter/puzzle.c
Add --puzzle-black-slot option to change puzzle type
[vlc] / modules / video_filter / puzzle.c
1 /*****************************************************************************
2  * puzzle.c : Puzzle game
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include <math.h>
34
35 #include "filter_common.h"
36 #include "vlc_image.h"
37 #include "vlc_input.h"
38 #include "vlc_playlist.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static int  Init      ( vout_thread_t * );
47 static void End       ( vout_thread_t * );
48 static void Render    ( vout_thread_t *, picture_t * );
49
50 static int  SendEvents   ( vlc_object_t *, char const *,
51                            vlc_value_t, vlc_value_t, void * );
52 static int  MouseEvent   ( vlc_object_t *, char const *,
53                            vlc_value_t, vlc_value_t, void * );
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58
59 #define ROWS_TEXT N_("Number of puzzle rows")
60 #define ROWS_LONGTEXT N_("Number of puzzle rows")
61 #define COLS_TEXT N_("Number of puzzle columns")
62 #define COLS_LONGTEXT N_("Number of puzzle columns")
63 #define BLACKSLOT_TEXT N_("Make one tile a black slot")
64 #define BLACKSLOT_LONGTEXT N_("Make one slot black. Other tiles can only be swapped with the black slot.")
65
66 vlc_module_begin();
67     set_description( _("Puzzle interactive game video filter") );
68     set_shortname( _( "Puzzle" ));
69     set_capability( "video filter", 0 );
70     set_category( CAT_VIDEO );
71     set_subcategory( SUBCAT_VIDEO_VFILTER );
72
73     add_integer_with_range( "puzzle-rows", 4, 1, 128, NULL,
74                             ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
75     add_integer_with_range( "puzzle-cols", 4, 1, 128, NULL,
76                             COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
77     add_bool( "puzzle-black-slot", 0, NULL,
78               BLACKSLOT_TEXT, BLACKSLOT_LONGTEXT, VLC_FALSE );
79
80     set_callbacks( Create, Destroy );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * vout_sys_t: Magnify video output method descriptor
85  *****************************************************************************/
86 struct vout_sys_t
87 {
88     vout_thread_t *p_vout;
89
90     image_handler_t *p_image;
91
92     int i_cols;
93     int i_rows;
94     int *pi_order;
95     int i_selected;
96     vlc_bool_t b_finished;
97
98     vlc_bool_t b_blackslot;
99 };
100
101 /*****************************************************************************
102  * Control: control facility for the vout (forwards to child vout)
103  *****************************************************************************/
104 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
105 {
106     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
107 }
108
109 /*****************************************************************************
110  * Misc stuff...
111  *****************************************************************************/
112 static vlc_bool_t finished( vout_sys_t *p_sys )
113 {
114     int i;
115     for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
116     {
117         if( i != p_sys->pi_order[i] ) return VLC_FALSE;
118     }
119     return VLC_TRUE;
120 }
121 static void shuffle( vout_sys_t *p_sys )
122 {
123     int i, c;
124     free( p_sys->pi_order );
125     p_sys->pi_order = malloc( p_sys->i_cols * p_sys->i_rows * sizeof( int ) );
126     do
127     {
128         for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
129         {
130             p_sys->pi_order[i] = -1;
131         }
132         i = 0;
133         for( c = 0; c < p_sys->i_cols * p_sys->i_rows; )
134         {
135             i = rand()%( p_sys->i_cols * p_sys->i_rows );
136             if( p_sys->pi_order[i] == -1 )
137             {
138                 p_sys->pi_order[i] = c;
139                 c++;
140             }
141         }
142         p_sys->b_finished = finished( p_sys );
143     } while( p_sys->b_finished == VLC_TRUE );
144
145     if( p_sys->b_blackslot == VLC_TRUE )
146     {
147         for( i = 0; i < p_sys->i_cols * p_sys->i_rows; i++ )
148         {
149             if( p_sys->pi_order[i] ==
150                 ( p_sys->i_cols - 1 ) * p_sys->i_rows + 1 )
151             {
152                 p_sys->i_selected = i;
153                 break;
154             }
155         }
156     }
157     else
158     {
159         p_sys->i_selected = -1;
160     }
161     printf( "selected: %d\n", p_sys->i_selected );
162 }
163
164 /*****************************************************************************
165  * Create: allocates Magnify video thread output method
166  *****************************************************************************/
167 static int Create( vlc_object_t *p_this )
168 {
169     vout_thread_t *p_vout = (vout_thread_t *)p_this;
170
171     /* Allocate structure */
172     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
173     if( p_vout->p_sys == NULL )
174     {
175         msg_Err( p_vout, "out of memory" );
176         return VLC_ENOMEM;
177     }
178
179     p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
180
181     p_vout->p_sys->i_rows = config_GetInt( p_vout, "puzzle-rows" );
182     p_vout->p_sys->i_cols = config_GetInt( p_vout, "puzzle-cols" );
183     p_vout->p_sys->b_blackslot = config_GetInt( p_vout, "puzzle-black-slot" );
184
185     p_vout->p_sys->pi_order = NULL;
186     shuffle( p_vout->p_sys );
187
188     p_vout->pf_init = Init;
189     p_vout->pf_end = End;
190     p_vout->pf_manage = NULL;
191     p_vout->pf_render = Render;
192     p_vout->pf_display = NULL;
193     p_vout->pf_control = Control;
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * Init: initialize Magnify video thread output method
200  *****************************************************************************/
201 static int Init( vout_thread_t *p_vout )
202 {
203     int i_index;
204     picture_t *p_pic;
205     video_format_t fmt = {0};
206
207     I_OUTPUTPICTURES = 0;
208
209     /* Initialize the output structure */
210     p_vout->output.i_chroma = p_vout->render.i_chroma;
211     p_vout->output.i_width  = p_vout->render.i_width;
212     p_vout->output.i_height = p_vout->render.i_height;
213     p_vout->output.i_aspect = p_vout->render.i_aspect;
214
215     p_vout->fmt_out = p_vout->fmt_in;
216     fmt = p_vout->fmt_out;
217
218     /* Try to open the real video output */
219     msg_Dbg( p_vout, "spawning the real video output" );
220
221     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
222
223     /* Everything failed */
224     if( p_vout->p_sys->p_vout == NULL )
225     {
226         msg_Err( p_vout, "cannot open vout, aborting" );
227         return VLC_EGENERIC;
228     }
229
230     var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout );
231     var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout );
232     var_AddCallback( p_vout->p_sys->p_vout, "mouse-clicked",
233                      MouseEvent, p_vout);
234
235     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
236     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
237     ADD_PARENT_CALLBACKS( SendEventsToChild );
238
239     return VLC_SUCCESS;
240 }
241
242 /*****************************************************************************
243  * End: terminate Magnify video thread output method
244  *****************************************************************************/
245 static void End( vout_thread_t *p_vout )
246 {
247     int i_index;
248
249     /* Free the fake output buffers we allocated */
250     for( i_index = I_OUTPUTPICTURES ; i_index ; )
251     {
252         i_index--;
253         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
254     }
255
256     var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
257     var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
258     var_DelCallback( p_vout->p_sys->p_vout, "mouse-clicked", MouseEvent, p_vout);
259 }
260
261 /*****************************************************************************
262  * Destroy: destroy Magnify video thread output method
263  *****************************************************************************/
264 static void Destroy( vlc_object_t *p_this )
265 {
266     vout_thread_t *p_vout = (vout_thread_t *)p_this;
267
268     if( p_vout->p_sys->p_vout )
269     {
270         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
271         vlc_object_detach( p_vout->p_sys->p_vout );
272         vout_Destroy( p_vout->p_sys->p_vout );
273     }
274
275     image_HandlerDelete( p_vout->p_sys->p_image );
276     free( p_vout->p_sys->pi_order );
277
278     DEL_PARENT_CALLBACKS( SendEventsToChild );
279
280     free( p_vout->p_sys );
281 }
282
283 /*****************************************************************************
284  * Render: displays previously rendered output
285  *****************************************************************************/
286 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
287 {
288     picture_t *p_outpic;
289
290     //video_format_t fmt_out = {0};
291     //picture_t *p_converted;
292
293     int i_plane;
294
295     int i_rows = p_vout->p_sys->i_rows;
296     int i_cols = p_vout->p_sys->i_cols;
297
298     /* This is a new frame. Get a structure from the video_output. */
299     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
300               == NULL )
301     {
302         if( p_vout->b_die || p_vout->b_error )
303         {
304             return;
305         }
306         msleep( VOUT_OUTMEM_SLEEP );
307     }
308
309     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
310
311     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
312     {
313         plane_t *p_in = p_pic->p+i_plane;
314         plane_t *p_out = p_outpic->p+i_plane;
315         int i_pitch = p_in->i_pitch;
316         int i;
317
318         for( i = 0; i < i_cols * i_rows; i++ )
319         {
320             int i_col = i % i_cols;
321             int i_row = i / i_cols;
322             int i_ocol = p_vout->p_sys->pi_order[i] % i_cols;
323             int i_orow = p_vout->p_sys->pi_order[i] / i_cols;
324             int i_last_row = i_row + 1;
325             i_orow *= p_in->i_lines / i_rows;
326             i_row *= p_in->i_lines / i_rows;
327             i_last_row *= p_in->i_lines / i_rows;
328
329             if( p_vout->p_sys->b_blackslot == VLC_TRUE
330                 && i == p_vout->p_sys->i_selected )
331             {
332                 uint8_t color = ( i_plane == Y_PLANE ? 0x0 : 0x80 );
333                 for( ; i_row < i_last_row; i_row++, i_orow++ )
334                 {
335                     memset( p_out->p_pixels + i_row * i_pitch
336                                             + i_col * i_pitch / i_cols,
337                             color, i_pitch / i_cols );
338                 }
339             }
340             else
341             {
342                 for( ; i_row < i_last_row; i_row++, i_orow++ )
343                 {
344                     memcpy( p_out->p_pixels + i_row * i_pitch
345                                             + i_col * i_pitch / i_cols,
346                             p_in->p_pixels + i_orow * i_pitch
347                                            + i_ocol * i_pitch / i_cols,
348                             i_pitch / i_cols );
349                 }
350             }
351         }
352     }
353
354     if(    p_vout->p_sys->i_selected != -1
355         && p_vout->p_sys->b_blackslot == VLC_FALSE )
356     {
357         plane_t *p_in = p_pic->p+Y_PLANE;
358         plane_t *p_out = p_outpic->p+Y_PLANE;
359         int i_pitch = p_in->i_pitch;
360         int i_col = p_vout->p_sys->i_selected % i_cols;
361         int i_row = p_vout->p_sys->i_selected / i_cols;
362         int i_last_row = i_row + 1;
363         i_row *= p_in->i_lines / i_rows;
364         i_last_row *= p_in->i_lines / i_rows;
365         memset( p_out->p_pixels + i_row * i_pitch
366                                 + i_col * i_pitch / i_cols,
367                 0xff, i_pitch / i_cols );
368         for( ; i_row < i_last_row; i_row++ )
369         {
370             p_out->p_pixels[   i_row * i_pitch
371                              + i_col * i_pitch / i_cols ] = 0xff;
372             p_out->p_pixels[ i_row * i_pitch
373                              + (i_col+1) * i_pitch / i_cols - 1 ] = 0xff;
374         }
375         i_row--;
376         memset( p_out->p_pixels + i_row * i_pitch
377                                 + i_col * i_pitch / i_cols,
378                 0xff, i_pitch / i_cols );
379     }
380
381     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
382 }
383
384 /*****************************************************************************
385  * SendEvents: forward mouse and keyboard events to the parent p_vout
386  *****************************************************************************/
387 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
388                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
389 {
390     var_Set( (vlc_object_t *)p_data, psz_var, newval );
391
392     return VLC_SUCCESS;
393 }
394
395 /*****************************************************************************
396  * SendEventsToChild: forward events to the child/children vout
397  *****************************************************************************/
398 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
399                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
400 {
401     vout_thread_t *p_vout = (vout_thread_t *)p_this;
402     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
403     return VLC_SUCCESS;
404 }
405
406 /*****************************************************************************
407  * MouseEvent: callback for mouse events
408  *****************************************************************************/
409 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
410                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
411 {
412     vout_thread_t *p_vout = (vout_thread_t*)p_data;
413     int i_x, i_y;
414     int i_v;
415
416 #define MOUSE_DOWN    1
417 #define MOUSE_CLICKED 2
418 #define MOUSE_MOVE_X  4
419 #define MOUSE_MOVE_Y  8
420 #define MOUSE_MOVE    12
421     uint8_t mouse= 0;
422
423     int v_h = p_vout->output.i_height;
424     int v_w = p_vout->output.i_width;
425     int i_pos;
426
427     if( psz_var[6] == 'x' ) mouse |= MOUSE_MOVE_X;
428     if( psz_var[6] == 'y' ) mouse |= MOUSE_MOVE_Y;
429     if( psz_var[6] == 'c' ) mouse |= MOUSE_CLICKED;
430
431     i_v = var_GetInteger( p_vout->p_sys->p_vout, "mouse-button-down" );
432     if( i_v & 0x1 ) mouse |= MOUSE_DOWN;
433     i_y = var_GetInteger( p_vout->p_sys->p_vout, "mouse-y" );
434     i_x = var_GetInteger( p_vout->p_sys->p_vout, "mouse-x" );
435
436     if( mouse & MOUSE_CLICKED )
437     {
438         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;
439         if( p_vout->p_sys->b_finished == VLC_TRUE )
440         {
441             shuffle( p_vout->p_sys );
442         }
443         else if( p_vout->p_sys->i_selected == -1 )
444         {
445             p_vout->p_sys->i_selected = i_pos;
446         }
447         else if( p_vout->p_sys->i_selected == i_pos
448                  && p_vout->p_sys->b_blackslot == VLC_FALSE )
449         {
450             p_vout->p_sys->i_selected = -1;
451         }
452         else if(    p_vout->p_sys->i_selected == i_pos + 1
453                  || p_vout->p_sys->i_selected == i_pos - 1
454                  || p_vout->p_sys->i_selected == i_pos + p_vout->p_sys->i_cols
455                  || p_vout->p_sys->i_selected == i_pos - p_vout->p_sys->i_cols )
456         {
457             int a = p_vout->p_sys->pi_order[ p_vout->p_sys->i_selected ];
458             p_vout->p_sys->pi_order[ p_vout->p_sys->i_selected ] =
459                 p_vout->p_sys->pi_order[ i_pos ];
460             p_vout->p_sys->pi_order[ i_pos ] = a;
461             if( p_vout->p_sys->b_blackslot == VLC_TRUE )
462                 p_vout->p_sys->i_selected = i_pos;
463             else
464                 p_vout->p_sys->i_selected = -1;
465
466             p_vout->p_sys->b_finished = finished( p_vout->p_sys );
467         }
468     }
469     return VLC_SUCCESS;
470 }