Recognizes if input is ogg or not.
[xiph/unicode.git] / planarity / main.c
blobb0438b3a5524078cd713cc40f6073b43c4cc6f85
1 /*
3 * gPlanarity:
4 * The geeky little puzzle game with a big noodly crunch!
5 *
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)
13 * any later version.
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.
27 #define _GNU_SOURCE
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <math.h>
33 #include <signal.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <gtk/gtk.h>
37 #include <gdk-pixbuf/gdk-pixbuf.h>
38 #include <cairo-ft.h>
39 #include <time.h>
40 #include <fontconfig/fontconfig.h>
41 #include "version.h"
42 #include "graph.h"
43 #include "gameboard.h"
44 #include "levelstate.h"
45 #include "main.h"
47 #define boardstate "/.gPlanarity/boards/"
48 #define mainstate "/.gPlanarity/"
50 char *boarddir;
51 char *statedir;
52 Gameboard *gameboard;
53 GtkWidget *toplevel_window;
54 graph maingraph;
56 char *version = "";
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){
67 if(mkdir(name,0700)){
68 switch(errno){
69 case EEXIST:
70 // this is ok
71 return 0;
72 default:
73 fprintf(stderr,_("ERROR: Could not create directory (%s) to save game state:\n\t%s\n"),
74 name,strerror(errno));
75 return errno;
78 return 0;
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){
90 signal(sig,SIG_IGN);
91 if(sig!=SIGINT)
92 fprintf(stderr,
93 _("\nTrapped signal %d; saving state and exiting!\n"),sig);
95 levelstate_write(statedir);
96 gtk_main_quit();
97 exit(0);
100 static void clean_exit_delete_post(Gameboard *g){
101 gameboard = 0;
102 gtk_main_quit();
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;
124 char *fontface;
125 FcPattern *fc_pattern;
126 FcBool scalable;
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);
135 if(slant)
136 FcPatternAddInteger(fc_pattern,FC_SLANT,FC_SLANT_ITALIC);
137 if(bold)
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);
145 if(!fc_pattern){
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")),
159 list,fontface);
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);
191 return ret;
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";
209 cairo_surface_t *s =
210 cairo_image_surface_create (CAIRO_FORMAT_ARGB32,16,16);
211 cairo_t *c = cairo_create(s);
212 cairo_matrix_t m;
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;
228 cairo_destroy(c);
229 cairo_surface_destroy(s);
233 void set_font (cairo_t *c, float w, float h, int slant, int bold){
234 cairo_matrix_t m;
236 if(slant){
237 cairo_set_font_face(c,ital_face);
238 cairo_matrix_init_scale (&m, ceil(w*adjust_x_normal),ceil(h*adjust_y_normal));
239 }else if (bold){
240 cairo_set_font_face(c,bold_face);
241 cairo_matrix_init_scale (&m, ceil(w*adjust_x_bold),ceil(h*adjust_y_bold));
242 }else{
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);
250 #include "icon.h"
251 void set_icons(){
252 GError *error=NULL;
253 GList *pb=NULL;
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 #ifdef ENABLE_NLS
274 setlocale(LC_ALL, "");
275 textdomain(GT_DOMAIN);
276 bindtextdomain(GT_DOMAIN, GT_DIR);
277 #endif
279 char *homedir = getenv("home");
280 if(!homedir)
281 homedir = getenv("HOME");
282 if(!homedir)
283 homedir = getenv("homedir");
284 if(!homedir)
285 homedir = getenv("HOMEDIR");
286 if(!homedir){
287 fprintf(stderr,
288 _("No homedir environment variable set! gPlanarity will be\n"
289 "unable to permanently save any progress or board state.\n"));
290 boarddir=NULL;
291 statedir=NULL;
292 }else{
293 boarddir=calloc(strlen(homedir)+strlen(boardstate)+1,1);
294 strcat(boarddir,homedir);
295 strcat(boarddir,boardstate);
297 statedir=calloc(strlen(homedir)+strlen(mainstate)+1,1);
298 strcat(statedir,homedir);
299 strcat(statedir,mainstate);
301 dir_create(statedir);
302 dir_create(boarddir);
305 version=strstr(VERSION,"version.h");
306 if(version){
307 char *versionend=strchr(version,' ');
308 if(versionend)versionend=strchr(versionend+1,' ');
309 if(versionend)versionend=strchr(versionend+1,' ');
310 if(versionend)versionend=strchr(versionend+1,' ');
311 if(versionend){
312 int len=versionend-version-9;
313 version=strdup(version+10);
314 version[len-1]=0;
316 }else{
317 version="";
320 init_fonts("");
322 gtk_init (&argc, &argv);
324 toplevel_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
325 g_signal_connect (G_OBJECT (toplevel_window), "delete-event",
326 G_CALLBACK (clean_exit_delete), NULL);
328 gameboard = gameboard_new();
329 levelstate_read();
331 gtk_container_add (GTK_CONTAINER (toplevel_window), GTK_WIDGET(gameboard));
332 gtk_widget_realize(toplevel_window);
333 gtk_widget_realize(GTK_WIDGET(gameboard));
335 memset(&maingraph,0,sizeof(maingraph));
337 /* get the setup processed before we fire up animations */
338 while (gtk_events_pending ()){
339 gtk_main_iteration ();
340 gdk_window_process_updates(toplevel_window->window,1);
341 gdk_flush();
344 levelstate_resume();
345 set_icons();
347 gtk_widget_show_all(toplevel_window);
348 signal(SIGINT,clean_exit);
350 //signal(SIGSEGV,clean_exit); /* would be a bad idea; corrupt state
351 //could prevent us from restarting */
353 gtk_main ();
355 if(gameboard !=0 )
356 levelstate_write();
358 cairo_font_face_destroy(font_face);
359 cairo_font_face_destroy(bold_face);
360 cairo_font_face_destroy(ital_face);
362 return 0;