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

« back to all changes in this revision

Viewing changes to src/boards/erase.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 - erase.c
 
2
 *
 
3
 * Copyright (C) 2001 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 2 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, write to the Free Software
 
17
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
#include <ctype.h>
 
21
#include <math.h>
 
22
#include <assert.h>
 
23
 
 
24
#include "gcompris/gcompris.h"
 
25
 
 
26
#define SOUNDLISTFILE PACKAGE
 
27
 
 
28
GcomprisBoard *gcomprisBoard = NULL;
 
29
gboolean board_paused = TRUE;
 
30
 
 
31
static void      start_board (GcomprisBoard *agcomprisBoard);
 
32
static void      pause_board (gboolean pause);
 
33
static void      end_board (void);
 
34
static gboolean  is_our_board (GcomprisBoard *gcomprisBoard);
 
35
static void      set_level (guint level);
 
36
static int       gamewon;
 
37
static void      game_won(void);
 
38
 
 
39
#define VERTICAL_SEPARATION 30
 
40
#define HORIZONTAL_SEPARATION 30
 
41
#define TEXT_COLOR "white"
 
42
 
 
43
static GnomeCanvasGroup *boardRootItem = NULL;
 
44
 
 
45
static GnomeCanvasItem  *erase_create_item(GnomeCanvasGroup *parent);
 
46
static void              erase_destroy_all_items(void);
 
47
static void              erase_next_level(void);
 
48
static gint              item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
 
49
 
 
50
static int number_of_item = 0;
 
51
static int number_of_item_x = 0;
 
52
static int number_of_item_y = 0;
 
53
 
 
54
// List of images to use in the game
 
55
static gchar *imageList[] =
 
56
{
 
57
  "gcompris/animals/bear001.jpg",
 
58
  "gcompris/animals/malaybear002.jpg",
 
59
  "gcompris/animals/polabear011.jpg",
 
60
  "gcompris/animals/spectbear001.jpg",
 
61
  "gcompris/animals/joybear001.jpg",
 
62
  "gcompris/animals/polarbear001.jpg",
 
63
  "gcompris/animals/joybear002.jpg",
 
64
  "gcompris/animals/poolbears001.jpg"
 
65
};
 
66
#define NUMBER_OF_IMAGES 8
 
67
 
 
68
/* Description of this plugin */
 
69
BoardPlugin menu_bp =
 
70
  {
 
71
    NULL,
 
72
    NULL,
 
73
    N_("Move the mouse"),
 
74
    N_("Move the mouse to erase the area and discover the background"),
 
75
    "Bruno Coudoin <bruno.coudoin@free.fr>",
 
76
    NULL,
 
77
    NULL,
 
78
    NULL,
 
79
    NULL,
 
80
    start_board,
 
81
    pause_board,
 
82
    end_board,
 
83
    is_our_board,
 
84
    NULL,
 
85
    NULL,
 
86
    set_level,
 
87
    NULL,
 
88
    NULL
 
89
  };
 
90
 
 
91
/*
 
92
 * Main entry point mandatory for each Gcompris's game
 
93
 * ---------------------------------------------------
 
94
 *
 
95
 */
 
96
 
 
97
BoardPlugin
 
98
*get_bplugin_info(void)
 
99
{
 
100
  return &menu_bp;
 
101
}
 
102
 
 
103
/*
 
104
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 
105
 *
 
106
 */
 
107
static void pause_board (gboolean pause)
 
108
{
 
109
  if(gcomprisBoard==NULL)
 
110
    return;
 
111
 
 
112
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
 
113
    {
 
114
      game_won();
 
115
    }
 
116
 
 
117
  board_paused = pause;
 
118
}
 
119
 
 
120
/*
 
121
 */
 
122
static void start_board (GcomprisBoard *agcomprisBoard)
 
123
{
 
124
 
 
125
  if(agcomprisBoard!=NULL)
 
126
    {
 
127
      gcomprisBoard=agcomprisBoard;
 
128
      gcomprisBoard->level=1;
 
129
      gcomprisBoard->maxlevel=2;
 
130
      gcomprisBoard->sublevel=1;
 
131
      gcomprisBoard->number_of_sublevel=1; /* Go to next level after this number of 'play' */
 
132
      gcompris_bar_set(GCOMPRIS_BAR_LEVEL);
 
133
 
 
134
      erase_next_level();
 
135
 
 
136
      gamewon = FALSE;
 
137
      pause_board(FALSE);
 
138
    }
 
139
}
 
140
/* ======================================= */
 
141
static void end_board ()
 
142
{
 
143
  if(gcomprisBoard!=NULL)
 
144
    {
 
145
      pause_board(TRUE);
 
146
      erase_destroy_all_items();
 
147
    }
 
148
  gcomprisBoard = NULL;
 
149
}
 
150
 
 
151
/* ======================================= */
 
152
static void set_level (guint level)
 
153
{
 
154
 
 
155
  if(gcomprisBoard!=NULL)
 
156
    {
 
157
      gcomprisBoard->level=level;
 
158
      gcomprisBoard->sublevel=1;
 
159
      erase_next_level();
 
160
    }
 
161
}
 
162
/* ======================================= */
 
163
gboolean is_our_board (GcomprisBoard *gcomprisBoard)
 
164
{
 
165
  if (gcomprisBoard)
 
166
    {
 
167
      if(g_strcasecmp(gcomprisBoard->type, "erase")==0)
 
168
        {
 
169
          /* Set the plugin entry */
 
170
          gcomprisBoard->plugin=&menu_bp;
 
171
 
 
172
          return TRUE;
 
173
        }
 
174
    }
 
175
  return FALSE;
 
176
}
 
177
 
 
178
/*-------------------------------------------------------------------------------*/
 
179
/*-------------------------------------------------------------------------------*/
 
180
/* set initial values for the next level */
 
181
static void erase_next_level()
 
182
{
 
183
 
 
184
  gcompris_set_background(gnome_canvas_root(gcomprisBoard->canvas),
 
185
                          imageList[RAND(0, NUMBER_OF_IMAGES-1)]);
 
186
 
 
187
  gcompris_bar_set_level(gcomprisBoard);
 
188
 
 
189
  erase_destroy_all_items();
 
190
  gamewon = FALSE;
 
191
 
 
192
  /* Select level difficulty */
 
193
  number_of_item_x = gcomprisBoard->level*5;
 
194
  number_of_item_y = gcomprisBoard->level*5;
 
195
 
 
196
  /* Try the next level */
 
197
  erase_create_item(gnome_canvas_root(gcomprisBoard->canvas));
 
198
}
 
199
/* ==================================== */
 
200
/* Destroy all the items */
 
201
static void erase_destroy_all_items()
 
202
{
 
203
  if(boardRootItem!=NULL)
 
204
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
 
205
 
 
206
  boardRootItem = NULL;
 
207
}
 
208
/* ==================================== */
 
209
static GnomeCanvasItem *erase_create_item(GnomeCanvasGroup *parent)
 
210
{
 
211
  int i,j;
 
212
  GnomeCanvasItem *item = NULL;
 
213
 
 
214
  boardRootItem = GNOME_CANVAS_GROUP(
 
215
                                     gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
 
216
                                                            gnome_canvas_group_get_type (),
 
217
                                                            "x", (double) 0,
 
218
                                                            "y", (double) 0,
 
219
 
 
220
                                                            NULL));
 
221
 
 
222
  number_of_item = 0;
 
223
 
 
224
  for(i=0; i<BOARDWIDTH; i+=BOARDWIDTH/number_of_item_x)
 
225
    {
 
226
      for(j=0; j<BOARDHEIGHT; j+=BOARDHEIGHT/number_of_item_y)
 
227
        {
 
228
 
 
229
          item = gnome_canvas_item_new (boardRootItem,
 
230
                                        gnome_canvas_rect_get_type (),
 
231
                                        "x1", (double) i,
 
232
                                        "y1", (double) j,
 
233
                                        "x2", (double) i+BOARDWIDTH/number_of_item_x,
 
234
                                        "y2", (double)  j+BOARDHEIGHT/number_of_item_y,
 
235
                                        "fill_color", "blue",
 
236
                                        "outline_color", "green",
 
237
                                        "width_units", (double)1,
 
238
                                        NULL);
 
239
 
 
240
          gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, NULL);
 
241
          number_of_item++;
 
242
        }
 
243
    }
 
244
 
 
245
  return NULL;
 
246
}
 
247
/* ==================================== */
 
248
static void game_won()
 
249
{
 
250
  gcomprisBoard->sublevel++;
 
251
 
 
252
  if(gcomprisBoard->sublevel>gcomprisBoard->number_of_sublevel) {
 
253
    /* Try the next level */
 
254
    gcomprisBoard->sublevel=1;
 
255
    gcomprisBoard->level++;
 
256
    if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
 
257
      board_finished(BOARD_FINISHED_RANDOM);
 
258
      return;
 
259
    }
 
260
    gcompris_play_sound (SOUNDLISTFILE, "bonus");
 
261
  }
 
262
  erase_next_level();
 
263
}
 
264
 
 
265
/* ==================================== */
 
266
static gint
 
267
item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
 
268
{
 
269
  double item_x, item_y;
 
270
  item_x = event->button.x;
 
271
  item_y = event->button.y;
 
272
  gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
 
273
 
 
274
  if(board_paused)
 
275
    return FALSE;
 
276
 
 
277
  gtk_object_destroy(GTK_OBJECT(item));
 
278
 
 
279
  if(--number_of_item == 0)
 
280
    {
 
281
      gamewon = TRUE;
 
282
      erase_destroy_all_items();
 
283
      gcompris_display_bonus(gamewon, BONUS_SMILEY);
 
284
    }
 
285
  
 
286
  return FALSE;
 
287
}