gstpuzzle.c 16.2 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1 2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
3 4
 *               <2003> David Schleef <ds@schleef.org>
 *               <2004> Benjamin Otte <otte@gnome.org>
Benjamin Otte's avatar
Benjamin Otte committed
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
18 19
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Benjamin Otte's avatar
Benjamin Otte committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
#include <gstvideofilter.h>
#include "gstvideoimage.h"
#include <string.h>

#define GST_TYPE_PUZZLE \
  (gst_puzzle_get_type())
#define GST_PUZZLE(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PUZZLE,GstPuzzle))
#define GST_PUZZLE_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PUZZLE,GstPuzzleClass))
#define GST_IS_PUZZLE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PUZZLE))
Stefan Kost's avatar
Stefan Kost committed
39
#define GST_IS_PUZZLE_CLASS(klass) \
Benjamin Otte's avatar
Benjamin Otte committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PUZZLE))

typedef struct _GstPuzzle GstPuzzle;
typedef struct _GstPuzzleClass GstPuzzleClass;

struct _GstPuzzle
{
  GstVideofilter videofilter;

  const GstVideoFormat *format;
  /* properties */
  guint rows;
  guint columns;
  guint tiles;
  /* state */
  guint *permutation;
  guint position;
  gboolean solved;
};

struct _GstPuzzleClass
{
  GstVideofilterClass parent_class;
};


/* GstPuzzle signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
75 76 77
  PROP_0,
  PROP_COLUMNS,
  PROP_ROWS
Benjamin Otte's avatar
Benjamin Otte committed
78 79 80 81 82 83
      /* FILL ME */
};

static void gst_puzzle_base_init (gpointer g_class);
static void gst_puzzle_class_init (gpointer g_class, gpointer class_data);
static void gst_puzzle_init (GTypeInstance * instance, gpointer g_class);
84
static void gst_puzzle_finalize (GObject * object);
Benjamin Otte's avatar
Benjamin Otte committed
85 86 87 88 89 90 91 92 93

static void gst_puzzle_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_puzzle_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static void gst_puzzle_setup (GstVideofilter * videofilter);
static void draw_puzzle (GstVideofilter * videofilter, void *destp, void *srcp);

94 95
static GstVideofilterClass *parent_class;

Benjamin Otte's avatar
Benjamin Otte committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
GType
gst_puzzle_get_type (void)
{
  static GType puzzle_type = 0;

  if (!puzzle_type) {
    static const GTypeInfo puzzle_info = {
      sizeof (GstPuzzleClass),
      gst_puzzle_base_init,
      NULL,
      gst_puzzle_class_init,
      NULL,
      NULL,
      sizeof (GstPuzzle),
      0,
      gst_puzzle_init,
    };

    puzzle_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
        "GstPuzzle", &puzzle_info, 0);
  }
  return puzzle_type;
}

static void
gst_puzzle_base_init (gpointer g_class)
{
123

Benjamin Otte's avatar
Benjamin Otte committed
124 125 126 127 128
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
  int i;
  GstVideofilterFormat *f;

129
  gst_element_class_set_static_metadata (element_class, "A simple puzzle",
130 131 132
      "Filter/Effect/Video/Games",
      "A simple puzzle, use arrow keys and space to restart/solve",
      "Benjamin Otte <otte@gnome.org>");
Benjamin Otte's avatar
Benjamin Otte committed
133 134 135 136 137 138 139 140

  for (i = 0; i < gst_video_format_count; i++) {
    f = g_new0 (GstVideofilterFormat, 1);
    f->fourcc = gst_video_format_list[i].fourcc;
    f->bpp = gst_video_format_list[i].bitspp;
    f->filter_func = draw_puzzle;
    if (gst_video_format_list[i].ext_caps) {
      f->depth = gst_video_format_list[i].depth;
141 142
      f->endianness =
          gst_video_format_list[i].bitspp < 24 ? G_BYTE_ORDER : G_BIG_ENDIAN;
Benjamin Otte's avatar
Benjamin Otte committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      f->red_mask = gst_video_format_list[i].red_mask;
      f->green_mask = gst_video_format_list[i].green_mask;
      f->blue_mask = gst_video_format_list[i].blue_mask;
    }
    gst_videofilter_class_add_format (videofilter_class, f);
  }

  gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
}

static void
gst_puzzle_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *gobject_class;
  GstVideofilterClass *videofilter_class;

  gobject_class = G_OBJECT_CLASS (g_class);
  videofilter_class = GST_VIDEOFILTER_CLASS (g_class);

162 163
  parent_class = g_type_class_peek_parent (g_class);

Benjamin Otte's avatar
Benjamin Otte committed
164 165
  gobject_class->set_property = gst_puzzle_set_property;
  gobject_class->get_property = gst_puzzle_get_property;
166
  gobject_class->finalize = gst_puzzle_finalize;
Benjamin Otte's avatar
Benjamin Otte committed
167

168
  g_object_class_install_property (gobject_class, PROP_ROWS,
Benjamin Otte's avatar
Benjamin Otte committed
169
      g_param_spec_uint ("rows", "rows", "number of rows in puzzle",
170 171
          1, G_MAXUINT, 4,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
172
  g_object_class_install_property (gobject_class, PROP_COLUMNS,
Benjamin Otte's avatar
Benjamin Otte committed
173
      g_param_spec_uint ("columns", "columns", "number of columns in puzzle",
174 175
          1, G_MAXUINT, 4,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Benjamin Otte's avatar
Benjamin Otte committed
176 177 178 179

  videofilter_class->setup = gst_puzzle_setup;
}

180 181 182 183 184 185 186 187 188 189 190
static void
gst_puzzle_finalize (GObject * object)
{
  GstPuzzle *puzzle;

  puzzle = GST_PUZZLE (object);
  g_free (puzzle->permutation);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

Benjamin Otte's avatar
Benjamin Otte committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static void G_GNUC_UNUSED
gst_puzzle_solve (GstPuzzle * puzzle)
{
  guint i;

  for (i = 0; i < puzzle->tiles; i++) {
    puzzle->permutation[i] = i;
  }
  puzzle->position = puzzle->tiles - 1;
  puzzle->solved = TRUE;
}

static gboolean
gst_puzzle_is_solved (GstPuzzle * puzzle)
{
  guint i;

  if (puzzle->position != puzzle->tiles - 1)
    return FALSE;

  for (i = 0; i < puzzle->tiles; i++) {
    if (puzzle->permutation[i] != i)
      return FALSE;
  }

  return TRUE;
}

#if 0
static void
gst_puzzle_show (GstPuzzle * puzzle)
{
  guint i;

  for (i = 0; i < puzzle->tiles; i++) {
    g_print ("%d ", puzzle->permutation[i]);
  }
  g_print ("\n");
}
#endif

232 233 234 235 236 237 238 239 240 241 242 243
static void
gst_puzzle_swap (GstPuzzle * puzzle, guint next)
{
  guint tmp;

  g_assert (next < puzzle->tiles);
  tmp = puzzle->permutation[puzzle->position];
  puzzle->permutation[puzzle->position] = puzzle->permutation[next];
  puzzle->permutation[next] = tmp;
  puzzle->position = next;
}

Benjamin Otte's avatar
Benjamin Otte committed
244 245 246 247 248 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
typedef enum
{
  DIR_UP,
  DIR_DOWN,
  DIR_LEFT,
  DIR_RIGHT
} GstPuzzleDirection;

static void
gst_puzzle_move (GstPuzzle * puzzle, GstPuzzleDirection dir)
{
  guint next = puzzle->tiles;

  switch (dir) {
    case DIR_UP:
      if (puzzle->position >= puzzle->columns)
        next = puzzle->position - puzzle->columns;
      break;
    case DIR_DOWN:
      if (puzzle->tiles - puzzle->position > puzzle->columns)
        next = puzzle->position + puzzle->columns;
      break;
    case DIR_LEFT:
      if ((puzzle->position % puzzle->columns) > 0)
        next = puzzle->position - 1;
      break;
    case DIR_RIGHT:
      if ((puzzle->position % puzzle->columns) < puzzle->columns - 1)
        next = puzzle->position + 1;
      break;
    default:
      g_assert_not_reached ();
  }

  if (next < puzzle->tiles) {
    /* the move was valid */
280
    gst_puzzle_swap (puzzle, next);
Benjamin Otte's avatar
Benjamin Otte committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  }
}

static void
gst_puzzle_shuffle (GstPuzzle * puzzle)
{
  guint i;

  do {
    for (i = 0; i < 100 * puzzle->tiles; i++) {
      gst_puzzle_move (puzzle, g_random_int_range (0, 4));
    }
  } while (gst_puzzle_is_solved (puzzle));
  puzzle->solved = FALSE;
}

297 298 299 300
/* The nav event handler handles nav events, but still forwards them, so you
 * should be able to even use puzzle while navigating a dvd menu. We return 
 * TRUE of course even when noone downstream handles the event.
 */
Benjamin Otte's avatar
Benjamin Otte committed
301 302 303 304
static gboolean
nav_event_handler (GstPad * pad, GstEvent * event)
{
  GstPuzzle *puzzle;
305
  GstVideofilter *filter;
Benjamin Otte's avatar
Benjamin Otte committed
306
  const gchar *type;
307 308 309
  gboolean result = FALSE;
  gdouble x, y;
  gint xpos = 0, ypos = 0;
Benjamin Otte's avatar
Benjamin Otte committed
310 311

  puzzle = GST_PUZZLE (gst_pad_get_parent (pad));
312
  filter = GST_VIDEOFILTER (puzzle);
Benjamin Otte's avatar
Benjamin Otte committed
313 314 315

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_NAVIGATION:
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 349 350
      /* translate the event */
      if (gst_structure_get_double (event->event_data.structure.structure,
              "pointer_x", &x) &&
          gst_structure_get_double (event->event_data.structure.structure,
              "pointer_y", &y)) {
        gint width, height;

        width = gst_videofilter_get_input_width (filter);
        height = gst_videofilter_get_input_height (filter);
        width = (width / puzzle->columns) & ~3;
        height = (height / puzzle->rows) & ~3;
        xpos = (int) x / width;
        ypos = (int) y / height;
        if (xpos >= 0 && xpos < puzzle->columns && ypos >= 0
            && ypos < puzzle->rows) {
          GstEvent *copy;
          guint lookup;

          lookup = puzzle->permutation[ypos * puzzle->columns + xpos];
          GST_DEBUG_OBJECT (puzzle, "translated %dx%d (%gx%g) to %dx%d (%gx%g)",
              xpos, ypos, x, y,
              lookup % puzzle->columns, lookup / puzzle->columns,
              x + ((gint) (lookup % puzzle->columns) - xpos) * width,
              y + ((gint) (lookup / puzzle->columns) - ypos) * height);
          x += ((gint) (lookup % puzzle->columns) - xpos) * width;
          y += ((gint) (lookup / puzzle->columns) - ypos) * height;
          copy = gst_event_copy (event);
          gst_structure_set (copy->event_data.structure.structure,
              "pointer_x", G_TYPE_DOUBLE, x,
              "pointer_y", G_TYPE_DOUBLE, y, NULL);
          gst_event_unref (event);
          event = copy;
        }
      }
      /* handle the event. NOTE: it has already been translated! */
Benjamin Otte's avatar
Benjamin Otte committed
351 352 353 354 355 356
      type = gst_structure_get_string (event->event_data.structure.structure,
          "event");
      if (g_str_equal (type, "key-press")) {
        const gchar *key =
            gst_structure_get_string (event->event_data.structure.structure,
            "key");
357

Benjamin Otte's avatar
Benjamin Otte committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        if (g_str_equal (key, "space")) {
          if (gst_puzzle_is_solved (puzzle)) {
            gst_puzzle_shuffle (puzzle);
          } else {
            gst_puzzle_solve (puzzle);
          }
        } else {
          if (puzzle->solved)
            break;
          if (g_str_equal (key, "Left")) {
            gst_puzzle_move (puzzle, DIR_LEFT);
          } else if (g_str_equal (key, "Right")) {
            gst_puzzle_move (puzzle, DIR_RIGHT);
          } else if (g_str_equal (key, "Up")) {
            gst_puzzle_move (puzzle, DIR_UP);
          } else if (g_str_equal (key, "Down")) {
            gst_puzzle_move (puzzle, DIR_DOWN);
          }
        }
377 378 379 380 381 382 383
        puzzle->solved = gst_puzzle_is_solved (puzzle);
      } else if (g_str_equal (type, "mouse-button-press")) {
        gint button;

        if (gst_structure_get_int (event->event_data.structure.structure,
                "button", &button)) {
          if (button == 1) {
384 385 386
            if (xpos >= 0 && xpos < puzzle->columns && ypos >= 0
                && ypos < puzzle->rows && !puzzle->solved) {
              gst_puzzle_swap (puzzle, ypos * puzzle->columns + xpos);
387 388 389
              puzzle->solved = gst_puzzle_is_solved (puzzle);
            }
          } else if (button == 2) {
390
            if (puzzle->solved) {
391 392 393 394 395 396 397
              gst_puzzle_shuffle (puzzle);
            } else {
              gst_puzzle_solve (puzzle);
            }
            puzzle->solved = gst_puzzle_is_solved (puzzle);
          }
        }
Benjamin Otte's avatar
Benjamin Otte committed
398
      }
399 400
      /* FIXME: only return TRUE for events we handle? */
      result = TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
401 402 403 404
      break;
    default:
      break;
  }
405
  return gst_pad_event_default (pad, event) || result;
Benjamin Otte's avatar
Benjamin Otte committed
406 407
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
static void
gst_puzzle_create (GstPuzzle * puzzle)
{
  guint i;

  puzzle->tiles = puzzle->rows * puzzle->columns;
  g_assert (puzzle->tiles);
  g_free (puzzle->permutation);

  puzzle->permutation = g_new (guint, puzzle->tiles);
  for (i = 0; i < puzzle->tiles; i++) {
    puzzle->permutation[i] = i;
  }
  puzzle->position = puzzle->tiles - 1;
  /* shuffle a bit */
  gst_puzzle_shuffle (puzzle);
}

Benjamin Otte's avatar
Benjamin Otte committed
426 427 428 429
static void
gst_puzzle_init (GTypeInstance * instance, gpointer g_class)
{
  GstVideofilter *videofilter;
430
  GstPuzzle *puzzle;
Benjamin Otte's avatar
Benjamin Otte committed
431 432

  videofilter = GST_VIDEOFILTER (instance);
433
  puzzle = GST_PUZZLE (instance);
Benjamin Otte's avatar
Benjamin Otte committed
434 435
  /* FIXME: this is evil */
  gst_pad_set_event_function (videofilter->srcpad, nav_event_handler);
436 437 438 439

  /* set this so we don't crash when initializing */
  puzzle->rows = 1;
  puzzle->columns = 1;
Benjamin Otte's avatar
Benjamin Otte committed
440 441 442 443 444 445 446 447 448 449 450 451 452
}

static void
gst_puzzle_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstPuzzle *src;

  g_return_if_fail (GST_IS_PUZZLE (object));
  src = GST_PUZZLE (object);

  GST_DEBUG ("gst_puzzle_set_property");
  switch (prop_id) {
453
    case PROP_COLUMNS:
Benjamin Otte's avatar
Benjamin Otte committed
454
      src->columns = g_value_get_uint (value);
455
      gst_puzzle_create (src);
Benjamin Otte's avatar
Benjamin Otte committed
456
      break;
457
    case PROP_ROWS:
Benjamin Otte's avatar
Benjamin Otte committed
458
      src->rows = g_value_get_uint (value);
459
      gst_puzzle_create (src);
Benjamin Otte's avatar
Benjamin Otte committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_puzzle_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstPuzzle *src;

  g_return_if_fail (GST_IS_PUZZLE (object));
  src = GST_PUZZLE (object);

  switch (prop_id) {
477
    case PROP_COLUMNS:
Benjamin Otte's avatar
Benjamin Otte committed
478 479
      g_value_set_uint (value, src->columns);
      break;
480
    case PROP_ROWS:
Benjamin Otte's avatar
Benjamin Otte committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
      g_value_set_uint (value, src->rows);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_puzzle_setup (GstVideofilter * videofilter)
{
  GstPuzzle *puzzle;

  g_return_if_fail (GST_IS_PUZZLE (videofilter));
  puzzle = GST_PUZZLE (videofilter);

  puzzle->format = NULL;
}

static void
draw_puzzle (GstVideofilter * videofilter, void *destp, void *srcp)
{
  GstPuzzle *puzzle;
  int width, height;
  guint i;
  GstVideoImage dest, src;

  puzzle = GST_PUZZLE (videofilter);
  if (!puzzle->format) {
    puzzle->format =
        gst_video_format_find_by_structure (gst_caps_get_structure
        (gst_pad_get_negotiated_caps (videofilter->sinkpad), 0));
  }
  width = gst_videofilter_get_input_width (videofilter);
  height = gst_videofilter_get_input_height (videofilter);
  gst_video_image_setup (&dest, puzzle->format, destp, width, height);
  gst_video_image_setup (&src, puzzle->format, srcp, width, height);
518 519 520
  /* use multiples of 4 here to get around drawing problems with YUV colorspaces */
  width = (width / puzzle->columns) & ~3;
  height = (height / puzzle->rows) & ~3;
521 522 523 524 525 526
  if (width == 0 || height == 0) {
    gst_video_image_copy_area (&dest, 0, 0, &src, 0, 0,
        gst_videofilter_get_input_width (videofilter),
        gst_videofilter_get_input_height (videofilter));
    return;
  }
527 528 529
  if (width * puzzle->columns != gst_videofilter_get_input_width (videofilter)) {
    guint w =
        gst_videofilter_get_input_width (videofilter) - width * puzzle->columns;
Benjamin Otte's avatar
Benjamin Otte committed
530

531 532 533
    gst_video_image_copy_area (&dest, width * puzzle->columns, 0, &src,
        width * puzzle->columns, 0, w,
        gst_videofilter_get_input_height (videofilter));
Benjamin Otte's avatar
Benjamin Otte committed
534
  }
535 536
  if (height * puzzle->rows != gst_videofilter_get_input_height (videofilter)) {
    guint h =
537
        gst_videofilter_get_input_height (videofilter) - height * puzzle->rows;
Benjamin Otte's avatar
Benjamin Otte committed
538

539 540
    gst_video_image_copy_area (&dest, 0, height * puzzle->rows, &src, 0,
        height * puzzle->rows, gst_videofilter_get_input_width (videofilter),
Benjamin Otte's avatar
Benjamin Otte committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
        h);
  }

  for (i = 0; i < puzzle->tiles; i++) {
    if (!puzzle->solved && i == puzzle->position) {
      gst_video_image_draw_rectangle (&dest, width * (i % puzzle->columns),
          height * (i / puzzle->columns), width, height,
          &GST_VIDEO_COLOR_WHITE, TRUE);
    } else {
      gst_video_image_copy_area (&dest, width * (i % puzzle->columns),
          height * (i / puzzle->columns), &src,
          width * (puzzle->permutation[i] % puzzle->columns),
          height * (puzzle->permutation[i] / puzzle->columns), width, height);
    }
  }
}

static gboolean
plugin_init (GstPlugin * plugin)
{
561 562 563 564
#ifdef HAVE_LIBOIL
  oil_init ();
#endif

Benjamin Otte's avatar
Benjamin Otte committed
565 566 567 568 569 570 571 572 573
  if (!gst_library_load ("gstvideofilter"))
    return FALSE;

  return gst_element_register (plugin, "puzzle", GST_RANK_NONE,
      GST_TYPE_PUZZLE);
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
574
    games,
Benjamin Otte's avatar
Benjamin Otte committed
575
    "a collection of games to showcase features",
576
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)