summaryrefslogtreecommitdiff
path: root/libgame/game-board.c
blob: a7a51a4b5f08c7b2781acc1ae4c879d9d3944c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
232
233
234
235
236
237
238
239
240
241
242
243
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 *  Copyright (C) 2003 Benjamin Otte <otte@gnome.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "game-private.h"
#include "game-board.h"
#include "game-sprite.h"
#include "game-actor.h"
#include "game-filter.h"
#include "game-container.h"

#include <string.h>
#include <math.h>
#include <gobject/gvaluecollector.h>

enum {
  PROP_0,
  PROP_COLLISIONS,
  PROP_REPEAT
};

GameGraphicClass *parent_class;

static void
game_board_draw_repeated (GameBoard *board, cairo_t *cr)
{
  GameRectangle rect, area = GAME_GRAPHIC (board)->rect;
  GamePoint p, move_x = { - board->repeat.x, 0 }, move_y = { 0, - board->repeat.y };
  GSList *walk;
  double tmp;
  int x_start, x_stop;
  int y, y_start, y_stop;

  if (board->repeat.x > 0.0) {
    area.x1 = MAX (area.x1, 0);
    area.x2 = MIN (area.x2, board->repeat.x);
  }
  if (board->repeat.y > 0.0) {
    area.y1 = MAX (area.y1, 0);
    area.y2 = MIN (area.y2, board->repeat.y);
  }
  if (!GAME_RECTANGLE_IS_VALID (&area))
    return;
  
  for (walk = board->sprites; walk; walk = g_slist_next (walk)) {
    GameSprite *sprite = walk->data;
    if (!sprite->graphic)
      continue;
    game_sprite_get_area (sprite, &rect);
    if (board->repeat.x > 0.0) {
      tmp = area.x1 - rect.x2;
      tmp /= board->repeat.x;
      x_start = floor (tmp + 1.0);
      tmp = area.x2 - rect.x1;
      tmp /= board->repeat.x;
      x_stop = ceil (tmp);
    } else if (area.x1 >= rect.x2 || area.x2 <= rect.x1) {
      continue;
    } else {
      x_start = 0;
      x_stop = 1;
    }
    if (board->repeat.y > 0.0) {
      tmp = area.y1 - rect.y2;
      tmp /= board->repeat.y;
      y_start = floor (tmp + 1.0);
      tmp = area.y2 - rect.y1;
      tmp /= board->repeat.y;
      y_stop = ceil (tmp);
    } else if (area.y1 >= rect.y2 || area.y2 <= rect.y1) {
      continue;
    } else {
      y_start = 0;
      y_stop = 1;
    }
    p.x = - x_start * board->repeat.x - sprite->pos.x;
    p.y = - y_start * board->repeat.y - sprite->pos.y;
    game_rectangle_move (&rect, &area, &p);
    move_x.y = - (y_start - y_stop) * board->repeat.y;
    for (; x_start < x_stop; x_start++) {
      for (y = y_start; y < y_stop; y++) {
	cairo_save (cr);
	cairo_translate (cr, x_start * board->repeat.x + sprite->pos.x, 
	    y * board->repeat.y + sprite->pos.y);
	game_graphic_draw (sprite->graphic, cr);
	cairo_restore (cr);
	game_rectangle_move (&rect, &rect, &move_y);
      }
      game_rectangle_move (&rect, &rect, &move_x);
    }
  }
}

static void
game_board_draw_unrepeated (GameBoard *board, cairo_t *cr)
{
  GSList *walk;

  for (walk = board->sprites; walk; walk = g_slist_next (walk)) {
    GameSprite *sprite = walk->data;
    if (!sprite->graphic)
      continue;
    cairo_save (cr);
    cairo_translate (cr, sprite->pos.x, sprite->pos.y);
    game_graphic_draw (sprite->graphic, cr);
    cairo_restore (cr);
  }
}

static void
game_board_draw (GameGraphic *graphic, cairo_t *cr)
{
  GameBoard *board = GAME_BOARD (graphic);
  
  if (board->repeat.x > 0.0 || board->repeat.y > 0.0)
    game_board_draw_repeated (board, cr);
  else
    game_board_draw_unrepeated (board, cr);
}

static gchar *
game_board_to_string (GameObject *object)
{
  GameBoard *board = GAME_BOARD (object);

  return g_strdup_printf ("%s %p with %u sprites", G_OBJECT_TYPE_NAME(object), 
      object, g_slist_length (board->sprites));
}

static void
game_board_dispose (GObject* object)
{
  GSList *walk;
  GameBoard *board = GAME_BOARD (object);
  
  for (walk = board->sprites; walk; walk = g_slist_next (walk)) {
    GameSprite *sprite = GAME_SPRITE (walk->data);
    game_container_remove (GAME_CONTAINER (board), GAME_OBJECT (sprite));
    sprite->board = NULL;
    g_object_unref (sprite);
  }
  g_slist_free (board->sprites);
  board->sprites = NULL;
  if (board->actors) {
    g_object_unref (board->actors);
    board->actors = NULL;
  }
  g_assert (game_container_get_size (GAME_CONTAINER (board)) == 0);

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

static void
game_board_get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
{
  GameBoard *board = GAME_BOARD (object);
  
  switch (param_id) {
    case PROP_COLLISIONS:
      g_value_set_boolean (value, board->collisions);
      break;
    case PROP_REPEAT:
      g_value_set_boxed (value, &board->repeat);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
game_board_set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
{
  GameBoard *board = GAME_BOARD (object);
  
  switch (param_id) {
    case PROP_COLLISIONS:
      board->collisions = g_value_get_boolean (value);
      break;
    case PROP_REPEAT:
      if (g_value_get_boxed (value))
	board->repeat = *((GamePoint *) g_value_get_boxed (value));
      else
	board->repeat.x = board->repeat.y = 0.0;
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
game_board_game_set (GameObject *object)
{
  GameBoard *board = GAME_BOARD (object);
  GameGame *game = object->game;

  board->actors = game_filter_new_for_type (game, GAME_CONTAINER (board), GAME_TYPE_ACTOR);
}

static void
game_board_flush (GameObject *object)
{
  game_container_flush (GAME_CONTAINER (object));
}

static void
game_board_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GameObjectClass *gameobject_class = GAME_OBJECT_CLASS (g_class);
  GameGraphicClass *graphic_class = GAME_GRAPHIC_CLASS (g_class);
  
  parent_class = g_type_class_peek_parent (g_class);
  
  object_class->set_property = game_board_set_property;
  object_class->get_property = game_board_get_property;
  object_class->dispose = game_board_dispose;

  g_object_class_install_property (object_class, PROP_COLLISIONS,
      g_param_spec_boolean ("collisions", _("collisions"), _("switch off to make actors not collide"),
	  TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  g_object_class_install_property (object_class, PROP_REPEAT,
      g_param_spec_boxed ("repeat", _("repeat"), _("at which point the board repeats"),
	  GAME_TYPE_POINT, G_PARAM_READWRITE));

  gameobject_class->flush = game_board_flush;
  gameobject_class->to_string = game_board_to_string;
  gameobject_class->game_set = game_board_game_set;

  graphic_class->draw = game_board_draw;
}

static void
game_board_init (GTypeInstance *instance, gpointer g_class)
{
  GameBoard *board = GAME_BOARD (instance);

  board->sprite_type = GAME_TYPE_SPRITE;
}

static void
game_board_container_init (gpointer g_iface, gpointer iface_data)
{
  GameContainerInterface *iface = g_iface;

  iface->offset = G_STRUCT_OFFSET (GameBoard, sprite_data);
}

GType
game_board_get_type ()
{
  static GType board_type = 0;
  
  if (!board_type) {
    static const GTypeInfo board_info = {
      sizeof (GameBoardClass),
      NULL,
      NULL,
      game_board_class_init,
      NULL,
      NULL,
      sizeof (GameBoard),
      0,
      game_board_init,
    };
    static const GInterfaceInfo container_info = {
      game_board_container_init,
      NULL,
      NULL,
    };
    
    board_type = g_type_register_static (GAME_TYPE_GRAPHIC, "GameBoard", &board_info, 0);
    g_type_add_interface_static (board_type, GAME_TYPE_CONTAINER,
        &container_info);
  }
  
  return board_type;
}

GameBoard *
game_board_new (GameGame *game)
{
  GameBoard *board;
 
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  
  /* FIXME: can we get this going with G_MAXDOUBLE * 2 width? */
  board = GAME_BOARD (game_game_add_object (game, GAME_TYPE_BOARD, 
      "permanent", TRUE, 
      "x", G_MAXDOUBLE / 2, "y", G_MAXDOUBLE / 2, 
      "width", G_MAXDOUBLE, "height", G_MAXDOUBLE, NULL));

  return board; 
}

GameSprite *
game_board_add_sprite (GameBoard *board, GType object_type, ...)
{
  va_list args;
  GameSprite *sprite;
  
  g_return_val_if_fail (GAME_IS_BOARD (board), NULL);
  g_return_val_if_fail (g_type_is_a (object_type, board->sprite_type), NULL);

  va_start (args, object_type);
  sprite = game_board_add_sprite_valist (board, object_type, args);
  va_end (args);
  
  return sprite;
}

GameSprite *
game_board_add_sprite_valist (GameBoard *board, GType object_type,
    va_list varargs)
{
  GObjectClass *class;
  GParameter *params;
  const gchar *name;
  GameSprite *sprite;
  guint n_params = 0, n_alloced_params = 16;
  
  g_return_val_if_fail (GAME_IS_BOARD (board), NULL);
  g_return_val_if_fail (g_type_is_a (object_type, GAME_TYPE_SPRITE), NULL);

  class = g_type_class_ref (object_type);
  
  name = va_arg (varargs, gchar*);
  if (name) {
    params = g_new (GParameter, n_alloced_params);
    while (name) {
      gchar *error = NULL;
      GParamSpec *pspec = g_object_class_find_property (class, name);
      if (!pspec) {
	g_warning ("%s: object class `%s' has no property named `%s'", G_STRFUNC,
	    g_type_name (object_type), name);
	break;
      }
      if (n_params >= n_alloced_params) {
	n_alloced_params += 16;
	params = g_renew (GParameter, params, n_alloced_params);
      }
      params[n_params].name = name;
      params[n_params].value.g_type = 0;
      g_value_init (&params[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
      G_VALUE_COLLECT (&params[n_params].value, varargs, 0, &error);
      if (error) {
	g_warning ("%s: %s", G_STRFUNC, error);
      g_free (error);
      g_value_unset (&params[n_params].value);
      break;
      }
      n_params++;
      name = va_arg (varargs, gchar*);
    }
  } else {
    params = NULL;
    n_params = 0;
  }
  
  sprite = game_board_add_spritev (board, object_type, n_params, params);

  while (n_params--)
    g_value_unset (&params[n_params].value);
  g_free (params);

  g_type_class_unref (class);

  return sprite;
}

int
game_compare_sprite_layer (gconstpointer a, gconstpointer b)
{
  return GAME_SPRITE (a)->layer - GAME_SPRITE (b)->layer;
}

GameSprite *
game_board_add_spritev (GameBoard *board, GType object_type, guint n_parameters, 
    GParameter *parameters)
{
  GameSprite *sprite;
  GParameter all_parameters[n_parameters + 2];
  GameRectangle rect;
  
  g_return_val_if_fail (GAME_IS_BOARD (board), NULL);
  g_return_val_if_fail (g_type_is_a (object_type, GAME_TYPE_SPRITE), NULL);
  g_return_val_if_fail (parameters != NULL || n_parameters == 0, NULL);

  if (n_parameters)
    memcpy (all_parameters + 1, parameters, sizeof (GParameter) * n_parameters);
  all_parameters[0].name = "board";
  all_parameters[0].value.g_type = 0;
  g_value_init (&all_parameters[0].value, GAME_TYPE_BOARD);
  g_value_set_object (&all_parameters[0].value, board);
  
  sprite = GAME_SPRITE (game_game_add_objectv (GAME_OBJECT (board)->game, 
      object_type, n_parameters + 1, all_parameters));
  
  g_value_unset (&all_parameters[0].value);
  
  game_container_add (GAME_CONTAINER (board), GAME_OBJECT (sprite));
  board->sprites = g_slist_insert_sorted (board->sprites, sprite, game_compare_sprite_layer);
  game_sprite_get_area (sprite, &rect);
  game_graphic_invalidate_area (GAME_GRAPHIC (board), &rect);

  return sprite;
}

void
_game_board_sprite_changed_layer (GameBoard *board, GameSprite *sprite)
{
  GameRectangle rect;
  GSList *list;
  
  list = g_slist_find (board->sprites, sprite);
  if (list) {
    board->sprites = g_slist_remove (board->sprites, sprite);
    board->sprites = g_slist_insert_sorted (board->sprites, sprite, game_compare_sprite_layer);
  }

  game_sprite_get_area (sprite, &rect);
  game_graphic_invalidate_area (GAME_GRAPHIC (sprite->board), &rect);
}