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
|
/* Atomix -- a little puzzle game about atoms and molecules.
* Copyright (C) 1999-2001 Jens Finke
* Copyright (C) 2005 Guilherme de S. Pastore
*
* 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.
*/
#include "config.h"
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "board-gtk.h"
#include "playfield.h"
#include "main.h"
#include "goal.h"
#include "level.h"
#include "goal-view.h"
#include "clock.h"
#include "undo.h"
AtomixApp *app;
typedef enum
{
GAME_ACTION_NEW,
GAME_ACTION_END,
GAME_ACTION_PAUSE,
GAME_ACTION_CONTINUE,
GAME_ACTION_SKIP,
GAME_ACTION_UNDO,
GAME_ACTION_FINISHED,
GAME_ACTION_RESTART,
} GameAction;
static void controller_handle_action (GameAction action);
static void set_game_not_running_state (void);
static gboolean set_next_level (void);
static void setup_level (void);
static void level_cleanup_view (void);
static void atomix_exit (void);
static gboolean on_key_press_event (GObject *widget, GdkEventKey *event,
gpointer user_data);
static void game_init (void);
static void update_statistics (void);
static void view_congratulations (void);
static void calculate_score (void);
/* ===============================================================
Menu callback functions
-------------------------------------------------------------- */
static void verb_GameNew_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_NEW);
}
static void verb_GameEnd_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_END);
}
static void verb_GameSkip_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_SKIP);
}
static void verb_GameReset_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_RESTART);
}
static void verb_GamePause_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_PAUSE);
}
static void verb_GameContinue_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_CONTINUE);
}
static void verb_GameUndo_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
controller_handle_action (GAME_ACTION_UNDO);
}
static void verb_GameExit_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
atomix_exit ();
}
static void verb_HelpAbout_cb (GSimpleAction *action, GVariant *variant, gpointer data)
{
const char *authors[] =
{
"Robert Roth <robert.roth.off@gmail.com>",
"Guilherme de S. Pastore <gpastore@gnome.org>",
"Jens Finke <jens@triq.net>",
NULL
};
const char *artists[] =
{
"Jakub Steiner <jimmac@ximian.com>",
NULL
};
gtk_show_about_dialog(GTK_WINDOW(app->mainwin),
"program-name", _("Atomix"),
"logo-icon-name", "atomix",
"version", VERSION,
"comments", _("A puzzle game about atoms and molecules"),
"website", "https://github.com/GNOME/atomix",
"authors", authors,
"artists", artists,
"translator_credits", _("translator-credits"),
NULL);
}
static gboolean on_app_destroy_event (GtkWidget *widget, GdkEvent *event,
gpointer user_data)
{
atomix_exit ();
return TRUE;
}
/* ===============================================================
Game steering functions
-------------------------------------------------------------- */
static void controller_handle_action (GameAction action)
{
switch (app->state)
{
case GAME_STATE_NOT_RUNNING:
if (action == GAME_ACTION_NEW)
{
if (set_next_level ())
{
app->level_no = 1;
app->score = 0;
setup_level ();
app->state = GAME_STATE_RUNNING_UNMOVED;
}
}
break;
case GAME_STATE_RUNNING_UNMOVED:
case GAME_STATE_RUNNING:
switch (action)
{
case GAME_ACTION_END:
level_cleanup_view ();
set_game_not_running_state ();
break;
case GAME_ACTION_PAUSE:
clock_stop (CLOCK (app->clock));
board_gtk_hide ();
app->state = GAME_STATE_PAUSED;
break;
case GAME_ACTION_SKIP:
level_cleanup_view ();
if (set_next_level ())
setup_level ();
else
set_game_not_running_state ();
break;
case GAME_ACTION_FINISHED:
calculate_score ();
if (level_manager_is_last_level (app->lm, app->level))
{
view_congratulations ();
level_cleanup_view ();
set_game_not_running_state ();
}
else
{
level_cleanup_view ();
set_next_level ();
setup_level ();
}
break;
case GAME_ACTION_RESTART:
g_assert (app->state != GAME_STATE_RUNNING_UNMOVED);
level_cleanup_view ();
setup_level ();
break;
case GAME_ACTION_UNDO:
g_assert (app->state != GAME_STATE_RUNNING_UNMOVED);
board_gtk_undo_move ();
break;
case GAME_ACTION_NEW:
case GAME_ACTION_CONTINUE:
default:
break;
}
break;
case GAME_STATE_PAUSED:
if (action == GAME_ACTION_CONTINUE)
{
clock_resume (CLOCK(app->clock));
board_gtk_show ();
app->state = (undo_exists())?GAME_STATE_RUNNING:GAME_STATE_RUNNING_UNMOVED;
}
break;
default:
g_assert_not_reached ();
}
update_menu_item_state ();
update_statistics ();
}
static void set_game_not_running_state (void)
{
board_gtk_show_logo (TRUE);
if (app->level)
g_object_unref (app->level);
if (app->goal)
g_object_unref (app->goal);
app->level = NULL;
app->goal = NULL;
app->score = 0;
app->state = GAME_STATE_NOT_RUNNING;
}
static gboolean set_next_level (void)
{
Level *next_level;
PlayField *goal_pf;
next_level = level_manager_get_next_level (app->lm, app->level);
if (app->level)
{
g_object_unref (app->level);
}
app->level = NULL;
if (app->goal)
{
g_object_unref (app->goal);
}
app->goal = NULL;
if (next_level == NULL)
{
return FALSE;
}
app->level = next_level;
app->level_no++;
goal_pf = level_get_goal (app->level);
app->goal = goal_new (goal_pf);
g_object_unref (goal_pf);
return TRUE;
}
static void setup_level (void)
{
PlayField *env_pf;
PlayField *sce_pf;
g_return_if_fail (app != NULL);
if (app->level == NULL)
return;
g_return_if_fail (app->goal != NULL);
/* init board */
env_pf = level_get_environment (app->level);
sce_pf = level_get_scenario (app->level);
board_gtk_show_logo (FALSE);
board_gtk_init_level (env_pf, sce_pf, app->goal);
/* init goal */
goal_view_render (app->goal);
/* init clock */
clock_reset (CLOCK(app->clock));
clock_start (CLOCK(app->clock));
g_object_unref (env_pf);
g_object_unref (sce_pf);
}
static void level_cleanup_view (void)
{
board_gtk_clear ();
goal_view_clear ();
clock_stop (CLOCK(app->clock));
}
static void atomix_exit (void)
{
#ifdef DEBUG
g_message ("Destroy application");
#endif
g_return_if_fail (app != NULL);
if (app->state != GAME_STATE_NOT_RUNNING)
{
set_game_not_running_state ();
}
board_gtk_destroy ();
if (app->level)
g_object_unref (app->level);
if (app->lm)
g_object_unref (app->lm);
if (app->theme)
g_object_unref (app->theme);
if (app->tm)
g_object_unref (app->tm);
/* quit application */
gtk_widget_destroy (app->mainwin);
}
static gboolean on_key_press_event (GObject *widget, GdkEventKey *event,
gpointer user_data)
{
if ((app->state == GAME_STATE_RUNNING) || (app->state == GAME_STATE_RUNNING_UNMOVED))
{
return board_gtk_handle_key_event (NULL, event, NULL);
}
return FALSE;
}
static void game_init (void)
{
g_return_if_fail (app != NULL);
/* init theme manager */
app->tm = theme_manager_new ();
theme_manager_init_themes (app->tm);
app->theme = theme_manager_get_theme (app->tm, "default");
g_assert (app->theme != NULL);
/* init level manager */
app->lm = level_manager_new ();
level_manager_init_levels (app->lm);
/* init level statistics */
app->level = NULL;
app->level_no = 0;
app->score = 0;
clock_set_format (CLOCK(app->clock), "%M:%S");
clock_reset (CLOCK(app->clock));
/* init the board */
board_gtk_init (app->theme, GTK_FIXED (app->fi_matrix));
/* init goal */
goal_view_init (app->theme, GTK_FIXED (app->fi_goal));
/* update user visible information */
app->state = GAME_STATE_NOT_RUNNING;
update_menu_item_state ();
update_statistics ();
gtk_widget_grab_focus (GTK_WIDGET (app->fi_matrix));
}
void game_level_finished (void)
{
controller_handle_action (GAME_ACTION_FINISHED);
}
static void calculate_score (void)
{
gint seconds = clock_get_elapsed (CLOCK (app->clock));
if (seconds > 300)
return;
if (app->score == 0)
app->score = 300 - seconds;
else
app->score = app->score * (2 - (seconds / 300));
}
static void view_congratulations (void)
{
GtkWidget *dlg;
dlg = gtk_message_dialog_new (GTK_WINDOW (app->mainwin),
GTK_DIALOG_MODAL,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
"%s",
_("Congratulations! You have finished all Atomix levels."));
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (GTK_WIDGET (dlg));
}
/* ===============================================================
UI update functions
-------------------------------------------------------------- */
static void update_statistics (void)
{
gchar *str_buffer;
g_return_if_fail (app != NULL);
if (app->state == GAME_STATE_NOT_RUNNING)
{
/* don't show anything */
gtk_label_set_text (GTK_LABEL (app->lb_level), "");
gtk_label_set_text (GTK_LABEL (app->lb_name), "");
gtk_label_set_text (GTK_LABEL (app->lb_formula), "");
gtk_label_set_text (GTK_LABEL (app->lb_score), "");
gtk_widget_hide (GTK_WIDGET (app->clock));
}
else
{
/* set level number */
str_buffer = g_new0 (gchar, 10);
g_snprintf (str_buffer, 10, "%i", app->level_no);
gtk_label_set_text (GTK_LABEL (app->lb_level), str_buffer);
/* set levelname */
gtk_label_set_text (GTK_LABEL (app->lb_name),
_(level_get_name (app->level)));
/* set the formula of the compound */
gtk_label_set_markup (GTK_LABEL (app->lb_formula),
level_get_formula (app->level));
/* set score */
g_snprintf (str_buffer, 10, "%i", app->score);
gtk_label_set_text (GTK_LABEL (app->lb_score), str_buffer);
/* show clock */
gtk_widget_show (GTK_WIDGET (app->clock));
g_free (str_buffer);
}
}
typedef struct
{
const gchar *cmd;
gboolean enabled;
} CmdEnable;
static const GActionEntry app_entries[] =
{
{ "GameAbout", verb_HelpAbout_cb, NULL, NULL, NULL},
{ "GameQuit", verb_GameExit_cb, NULL, NULL, NULL}
};
static const GActionEntry win_entries[] =
{
{ "GameNew", verb_GameNew_cb, NULL, NULL, NULL},
{ "GameEnd", verb_GameEnd_cb, NULL, NULL, NULL},
{ "GameSkip", verb_GameSkip_cb, NULL, NULL, NULL},
{ "GameReset", verb_GameReset_cb, NULL, NULL, NULL},
{ "GameUndo", verb_GameUndo_cb, NULL, NULL, NULL},
{ "GamePause", verb_GamePause_cb, NULL, NULL, NULL},
{ "GameContinue", verb_GameContinue_cb, NULL, NULL, NULL},
};
static const CmdEnable not_running[] =
{
{ "GameNew", TRUE },
{ "GameEnd", FALSE },
{ "GameSkip", FALSE },
{ "GameReset", FALSE },
{ "GameUndo", FALSE },
{ "GamePause", FALSE },
{ "GameContinue", FALSE },
{ NULL, FALSE }
};
static const CmdEnable running_unmoved[] =
{
{ "GameNew", FALSE },
{ "GameEnd", TRUE },
{ "GameSkip", TRUE },
{ "GameReset", FALSE },
{ "GameUndo", FALSE },
{ "GamePause", TRUE },
{ "GameContinue", FALSE },
{ NULL, FALSE }
};
static const CmdEnable running[] =
{
{ "GameNew", FALSE },
{ "GameEnd", TRUE },
{ "GameSkip", TRUE },
{ "GameReset", TRUE },
{ "GameUndo", TRUE },
{ "GamePause", TRUE },
{ "GameContinue", FALSE },
{ NULL, FALSE }
};
static const CmdEnable paused[] =
{
{ "GameNew", FALSE },
{ "GameEnd", FALSE },
{ "GameSkip", FALSE },
{ "GameReset", FALSE },
{ "GameUndo", FALSE },
{ "GamePause", FALSE },
{ "GameContinue", TRUE },
{ NULL, FALSE }
};
static const CmdEnable *state_sensitivity[] =
{
not_running, running_unmoved, running, paused
};
void update_menu_item_state (void)
{
gint i;
const CmdEnable *cmd_list = state_sensitivity[app->state];
GAction *action;
for (i = 0; cmd_list[i].cmd != NULL; i++)
{
action = g_action_map_lookup_action (G_ACTION_MAP (app->mainwin), cmd_list[i].cmd);
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), cmd_list[i].enabled);
}
}
/* ===============================================================
GUI creation functions
-------------------------------------------------------------- */
static AtomixApp *create_gui (GApplication *app_instance)
{
gchar *ui_path;
GtkBuilder *builder;
GtkWidget *stats_grid;
GtkWidget *time_label;
app = g_new0 (AtomixApp, 1);
app->level = NULL;
app->app_instance = app_instance;
builder = gtk_builder_new ();
ui_path = g_build_filename (PKGDATADIR, "ui", "interface.ui", NULL);
gtk_builder_add_from_file (builder, ui_path, NULL);
g_free (ui_path);
app->mainwin = GTK_WIDGET (gtk_builder_get_object (builder, "mainwin"));
g_signal_connect (G_OBJECT (app->mainwin), "delete_event",
(GCallback) on_app_destroy_event, app);
/* create canvas widgets */
app->fi_matrix = GTK_WIDGET (gtk_builder_get_object (builder, "game_fixed"));
app->fi_goal = GTK_WIDGET (gtk_builder_get_object (builder, "preview_fixed"));
stats_grid = GTK_WIDGET (gtk_builder_get_object (builder, "stats_grid"));
time_label = GTK_WIDGET (gtk_builder_get_object (builder, "time_label"));
app->clock = clock_new ();
gtk_grid_attach_next_to (GTK_GRID (stats_grid), app->clock,
time_label, GTK_POS_RIGHT, 1, 1);
/* add playfield canvas to left side */
g_signal_connect (G_OBJECT (app->mainwin), "key-press-event",
G_CALLBACK (on_key_press_event), app);
app->lb_level = GTK_WIDGET (gtk_builder_get_object (builder, "level_value"));
app->lb_name = GTK_WIDGET (gtk_builder_get_object (builder, "molecule_value"));
app->lb_formula = GTK_WIDGET (gtk_builder_get_object (builder, "formula_value"));
app->lb_score = GTK_WIDGET (gtk_builder_get_object (builder, "score_value"));
gtk_widget_show_all (GTK_WIDGET (app->mainwin));
if (gtk_application_prefers_app_menu (GTK_APPLICATION (app_instance))) {
GMenu * menu = G_MENU (gtk_builder_get_object (builder, "app-menu"));
gtk_application_set_app_menu (GTK_APPLICATION (app->app_instance), G_MENU_MODEL (menu));
// GtkWidget *menubar = GTK_WIDGET (gtk_builder_get_object (builder, "menubar"));
// gtk_widget_set_visible (menubar, FALSE);
}
g_object_unref (builder);
return app;
}
static void
app_activate (GApplication *app_instance, gpointer user_data)
{
if (app == NULL) {
/* make a few initalisations here */
g_action_map_add_action_entries (G_ACTION_MAP (app_instance), app_entries, G_N_ELEMENTS (app_entries), app_instance);
app = create_gui (app_instance);
g_action_map_add_action_entries (G_ACTION_MAP (app->mainwin), win_entries, G_N_ELEMENTS (win_entries), app_instance);
game_init ();
gtk_widget_set_size_request (GTK_WIDGET (app->mainwin), 678, 520);
//gtk_window_set_resizable (GTK_WINDOW (app->mainwin), FALSE);
gtk_widget_show (app->mainwin);
gtk_application_add_window (GTK_APPLICATION (app_instance), GTK_WINDOW (app->mainwin));
} else {
gtk_window_present (GTK_WINDOW (app->mainwin));
}
}
int main (int argc, char *argv[])
{
GtkApplication *gtk_app;
int status;
g_set_application_name (_("Atomix"));
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
gtk_app = gtk_application_new ("org.gnome.atomix", G_APPLICATION_FLAGS_NONE);
g_signal_connect (gtk_app, "activate", G_CALLBACK (app_activate), NULL);
status = g_application_run (G_APPLICATION (gtk_app), argc, argv);
g_object_unref (gtk_app);
return status;
}
|