Support for g++-4.1.
[gf1.git] / core.cxx
blob9ec6889d9057323f08364c0a05d622bc1763ac65
1 /*
2 ** $Id$
3 **
4 ** the core routines for the program
5 ** compile it with C++ but most of it will probably be C
6 */
7 /*
8 ** Copyright (C) 1998-1999 Kurt Van den Branden
9 **
10 ** This program 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 of the License, or
13 ** (at your option) any later version.
14 **
15 ** This program 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.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 char versiontext[] = "Gipf for one v 1.03 (28/02/2000)";
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include <FL/Fl_Menu_Button.H>
32 #include "fl_board.h"
33 #include "gipf_ui.H"
34 #include "callbacks.h"
35 #include "setup_win.h"
36 #include "thegame.h"
37 #include "configfile.h"
39 //#define GIPF_ICON 1
40 #ifdef GIPF_ICON
41 #include <FL/x.H>
42 #include "i_blauw.xpm"
43 #endif
45 #ifdef MSWIN // for the icon
46 #define GF1_ICON 345
47 #include <FL/x.H>
48 #endif
50 int stop_program = 0; /* set by the callback from the main window */
52 board * loadboard (void);
53 void saveboard (board * boardptr);
54 void savelog (gamelog * logptr);
57 int main (int argc, char ** argv)
59 int theend = 0,
60 newval;
61 Fl_Window * mainwin,
62 * helpwin;
63 configvalues gf1_config;
64 gamestruct * currentgame = NULL,
65 * newgame = NULL;
66 #ifdef GIPF_ICON
67 Pixmap p;
68 #endif
70 // new random seed
71 srand (time (NULL));
73 /* read configuration */
74 retrieveconfig (& gf1_config);
76 /* create the main window */
77 mainwin = create_mainwindow();
79 #ifdef GIPF_ICON
80 /* set the icon */
81 p = XCreateBitmapFromData(fl_display, DefaultRootWindow(fl_display),
82 (char *) i_blauw_xpm, 64, 64);
83 mainwin->icon((char *)p);
84 #endif
86 #ifdef MSWIN
87 if (fl_display == 0)
88 fl_display = GetModuleHandle (NULL);
90 mainwin->icon((char *)LoadIcon(fl_display, MAKEINTRESOURCE(GF1_ICON)));
91 #endif
93 /* setup the gameboard */
94 gameboard->setoutputwidgets(whitepieces, lostwhite, drawwhite,
95 blackpieces, lostblack, drawblack,
96 text_from, text_to);
97 gameboard->setexecbutton (button_execute);
98 gameboard->setshowposname (gf1_config.showposname);
100 /* create helpwindow but don't show it */
101 helpwin = gf1_help ();
103 /* set interface-items */
104 changeinterface (INTERFACE_PLAY, NULL);
106 /* show the main window */
107 Fl::visual (FL_DOUBLE|FL_INDEX);
108 Fl::background (0xb0, 0xb0, 0xb0);
109 mainwin->show();
110 // mainwin->show(argc, argv); // don't do this, uses default user
111 // colors on windows
114 ** on windows95 the window doesn't stay on for 5 seconds
115 ** if I don't add the following line
117 Fl::wait();
118 /* show the about-window, it will go away after 5 seconds */
119 show_about (1);
121 while (!theend)
123 /* The program must be stopped when wait() returns 0 */
124 if (Fl::wait() == 0)
126 break;
129 Fl_Widget *x;
130 while ((x = Fl::readqueue()))
132 if (x == gf1_window)
134 // printf ("mainwin event\n");
136 else if (x == menubar)
138 const Fl_Menu * z;
139 Fl_Menu_Bar * y = (Fl_Menu_Bar *) x;
141 z = y->mvalue ();
142 if (z == menu_new)
144 newgame = show_new (currentgame);
145 if (newgame != NULL)
147 stoptimer (currentgame);
148 delete_game (currentgame);
149 currentgame = newgame;
150 currentgame->config = & gf1_config;
152 gameboard->setboard (currentgame->boards[0]);
154 setupmove (currentgame);
157 else if (z == menu_loadgame)
159 newgame = loadgame ();
160 if (newgame != NULL)
162 stoptimer (currentgame);
163 delete_game (currentgame);
164 currentgame = newgame;
165 currentgame->config = & gf1_config;
167 gameboard->setboard (newgame->boards[0]);
169 setupmove (currentgame);
172 else if (z == menu_savegame)
174 savegame (currentgame);
176 else if (z == menu_savelog)
178 if (currentgame != NULL)
179 savelog (currentgame->movelog);
180 else
181 savelog (NULL);
183 else if (z == menu_setup)
185 if (show_setup (&gf1_config))
187 writeconfig (&gf1_config);
188 gameboard->setshowposname (gf1_config.showposname);
191 else if (z == menu_exit)
193 theend = 1;
195 else if (z == menu_edit)
197 board * tempboard;
199 stoptimer (currentgame);
200 if (currentgame == NULL)
202 tempboard = b_new (T_TOURNAMENT);
203 gameboard->setboard (tempboard);
204 b_del (tempboard);
206 else
207 { /* convert board to tournament configuration */
208 tempboard = b_copy (gameboard->getboard ());
209 if (b_white_gipf (gameboard->getboard ()) == -1)
211 tempboard->gipfwhite = 0;
212 tempboard->gipfblack = 0;
213 tempboard->white += 3;
214 tempboard->black += 3;
216 tempboard->movecounter = 0;
217 gameboard->setboard (tempboard);
218 b_del (tempboard);
220 changeinterface (INTERFACE_EDIT, currentgame);
222 setlostcounters ();
224 else if (z == menu_start)
226 newgame = show_start (gameboard->getboard ());
227 if (newgame != NULL)
229 delete_game (currentgame);
230 currentgame = newgame;
231 currentgame->config = & gf1_config;
233 gameboard->setboard (currentgame->boards[0]);
235 setupmove (currentgame);
238 else if (z == menu_compumove)
240 newgame = show_onemove (gameboard->getboard (),
241 & gf1_config);
242 if (newgame != NULL)
244 delete_game (currentgame);
245 currentgame = newgame;
248 else if (z == menu_clear)
250 board * tempboard;
252 tempboard = b_new (T_TOURNAMENT);
253 gameboard->setboard (tempboard);
254 b_del (tempboard);
255 setlostcounters ();
257 else if (z == menu_restore)
259 changeinterface (INTERFACE_PLAY, currentgame);
260 if (currentgame == NULL)
262 gameboard->setboard (NULL);
264 else
266 gameboard->setboard (currentgame->boards[0]);
268 setupmove (currentgame);
271 else if (z == menu_loadboard)
273 board * tempboard = NULL;
275 if ((tempboard = loadboard ()) != NULL)
277 gameboard->setboard (tempboard);
278 b_del (tempboard);
280 setlostcounters ();
283 else if (z == menu_saveboard)
285 saveboard (gameboard->getboard());
287 else if (z == menu_makedrawing)
289 show_makegif (gameboard->getboard ());
291 else if (z == menu_undo)
293 if ((currentgame != NULL) &&
294 (currentgame->boards[2] != NULL))
296 b_del (currentgame->boards[0]);
297 b_del (currentgame->boards[1]);
298 currentgame->boards[0] = currentgame->boards[2];
299 currentgame->boards[1] = NULL;
300 currentgame->boards[2] = NULL;
301 currentgame->movecounter -=
302 remlastmove (currentgame->movelog);
303 currentgame->movecounter -=
304 remlastmove (currentgame->movelog);
306 setupmove (currentgame);
307 gameboard->setboard (currentgame->boards[0]);
310 else if (z == menu_help)
312 helpwin->show();
314 else if (z == menu_about)
316 show_about (0);
319 else if (x == choice_piecetype)
321 const Fl_Menu * z;
322 Fl_Choice * y = (Fl_Choice *) x;
324 z = y->mvalue ();
325 if (z == choice_white)
327 gameboard->seteditpiece ('o');
329 else if (z == choice_whitegipf)
331 gameboard->seteditpiece ('O');
333 else if (z == choice_black)
335 gameboard->seteditpiece ('x');
337 else if (z == choice_blackgipf)
339 gameboard->seteditpiece ('X');
342 else if (x == menu_piecetype)
344 const Fl_Menu * z;
345 Fl_Menu_Button * y = (Fl_Menu_Button *) x;
347 z = y->mvalue ();
348 if (z == menu_white)
350 choice_piecetype->value (0);
351 gameboard->seteditpiece ('o');
353 else if (z == menu_whitegipf)
355 choice_piecetype->value (1);
356 gameboard->seteditpiece ('O');
358 else if (z == menu_black)
360 choice_piecetype->value (2);
361 gameboard->seteditpiece ('x');
363 else if (z == menu_blackgipf)
365 choice_piecetype->value (3);
366 gameboard->seteditpiece ('X');
369 else if (x == count_lostwhite)
371 newval = gameboard->setlostwhite
372 ((int) count_lostwhite->value());
373 /* just in case the new value is not valid */
374 count_lostwhite->value ((double) newval);
376 else if (x == count_lostblack)
378 newval = gameboard->setlostblack
379 ((int) count_lostblack->value());
380 /* just in case the new value is not valid */
381 count_lostblack->value ((double) newval);
383 else if (x == button_execute)
385 if (currentgame != NULL)
387 if (humanmove (currentgame) != 0)
388 gf1_alert (
389 "invalid move, check the from- and to-position !");
391 setupmove (currentgame);
396 if (stop_program)
398 theend = 1;
402 stoptimer (currentgame);
403 delete_game (currentgame);
404 delete helpwin;
405 delete mainwin;
406 clearconfiglist (gf1_config.configlist);
408 return (0);
413 ** load a board from a file
415 ** return NULL if nothing loaded
417 board * loadboard (void)
419 const char * filename;
420 board * newboard;
421 xmlite_entity * root;
422 xmlite_parser * theparser;
424 // ask for filename
425 if ((filename = file_chooser ("load board", "*.brd", NULL)) == NULL)
427 return (NULL);
430 theparser = new xmlite_parser (filename);
431 root = theparser->parse();
432 delete (theparser);
433 if (root == NULL)
435 gf1_alert (" ERROR: wrong inputfile format");
436 return (NULL);
439 newboard = b_from_xml (root);
440 delete (root);
442 return (newboard);
447 ** save a board to a file
449 void saveboard (board * boardptr)
451 const char * filename;
452 xmlite_entity * root;
454 if (boardptr == NULL)
456 gf1_alert (" no current board to be saved");
457 return;
460 // ask for filename
461 if ((filename = file_chooser ("save board", "*.brd", "./board.brd"))
462 == NULL)
464 return;
467 root = b_to_xml (boardptr);
468 root->writetofile (filename);
469 delete (root);
471 return;
476 ** save a gamelog to a file
478 void savelog (gamelog * logptr)
480 const char * filename;
481 FILE * fp;
483 if (logptr == NULL)
485 gf1_alert (" no gamelog to be saved");
486 return;
489 // ask for filename
490 if ((filename = file_chooser ("save board", "*.log", "./gamelog.log"))
491 == NULL)
493 return;
496 if ((fp = fopen (filename, "w")) == NULL)
498 gf1_alert (" ERROR: Can't create the file");
499 return;
502 logtofile (logptr, fp);
504 fclose (fp);
506 return;
509 extern "C" void checkwindowevents (void);
511 void checkwindowevents (void)
513 Fl::check ();
515 return;