summaryrefslogtreecommitdiff
path: root/games
diff options
context:
space:
mode:
authorBenjamin Otte <otte@gnome.org>2005-09-07 18:28:32 +0000
committerBenjamin Otte <otte@gnome.org>2005-09-07 18:28:32 +0000
commitfd2f5a0a4206f1cb201a6cf41e27371c8d2f3929 (patch)
treebf53247b511433c0a990c3205a5da6deba0f0481 /games
parent3e5b6273ae504b1679f12ef19b125cb4e1d48870 (diff)
* libgame/game-filter.c:
* libgame/game-filter.h: object that keeps track of a subset of game objects automatically * libgame/game-info.c: * libgame/game-info.h: put general infos for a game in const structs - replaces GamePlayerInfo * libgame/Makefile.am: * libgame/libgame.h: adapt for above 2 files * libgame/game-actor.c: (game_actor_move_to): * libgame/game-game.c: (game_game_class_init), (game_game_init), (game_game_dispose), (game_game_get_property), (game_game_set_property), (game_game_new), (game_game_tick_cb), (game_game_tick), (game_game_kill_player), (game_game_set_state), (game_game_get_state), (game_game_set_max_players), (game_game_get_player_count), (game_game_add_object_valist), (game_game_add_objectv), (game_game_flush): * libgame/game-game.h: - use a GameFilter for the actors and players list - fix bug by (useless) optimization in _add_objectv * libgame/game-object.c: (game_object_class_init): add permanent and active properties * libgame/game-object.h: copyright header fix * libgame/game-player.c: (game_player_tick), (game_player_get_type), (game_player_class_init), (game_player_init), (game_player_dispose), (game_player_get_property), (game_player_set_property), (game_player_add_score), (game_player_set_score), (game_player_get_score), (game_player_key_pressed), (game_player_key_released): * libgame/game-player.h: players are normal GameObjects now * libgame/game-highscore.c: (game_high_score_new), (game_high_score_new_from_game), (game_high_score_get_score), (game_high_score_add_entry), (info_entry_new), (info_get_score_id), (game_high_score_save): * libgame/game-highscore.h: * libgame/game-recorder.c: (game_recorder_new), (handle_key), (add_player), (game_recorder_start_recording), (game_recorder_stop_recording), (cb_player_added), (game_recorder_set_game): * libgame/game-replay.c: (info_pressed), (info_player_create), (die_plz), (info_game_end), (game_replay_init), (game_replay_dispose), (game_replay_tick), (game_replay_attach): * libgame/gnome-highscore.c: (update_score): * libgame/gnome-keyboard-configuration.c: (update_keylist): * libgame/gnome-player.c: (game_gnome_player_init), (game_gnome_player_new), (callback_pressed), (callback_released): * libgame/gnome-window.c: (callback_new), (cb_stop_recording), (update_keys), (game_notify_callback), (game_gnome_window_set_key): * games/sheep/sheep.c: (switch_lanes), (cb_player_added), (main): * games/tetris/tetris.c: (block_vertical), (rotate_block), (block_fast), (delete_full_rows), (finish_block), (block_drop), (cb_tick), (cb_player_added), (cb_player_removed), (main): adapt to new palyer and info APIs
Diffstat (limited to 'games')
-rw-r--r--games/sheep/sheep.c35
-rw-r--r--games/tetris/tetris.c174
2 files changed, 128 insertions, 81 deletions
diff --git a/games/sheep/sheep.c b/games/sheep/sheep.c
index 562e6c5..a4da9dd 100644
--- a/games/sheep/sheep.c
+++ b/games/sheep/sheep.c
@@ -18,6 +18,17 @@
#define BOARD_HEIGHT 9
#define STARTING_SPEED 0.03
+static void switch_lanes (GamePlayer *player, gpointer unused);
+static const GameInfoKey keys[] = {
+ { "switch", N_("switch"), N_("switch lanes"), switch_lanes, NULL, NULL },
+ { NULL, NULL, NULL, NULL, NULL, NULL }
+};
+
+static const GameInfoScore scores[] = {
+ { "kilometers", N_("kilometers"), N_("how far you-ve been driving"), FALSE, TRUE },
+ { NULL, NULL, NULL, FALSE, FALSE }
+};
+
double speed;
double next_sheep;
int boom;
@@ -75,22 +86,23 @@ cb_port_tick (GameViewport *port)
}
static void
-cb_key_pressed (GamePlayer *player, gchar *keyname, gpointer unuused)
+switch_lanes (GamePlayer *player, gpointer unuused)
{
- //if (strcmp (keyname, "switch") == 0) {
- double x;
- game_sprite_get_position (GAME_SPRITE (car), &x, NULL);
- game_sprite_move (GAME_SPRITE (car), x > 0.5 ? -1 : 1, 0);
- //}
+ double x;
+ game_sprite_get_position (GAME_SPRITE (car), &x, NULL);
+ game_sprite_move (GAME_SPRITE (car), x > 0.5 ? -1 : 1, 0);
}
+
static void
cb_player_added (GameGame *game, GamePlayer *player_added, gpointer unused)
{
+ if (!GAME_IS_PLAYER (player_added))
+ return;
player = player_added;
- g_signal_connect (player, "key-pressed", (GCallback) cb_key_pressed, NULL);
game_player_set_score (player, "kilometers", 0);
}
+
static void
car_collide (GameActor *car, GameActor *sheep)
{
@@ -129,7 +141,6 @@ main (int argc, char** argv)
{
GtkWidget *window;
GameGraphic *graphic;
- GamePlayerInfo *info;
int i;
#ifdef ENABLE_NLS
@@ -142,12 +153,8 @@ main (int argc, char** argv)
gtk_init (&argc, &argv);
/* set up game */
- info = game_player_info_new ();
- game_player_info_add_score (info, "kilometers", _("kilometers"), _("how long you've been driving"));
- game_player_info_add_key (info, "switch", _("switch"), _("switch lanes"));
- game = game_game_new ("sheep", VERSION);
+ game = game_game_new ("sheep", VERSION, keys, scores);
game_game_set_score_func (game, compare_high_scores);
- game_game_set_player_info (game, info);
port = game_viewport_new (game, BOARD_WIDTH, BOARD_HEIGHT);
game_viewport_set_block_size (port, 40);
game_game_set_frame_rate (game, 20);
@@ -181,7 +188,7 @@ main (int argc, char** argv)
g_object_set (car, "tick-function", cb_car_tick, NULL);
g_object_set (port, "tick-function", cb_port_tick, NULL);
g_signal_connect (port, "block-shown", (GCallback) cb_block_shown, NULL);
- g_signal_connect (game, "player-added", (GCallback) cb_player_added, NULL);
+ g_signal_connect (game, "object-added", (GCallback) cb_player_added, NULL);
g_signal_connect (game, "start-game", (GCallback) cb_start, NULL);
gtk_widget_show_all (window);
diff --git a/games/tetris/tetris.c b/games/tetris/tetris.c
index 337ce0d..a75092c 100644
--- a/games/tetris/tetris.c
+++ b/games/tetris/tetris.c
@@ -1,6 +1,24 @@
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+/*
+ * Copyright (C) 2003,2005 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 "libgame/game-private.h"
#include <gtk/gtk.h>
#include <libgame/libgame.h>
@@ -9,7 +27,6 @@
#include <string.h>
#include <math.h>
-#include "libgame/game-private.h"
#include <foreground.xpm>
#include <background.xpm>
@@ -17,6 +34,27 @@
#define BOARD_HEIGHT 20
#define TICK_RATE 10
+static void block_vertical (GamePlayer *player, gpointer direction);
+static void rotate_block (GamePlayer *player, gpointer direction);
+static void block_fast (GamePlayer *player, gpointer unused);
+static void block_drop (GamePlayer *player, gpointer unused);
+static const GameInfoKey keys[] = {
+ { "left", N_("left"), N_("move block to the left"), block_vertical, NULL, GINT_TO_POINTER (-1) },
+ { "right", N_("right"), N_("move block to the right"), block_vertical, NULL, GINT_TO_POINTER (1) },
+ { "rotate_clock", N_("rotate clock"), N_("rotate block clockwise"), rotate_block, NULL, GINT_TO_POINTER (1) },
+ { "rotate_counter", N_("rotate counter"), N_("rotate block counter-clockwise"), rotate_block, NULL, GINT_TO_POINTER (-1) },
+ { "fast", N_("fast"), N_("drop block faster"), block_fast, NULL, NULL },
+ { "drop", N_("drop"), N_("drop block to the bottom"), block_drop, NULL, NULL },
+ { NULL, NULL, NULL, NULL, NULL, NULL }
+};
+
+static const GameInfoScore scores[] = {
+ { "level", N_("level"), N_("level reached"), FALSE, TRUE },
+ { "lines", N_("lines"), N_("number of deleted lines"), FALSE, TRUE },
+ { "points", N_("points"), N_("points achieved"), FALSE, TRUE },
+ { NULL, NULL, NULL, FALSE, FALSE }
+};
+
/* type, rotation, block */
static const int block_x[7][4][4] = {
{{0, 0, 1, 1}, /* [][] */
@@ -79,7 +117,6 @@ static const int block_y[7][4][4] = {
GameGame *game;
GameViewport *port;
-GamePlayer *player;
GameGraphic *bg, *fg[7], *trans[7];
GameSprite *field[BOARD_WIDTH][BOARD_HEIGHT];
gboolean filled[BOARD_WIDTH][BOARD_HEIGHT];
@@ -116,25 +153,6 @@ block_move (int dx, int dy)
return TRUE;
}
-static int
-block_drop (void)
-{
- int i, j, max = BOARD_HEIGHT;
- double x, y;
-
- for (i = 0; i < 4; i++) {
- game_sprite_get_position (block[i], &x, &y);
- for (j = 0; j <= max; j++) {
- if (!can_enter (x, y + j))
- break;
- }
- max = j - 1;
- }
- if (!block_move (0, max))
- g_assert_not_reached ();
- return max;
-}
-
static gboolean
block_rotate (int direction)
{
@@ -189,6 +207,25 @@ block_rotate (int direction)
}
return TRUE;
}
+
+static void
+block_vertical (GamePlayer *player, gpointer direction)
+{
+ block_move (GPOINTER_TO_INT (direction), 0);
+}
+
+static void
+rotate_block (GamePlayer *player, gpointer direction)
+{
+ block_rotate (GPOINTER_TO_INT (direction));
+}
+
+static void block_fast (GamePlayer *player, gpointer unused)
+{
+ if (block_move (0, 1))
+ game_player_add_score (player, "points", game_player_get_score (player, "level") / 5);
+}
+
static void
set_up_new_block (void)
{
@@ -226,7 +263,7 @@ set_up_new_block (void)
}
}
static void
-delete_full_rows (void)
+delete_full_rows (GamePlayer *player)
{
int i, j, cur;
@@ -260,7 +297,7 @@ next_row:
}
}
static void
-finish_block (void)
+finish_block (GamePlayer *player)
{
int i;
double x, y;
@@ -276,40 +313,43 @@ finish_block (void)
game_sprite_set_graphic (field[ix][iy], game_sprite_get_graphic (block[i]));
}
game_player_add_score (player, "points", game_player_get_score (player, "level") * maxy / 10);
- delete_full_rows ();
+ delete_full_rows (player);
set_up_new_block ();
}
+
static void
-cb_tick (GameGame *game, gpointer unused)
+block_drop (GamePlayer *player, gpointer unused)
{
- waited++;
- if (waited >= wait_time) {
- if (!block_move (0, 1)) {
- finish_block ();
+ int i, j, max = BOARD_HEIGHT;
+ double x, y;
+
+ for (i = 0; i < 4; i++) {
+ game_sprite_get_position (block[i], &x, &y);
+ for (j = 0; j <= max; j++) {
+ if (!can_enter (x, y + j))
+ break;
}
- waited = 0;
+ max = j - 1;
}
+ if (!block_move (0, max))
+ g_assert_not_reached ();
+
+ game_player_add_score (player, "points", max * (max + 1) * game_player_get_score (player, "level") / 20);
+ finish_block (player);
}
+
static void
-cb_key_pressed (GamePlayer *player, gchar *keyname, gpointer unuused)
+cb_tick (GameGame *game, guint tick, gpointer player)
{
- if (strcmp (keyname, "left") == 0) {
- block_move (-1, 0);
- } else if (strcmp (keyname, "right") == 0) {
- block_move (1, 0);
- } else if (strcmp (keyname, "rotate_clock") == 0) {
- block_rotate (1);
- } else if (strcmp (keyname, "rotate_counter") == 0) {
- block_rotate (-1);
- } else if (strcmp (keyname, "fast") == 0) {
- if (block_move (0, 1))
- game_player_add_score (player, "points", game_player_get_score (player, "level") / 5);
- } else if (strcmp (keyname, "drop") == 0) {
- guint i = block_drop ();
- game_player_add_score (player, "points", i * (i + 1) * game_player_get_score (player, "level") / 20);
- finish_block();
+ waited++;
+ if (waited >= wait_time) {
+ if (!block_move (0, 1)) {
+ finish_block (player);
+ }
+ waited = 0;
}
}
+
static void
cb_scores (GamePlayer *player, gchar *name, gint score, gpointer unused)
{
@@ -327,16 +367,28 @@ cb_scores (GamePlayer *player, gchar *name, gint score, gpointer unused)
g_free (str);
}
static void
-cb_player_added (GameGame *game, GamePlayer *player_added, gpointer unused)
+cb_player_added (GameGame *game, GamePlayer *player, gpointer unused)
{
- player = player_added;
+ if (!GAME_IS_PLAYER (player))
+ return;
- g_signal_connect (player, "key-pressed", (GCallback) cb_key_pressed, NULL);
+ g_signal_connect (game, "tick", (GCallback) cb_tick, player);
g_signal_connect (player, "scores-changed", (GCallback) cb_scores, NULL);
game_player_set_score (player, "points", 0);
game_player_set_score (player, "lines", 0);
game_player_set_score (player, "level", 1);
}
+
+static void
+cb_player_removed (GameGame *game, GamePlayer *player, gpointer unused)
+{
+ if (!GAME_IS_PLAYER (player))
+ return;
+
+ if (g_signal_handlers_disconnect_by_func (game, G_CALLBACK (cb_tick), player) != 1)
+ g_assert_not_reached ();
+}
+
static void
cb_game_over (GameGame *game, gpointer unused)
{
@@ -393,7 +445,6 @@ main (int argc, char** argv)
GtkWidget *widget;
GdkPixbuf *tmp, *tmp2;
- GamePlayerInfo *info;
int i, j;
gtk_init (&argc, &argv);
@@ -405,18 +456,7 @@ main (int argc, char** argv)
textdomain (GETTEXT_PACKAGE);
#endif
- game = game_game_new ("tetris", VERSION);
- info = game_player_info_new ();
- game_player_info_add_score (info, "level", _("level"), _("level reached"));
- game_player_info_add_score (info, "lines", _("lines"), _("number of deleted lines"));
- game_player_info_add_score (info, "points", _("points"), _("points achieved"));
- game_player_info_add_key (info, "left", _("left"), _("move block to the left"));
- game_player_info_add_key (info, "right", _("right"), _("move block to the right"));
- game_player_info_add_key (info, "rotate_clock", _("rotate clock"), _("rotate block clockwise"));
- game_player_info_add_key (info, "rotate_counter", _("rotate counter"), _("rotate block counter-clockwise"));
- game_player_info_add_key (info, "fast", _("fast"), _("drop block faster"));
- game_player_info_add_key (info, "drop", _("drop"), _("drop block to the bottom"));
- game_game_set_player_info (game, info);
+ game = game_game_new ("tetris", VERSION, keys, scores);
game_game_set_score_func (game, compare_high_scores);
game_game_set_frame_rate (game, TICK_RATE);
port = game_viewport_new (game, BOARD_WIDTH, BOARD_HEIGHT);
@@ -471,9 +511,9 @@ main (int argc, char** argv)
}
g_object_unref (tmp);
- g_signal_connect (game, "player-added", (GCallback) cb_player_added, NULL);
+ g_signal_connect (game, "object-added", (GCallback) cb_player_added, NULL);
+ g_signal_connect (game, "object-removed", (GCallback) cb_player_removed, NULL);
g_signal_connect (game, "game-over", (GCallback) cb_game_over, NULL);
- g_signal_connect (game, "tick", (GCallback) cb_tick, NULL);
g_signal_connect (game, "start-game", (GCallback) cb_start, NULL);
g_signal_connect (game, "end-game", (GCallback) cb_end, NULL);