2 /****************************************************************************
3 * Copyright (C) 2002 by Leo Khramov
4 * email: leo@xnc.dubna.su
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 ****************************************************************************/
16 // $Id: wellintro.cxx,v 1.3 2003/02/27 08:27:51 leo Exp $
18 /// module description
19 /// WellIntro class - implements Introduction screen with options switches
20 /// new game, top nine and exit keys
22 #include "wellclass.h"
23 #include "wellintro.h"
24 #include "wellengine.h"
25 #include "welltopnine.h"
28 //===========================================================================
29 /// global WellIntro()
30 /// constructor - fill table
32 WellIntro::WellIntro() : WellObject()
37 txt_level
=default_well_engine
->new_well_image_font(imFont1
,FONT3_L
,FONT3_H
,
39 txt_level
->set_screen_region(674,403,34,20);
41 key_exit
=default_well_engine
->new_well_key("intro_key_exit");
42 key_new_game
=default_well_engine
->new_well_key("intro_key_new_game");
43 key_top_nine
=default_well_engine
->new_well_key("intro_key_top_nine");
44 key_plus
=default_well_engine
->new_well_key("intro_key_plus");
45 key_minus
=default_well_engine
->new_well_key("intro_key_minus");
47 key_exit
->set_object_on_press(ObjectCaller(this,&WellObject::hide_by_call
));
48 key_new_game
->set_object_on_press(ObjectCaller(this,&WellObject::hide_by_call
));
49 key_top_nine
->set_object_on_press(ObjectCaller(this,&WellObject::hide_by_call
));
50 key_plus
->set_object_on_press(ObjectCaller(this,&WellObject::process_event
));
51 key_minus
->set_object_on_press(ObjectCaller(this,&WellObject::process_event
));
53 sw_rotation
=default_well_engine
->new_well_switch("intro_sw_rotation");
54 sw_rotation
->set_object_on_switch(ObjectCaller(this,
55 &WellObject::process_event
));
56 sw_next_piece
=default_well_engine
->new_well_switch("intro_sw_next");
57 sw_next_piece
->set_object_on_switch(ObjectCaller(this,
58 &WellObject::process_event
));
59 sw_mixed
=default_well_engine
->new_well_switch("intro_sw_mixed");
60 sw_mixed
->set_object_on_switch(ObjectCaller(this,
61 &WellObject::process_event
));
62 for(i
=0;i
<TOTAL_SQUARES
;i
++)
64 sprintf(buf
,"intro_sw_square%d",i
);
65 sw_squares
[i
]=default_well_engine
->new_well_switch(buf
);
66 sw_squares
[i
]->set_object_on_switch(ObjectCaller(this,
67 &WellObject::process_event
));
68 sw_squares
[i
]->set_mode(OnlySet
);
71 inp_player
=default_well_engine
->new_well_input("intro_inp_player");
72 inp_player
->set_object_on_enter(ObjectCaller(this,
73 &WellObject::hide_by_call
));
74 inp_player
->set_max_len(PLAYER_NAME_LEN
-1);
78 //===========================================================================
79 /// global process_event(wEvent)
80 /// stub that process events
82 bool WellIntro::process_event(wEvent ev
)
85 WellObject
* wo
=(WellObject
*)ev
.data
;
93 hide_by_call(wEvent(aKeyPressed
,key_new_game
));
97 if(wo
==key_plus
&& start_level
+1<NUM_LEVELS
)
103 if(wo
==key_minus
&& start_level
>0)
114 well
->set_rotation(sw_rotation
->get_value());
117 if(wo
==sw_next_piece
)
119 well
->set_next_piece(sw_next_piece
->get_value());
124 well
->set_mixed(sw_mixed
->get_value());
127 for(i
=0;i
<TOTAL_SQUARES
;i
++)
128 if(wo
==sw_squares
[i
])
130 well
->set_squares(MIN_SQUARES
+i
);
132 sw_squares
[i
]->set_value(false);
138 //===========================================================================
142 void WellIntro::show()
145 dbgprintf(("WellIntro::show() called\n"));
146 default_well_engine
->set_main_background_image(imIntroBG
);
147 default_well_engine
->add_object(this);
149 key_new_game
->show();
150 key_top_nine
->show();
154 sw_next_piece
->show();
156 for(int i
=0;i
<TOTAL_SQUARES
;i
++)
157 sw_squares
[i
]->show();
162 //===========================================================================
164 /// hide object (empty)
166 void WellIntro::hide()
171 default_well_engine
->del_object(this);
173 key_new_game
->hide();
174 key_top_nine
->hide();
178 sw_next_piece
->hide();
180 for(int i
=0;i
<TOTAL_SQUARES
;i
++)
181 sw_squares
[i
]->hide();
186 //===========================================================================
187 /// global hide_by_call()
190 bool WellIntro::hide_by_call(wEvent ev
)
192 WellObject
* wo
=(WellObject
*)ev
.data
;
202 return object_on_new_game
.call(wEvent(aIntroExit
,this));
207 default_well_engine
->done_loop();
218 return object_on_top_nine
.call(wEvent(aIntroExit
,this));
225 //===========================================================================
226 /// global draw_start_level()
227 /// draw start level on the screen
229 void WellIntro::draw_start_level()
232 sprintf(buf
,"%02d",start_level
);
233 txt_level
->set_text(buf
);
234 txt_level
->draw_text();
235 well
->set_level(start_level
);
239 //===========================================================================
241 /// redraw all scene object
243 void WellIntro::redraw()
247 key_new_game
->redraw();
248 key_top_nine
->redraw();
251 sw_rotation
->redraw();
252 sw_next_piece
->redraw();
254 for(int i
=0;i
<TOTAL_SQUARES
;i
++)
255 sw_squares
[i
]->redraw();
258 //===========================================================================
259 /// global put_all_to_game()
260 /// put all values to the game
262 void WellIntro::put_all_to_game()
265 well
->set_rotation(sw_rotation
->get_value());
266 well
->set_mixed(sw_mixed
->get_value());
267 well
->set_next_piece(sw_next_piece
->get_value());
268 well
->set_level(start_level
);
269 for(i
=0;i
<TOTAL_SQUARES
;i
++)
270 if(sw_squares
[i
]->get_value())
272 well
->set_squares(i
+MIN_SQUARES
);
273 default_top_nine
->set_page(i
+1);
274 dbgprintf(("Set top_nine page to %d\n",i
+1));
276 well
->set_player_name(inp_player
->get_text());
280 //===========================================================================
281 /// global load_defaults()
282 /// set default values and try to load it from file
284 void WellIntro::load_defaults()
286 sw_rotation
->set_value(true);
287 sw_mixed
->set_value(true);
288 sw_next_piece
->set_value(true);
290 sw_squares
[4-MIN_SQUARES
]->set_value(true);
291 inp_player
->set_text(getenv("USER"));
298 //===========================================================================
299 /// global set_well_base(WellBase*)
300 /// set pointer to the game object, loads and set default values
302 void WellIntro::set_well_base(WellBase
* o
)
308 //===========================================================================
309 /// global save_options()
310 /// save options to file
312 void WellIntro::save_options()
315 char fname
[L_MAXPATH
];
318 sprintf(fname
,"%s/%s",getenv("HOME"),XWELL_DIR
);
320 sprintf(fname
,"%s/%s/%s",getenv("HOME"),XWELL_DIR
,RC_FILE
);
322 sprintf(fname
,"data\\%s", RC_FILE
);
324 fp
=fopen(fname
, "w");
327 fprintf(stderr
,"Error in opening resource file for writing:\n\t%s\n",
332 fprintf(fp
,"#--------------- Options file for xwelltris %s ----------------#\n",VERSION
);
333 fprintf(fp
,"#This file generated automatically, so don't edit it by hands\n\n");
334 fprintf(fp
, "last_player: %s\n",inp_player
->get_text());
335 if(sw_rotation
->get_value())
336 fprintf(fp
,"use_rotation: yes\n");
338 fprintf(fp
,"use_rotation: no\n");
340 if(sw_mixed
->get_value())
341 fprintf(fp
,"use_mixed: yes\n");
343 fprintf(fp
,"use_mixed: no\n");
345 if(sw_next_piece
->get_value())
346 fprintf(fp
,"use_next_piece: yes\n");
348 fprintf(fp
,"use_next_piece: no\n");
350 fprintf(fp
, "start_level: %d\n",start_level
);
352 for(i
=0;i
<TOTAL_SQUARES
;i
++)
353 if(sw_squares
[i
]->get_value())
355 fprintf(fp
, "tris_squares: %d\n", i
+MIN_SQUARES
);
356 fprintf(fp
,"#------------------ End of file ---------------------#\n");
361 //===========================================================================
362 /// global load_options()
363 /// load options from file
365 void WellIntro::load_options()
368 char fname
[L_MAXPATH
];
370 char tok
[L_MAXPATH
], *pbuf
;
373 sprintf(fname
,"%s/%s/%s",getenv("HOME"),XWELL_DIR
,RC_FILE
);
375 sprintf(fname
,"data\\%s",RC_FILE
);
377 fp
=fopen(fname
, "r");
381 while(fgets(buf
,L_MAXPATH
-1,fp
))
386 pbuf
=get_next_token(buf
,tok
);
387 if(!strcmp(tok
,"use_rotation"))
389 get_next_token(pbuf
,tok
);
390 if(!strcmp(tok
,"yes"))
391 sw_rotation
->set_value(true);
393 sw_rotation
->set_value(false);
397 if(!strcmp(tok
,"use_mixed"))
399 get_next_token(pbuf
,tok
);
400 if(!strcmp(tok
,"yes"))
401 sw_mixed
->set_value(true);
403 sw_mixed
->set_value(false);
407 if(!strcmp(tok
,"use_next_piece"))
409 get_next_token(pbuf
,tok
);
410 if(!strcmp(tok
,"yes"))
411 sw_next_piece
->set_value(true);
413 sw_next_piece
->set_value(false);
417 if(!strcmp(tok
,"last_player"))
419 get_next_token(pbuf
,tok
);
420 inp_player
->set_text(tok
);
424 if(!strcmp(tok
,"start_level"))
426 get_next_token(pbuf
,tok
);
427 start_level
=atoi(tok
);
428 if(start_level
<0 || start_level
>=NUM_LEVELS
)
433 if(!strcmp(tok
,"tris_squares"))
435 get_next_token(pbuf
,tok
);
438 if(n
<0 || n
>=TOTAL_SQUARES
)
440 for(i
=0;i
<TOTAL_SQUARES
;i
++)
441 sw_squares
[i
]->set_value(n
==i
? true : false);
448 //===========================================================================
449 /// global get_next_token(char* from, char* to)
450 /// cut buffer by tokens
452 char* WellIntro::get_next_token(char* from
, char* to
)
461 while(*from
!=0 && *from
!=':' && *from
!='\n' && *from
!='\r' && *from
!=0)
463 while(to
!=orig_to
&& *to
==' ')