~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/boards/wordsgame.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-21 16:16:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020421161627-s07yahahm817qxs6
Tags: upstream-1.0.3
ImportĀ upstreamĀ versionĀ 1.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gcompris - wordsgame.c
 
2
 *
 
3
 * Time-stamp: <2002/04/14 00:29:36 bruno>
 
4
 *
 
5
 * Copyright (C) 2000 Bruno Coudoin
 
6
 * 
 
7
 *   This program is free software; you can redistribute it and/or modify
 
8
 *   it under the terms of the GNU General Public License as published by
 
9
 *   the Free Software Foundation; either version 2 of the License, or
 
10
 *   (at your option) any later version.
 
11
 *
 
12
 *   This program is distributed in the hope that it will be useful,
 
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *   GNU General Public License for more details.
 
16
 *
 
17
 *   You should have received a copy of the GNU General Public License
 
18
 *   along with this program; if not, write to the Free Software
 
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#include <errno.h>
 
23
 
 
24
#include "gcompris/gcompris.h"
 
25
 
 
26
 
 
27
#define SOUNDLISTFILE PACKAGE
 
28
#define MAXWORDSLENGTH 50
 
29
 
 
30
static GList *item_list = NULL;
 
31
static GList *item2del_list = NULL;
 
32
 
 
33
static GcomprisBoard *gcomprisBoard = NULL;
 
34
 
 
35
static gint dummy_id = 0;
 
36
static gint drop_items_id = 0;
 
37
 
 
38
/* Hash table of all displayed letters  */
 
39
static GHashTable *words_table= NULL;
 
40
 
 
41
typedef struct {
 
42
  char *word;
 
43
  char *overword;
 
44
  GnomeCanvasItem *rootitem;
 
45
  GnomeCanvasItem *overwriteItem;
 
46
} LettersItem;
 
47
 
 
48
static LettersItem *currentFocus = NULL;
 
49
 
 
50
static void              start_board (GcomprisBoard *agcomprisBoard);
 
51
static void              pause_board (gboolean pause);
 
52
static void              end_board (void);
 
53
static gboolean          is_our_board (GcomprisBoard *gcomprisBoard);
 
54
static void              set_level (guint level);
 
55
gint                     key_press(guint keyval);
 
56
 
 
57
static GnomeCanvasItem   *wordsgame_create_item(GnomeCanvasGroup *parent);
 
58
static gint              wordsgame_drop_items (GtkWidget *widget, gpointer data);
 
59
static gint              wordsgame_move_items (GtkWidget *widget, gpointer data);
 
60
static void              wordsgame_destroy_item(LettersItem *item);
 
61
static void              wordsgame_destroy_items(void);
 
62
static void              wordsgame_destroy_all_items(void);
 
63
static void              wordsgame_next_level(void);
 
64
static void              wordsgame_add_new_item(void);
 
65
 
 
66
static void              player_win(LettersItem *item);
 
67
static void              player_loose(void);
 
68
static LettersItem      *item_find_by_title (const gchar *title);
 
69
static char              *get_random_word(void);
 
70
static void              wordsgame_check_focus (char    *key,
 
71
                                   LettersItem *value,
 
72
                                   LettersItem **user_data);
 
73
 
 
74
static  guint32              fallSpeed = 0;
 
75
static  double               speed = 0.0;
 
76
 
 
77
/* Description of this plugin */
 
78
BoardPlugin menu_bp =
 
79
{
 
80
   NULL,
 
81
   NULL,
 
82
   N_("Falling Words"),
 
83
   N_("Fully type the falling words before they reach the ground"),
 
84
   "Bruno Coudoin <bruno.coudoin@free.fr>",
 
85
   NULL,
 
86
   NULL,
 
87
   NULL,
 
88
   NULL,
 
89
   start_board,
 
90
   pause_board,
 
91
   end_board,
 
92
   is_our_board,
 
93
   key_press,
 
94
   NULL,
 
95
   set_level,
 
96
   NULL,
 
97
   NULL
 
98
};
 
99
 
 
100
/*
 
101
 * Main entry point mandatory for each Gcompris's game
 
102
 * ---------------------------------------------------
 
103
 *
 
104
 */
 
105
 
 
106
BoardPlugin 
 
107
*get_bplugin_info(void)
 
108
{
 
109
  return &menu_bp;
 
110
}
 
111
 
 
112
/*
 
113
 * in : boolean TRUE = PAUSE : FALSE = UNPAUSE
 
114
 *
 
115
 */
 
116
static void pause_board (gboolean pause)
 
117
{
 
118
 
 
119
  if(gcomprisBoard==NULL)
 
120
    return;
 
121
 
 
122
  if(pause)
 
123
    {
 
124
      if (dummy_id) {
 
125
        gtk_timeout_remove (dummy_id);
 
126
        dummy_id = 0;
 
127
      }
 
128
      if (drop_items_id) {
 
129
        gtk_timeout_remove (drop_items_id);
 
130
        drop_items_id = 0;
 
131
      }
 
132
    }
 
133
  else
 
134
    {
 
135
      if(!drop_items_id) {
 
136
        drop_items_id = gtk_timeout_add (100,
 
137
                                         (GtkFunction) wordsgame_drop_items, NULL);
 
138
      }
 
139
      if(!dummy_id) {
 
140
        dummy_id = gtk_timeout_add (100, (GtkFunction) wordsgame_move_items, NULL);
 
141
      }
 
142
    }
 
143
}
 
144
 
 
145
/*
 
146
 */
 
147
static void start_board (GcomprisBoard *agcomprisBoard)
 
148
{
 
149
 
 
150
  if(agcomprisBoard!=NULL)
 
151
    {
 
152
      gcomprisBoard=agcomprisBoard;
 
153
 
 
154
      gcompris_set_background(gnome_canvas_root(gcomprisBoard->canvas), "gcompris/gcompris-bg.jpg");
 
155
 
 
156
 
 
157
      gcomprisBoard->level = 1;
 
158
      gcomprisBoard->maxlevel = 6;
 
159
      gcomprisBoard->sublevel = 1;
 
160
      gcompris_bar_set(GCOMPRIS_BAR_LEVEL);
 
161
 
 
162
      wordsgame_next_level();
 
163
    }
 
164
}
 
165
 
 
166
gboolean words_table_foreach_remove (char *key,
 
167
                                     LettersItem *value,
 
168
                                     LettersItem *user_data)
 
169
{
 
170
  free(value->word);
 
171
  free(value->overword);
 
172
  return TRUE;
 
173
}
 
174
 
 
175
static void
 
176
end_board ()
 
177
{
 
178
 
 
179
  if(gcomprisBoard!=NULL)
 
180
    {
 
181
      pause_board(TRUE);
 
182
      gcompris_score_end();
 
183
      wordsgame_destroy_all_items();
 
184
      if (words_table)
 
185
        {
 
186
          g_hash_table_foreach_remove (words_table,
 
187
                                       (GHRFunc)words_table_foreach_remove,
 
188
                                       NULL);
 
189
          g_hash_table_destroy (words_table);
 
190
          words_table=NULL;
 
191
        }
 
192
    }
 
193
  gcomprisBoard = NULL;
 
194
}
 
195
 
 
196
static void
 
197
set_level (guint level)
 
198
{
 
199
 
 
200
  if(gcomprisBoard!=NULL)
 
201
    {
 
202
      gcomprisBoard->level=level;
 
203
      wordsgame_next_level();
 
204
    }
 
205
}
 
206
 
 
207
static void wordsgame_check_focus (char *key,
 
208
                                   LettersItem *value,
 
209
                                   LettersItem **user_data)
 
210
{
 
211
  LettersItem *usrdata = *user_data;
 
212
 
 
213
  if(usrdata->rootitem!=NULL)
 
214
    return;
 
215
 
 
216
  if(key[0]==usrdata->word[0])
 
217
    {
 
218
      free(*user_data);
 
219
      *user_data = value;
 
220
    }
 
221
 
 
222
}
 
223
 
 
224
gint key_press(guint keyval)
 
225
{
 
226
  char str[2];
 
227
 
 
228
  if(!gcomprisBoard)
 
229
    return TRUE;
 
230
 
 
231
  /* Add some filter for control and shift key */
 
232
  switch (keyval)
 
233
    {
 
234
    case GDK_Shift_L:
 
235
    case GDK_Shift_R:
 
236
    case GDK_Control_L:
 
237
    case GDK_Control_R:
 
238
    case GDK_Caps_Lock:
 
239
    case GDK_Shift_Lock:
 
240
    case GDK_Meta_L:
 
241
    case GDK_Meta_R:
 
242
    case GDK_Alt_L:
 
243
    case GDK_Alt_R:
 
244
    case GDK_Super_L:
 
245
    case GDK_Super_R:
 
246
    case GDK_Hyper_L:
 
247
    case GDK_Hyper_R:
 
248
    case GDK_Mode_switch:
 
249
    case GDK_dead_circumflex:
 
250
    case GDK_Num_Lock:
 
251
      return FALSE; 
 
252
    case GDK_KP_0:
 
253
      keyval=GDK_0;
 
254
      break;
 
255
    case GDK_KP_1:
 
256
      keyval=GDK_1;
 
257
      break;
 
258
    case GDK_KP_2:
 
259
      keyval=GDK_2;
 
260
      break;
 
261
    case GDK_KP_3:
 
262
      keyval=GDK_3;
 
263
      break;
 
264
    case GDK_KP_4:
 
265
      keyval=GDK_4;
 
266
      break;
 
267
    case GDK_KP_5:
 
268
      keyval=GDK_5;
 
269
      break;
 
270
    case GDK_KP_6:
 
271
      keyval=GDK_6;
 
272
      break;
 
273
    case GDK_KP_7:
 
274
      keyval=GDK_7;
 
275
      break;
 
276
    case GDK_KP_8:
 
277
      keyval=GDK_8;
 
278
      break;
 
279
    case GDK_KP_9:
 
280
      keyval=GDK_9;
 
281
      break;
 
282
    }
 
283
 
 
284
  sprintf(str, "%c", keyval);
 
285
 
 
286
  if(currentFocus==NULL) 
 
287
    {
 
288
      LettersItem *searchitem;
 
289
 
 
290
      searchitem = malloc(sizeof(LettersItem));
 
291
 
 
292
      /* Try to see if this letter matches the first one of any words */
 
293
      searchitem->word = (char *)&str;
 
294
      searchitem->rootitem=NULL;
 
295
      searchitem->overword="";
 
296
 
 
297
      g_hash_table_foreach (words_table, (GHFunc) wordsgame_check_focus, &searchitem);
 
298
 
 
299
      
 
300
      if(searchitem->rootitem!=NULL) 
 
301
        {
 
302
          currentFocus=searchitem;
 
303
        }
 
304
      else
 
305
        {
 
306
          free(searchitem);
 
307
        }
 
308
    }
 
309
 
 
310
  if(currentFocus!=NULL) 
 
311
    {
 
312
      if(currentFocus->rootitem!=NULL) 
 
313
        {
 
314
          char currentChar;
 
315
          
 
316
          /* Check this is the correct letter */
 
317
          currentChar = currentFocus->word[strlen(currentFocus->overword)];
 
318
 
 
319
          if(currentChar==str[0])
 
320
            {
 
321
              /* Increment the overword */
 
322
              snprintf(currentFocus->overword, 
 
323
                       strlen(currentFocus->overword)+2,
 
324
                       "%s", currentFocus->word);
 
325
              
 
326
              gnome_canvas_item_set (currentFocus->overwriteItem,
 
327
                                     "text", currentFocus->overword,
 
328
                                     NULL);
 
329
              
 
330
              if(strlen(currentFocus->overword)==strlen(currentFocus->word))
 
331
                {
 
332
                  /* You won Guy */
 
333
                  player_win(item_find_by_title(currentFocus->word));
 
334
 
 
335
                  currentFocus=NULL;
 
336
                }
 
337
            }
 
338
          else
 
339
            {
 
340
              /* It is a loose : unselect the word and defocus */
 
341
              currentFocus->overword[0]='\0';
 
342
              gnome_canvas_item_set (currentFocus->overwriteItem,
 
343
                                     "text", currentFocus->overword,
 
344
                                     NULL);
 
345
              
 
346
              currentFocus=NULL;
 
347
              player_loose();
 
348
            }
 
349
        }
 
350
    }
 
351
  else
 
352
    {
 
353
      /* Anyway kid you clicked on the wrong key */
 
354
      player_loose();
 
355
    }
 
356
  
 
357
  return TRUE;
 
358
}
 
359
 
 
360
gboolean
 
361
is_our_board (GcomprisBoard *gcomprisBoard)
 
362
{
 
363
  if (gcomprisBoard)
 
364
    {
 
365
      if(g_strcasecmp(gcomprisBoard->type, "wordsgame")==0)
 
366
        {
 
367
          /* Set the plugin entry */
 
368
          gcomprisBoard->plugin=&menu_bp;
 
369
 
 
370
          return TRUE;
 
371
        }
 
372
    }
 
373
  return FALSE;
 
374
}
 
375
 
 
376
 
 
377
/*-------------------------------------------------------------------------------*/
 
378
/*-------------------------------------------------------------------------------*/
 
379
/*-------------------------------------------------------------------------------*/
 
380
/*-------------------------------------------------------------------------------*/
 
381
 
 
382
/* set initial values for the next level */
 
383
static void wordsgame_next_level() 
 
384
{
 
385
 
 
386
  gcomprisBoard->number_of_sublevel = 10 + 
 
387
    ((gcomprisBoard->level-1) * 5);
 
388
  gcompris_score_start(SCORESTYLE_NOTE, 
 
389
                       gcomprisBoard->width - 220, 
 
390
                       gcomprisBoard->height - 50, 
 
391
                       gcomprisBoard->number_of_sublevel);
 
392
  
 
393
  gcompris_bar_set_level(gcomprisBoard);
 
394
  gcompris_score_set(gcomprisBoard->sublevel);
 
395
 
 
396
  wordsgame_destroy_all_items();
 
397
 
 
398
  /* Default speed */
 
399
  speed=150;
 
400
  fallSpeed=7000;
 
401
  
 
402
  /* Increase speed only after 5 levels */
 
403
  if(gcomprisBoard->level>5)
 
404
    {
 
405
      fallSpeed=7000-gcomprisBoard->level*200;
 
406
    }
 
407
 
 
408
  pause_board(FALSE);
 
409
}
 
410
 
 
411
static void remove_old_word(LettersItem *item)
 
412
{
 
413
  /* Remove old word */
 
414
  g_hash_table_remove (words_table, (item->word));
 
415
  /* The items are freed by player_win */
 
416
  free(item->word);
 
417
  free(item->overword);           
 
418
  free(item);
 
419
}
 
420
 
 
421
static void wordsgame_move_item(LettersItem *item)
 
422
{
 
423
  double x1, y1, x2, y2;
 
424
 
 
425
  gnome_canvas_item_move(item->rootitem, 0, 2.0);
 
426
 
 
427
  gnome_canvas_item_get_bounds    (item->rootitem,
 
428
                                   &x1,
 
429
                                   &y1,
 
430
                                   &x2,
 
431
                                   &y2);
 
432
  
 
433
  if(y1>gcomprisBoard->height) {
 
434
    item2del_list = g_list_append (item2del_list, item);
 
435
    player_loose();
 
436
  }
 
437
}
 
438
 
 
439
static void wordsgame_destroy_item(LettersItem *item)
 
440
{
 
441
 
 
442
  item_list = g_list_remove (item_list, item);
 
443
  item2del_list = g_list_remove (item2del_list, item);
 
444
  gtk_object_destroy (GTK_OBJECT(item->rootitem));
 
445
  remove_old_word(item);
 
446
}
 
447
 
 
448
/* Destroy items that falls out of the canvas */
 
449
static void wordsgame_destroy_items()
 
450
{
 
451
  LettersItem *item;
 
452
 
 
453
  while(g_list_length(item2del_list)>0) 
 
454
    {
 
455
      item = g_list_nth_data(item2del_list, 0);
 
456
      wordsgame_destroy_item(item);
 
457
    }
 
458
}
 
459
 
 
460
/* Destroy all the items */
 
461
static void wordsgame_destroy_all_items()
 
462
{
 
463
  LettersItem *item;
 
464
 
 
465
  if(item_list == NULL)
 
466
    return;
 
467
 
 
468
  while(g_list_length(item_list)>0) 
 
469
    {
 
470
      item = g_list_nth_data(item_list, 0);
 
471
      wordsgame_destroy_item(item);
 
472
    }
 
473
}
 
474
 
 
475
/*
 
476
 * This does the moves of the game items on the play canvas
 
477
 *
 
478
 */
 
479
static gint wordsgame_move_items (GtkWidget *widget, gpointer data)
 
480
{
 
481
  g_list_foreach (item_list, (GFunc) wordsgame_move_item, NULL);
 
482
 
 
483
  /* Destroy items that falls out of the canvas */
 
484
  wordsgame_destroy_items();
 
485
 
 
486
  dummy_id = gtk_timeout_add (speed, 
 
487
                              (GtkFunction) wordsgame_move_items, NULL);
 
488
 
 
489
  return(FALSE);
 
490
}
 
491
 
 
492
static GnomeCanvasItem *wordsgame_create_item(GnomeCanvasGroup *parent)
 
493
{
 
494
  GnomeCanvasItem *item2;
 
495
  LettersItem *lettersItem;
 
496
  GdkFont *gdk_font;
 
497
 
 
498
  lettersItem = malloc(sizeof(LettersItem));
 
499
 
 
500
  if (!words_table)
 
501
    {
 
502
      words_table= g_hash_table_new (g_str_hash, g_str_equal);
 
503
    }
 
504
 
 
505
  /* Load a gdk font */
 
506
  gdk_font = gdk_font_load (FONT_BOARD_BIG);
 
507
 
 
508
  /* Beware, since we put the words in a hash table, we do not allow the same
 
509
     letter to be displayed two times */
 
510
  do {
 
511
    lettersItem->word = get_random_word();
 
512
  } while(item_find_by_title(lettersItem->word)!=NULL);
 
513
 
 
514
  /* fill up the overword with zeros */
 
515
  lettersItem->overword=calloc(strlen(lettersItem->word), 1);
 
516
 
 
517
  lettersItem->rootitem = \
 
518
    gnome_canvas_item_new (parent,
 
519
                           gnome_canvas_group_get_type (),
 
520
                           "x", (double)(rand()%(gcomprisBoard->width-
 
521
                                                 (guint)(gdk_string_width(gdk_font, 
 
522
                                                                          lettersItem->word)))),
 
523
                           "y", (double) -gdk_string_height(gdk_font, lettersItem->word),
 
524
                           NULL);
 
525
 
 
526
  /* To 'erase' words, I create 2 times the text item. One is empty now */
 
527
  /* It will be filled each time the user enters the right key         */  
 
528
  item2 = \
 
529
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(lettersItem->rootitem),
 
530
                           gnome_canvas_text_get_type (),
 
531
                           "text", lettersItem->word,
 
532
                           "font_gdk", gdk_font,
 
533
                           "x", (double) 0,
 
534
                           "y", (double) 0,
 
535
                           "anchor", GTK_ANCHOR_NW,
 
536
                           "fill_color", "white",
 
537
                           NULL);
 
538
 
 
539
  lettersItem->overwriteItem = \
 
540
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(lettersItem->rootitem),
 
541
                           gnome_canvas_text_get_type (),
 
542
                           "text", "",
 
543
                           "font_gdk", gdk_font,
 
544
                           "x", (double) 0,
 
545
                           "y", (double) 0,
 
546
                           "anchor", GTK_ANCHOR_NW,
 
547
                           "fill_color", "green",
 
548
                           NULL);
 
549
 
 
550
  item_list = g_list_append (item_list, lettersItem);
 
551
 
 
552
  /* Add word to hash table of all falling words. */
 
553
  g_hash_table_insert (words_table, lettersItem->word, lettersItem);
 
554
 
 
555
  return (lettersItem->rootitem);
 
556
}
 
557
 
 
558
static void wordsgame_add_new_item() 
 
559
{
 
560
  wordsgame_create_item(gnome_canvas_root(gcomprisBoard->canvas));
 
561
}
 
562
 
 
563
/*
 
564
 * This is called on a low frequency and is used to drop new items
 
565
 *
 
566
 */
 
567
static gint wordsgame_drop_items (GtkWidget *widget, gpointer data)
 
568
{
 
569
  wordsgame_add_new_item();
 
570
 
 
571
  drop_items_id = gtk_timeout_add (fallSpeed,
 
572
                                   (GtkFunction) wordsgame_drop_items, NULL);
 
573
  return (FALSE);
 
574
}
 
575
 
 
576
static void player_win(LettersItem *item)
 
577
{
 
578
 
 
579
  wordsgame_destroy_item(item);
 
580
  gcompris_play_sound (SOUNDLISTFILE, "gobble");
 
581
 
 
582
  gcomprisBoard->sublevel++;
 
583
  gcompris_score_set(gcomprisBoard->sublevel);
 
584
 
 
585
  if(gcomprisBoard->sublevel>gcomprisBoard->number_of_sublevel) 
 
586
    {
 
587
      /* Try the next level */
 
588
      gcomprisBoard->level++;
 
589
      gcomprisBoard->sublevel = 1;
 
590
      if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
 
591
        board_finished(BOARD_FINISHED_RANDOM);
 
592
        return;
 
593
      }
 
594
      wordsgame_next_level();
 
595
      gcompris_play_sound (SOUNDLISTFILE, "bonus");
 
596
    }
 
597
  else
 
598
    {
 
599
      /* Drop a new item now to speed up the game */
 
600
      if(g_list_length(item_list)==0)
 
601
        {
 
602
          if (drop_items_id) {
 
603
            /* Remove pending new item creation to sync the falls */
 
604
            gtk_timeout_remove (drop_items_id);
 
605
            drop_items_id = 0;
 
606
          }
 
607
          if(!drop_items_id) {
 
608
            drop_items_id = gtk_timeout_add (0,
 
609
                                             (GtkFunction) wordsgame_drop_items, NULL);
 
610
          }
 
611
        }
 
612
    }
 
613
}
 
614
 
 
615
static void player_loose()
 
616
{
 
617
  gcompris_play_sound (SOUNDLISTFILE, "crash");
 
618
}
 
619
 
 
620
static LettersItem *
 
621
item_find_by_title (const gchar *title)
 
622
{
 
623
  if (!words_table)
 
624
    return NULL;
 
625
  
 
626
  return g_hash_table_lookup (words_table, title);
 
627
}
 
628
 
 
629
static FILE *get_wordfile(char *locale)
 
630
{
 
631
  char *filename;
 
632
  FILE *wordsfd = NULL;
 
633
 
 
634
  /* First Try to find a file matching the level and the locale */
 
635
  filename = g_strdup_printf("%s%s%d.%.2s",  
 
636
                             PACKAGE_DATA_DIR, "/wordsgame/wordslevel", 
 
637
                             gcomprisBoard->level, locale);
 
638
  g_message("Trying to open file %s ", filename);
 
639
  wordsfd = fopen (filename, "r");
 
640
 
 
641
  if(wordsfd==NULL)
 
642
    {
 
643
      /* Second Try to find a file matching the 'max' and the locale */
 
644
      sprintf(filename, "%s%s%.2s",  
 
645
              PACKAGE_DATA_DIR, "/wordsgame/wordslevelmax.", 
 
646
              locale);
 
647
      g_message("Trying to open file %s ", filename);
 
648
 
 
649
      wordsfd = fopen (filename, "r");
 
650
    }
 
651
 
 
652
  g_free(filename);
 
653
 
 
654
  return wordsfd;
 
655
}
 
656
/*
 
657
 * Return a random word from a set of text file depending on 
 
658
 * the current level and language
 
659
 */
 
660
static char *get_random_word()
 
661
{
 
662
  FILE *wordsfd;
 
663
  long size, i;
 
664
  char *str;
 
665
 
 
666
  str = malloc(MAXWORDSLENGTH);
 
667
 
 
668
  wordsfd = get_wordfile(gcompris_get_locale());
 
669
 
 
670
  if(wordsfd==NULL)
 
671
    {
 
672
      /* Try to open the english locale by default */
 
673
      wordsfd = get_wordfile("en");
 
674
      
 
675
      /* Too bad, even english is not there. Check your Install */
 
676
      if(wordsfd==NULL)
 
677
        g_error("Cannot open file %s : Check your GCompris install", strerror(errno));
 
678
    }
 
679
 
 
680
  fseek (wordsfd, 0L, SEEK_END);
 
681
  size = ftell (wordsfd);
 
682
 
 
683
  i=rand()%size;
 
684
  fseek(wordsfd, i, SEEK_SET);
 
685
 
 
686
  /* Read 2 times so that we are sure to sync on an end of line */
 
687
  fgets(str, MAXWORDSLENGTH, wordsfd);
 
688
  if(ftell(wordsfd)==size)
 
689
    rewind(wordsfd);
 
690
  fgets(str, MAXWORDSLENGTH, wordsfd);
 
691
 
 
692
  /* Chop the return */
 
693
  str[strlen(str)-1]='\0';
 
694
 
 
695
  fclose(wordsfd);
 
696
 
 
697
  return (str);
 
698
}
 
699
 
 
700
 
 
701
/* Local Variables: */
 
702
/* mode:c */
 
703
/* eval:(load-library "time-stamp") */
 
704
/* eval:(make-local-variable 'write-file-hooks) */
 
705
/* eval:(add-hook 'write-file-hooks 'time-stamp) */
 
706
/* eval:(setq time-stamp-format '(time-stamp-yyyy/mm/dd time-stamp-hh:mm:ss user-login-name)) */
 
707
/* End: */