~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/boards/wordsgame.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* gcompris - wordsgame.c
2
 
 *
3
 
 * Copyright (C) 2000 Bruno Coudoin
4
 
 *
5
 
 *   This program is free software; you can redistribute it and/or modify
6
 
 *   it under the terms of the GNU General Public License as published by
7
 
 *   the Free Software Foundation; either version 3 of the License, or
8
 
 *   (at your option) any later version.
9
 
 *
10
 
 *   This program is distributed in the hope that it will be useful,
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *   GNU General Public License for more details.
14
 
 *
15
 
 *   You should have received a copy of the GNU General Public License
16
 
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#include <string.h>
20
 
 
21
 
#include "gcompris/gcompris.h"
22
 
#include "gcompris/gameutil.h"
23
 
 
24
 
 
25
 
#define SOUNDLISTFILE PACKAGE
26
 
#define MAXWORDSLENGTH 50
27
 
static GcomprisWordlist *gc_wordlist = NULL;
28
 
static gboolean board_paused = TRUE;
29
 
 
30
 
GStaticRWLock items_lock = G_STATIC_RW_LOCK_INIT;
31
 
GStaticRWLock items2del_lock = G_STATIC_RW_LOCK_INIT;
32
 
 
33
 
/*
34
 
  word - word to type
35
 
  overword - part of word allready typed
36
 
  count - number of allready typed letters in word
37
 
  pos - pointer to current position in word
38
 
  letter - current expected letter to type
39
 
*/
40
 
typedef struct {
41
 
  GnomeCanvasItem *rootitem;
42
 
  GnomeCanvasItem *overwriteItem;
43
 
  gchar *word;
44
 
  gchar *overword;
45
 
  gint  count;
46
 
  gchar *pos;
47
 
  gchar *letter;
48
 
} LettersItem;
49
 
 
50
 
/*
51
 
  items - array of displayed items
52
 
  items2del - array of items where moved offscreen
53
 
  item_on_focus -  item on focus in array items. NULL - not set.
54
 
*/
55
 
 
56
 
static GPtrArray        *items=NULL;
57
 
static GPtrArray        *items2del=NULL;
58
 
static LettersItem      *item_on_focus=NULL;
59
 
 
60
 
 
61
 
static GcomprisBoard *gcomprisBoard = NULL;
62
 
 
63
 
static gint dummy_id = 0;
64
 
static gint drop_items_id = 0;
65
 
 
66
 
 
67
 
static void              start_board (GcomprisBoard *agcomprisBoard);
68
 
static void              pause_board (gboolean pause);
69
 
static void              end_board (void);
70
 
static gboolean          is_our_board (GcomprisBoard *gcomprisBoard);
71
 
static void              set_level (guint level);
72
 
static gint              key_press(guint keyval, gchar *commit_str, gchar *preedit_str);
73
 
 
74
 
static GnomeCanvasItem   *wordsgame_create_item(GnomeCanvasGroup *parent);
75
 
static gint              wordsgame_drop_items (GtkWidget *widget, gpointer data);
76
 
static gint              wordsgame_move_items (GtkWidget *widget, gpointer data);
77
 
static void              wordsgame_destroy_item(LettersItem *item);
78
 
static gboolean          wordsgame_destroy_items(GPtrArray *items);
79
 
static void              wordsgame_destroy_all_items(void);
80
 
static void              wordsgame_next_level(void);
81
 
static void              wordsgame_add_new_item(void);
82
 
static void              wordsgame_config_start(GcomprisBoard *agcomprisBoard,
83
 
                                             GcomprisProfile *aProfile);
84
 
static void              wordsgame_config_stop(void);
85
 
 
86
 
 
87
 
static void              player_win(LettersItem *item);
88
 
static void              player_loose(void);
89
 
 
90
 
 
91
 
#define MAX_FALLSPEED  7000
92
 
#define MAX_SPEED  150
93
 
#define MIN_FALLSPEED  3000
94
 
#define MIN_SPEED  50
95
 
#define DEFAULT_FALLSPEED  7000
96
 
#define DEFAULT_SPEED  150
97
 
 
98
 
#define INCREMENT_FALLSPEED  1000
99
 
#define INCREMENT_SPEED  10
100
 
 
101
 
 
102
 
static  guint32              fallSpeed = 0;
103
 
static  double               speed = 0.0;
104
 
 
105
 
static GnomeCanvasItem *preedit_text = NULL;
106
 
 
107
 
/* Description of this plugin */
108
 
static BoardPlugin menu_bp =
109
 
  {
110
 
    NULL,
111
 
    NULL,
112
 
    N_("Falling Words"),
113
 
    N_("Type the falling words before they reach the ground"),
114
 
    "Bruno Coudoin <bruno.coudoin@free.fr>",
115
 
    NULL,
116
 
    NULL,
117
 
    NULL,
118
 
    NULL,
119
 
    start_board,
120
 
    pause_board,
121
 
    end_board,
122
 
    is_our_board,
123
 
    key_press,
124
 
    NULL,
125
 
    set_level,
126
 
    NULL,
127
 
    NULL,
128
 
    wordsgame_config_start,
129
 
    wordsgame_config_stop
130
 
  };
131
 
 
132
 
/*
133
 
 * Main entry point mandatory for each Gcompris's game
134
 
 * ---------------------------------------------------
135
 
 *
136
 
 */
137
 
 
138
 
GET_BPLUGIN_INFO(wordsgame)
139
 
 
140
 
/*
141
 
 * in : boolean TRUE = PAUSE : FALSE = UNPAUSE
142
 
 *
143
 
 */
144
 
     static void pause_board (gboolean pause)
145
 
{
146
 
 
147
 
  if(gcomprisBoard==NULL)
148
 
    return;
149
 
 
150
 
  if(pause)
151
 
    {
152
 
      if (dummy_id) {
153
 
        g_source_remove (dummy_id);
154
 
        dummy_id = 0;
155
 
      }
156
 
      if (drop_items_id) {
157
 
        g_source_remove (drop_items_id);
158
 
        drop_items_id = 0;
159
 
      }
160
 
    }
161
 
  else
162
 
    {
163
 
      if(!drop_items_id) {
164
 
        drop_items_id = g_timeout_add (0,
165
 
                                       (GtkFunction) wordsgame_drop_items, NULL);
166
 
      }
167
 
      if(!dummy_id) {
168
 
        dummy_id = g_timeout_add (10, (GtkFunction) wordsgame_move_items, NULL);
169
 
      }
170
 
    }
171
 
  board_paused = pause;
172
 
}
173
 
 
174
 
/*
175
 
 */
176
 
static void start_board (GcomprisBoard *agcomprisBoard)
177
 
{
178
 
 
179
 
  if(agcomprisBoard!=NULL)
180
 
    {
181
 
      gcomprisBoard=agcomprisBoard;
182
 
 
183
 
      /* disable im_context */
184
 
      //gcomprisBoard->disable_im_context = TRUE;
185
 
 
186
 
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
187
 
                        "opt/scenery_background.png");
188
 
 
189
 
 
190
 
      gcomprisBoard->level = 1;
191
 
      gcomprisBoard->maxlevel = 6;
192
 
      gcomprisBoard->sublevel = 0;
193
 
      gc_bar_set(GC_BAR_LEVEL|GC_BAR_CONFIG);
194
 
 
195
 
      /* Default speed */
196
 
      speed=DEFAULT_SPEED;
197
 
      fallSpeed=DEFAULT_FALLSPEED;
198
 
 
199
 
      gc_wordlist = gc_wordlist_get_from_file("wordsgame/default-$LOCALE.xml");
200
 
 
201
 
      if(!gc_wordlist)
202
 
        {
203
 
          /* Fallback to english */
204
 
          gc_wordlist = gc_wordlist_get_from_file("wordsgame/default-en.xml");
205
 
 
206
 
          if(!gc_wordlist)
207
 
            {
208
 
              gcomprisBoard = NULL;
209
 
              gc_dialog(_("Error: We can't find\na list of words to play this game.\n"), gc_board_end);
210
 
              return;
211
 
            }
212
 
        }
213
 
 
214
 
      wordsgame_next_level();
215
 
    }
216
 
}
217
 
 
218
 
static void
219
 
end_board ()
220
 
{
221
 
 
222
 
  if(gcomprisBoard!=NULL)
223
 
    {
224
 
      pause_board(TRUE);
225
 
      gc_score_end();
226
 
      wordsgame_destroy_all_items();
227
 
      if (preedit_text){
228
 
        gtk_object_destroy(GTK_OBJECT(preedit_text));
229
 
        preedit_text=NULL;
230
 
      }
231
 
      gc_im_reset();
232
 
      gcomprisBoard = NULL;
233
 
 
234
 
      if (gc_wordlist != NULL){
235
 
        gc_wordlist_free(gc_wordlist);
236
 
        gc_wordlist = NULL;
237
 
      }
238
 
 
239
 
    }
240
 
}
241
 
 
242
 
static void
243
 
set_level (guint level)
244
 
{
245
 
 
246
 
  if(gcomprisBoard!=NULL)
247
 
    {
248
 
      gcomprisBoard->level=level;
249
 
      wordsgame_next_level();
250
 
    }
251
 
}
252
 
 
253
 
 
254
 
static gint key_press(guint keyval, gchar *commit_str, gchar *preedit_str)
255
 
{
256
 
  gchar *letter;
257
 
  gint i;
258
 
  LettersItem *item;
259
 
  gchar *str;
260
 
  gunichar unichar_letter;
261
 
 
262
 
  if(board_paused && !gcomprisBoard)
263
 
    return FALSE;
264
 
 
265
 
  if (keyval){
266
 
    g_warning("keyval %d", keyval);
267
 
    return TRUE;
268
 
  }
269
 
 
270
 
 
271
 
  if (preedit_str){
272
 
    g_warning("preedit_str %s", preedit_str);
273
 
    /* show the preedit string on bottom of the window */
274
 
    GcomprisProperties  *properties = gc_prop_get ();
275
 
    gchar *text;
276
 
    PangoAttrList *attrs;
277
 
    gint cursor_pos;
278
 
    gtk_im_context_get_preedit_string (properties->context,
279
 
                                       &text,
280
 
                                       &attrs,
281
 
                                       &cursor_pos);
282
 
 
283
 
    if (!preedit_text)
284
 
      preedit_text = \
285
 
        gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
286
 
                               gnome_canvas_text_get_type (),
287
 
                               "font", gc_skin_font_board_huge_bold,
288
 
                               "x", (double) BOARDWIDTH/2,
289
 
                               "y", (double) BOARDHEIGHT - 100,
290
 
                               "anchor", GTK_ANCHOR_N,
291
 
                               //"fill_color_rgba", 0xba00ffff,
292
 
                               NULL);
293
 
 
294
 
 
295
 
    gnome_canvas_item_set (preedit_text,
296
 
                           "text", text,
297
 
                           "attributes", attrs,
298
 
                           NULL);
299
 
 
300
 
    return TRUE;
301
 
 
302
 
  }
303
 
 
304
 
  /* commit str */
305
 
  g_warning("commit_str %s", commit_str);
306
 
 
307
 
  str = commit_str;
308
 
 
309
 
  for (i=0; i < g_utf8_strlen(commit_str,-1); i++){
310
 
    unichar_letter = g_utf8_get_char(str);
311
 
    str = g_utf8_next_char(str);
312
 
    if(!g_unichar_isalnum (unichar_letter)){
313
 
      player_loose();
314
 
      return FALSE;
315
 
    }
316
 
 
317
 
    letter = g_new0(gchar,6);
318
 
    g_unichar_to_utf8 (unichar_letter, letter);
319
 
 
320
 
    if(item_on_focus==NULL)
321
 
      {
322
 
        g_static_rw_lock_reader_lock (&items_lock);
323
 
        gint count=items->len;
324
 
        g_static_rw_lock_reader_unlock (&items_lock);
325
 
 
326
 
        for (i=0;i<count;i++)
327
 
          {
328
 
            g_static_rw_lock_reader_lock (&items_lock);
329
 
            item=g_ptr_array_index(items,i);
330
 
            g_static_rw_lock_reader_unlock (&items_lock);
331
 
            g_assert (item!=NULL);
332
 
            if (strcmp(item->letter,letter)==0)
333
 
              {
334
 
                item_on_focus=item;
335
 
                break;
336
 
              }
337
 
          }
338
 
      }
339
 
 
340
 
 
341
 
    if(item_on_focus!=NULL)
342
 
      {
343
 
 
344
 
        if(strcmp(item_on_focus->letter, letter)==0)
345
 
          {
346
 
            gchar *tmpstr;
347
 
            item_on_focus->count++;
348
 
            g_free(item_on_focus->overword);
349
 
            tmpstr = g_utf8_strndup(item_on_focus->word,
350
 
                                    item_on_focus->count);
351
 
            /* Add the ZERO WIDTH JOINER to force joined char in Arabic and Hangul
352
 
             *  http://en.wikipedia.org/wiki/Zero-width_joiner (UTF-8: e2808d)
353
 
             */
354
 
            item_on_focus->overword = g_strdup_printf("%s%c%c%c", tmpstr, 0xe2, 0x80, 0x8d);
355
 
            g_free(tmpstr);
356
 
            gnome_canvas_item_set (item_on_focus->overwriteItem,
357
 
                                   "text", item_on_focus->overword,
358
 
                                   NULL);
359
 
 
360
 
 
361
 
            if (item_on_focus->count<g_utf8_strlen(item_on_focus->word,-1))
362
 
              {
363
 
                g_free(item_on_focus->letter);
364
 
                item_on_focus->letter=g_utf8_strndup(item_on_focus->pos,1);
365
 
                item_on_focus->pos=g_utf8_find_next_char(item_on_focus->pos,NULL);
366
 
              }
367
 
            else
368
 
              {
369
 
                player_win(item_on_focus);
370
 
                item_on_focus=NULL;
371
 
              }
372
 
          }
373
 
        else
374
 
          {
375
 
            /* It is a loose : unselect the word and defocus */
376
 
            g_free(item_on_focus->overword);
377
 
            item_on_focus->overword=g_strdup(" ");
378
 
            item_on_focus->count=0;
379
 
            g_free(item_on_focus->letter);
380
 
            item_on_focus->letter=g_utf8_strndup(item_on_focus->word,1);
381
 
 
382
 
            item_on_focus->pos=g_utf8_find_next_char(item_on_focus->word,NULL);
383
 
 
384
 
            gnome_canvas_item_set (item_on_focus->overwriteItem,
385
 
                                   "text", item_on_focus->overword,
386
 
                                   NULL);
387
 
            item_on_focus=NULL;
388
 
            g_free(letter);
389
 
            player_loose();
390
 
            break;
391
 
          }
392
 
      }
393
 
    else
394
 
      {
395
 
        /* Anyway kid you clicked on the wrong key */
396
 
        player_loose();
397
 
        g_free(letter);
398
 
        break;
399
 
      }
400
 
 
401
 
    g_free(letter);
402
 
  }
403
 
  return TRUE;
404
 
}
405
 
 
406
 
static gboolean
407
 
is_our_board (GcomprisBoard *gcomprisBoard)
408
 
{
409
 
  if (gcomprisBoard)
410
 
    {
411
 
      if(g_strcasecmp(gcomprisBoard->type, "wordsgame")==0)
412
 
        {
413
 
          /* Set the plugin entry */
414
 
          gcomprisBoard->plugin=&menu_bp;
415
 
 
416
 
          return TRUE;
417
 
        }
418
 
    }
419
 
  return FALSE;
420
 
}
421
 
 
422
 
 
423
 
/*-------------------------------------------------------------------------------*/
424
 
/*-------------------------------------------------------------------------------*/
425
 
/*-------------------------------------------------------------------------------*/
426
 
/*-------------------------------------------------------------------------------*/
427
 
 
428
 
/* set initial values for the next level */
429
 
static void wordsgame_next_level()
430
 
{
431
 
 
432
 
 
433
 
  gcomprisBoard->number_of_sublevel = 10 +
434
 
    ((gcomprisBoard->level-1) * 5);
435
 
  gc_score_start(SCORESTYLE_NOTE,
436
 
                       gcomprisBoard->width - 220,
437
 
                       gcomprisBoard->height - 50,
438
 
                       gcomprisBoard->number_of_sublevel);
439
 
 
440
 
  gc_bar_set_level(gcomprisBoard);
441
 
  gc_score_set(gcomprisBoard->sublevel);
442
 
 
443
 
  wordsgame_destroy_all_items();
444
 
 
445
 
  if (preedit_text){
446
 
    gtk_object_destroy(GTK_OBJECT(preedit_text));
447
 
    preedit_text=NULL;
448
 
  }
449
 
  gc_im_reset();
450
 
 
451
 
  items=g_ptr_array_new();
452
 
  items2del=g_ptr_array_new();
453
 
 
454
 
 
455
 
  /* Increase speed only after 5 levels */
456
 
  if(gcomprisBoard->level>5)
457
 
    {
458
 
      gint temp = fallSpeed-gcomprisBoard->level*200;
459
 
      if (temp > MIN_FALLSPEED) fallSpeed=temp;
460
 
    }
461
 
 
462
 
  pause_board(FALSE);
463
 
}
464
 
 
465
 
 
466
 
static void wordsgame_move_item(LettersItem *item)
467
 
{
468
 
  double x1, y1, x2, y2;
469
 
 
470
 
 
471
 
  gnome_canvas_item_move(item->rootitem, 0, 2.0);
472
 
 
473
 
  gnome_canvas_item_get_bounds    (item->rootitem,
474
 
                                   &x1,
475
 
                                   &y1,
476
 
                                   &x2,
477
 
                                   &y2);
478
 
 
479
 
  if(y1>gcomprisBoard->height) {
480
 
 
481
 
    if (item == item_on_focus)
482
 
      item_on_focus = NULL;
483
 
 
484
 
    g_static_rw_lock_writer_lock (&items_lock);
485
 
    g_ptr_array_remove (items, item);
486
 
    g_static_rw_lock_writer_unlock (&items_lock);
487
 
 
488
 
    g_static_rw_lock_writer_lock (&items2del_lock);
489
 
    g_ptr_array_add (items2del, item);
490
 
    g_static_rw_lock_writer_unlock (&items2del_lock);
491
 
 
492
 
    g_timeout_add (100,(GtkFunction) wordsgame_destroy_items, items2del);
493
 
 
494
 
    player_loose();
495
 
  }
496
 
}
497
 
 
498
 
/*
499
 
 * This does the moves of the game items on the play canvas
500
 
 *
501
 
 */
502
 
static gint wordsgame_move_items (GtkWidget *widget, gpointer data)
503
 
{
504
 
  g_assert (items!=NULL);
505
 
  gint i;
506
 
  LettersItem *item;
507
 
 
508
 
  for (i=items->len-1;i>=0;i--)
509
 
    {
510
 
 
511
 
      g_static_rw_lock_reader_lock (&items_lock);
512
 
      item=g_ptr_array_index(items,i);
513
 
      g_static_rw_lock_reader_unlock (&items_lock);
514
 
      wordsgame_move_item(item);
515
 
    }
516
 
  dummy_id = g_timeout_add (speed,(GtkFunction) wordsgame_move_items, NULL);
517
 
  return (FALSE);
518
 
}
519
 
 
520
 
 
521
 
 
522
 
static void wordsgame_destroy_item(LettersItem *item)
523
 
{
524
 
 
525
 
  /* The items are freed by player_win */
526
 
  gtk_object_destroy (GTK_OBJECT(item->rootitem));
527
 
  g_free(item->word);
528
 
  g_free(item->overword);
529
 
  g_free(item->letter);
530
 
  g_free(item);
531
 
}
532
 
 
533
 
/* Destroy items that falls out of the canvas */
534
 
static gboolean wordsgame_destroy_items(GPtrArray *item_list)
535
 
{
536
 
  LettersItem *item;
537
 
 
538
 
  g_assert(item_list!=NULL);
539
 
 
540
 
 
541
 
 
542
 
  if  (item_list==items) {
543
 
    g_static_rw_lock_writer_lock (&items_lock);
544
 
    while (item_list->len>0)
545
 
      {
546
 
        item = g_ptr_array_index(item_list,0);
547
 
        g_ptr_array_remove_index_fast(item_list,0);
548
 
        wordsgame_destroy_item(item);
549
 
      }
550
 
    g_static_rw_lock_writer_unlock (&items_lock);
551
 
  }
552
 
 
553
 
  if  (item_list==items2del) {
554
 
    g_static_rw_lock_writer_lock (&items2del_lock);
555
 
    while (item_list->len>0)
556
 
      {
557
 
        item = g_ptr_array_index(item_list,0);
558
 
        g_ptr_array_remove_index_fast(item_list,0);
559
 
        wordsgame_destroy_item(item);
560
 
      }
561
 
    g_static_rw_lock_writer_unlock (&items2del_lock);
562
 
  }
563
 
 
564
 
  return (FALSE);
565
 
}
566
 
 
567
 
/* Destroy all the items */
568
 
static void wordsgame_destroy_all_items()
569
 
{
570
 
 
571
 
  if (items!=NULL){
572
 
    if(items->len > 0) {
573
 
      wordsgame_destroy_items(items);
574
 
    }
575
 
    g_ptr_array_free (items, TRUE);
576
 
    items=NULL;
577
 
  }
578
 
 
579
 
  if (items2del!=NULL){
580
 
    if(items2del->len > 0) {
581
 
      wordsgame_destroy_items(items2del);
582
 
    }
583
 
    g_ptr_array_free (items2del, TRUE);
584
 
    items2del=NULL;
585
 
  }
586
 
 
587
 
}
588
 
 
589
 
 
590
 
static GnomeCanvasItem *wordsgame_create_item(GnomeCanvasGroup *parent)
591
 
{
592
 
 
593
 
  GnomeCanvasItem *item2;
594
 
  LettersItem *item;
595
 
  gchar *word = gc_wordlist_random_word_get(gc_wordlist, gcomprisBoard->level);
596
 
  GtkAnchorType direction_anchor = GTK_ANCHOR_NW;
597
 
 
598
 
  if(!word)
599
 
    /* Should display the dialog box here */
600
 
    return NULL;
601
 
 
602
 
  // create and init item
603
 
  item = g_new(LettersItem,1);
604
 
  item->word = word;
605
 
  item->overword=g_strdup("");
606
 
  item->count=0;
607
 
  item->letter=g_utf8_strndup(item->word,1);
608
 
  item->pos=g_utf8_find_next_char(item->word, NULL);
609
 
 
610
 
  if (pango_unichar_direction(g_utf8_get_char(item->word)))
611
 
    direction_anchor = GTK_ANCHOR_NE;
612
 
 
613
 
  item->rootitem = \
614
 
    gnome_canvas_item_new (parent,
615
 
                           gnome_canvas_group_get_type (),
616
 
                           "x", (double) 0,
617
 
                           "y", (double) -12,
618
 
                           NULL);
619
 
 
620
 
  /* To 'erase' words, I create 2 times the text item. One is empty now */
621
 
  /* It will be filled each time the user enters the right key         */
622
 
  item2 = \
623
 
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(item->rootitem),
624
 
                           gnome_canvas_text_get_type (),
625
 
                           "text", item->word,
626
 
                           "font", gc_skin_font_board_huge_bold,
627
 
                           "x", (double) 0,
628
 
                           "y", (double) 0,
629
 
                           "anchor", direction_anchor,
630
 
                           "fill_color_rgba", 0xba00ffff,
631
 
                           NULL);
632
 
 
633
 
  item->overwriteItem = \
634
 
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(item->rootitem),
635
 
                           gnome_canvas_text_get_type (),
636
 
                           "text", item->overword,
637
 
                           "font", gc_skin_font_board_huge_bold,
638
 
                           "x", (double) 0,
639
 
                           "y", (double) 0,
640
 
                           "anchor", direction_anchor,
641
 
                           "fill_color", "blue",
642
 
                           NULL);
643
 
 
644
 
  /*set right x position */
645
 
 
646
 
  double x1, y1, x2, y2;
647
 
 
648
 
 
649
 
  gnome_canvas_item_get_bounds    (item->rootitem,
650
 
                                   &x1,
651
 
                                   &y1,
652
 
                                   &x2,
653
 
                                   &y2);
654
 
 
655
 
  if(direction_anchor == GTK_ANCHOR_NW)
656
 
      gnome_canvas_item_move (item->rootitem,(double) (g_random_int()%(gcomprisBoard->width-(gint)(x2))),(double) 0);
657
 
  else
658
 
   {
659
 
      double new_x = (double)( g_random_int()%gcomprisBoard->width); 
660
 
      if ( new_x < -x1 )      
661
 
                new_x -=  x1;
662
 
      gnome_canvas_item_move (item->rootitem, new_x ,(double) 0);
663
 
   }
664
 
 
665
 
  g_static_rw_lock_writer_lock (&items_lock);
666
 
  g_ptr_array_add(items, item);
667
 
  g_static_rw_lock_writer_unlock (&items_lock);
668
 
 
669
 
  return (item->rootitem);
670
 
}
671
 
 
672
 
static void wordsgame_add_new_item()
673
 
{
674
 
 
675
 
  g_assert(gcomprisBoard->canvas!=NULL);
676
 
  wordsgame_create_item(gnome_canvas_root(gcomprisBoard->canvas));
677
 
 
678
 
}
679
 
 
680
 
/*
681
 
 * This is called on a low frequency and is used to drop new items
682
 
 *
683
 
 */
684
 
static gint wordsgame_drop_items (GtkWidget *widget, gpointer data)
685
 
{
686
 
  gc_sound_play_ogg ("sounds/level.wav", NULL);
687
 
  wordsgame_add_new_item();
688
 
  g_source_remove(drop_items_id);
689
 
  drop_items_id = g_timeout_add (fallSpeed,(GtkFunction) wordsgame_drop_items, NULL);
690
 
 
691
 
  return (FALSE);
692
 
}
693
 
 
694
 
static void player_win(LettersItem *item)
695
 
{
696
 
 
697
 
  gc_sound_play_ogg ("sounds/flip.wav", NULL);
698
 
 
699
 
  g_assert(gcomprisBoard!=NULL);
700
 
 
701
 
  gcomprisBoard->sublevel++;
702
 
  gc_score_set(gcomprisBoard->sublevel);
703
 
 
704
 
 
705
 
  g_static_rw_lock_writer_lock (&items_lock);
706
 
  g_ptr_array_remove(items,item);
707
 
  g_static_rw_lock_writer_unlock (&items_lock);
708
 
 
709
 
  g_static_rw_lock_writer_lock (&items2del_lock);
710
 
  g_ptr_array_add(items2del,item);
711
 
  g_static_rw_lock_writer_unlock (&items2del_lock);
712
 
 
713
 
  gnome_canvas_item_hide(item->rootitem);
714
 
  g_timeout_add (500,(GtkFunction) wordsgame_destroy_items, items2del);
715
 
 
716
 
 
717
 
  if(gcomprisBoard->sublevel > gcomprisBoard->number_of_sublevel)
718
 
    {
719
 
 
720
 
      /* Try the next level */
721
 
      gcomprisBoard->level++;
722
 
      gcomprisBoard->sublevel = 0;
723
 
      if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
724
 
        gc_bonus_end_display(GC_BOARD_FINISHED_RANDOM);
725
 
        return;
726
 
      }
727
 
      wordsgame_next_level();
728
 
      gc_sound_play_ogg ("sounds/bonus.wav", NULL);
729
 
    }
730
 
  else
731
 
    {
732
 
 
733
 
      /* Drop a new item now to speed up the game */
734
 
      g_static_rw_lock_reader_lock (&items_lock);
735
 
      gint count=items->len;
736
 
      g_static_rw_lock_reader_unlock (&items_lock);
737
 
 
738
 
      if(count==0)
739
 
        {
740
 
 
741
 
          if ((fallSpeed-=INCREMENT_FALLSPEED) < MIN_FALLSPEED) fallSpeed+=INCREMENT_FALLSPEED;
742
 
 
743
 
          if ((speed-=INCREMENT_SPEED) < MIN_SPEED) speed+=INCREMENT_SPEED;
744
 
 
745
 
          if (drop_items_id) {
746
 
            /* Remove pending new item creation to sync the falls */
747
 
            g_source_remove (drop_items_id);
748
 
            drop_items_id = 0;
749
 
          }
750
 
 
751
 
          if(!drop_items_id) {
752
 
            drop_items_id = g_timeout_add (0,
753
 
                                           (GtkFunction) wordsgame_drop_items,
754
 
                                           NULL);
755
 
          }
756
 
 
757
 
        }
758
 
    }
759
 
 
760
 
}
761
 
 
762
 
static void player_loose()
763
 
{
764
 
  gc_sound_play_ogg ("sounds/crash.wav", NULL);
765
 
}
766
 
 
767
 
static void conf_ok(gpointer data)
768
 
{
769
 
        pause_board(FALSE);
770
 
}
771
 
 
772
 
static void wordsgame_config_start(GcomprisBoard *agcomprisBoard, GcomprisProfile *aProfile)
773
 
{
774
 
        if (gcomprisBoard)
775
 
                pause_board(TRUE);
776
 
 
777
 
        gchar *label = g_strdup_printf(_("<b>%s</b> configuration\n for profile <b>%s</b>"),
778
 
                        agcomprisBoard->name,
779
 
                        aProfile? aProfile->name: "");
780
 
        GcomprisBoardConf *bconf;
781
 
        bconf = gc_board_config_window_display( label,
782
 
                        (GcomprisConfCallback )conf_ok);
783
 
 
784
 
        g_free(label);
785
 
 
786
 
        gc_board_config_wordlist(bconf, "wordsgame/default-$LOCALE.xml");
787
 
}
788
 
 
789
 
static void wordsgame_config_stop(void)
790
 
{
791
 
}
792