Debian Codegraph @

src/gtkballs.c

raw symbols SCIP
1 /* gtkballs.c - initialisation functions
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 */
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/time.h>
12 #include <gtk/gtk.h>
13
14 #include "gtkballs.h"
15 #include "gtkutils.h"
16 #include "prefs.h" /* preferences */
17 #include "scoreboard.h" /* read_score, score_setup */
18 #include "gfx.h"
19 #include "theme.h"
20 #include "inputname.h" /* input_name_dialog */
21 #include "game.h"
22 #include "mainmenu.h"
23 #include "mainwin.h"
24 #include <sys/types.h>
25 #include <sys/stat.h> /* mkdir */
26
27
28 #define P_DIR "games"
29 char * get_config_dir_file (const char * file)
30 {
31 /* returns a path that must be freed with g_free */
32 char * config_home, * res;
33 #if __MINGW32__
34 config_home = getenv ("LOCALAPPDATA"); /* XP */
35 if (!config_home) {
36 config_home = getenv ("APPDATA");
37 }
38 #else
39 config_home = getenv ("XDG_CONFIG_HOME");
40 #endif
41 if (config_home) {
42 res = g_build_filename (config_home, P_DIR, file, NULL);
43 } else {
44 res = g_build_filename (g_get_home_dir(), ".config", P_DIR, file, NULL);
45 }
46 return res;
47 }
48
49
50 gint destroy_lines(gboolean count_score) {
51 gint i = game_destroy_lines(count_score);
52 if (count_score && i) {
53 gint ml = rules_get_destroy();
54 gint sc = game_get_score();
55 gint nsc = sc + ml * 2 + (i - ml) * (i - ml) * 2 + (1 - pref_get_show_next_colors());
56 mw_set_user_score(nsc);
57 }
58 draw_board();
59 return i;
60 }
61
62 void undo_move(GtkWidget *widget, gpointer data) {
63 game_restore_state_from_undo();
64 stop_jumping_animation();
65 mw_set_hi_score(game_get_hi_score());
66 mw_set_user_score(game_get_score());
67 menu_set_sensitive_undo(FALSE);
68 draw_board();
69 draw_next_balls();
70 }
71
72 gint random_color(void) {
73 return (gint)(((gfloat)rules_get_colors()) * rand() / (RAND_MAX + 1.0));
74 }
75
76 gint random_cell(void) {
77 return (gint)(((gfloat)rules_get_width() * rules_get_height()) * rand() / (RAND_MAX + 1.0));
78 }
79
80 void new_turn(gint number, gboolean first)
81 {
82 gint i, k = 0, free_cells_number, c;
83 struct score_board scoreboard[10];
84
85 do {
86 timer_start();
87
88 k = 0;
89 if (number < rules_get_next()) {
90 number = rules_get_next();
91 }
92
93 if ((free_cells_number = game_count_free_cells()) <= number) {
94 if (!read_score(scoreboard, NULL, NULL) ||
95 game_get_score() > scoreboard[9].score) {
96 input_name_dialog();
97 }
98 new_game();
99 return;
100 }
101
102 do {
103 i = random_cell();
104 if (board_get_at_node(i) < 1) {
105 if (first) {
106 c = 1 + random_color();
107 board_set_at_node(i, c);
108 } else {
109 board_set_at_node(i, next_get(k));
110 }
111 k++;
112 } else {
113 continue;
114 }
115 /* I hope that k!=0 */
116 if (k <= rules_get_next()) {
117 next_set(k - 1, 1 + random_color());
118 }
119 } while (k < ((number < free_cells_number) ? number : free_cells_number));
120
121 if (!first) {
122 draw_next_balls();
123 /* if line complete, don't increase user's score--it's not his merit */
124 destroy_lines(FALSE);
125 }
126 } while ((free_cells_number = game_count_free_cells()) == rules_get_width() * rules_get_height());
127 }
128
129 void new_game(void)
130 {
131 stop_jumping_animation();
132 game_init_game(NULL, NULL);
133 menu_set_sensitive_undo(FALSE);
134 new_turn(rules_get_destroy(), TRUE);
135 mw_set_user_score(0);
136 draw_next_balls();
137 draw_board();
138 }
139
140 // ==================================================================
141
142 int main(int argc, char **argv)
143 {
144 gint i;
145 struct timeval tv;
146 struct timezone tz;
147 struct score_board scoreboard[10];
148 gchar *err, *mapfile, * confdir;
149
150 /* setup all i18n stuff */
151 #ifdef ENABLE_NLS
152 bindtextdomain (PACKAGE, LOCALEDIR);
153 textdomain (PACKAGE);
154 bind_textdomain_codeset(PACKAGE, "UTF8");
155 #endif /* ENABLE_NLS */
156
157 /* initialize random seed */
158 gettimeofday(&tv, &tz);
159 srand((unsigned int)tv.tv_usec);
160
161 /* initialize gtk */
162 gtk_init (&argc, &argv);
163
164 /* Make sure confdir exists */
165 confdir = get_config_dir_file (NULL);
166 g_mkdir_with_parents (confdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
167 g_free (confdir);
168
169 /* load user's preferences */
170 load_preferences();
171
172 /* load theme, fallback to default if specifed theme cannot be loaded */
173 if (!(i = load_theme(pref_get_theme_name()))) {
174 if (strcmp(pref_get_theme_name(), pref_get_default_theme_name()) != 0) {
175 err = g_strdup_printf(_("Failed loading theme \"%s\"! Trying \"%s\"\n"), pref_get_theme_name(), pref_get_default_theme_name());
176 ut_simple_message_box(err);
177 g_free(err);
178 pref_set_theme_name(pref_get_default_theme_name());
179 i = load_theme(pref_get_theme_name());
180 }
181 if (!i) {
182 err = g_strdup_printf(_("Failed loading theme \"%s\"! Exiting.\n"), pref_get_theme_name());
183 ut_simple_message_box(err);
184 g_free(err);
185 return 1;
186 }
187 }
188
189 /* create main application window */
190 mw_create(rules_get_width() * theme_get_width(), rules_get_height() * theme_get_height());
191
192 /* initialize and start new game */
193 game_init_game(NULL, NULL);
194 new_turn(rules_get_destroy(), TRUE);
195 remake_board(-1, 1);
196
197 /* read and set scores */
198 read_score (scoreboard, NULL, NULL);
199 mw_set_hi_score(scoreboard[0].score);
200 mw_set_user_score(0);
201
202 mapfile = get_config_dir_file ("gtkballs-accel.map");
203 gtk_accel_map_load (mapfile);
204
205 /* enter main application loop */
206 gtk_main();
207
208 gtk_accel_map_save (mapfile);
209 g_free (mapfile);
210
211 return 0;
212 }