4 * The geeky little puzzle game with a big noodly crunch!
6 * gPlanarity copyright (C) 2005 Monty <monty@xiph.org>
7 * Original Flash game by John Tantalo <john.tantalo@case.edu>
8 * Original game concept by Mary Radcliffe
10 * gPlanarity is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * gPlanarity is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Postfish; see the file COPYING. If not, write to the
22 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <sys/types.h>
37 #include <gdk-pixbuf/gdk-pixbuf.h>
40 #include <fontconfig/fontconfig.h>
43 #include "gameboard.h"
44 #include "levelstate.h"
47 #define boardstate "/.gPlanarity/boards/"
48 #define mainstate "/.gPlanarity/"
53 GtkWidget
*toplevel_window
;
57 static cairo_font_face_t
*font_face
=0;
58 static cairo_font_face_t
*bold_face
=0;
59 static cairo_font_face_t
*ital_face
=0;
61 static float adjust_x_normal
;
62 static float adjust_y_normal
;
63 static float adjust_x_bold
;
64 static float adjust_y_bold
;
66 static int dir_create(char *name
){
73 fprintf(stderr
,"ERROR: Could not create directory (%s) to save game state:\n\t%s\n",
74 name
,strerror(errno
));
81 void request_resize(int width
, int height
){
82 gtk_window_resize(GTK_WINDOW(toplevel_window
),width
,height
);
83 if(gameboard
->resize_timeout
)
84 gameboard
->resize_timeout
= time(NULL
)+3;
85 gameboard
->resize_w
= width
;
86 gameboard
->resize_h
= height
;
89 static void clean_exit(int sig
){
93 "\nTrapped signal %d; saving state and exiting!\n",sig
);
95 levelstate_write(statedir
);
100 static void clean_exit_delete_post(Gameboard
*g
){
105 static gint
clean_exit_delete(gpointer p
){
106 levelstate_write(statedir
);
107 undeploy_buttons(gameboard
,clean_exit_delete_post
);
108 return 1; // we're handling it, don't continue the delete chain
111 /* font handling... still a pain in the ass! ...but mostly due to
112 lack of documentation.
114 Try first to find one of the default fonts we prefer (FontConfig
115 can search for a list, Cairo cannot).
117 Regardless of what's found, try to determine the approximate best
118 aspect ratio of the text; gPlanarity looks best with a relatively
119 tall/narrow font that renders with horizontal/vertical strokes of
120 approximately identical width */
122 static cairo_font_face_t
*init_font(char *list
, int slant
,int bold
){
123 cairo_font_face_t
*ret
;
125 FcPattern
*fc_pattern
;
128 fc_pattern
= FcNameParse((unsigned char *)list
);
130 // load the system config
131 FcConfigSubstitute(0, fc_pattern
, FcMatchPattern
);
133 FcPatternDel(fc_pattern
,FC_SLANT
);
134 FcPatternDel(fc_pattern
,FC_WEIGHT
);
136 FcPatternAddInteger(fc_pattern
,FC_SLANT
,FC_SLANT_ITALIC
);
138 FcPatternAddInteger(fc_pattern
,FC_WEIGHT
,FC_WEIGHT_BOLD
);
140 // fill in missing defaults
141 FcDefaultSubstitute(fc_pattern
);
142 // find a font face on our list if possible
143 fc_pattern
= FcFontMatch(0, fc_pattern
, 0);
146 fprintf(stderr
,"\nUnable to find any suitable %s fonts!\n"
147 "Continuing, but the the results are likely to be poor.\n\n",
149 (slant
?(bold
?"bold italic":"italic"):(bold
?"bold":"medium")) );
152 FcPatternGetString (fc_pattern
, FC_FAMILY
, 0, (FcChar8
**)&fontface
);
155 if(!strstr(list,fontface)){
156 fprintf(stderr,"\nUnable to find any of gPlanarity's preferred %s fonts (%s);\n"
157 "Using system default font \"%s\".\n\n",
158 (slant?(bold?"bold italic":"italic"):(bold?"bold":"medium")),
162 FcPatternGetBool(fc_pattern
, FC_SCALABLE
, 0, &scalable
);
163 if (scalable
!= FcTrue
) {
164 fprintf(stderr
,"\nSelected %s font \"%s\" is not scalable! This is almost as bad\n"
165 "as not finding any font at all. Continuing, but this may look\n"
166 "very poor indeed.\n\n",
167 (slant
?(bold
?"bold italic":"italic"):(bold
?"bold":"medium")), fontface
);
170 /* Set the hinting behavior we want; only the autohinter is giving
171 decent results across a wide range of sizes and face options.
172 Don't obey the system config with respect to hinting; every one
173 of my systems installed a default config that resulted in the
174 available fonts looking like ass. Bitstream Vera Sans especially
175 looks bad without the autohinter, and that's the font most
176 systems will have as the only sans default. */
178 // Must remove the preexisting settings! The settings are a list,
179 // and we're tacking ours on the end; Cairo only inspects first hit
180 FcPatternDel(fc_pattern
,FC_HINT_STYLE
);
181 FcPatternDel(fc_pattern
,FC_HINTING
);
182 FcPatternDel(fc_pattern
,FC_AUTOHINT
);
184 // Add ours requesting autohint
185 FcPatternAddBool(fc_pattern
,FC_HINTING
,FcTrue
);
186 FcPatternAddBool(fc_pattern
,FC_AUTOHINT
,FcTrue
);
188 // Make the font face
189 ret
= cairo_ft_font_face_create_for_pattern(fc_pattern
);
190 FcPatternDestroy(fc_pattern
);
194 static void init_fonts(char *list
){
196 font_face
= init_font(list
,0,0);
197 bold_face
= init_font(list
,0,1);
198 ital_face
= init_font(list
,1,0);
200 // We've done the best we can, font-choice-wise. Determine an
201 // aspect ratio correction for whatever font we've got. Arial is
202 // declared to be the ideal aspect, correct other fonts to the same
203 // rough proportions (it looks better). This is a rough hack, but
204 // yields an unmistakable improvement.
207 cairo_text_extents_t ex
;
208 char *test_string
= "Practice Before the Handbasket: Three of Three";
210 cairo_image_surface_create (CAIRO_FORMAT_ARGB32
,16,16);
211 cairo_t
*c
= cairo_create(s
);
214 cairo_set_font_face (c
, font_face
);
215 cairo_matrix_init_scale (&m
, 50.,50.);
216 cairo_set_font_matrix (c
,&m
);
217 cairo_text_extents (c
, test_string
, &ex
);
218 adjust_x_normal
= 1058 / ex
.width
;
219 adjust_y_normal
= -37 / ex
.y_bearing
;
222 cairo_set_font_face (c
, bold_face
);
223 cairo_text_extents (c
, test_string
, &ex
);
224 adjust_x_bold
= 1130 / ex
.width
;
225 adjust_y_bold
= -37 / ex
.y_bearing
;
229 cairo_surface_destroy(s
);
233 void set_font (cairo_t
*c
, float w
, float h
, int slant
, int bold
){
237 cairo_set_font_face(c
,ital_face
);
238 cairo_matrix_init_scale (&m
, ceil(w
*adjust_x_normal
),ceil(h
*adjust_y_normal
));
240 cairo_set_font_face(c
,bold_face
);
241 cairo_matrix_init_scale (&m
, ceil(w
*adjust_x_bold
),ceil(h
*adjust_y_bold
));
243 cairo_set_font_face(c
,font_face
);
244 cairo_matrix_init_scale (&m
, ceil(w
*adjust_x_normal
),ceil(h
*adjust_y_normal
));
246 cairo_set_font_matrix (c
,&m
);
254 //GdkPixbufLoader *pbl1 = gdk_pixbuf_loader_new();
255 GdkPixbufLoader
*pbl2
= gdk_pixbuf_loader_new();
256 GdkPixbufLoader
*pbl3
= gdk_pixbuf_loader_new();
258 //gdk_pixbuf_loader_write(pbl1,icon134,sizeof(icon134),&error);
259 gdk_pixbuf_loader_write(pbl2
,icon64
,sizeof(icon64
),&error
);
260 gdk_pixbuf_loader_write(pbl3
,icon32
,sizeof(icon32
),&error
);
261 //gdk_pixbuf_loader_close(pbl1,NULL);
262 gdk_pixbuf_loader_close(pbl2
,NULL
);
263 gdk_pixbuf_loader_close(pbl3
,NULL
);
265 //pb = g_list_append(pb, gdk_pixbuf_loader_get_pixbuf(pbl1));
266 pb
= g_list_append(pb
, gdk_pixbuf_loader_get_pixbuf(pbl2
));
267 pb
= g_list_append(pb
, gdk_pixbuf_loader_get_pixbuf(pbl3
));
268 gtk_window_set_icon_list(GTK_WINDOW(toplevel_window
),pb
);
272 int main(int argc
, char *argv
[]){
273 char *homedir
= getenv("home");
275 homedir
= getenv("HOME");
277 homedir
= getenv("homedir");
279 homedir
= getenv("HOMEDIR");
281 fprintf(stderr
,"No homedir environment variable set! gPlanarity will be\n"
282 "unable to permanently save any progress or board state.\n");
286 boarddir
=calloc(strlen(homedir
)+strlen(boardstate
)+1,1);
287 strcat(boarddir
,homedir
);
288 strcat(boarddir
,boardstate
);
290 statedir
=calloc(strlen(homedir
)+strlen(mainstate
)+1,1);
291 strcat(statedir
,homedir
);
292 strcat(statedir
,mainstate
);
294 dir_create(statedir
);
295 dir_create(boarddir
);
298 version
=strstr(VERSION
,"version.h");
300 char *versionend
=strchr(version
,' ');
301 if(versionend
)versionend
=strchr(versionend
+1,' ');
302 if(versionend
)versionend
=strchr(versionend
+1,' ');
303 if(versionend
)versionend
=strchr(versionend
+1,' ');
305 int len
=versionend
-version
-9;
306 version
=strdup(version
+10);
315 gtk_init (&argc
, &argv
);
317 toplevel_window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
318 g_signal_connect (G_OBJECT (toplevel_window
), "delete-event",
319 G_CALLBACK (clean_exit_delete
), NULL
);
321 gameboard
= gameboard_new();
324 gtk_container_add (GTK_CONTAINER (toplevel_window
), GTK_WIDGET(gameboard
));
325 gtk_widget_realize(toplevel_window
);
326 gtk_widget_realize(GTK_WIDGET(gameboard
));
328 memset(&maingraph
,0,sizeof(maingraph
));
330 /* get the setup processed before we fire up animations */
331 while (gtk_events_pending ()){
332 gtk_main_iteration ();
333 gdk_window_process_updates(toplevel_window
->window
,1);
340 gtk_widget_show_all(toplevel_window
);
341 signal(SIGINT
,clean_exit
);
343 //signal(SIGSEGV,clean_exit); /* would be a bad idea; corrupt state
344 //could prevent us from restarting */
351 cairo_font_face_destroy(font_face
);
352 cairo_font_face_destroy(bold_face
);
353 cairo_font_face_destroy(ital_face
);