File: sge_core.c

package info (click to toggle)
gweled 0.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,468 kB
  • ctags: 516
  • sloc: ansic: 4,170; sh: 2,082; makefile: 100
file content (788 lines) | stat: -rw-r--r-- 20,984 bytes parent folder | download | duplicates (4)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/* Gweled - Seb's Graphic Engine (SGE)
 *
 * Copyright (C) 2003-2005 Sebastien Delestaing <sebastien.delestaing@wanadoo.fr>
 * Copyright (C) 2010 Daniele Napolitano <dnax88@gmail.com>
 *
 * 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
 * of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/* Levels:
 * 0 -> board background
 * 1 -> gems
 * 2 -> cursor
 * 3 -> text
 */

#include <gtk/gtk.h>
#include <math.h>
#include "sge_core.h"

#define ACCELERATION	1.0
#define SGE_TIMER_INTERVAL  20

// LOCAL FUNCTIONS
void invalidate_objects_above (T_SGEObject * object);
void invalidate_background_beneath (T_SGEObject * object);

// LOCAL VARS
static GRand *g_rand_generator;
static guint g_main_loop_id;
static GList *g_object_list;
static GdkPixmap *g_pixmap_buffer;
static GdkPixbuf **g_pixbufs;
static gint g_width, g_height;
static gint gi_nb_pixbufs;

static GtkWidget *g_drawing_area = NULL;

static gboolean layers_visibility[5] = {TRUE, TRUE, TRUE, TRUE, TRUE};

// helper functions
gint
compare_by_layer (gconstpointer a, gconstpointer b)
{
	return SGE_OBJECT(a)->layer - SGE_OBJECT(b)->layer;
}

// main loop functions
void
draw_object (gpointer object, gpointer user_data)
{
	int x, y;
	GdkGC *gc;
	GdkPixbuf *pixbuffer;


    if((SGE_OBJECT(object)->fadeout && SGE_OBJECT(object)->opacity <= 0)
    || (SGE_OBJECT(object)->zoomout && SGE_OBJECT(object)->zoom <= 0))
    {
	    sge_destroy_object (SGE_OBJECT(object), NULL);
        return;
    }

    if ((int) SGE_OBJECT(object)->needs_drawing
        && layers_visibility[SGE_OBJECT(object)->layer]
        && SGE_OBJECT(object)->opacity > 0) {

		x = (int) SGE_OBJECT(object)->x;
		y = (int) SGE_OBJECT(object)->y;

		if (SGE_OBJECT(object)->pre_rendered) {
			gc = gdk_gc_new (GDK_DRAWABLE (g_pixmap_buffer));
			gdk_draw_drawable (GDK_DRAWABLE (g_pixmap_buffer),
					   gc,
					   GDK_DRAWABLE (SGE_OBJECT(object)->pre_rendered),
					   0, 0, x, y,
					   SGE_OBJECT(object)->width,
					   SGE_OBJECT(object)->height);
			g_object_unref (gc);

		} else {

            if (SGE_OBJECT(object)->opacity < 255
                || SGE_OBJECT(object)->zoom != 1.0)
            {

                pixbuffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
                        TRUE, 8,
                        SGE_OBJECT(object)->width,
                        SGE_OBJECT(object)->height);

                gdk_pixbuf_fill (pixbuffer, 0x00000000);
                gdk_pixbuf_composite(g_pixbufs[SGE_OBJECT(object)->pixbuf_id],
                        pixbuffer,
                        0,
                        0,
                        SGE_OBJECT(object)->width,
                        SGE_OBJECT(object)->height,
                        0, 0,
                        SGE_OBJECT(object)->zoom,
                        SGE_OBJECT(object)->zoom,
                        GDK_INTERP_NEAREST,
                        SGE_OBJECT(object)->opacity);

                if(SGE_OBJECT(object)->animation)

                    gdk_draw_pixbuf (GDK_DRAWABLE (g_pixmap_buffer),
                            NULL, pixbuffer,
                            ((int)SGE_OBJECT(object)->animation_iter) * SGE_OBJECT(object)->height,
                            0,
                            x, y,
                            SGE_OBJECT(object)->height,
                            SGE_OBJECT(object)->height,
                            GDK_RGB_DITHER_NONE, 0, 0);
                else
                    gdk_draw_pixbuf (GDK_DRAWABLE (g_pixmap_buffer),
                            NULL, pixbuffer,
                            0, 0,
                            x +
                                (SGE_OBJECT(object)->width -
                                 SGE_OBJECT(object)->width *
                                 SGE_OBJECT(object)->zoom) / 2,
                            y +
                                (SGE_OBJECT(object)->height -
                                 SGE_OBJECT(object)->height *
                                 SGE_OBJECT(object)->zoom) / 2,
                            SGE_OBJECT(object)->width * SGE_OBJECT(object)->zoom,
                            SGE_OBJECT(object)->height * SGE_OBJECT(object)->zoom,
                            GDK_RGB_DITHER_NONE, 0, 0);

                g_object_unref(pixbuffer);

            } else if(SGE_OBJECT(object)->blink) {
                pixbuffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
                        TRUE, 8,
                        SGE_OBJECT(object)->width,
                        SGE_OBJECT(object)->height);

                gdk_pixbuf_fill (pixbuffer, 0x00000000);

                gdk_pixbuf_saturate_and_pixelate(
                            g_pixbufs[SGE_OBJECT(object)->pixbuf_id],
                            pixbuffer,
                            SGE_OBJECT(object)->saturation,
                            FALSE);

                gdk_draw_pixbuf (GDK_DRAWABLE (g_pixmap_buffer),
                        NULL, pixbuffer,
                        0, 0, x, y,
                        SGE_OBJECT(object)->width,
                        SGE_OBJECT(object)->height,
                        GDK_RGB_DITHER_NONE, 0, 0);

               g_object_unref(pixbuffer);

            } else {
                if(SGE_OBJECT(object)->animation) {
                    // width of each sprite of animation is the object height
                    gdk_draw_pixbuf (GDK_DRAWABLE (g_pixmap_buffer),
                            NULL, g_pixbufs[SGE_OBJECT(object)->pixbuf_id],
                            ((int)SGE_OBJECT(object)->animation_iter) * SGE_OBJECT(object)->height,
                            0,
                            x, y,
                            SGE_OBJECT(object)->height,
                            SGE_OBJECT(object)->height,
                            GDK_RGB_DITHER_NONE, 0, 0);

                } else {

                    // draw pixbuf without effects
                    gdk_draw_pixbuf (GDK_DRAWABLE (g_pixmap_buffer),
                            NULL, g_pixbufs[SGE_OBJECT(object)->pixbuf_id],
                            0, 0, x, y,
                            SGE_OBJECT(object)->width,
                            SGE_OBJECT(object)->height,
                            GDK_RGB_DITHER_NONE, 0, 0);
                }
            }
        }
        // request area drawing
        gtk_widget_queue_draw_area (g_drawing_area, x, y,
				SGE_OBJECT(object)->width,
				SGE_OBJECT(object)->height);

		SGE_OBJECT(object)->needs_drawing = 0;

		invalidate_objects_above (SGE_OBJECT(object));
	}

    if(SGE_OBJECT(object)->fadeout) {
        SGE_OBJECT(object)->opacity -= 30;
        invalidate_background_beneath (SGE_OBJECT(object));
    }
    if(SGE_OBJECT(object)->zoomout) {
        SGE_OBJECT(object)->zoom -= 0.20;
        invalidate_background_beneath (SGE_OBJECT(object));
    }
    if(SGE_OBJECT(object)->blink) {
        if(SGE_OBJECT(object)->saturation >= 2.0)
            SGE_OBJECT(object)->blink_increase = FALSE;

        if(SGE_OBJECT(object)->saturation <= 0.4)
            SGE_OBJECT(object)->blink_increase = TRUE;

        if(SGE_OBJECT(object)->blink_increase)
            SGE_OBJECT(object)->saturation += 0.2;
        else
            SGE_OBJECT(object)->saturation -= 0.2;

         invalidate_background_beneath (SGE_OBJECT(object));
    }
    if(SGE_OBJECT(object)->animation) {
        SGE_OBJECT(object)->animation_iter += 0.5;

        if(SGE_OBJECT(object)->animation_iter * SGE_OBJECT(object)->height == SGE_OBJECT(object)->width) {
            if(SGE_OBJECT(object)->animation_repeat)
                SGE_OBJECT(object)->animation_iter = 0;
            else {
                sge_destroy_object (SGE_OBJECT(object), NULL);
                return;
            }
        }
        invalidate_background_beneath (SGE_OBJECT(object));
    }
}

void
move_object (gpointer object, gpointer user_data)
{
    if(SGE_OBJECT(object)->y_delay > 0) {
        SGE_OBJECT(object)->y_delay--;
        return;
    }

	SGE_OBJECT(object)->vx += SGE_OBJECT(object)->ax;
	SGE_OBJECT(object)->vy += SGE_OBJECT(object)->ay;

	if (sge_object_is_moving (SGE_OBJECT(object)))
	{
		invalidate_background_beneath (SGE_OBJECT(object));
		SGE_OBJECT(object)->x += SGE_OBJECT(object)->vx;
		SGE_OBJECT(object)->y += SGE_OBJECT(object)->vy;
		SGE_OBJECT(object)->needs_drawing |= 0x02;
	}

	if (SGE_OBJECT(object)->stop_condition)

        if (SGE_OBJECT(object)->stop_condition(SGE_OBJECT(object)))
		{
            if (SGE_OBJECT(object)->stop_callback)
                SGE_OBJECT(object)->stop_callback (object, NULL);

            // needed for scores messages
            if(!SGE_OBJECT(object)->fadeout)
                SGE_OBJECT(object)->vy = 0.0;

		    SGE_OBJECT(object)->vx = 0.0;
            SGE_OBJECT(object)->ax = 0.0;
            SGE_OBJECT(object)->ay = 0.0;
		}
}

gboolean
sge_main_loop (gpointer data)
{
	g_list_foreach (g_object_list, draw_object, NULL);
	g_list_foreach (g_object_list, move_object, NULL);

    return TRUE;

}

// creation/destruction
void
sge_init (void)
{
	g_rand_generator = g_rand_new_with_seed (time (NULL));
	g_main_loop_id = g_timeout_add (SGE_TIMER_INTERVAL, sge_main_loop, NULL);
	gi_nb_pixbufs = 0;
	g_pixbufs = NULL;
}

void
scale_object_pos (gpointer object, gpointer user_data)
{

    gdouble ratio;

    ratio = *((gdouble *) user_data);
    SGE_OBJECT(object)->x = SGE_OBJECT(object)->x * ratio;
    SGE_OBJECT(object)->y = SGE_OBJECT(object)->y * ratio;

    SGE_OBJECT(object)->dest_x = (int) rint (SGE_OBJECT(object)->x);

    SGE_OBJECT(object)->dest_y = (int) rint (SGE_OBJECT(object)->y);
    SGE_OBJECT(object)->needs_drawing = -1;

}

void
sge_set_drawing_area (GtkWidget * drawing_area, GdkPixmap * pixmap_buffer,
		      gint width, gint height)
{
	gdouble ratio;

	if (g_drawing_area && width && height) {
		ratio = (gdouble) width / (gdouble) g_width;
		g_list_foreach (g_object_list, scale_object_pos, &ratio);
	}

	if (drawing_area)
		g_drawing_area = drawing_area;
	if (pixmap_buffer)
		g_pixmap_buffer = pixmap_buffer;
	if (width)
		g_width = width;
	if (height)
		g_height = height;
}

void
sge_destroy (void)
{
	int i;

	g_source_remove (g_main_loop_id);
	g_rand_free (g_rand_generator);
	for (i = 0; i < gi_nb_pixbufs; i++)
		g_object_unref (g_pixbufs[i]);
	g_free (g_pixbufs);
}

// pixbuf management
void
pixbuf_update_notify (gpointer item, gpointer data)
{
	int pixbuf_id;
	T_SGEObject *object;

	object = (T_SGEObject *) item;
	pixbuf_id = *((int *) data);

	if (object->pixbuf_id == pixbuf_id)
	{
        object->width = gdk_pixbuf_get_width (g_pixbufs[pixbuf_id]);
        object->height = gdk_pixbuf_get_height (g_pixbufs[pixbuf_id]);

		if (object->pre_rendered)
		{
			g_object_unref (G_OBJECT (object->pre_rendered));
			object->pre_rendered =
			    gdk_pixmap_new (g_drawing_area->window,
					    object->width, object->height,
					    -1);
			gdk_draw_pixbuf (GDK_DRAWABLE
					 (object->pre_rendered), NULL,
					 g_pixbufs[pixbuf_id], 0, 0, 0, 0,
					 object->width, object->height,
					 GDK_RGB_DITHER_NONE, 0, 0);
		}
	}
}

gint
sge_register_pixbuf (GdkPixbuf * pixbuf, int index)
{
	int i;

	if (index == -1) {
		for (i = 0; i < gi_nb_pixbufs; i++)
			if (g_pixbufs[i] == 0)
				break;
		if (i == gi_nb_pixbufs) {
			gi_nb_pixbufs++;
			g_pixbufs =
			    (GdkPixbuf **) g_realloc (g_pixbufs,
						      sizeof (GdkPixbuf *)
						      * gi_nb_pixbufs);
		}
		g_pixbufs[i] = pixbuf;
	} else {
		i = index;
		g_object_unref (g_pixbufs[i]);
		g_pixbufs[i] = pixbuf;
		g_list_foreach (g_object_list, pixbuf_update_notify,
				(void *) &i);
	}

	return i;
}

// object/layer handling functions
gboolean
objects_intersect (T_SGEObject * object1, T_SGEObject * object2)
{
	GdkRectangle rect1, rect2, dest;

	rect1.x = object1->x;
	rect1.y = object1->y;
	rect1.width = object1->width;
	rect1.height = object1->height;

	rect2.x = object2->x;
	rect2.y = object2->y;
	rect2.width = object2->width;
	rect2.height = object2->height;

	return gdk_rectangle_intersect (&rect1, &rect2, &dest);
}

void
invalidate_object_if_above (gpointer item, gpointer data)
{
	T_SGEObject *target_object, *source_object;

	source_object = (T_SGEObject *) data;
	target_object = (T_SGEObject *) item;

	if (source_object != target_object)
		if (source_object->layer < target_object->layer)
			if (objects_intersect
			    (source_object, target_object))
				target_object->needs_drawing = -1;
}

void
invalidate_objects_above (T_SGEObject * object)
{
	g_list_foreach (g_object_list, invalidate_object_if_above,
			(void *) object);
}

void
invalidate_background_if_under_object (gpointer item, gpointer data)
{
	T_SGEObject *target_object, *source_object;

	source_object = (T_SGEObject *) data;
	target_object = (T_SGEObject *) item;

	if (source_object != target_object)
		if (target_object->layer == 0)
			if (objects_intersect
			    (source_object, target_object))
				target_object->needs_drawing = -1;
}

void
invalidate_background_beneath (T_SGEObject * object)
{
	g_list_foreach (g_object_list,
			invalidate_background_if_under_object,
			(void *) object);
}

void
invalidate_if_layer (gpointer item, gpointer data)
{
	T_SGEObject *target_object;

	target_object = (T_SGEObject *) item;

	if (target_object->layer == *((int *) data))
		target_object->needs_drawing = -1;
}

void
sge_invalidate_layer (int layer)
{
	g_list_foreach (g_object_list, invalidate_if_layer,
			(void *) &layer);
}

//objects creation/destruction
T_SGEObject *
sge_create_object (gint x, gint y, gint layer, gint pixbuf_id)
{

    T_SGEObject * object;
	object = (T_SGEObject *) g_malloc (sizeof (T_SGEObject));
    object->x = x;
    object->y = y;
    object->vx = 0.0;
    object->vy = 0.0;
    object->ax = 0.0;
    object->ay = 0.0;
    object->pixbuf_id = pixbuf_id;
    object->dest_x = 0;
    object->dest_y = 0;

    object->y_delay = 0;
    object->opacity = 255;
    object->fadeout = FALSE;
    object->zoom = 1.0;
    object->zoomout = FALSE;

    object->saturation = 1.0;
    object->blink = FALSE;
    object->blink_increase = TRUE;

    object->animation = FALSE;
    object->animation_iter = 1;
    object->animation_repeat = TRUE;

    object->stop_condition = NULL;
	object->stop_callback = NULL;
	object->layer = layer;
	object->needs_drawing = 0x01;

	object->width = gdk_pixbuf_get_width (g_pixbufs[pixbuf_id]);
    object->height = gdk_pixbuf_get_height (g_pixbufs[pixbuf_id]);

	if (layer == 0) {
		object->pre_rendered =
		    gdk_pixmap_new (g_drawing_area->window, object->width,
				    object->height, -1);
		gdk_draw_pixbuf (GDK_DRAWABLE (object->pre_rendered), NULL,
				 g_pixbufs[pixbuf_id], 0, 0, 0, 0,
				 object->width, object->height,
				 GDK_RGB_DITHER_NONE, 0, 0);
	} else
		object->pre_rendered = NULL;

    g_object_list = g_list_append (g_object_list, (gpointer) object);
	g_object_list = g_list_sort (g_object_list, compare_by_layer);

	return object;
}

void
sge_destroy_object (gpointer object, gpointer user_data)
{
	invalidate_background_beneath (SGE_OBJECT(object));
	if (SGE_OBJECT(object)->pre_rendered)
		g_object_unref (G_OBJECT(SGE_OBJECT(object)->pre_rendered));
	g_object_list = g_list_remove (g_object_list, object);
	g_free(object);
}

void
sge_destroy_object_on_level (gpointer object, gpointer user_data)
{
    // destroy only objects in the specified level
    if(SGE_OBJECT(object)->layer != GPOINTER_TO_INT(user_data))
        return;

    sge_destroy_object(object, NULL);
}

void
sge_destroy_all_objects (void)
{
	g_list_foreach (g_object_list, sge_destroy_object, NULL);
}

void
sge_destroy_all_objects_on_level (int level)
{
	g_list_foreach (g_object_list, sge_destroy_object_on_level, GINT_TO_POINTER(level));
}

// Stop conditions
int
is_out_of_screen (T_SGEObject * object)
{
	if ((object->x > g_width) ||
	    (object->x < -object->width) ||
	    (object->y > g_height) || (object->y < -object->height))
		return -1;

	return 0;
}

int
has_reached_destination (T_SGEObject * object)
{
    //g_debug("has_reached_destination():\n");
	if (fabs (object->x - object->dest_x) < 1.0 &&
	    fabs (object->y - object->dest_y) < 1.0)
		return -1;

	return 0;
}

int
has_reached_floor (T_SGEObject * object)
{
	if (object->y >= object->dest_y) {
		object->y = object->dest_y;
		return -1;
	}

	return 0;
}

int
has_reached_time_limit (T_SGEObject * object)
{
	if (object->lifetime--)
		return 0;
	return -1;
}

// animations/effects
void
sge_object_rise (T_SGEObject * object)
{
	object->layer++;
	g_object_list = g_list_sort (g_object_list, compare_by_layer);
}

void
sge_object_take_down (T_SGEObject * object)
{
    //g_print("sge_object_take_down():\n");
	object->vx = g_rand_double_range (g_rand_generator, -1.0, 1.0);
    object->vy = g_rand_double_range (g_rand_generator, 0.0, 1.0);
    object->ax = 0.0;
    object->ay = ACCELERATION;
    object->stop_condition = is_out_of_screen;
	object->stop_callback = sge_destroy_object;

	g_object_list = g_list_sort (g_object_list, compare_by_layer);
}

#define NB_STEPS	10

// used for gems swapping
void
sge_object_move_to (T_SGEObject * object, gint dest_x, gint dest_y)
{
    //g_print("sge_object_move_to(): dest_x:%d dest_y:%d\n", dest_x, dest_y);

	object->vx = (dest_x - object->x) / NB_STEPS;
	object->vy = (dest_y - object->y) / NB_STEPS;
	object->dest_x = dest_x;
	object->dest_y = dest_y;
	object->stop_condition = has_reached_destination;
}

void sge_object_fadeout_cb (gpointer object, gpointer user_data)
{
    SGE_OBJECT(object)->fadeout = TRUE;
}

void
sge_object_set_lifetime (T_SGEObject * object, gint seconds)
{
	object->lifetime = (1000 / SGE_TIMER_INTERVAL) * seconds;
	object->stop_condition = has_reached_time_limit;
	object->stop_callback = sge_object_fadeout_cb;
}
void

sge_object_fall_to (T_SGEObject * object, gint y_pos)
{
    //g_print("sge_object_fall_to(): y_pos:%d\n", y_pos);
	if (object->y < y_pos) {
		object->ay = ACCELERATION;
		object->dest_y = y_pos;
		object->stop_condition = has_reached_floor;
	}
}
void
sge_object_fall_to_with_delay (T_SGEObject * object, gint y_pos, gint delay)
{
    //g_print("sge_object_fall_to_with_accel(): y_pos:%d y:%4.1f diff:%4.1f x:%4.1f delay:%4.1f\n", y_pos/48, object->y/48, (y_pos-object->y)/48 +1, object->x/48, 0-object->y/48);

	if (object->y < y_pos) {
		object->ay = ACCELERATION;
		object->dest_y = y_pos;
		object->stop_condition = has_reached_floor;

		object->y_delay = delay;
	}
}

//other useful stuff

gboolean
sge_object_is_moving (T_SGEObject * object)
{
	return ((object->vx != 0.0) || (object->vy != 0.0));
}

gboolean
sge_objects_are_moving (void)
{
	gint i;
	T_SGEObject *object;

	for (i = 0; i < g_list_length (g_object_list); i++) {
		object =
		    (T_SGEObject *) g_list_nth_data (g_object_list, i);
		if (sge_object_is_moving (object))
			return TRUE;
	}
	return FALSE;
}

gboolean
sge_objects_are_moving_on_layer (int layer)
{
	gint i;
	T_SGEObject *object;

	for (i = 0; i < g_list_length (g_object_list); i++) {
		object =
		    (T_SGEObject *) g_list_nth_data (g_object_list, i);
		if (object->layer == layer)
			if (sge_object_is_moving (object))
				return TRUE;
	}
	return FALSE;
}

void sge_set_layer_visibility (int layer, gboolean visibility)
{
    if(layer >= 0 && layer < 5) {
        layers_visibility[layer] = visibility;
        if(layer > 0)
            sge_invalidate_layer(layer-1);
        else
            sge_invalidate_layer(0);
    }
}

void sge_object_set_opacity (T_SGEObject *object, gint opacity)
{

    if(object->pre_rendered && object->opacity == opacity)
        return;

    if (object->pre_rendered)
        g_object_unref (G_OBJECT(object->pre_rendered));

    object->opacity = opacity;

}

// fadeout the object and then destroy it
void sge_object_fadeout (T_SGEObject *object)
{
    object->fadeout = TRUE;
}

// zoomout the object and then destroy it
void sge_object_zoomout (T_SGEObject *object)
{
    object->zoomout = TRUE;
}

void sge_object_blink_start (T_SGEObject *object)
{
    object->blink = TRUE;
    object->blink_increase = TRUE;
}

void sge_object_blink_stop (T_SGEObject *object)
{
    object->blink = FALSE;
}

void sge_object_animate (T_SGEObject *object, gboolean repeat)
{
    object->animation = TRUE;
    object->animation_repeat = repeat;
}

gboolean
sge_object_exists (T_SGEObject *object)
{
    if(g_list_index(g_object_list, object) == -1)
        return FALSE;
    else
        return TRUE;
}