original 1.0.1 release
[xwelltris.git] / src / wellintro.cxx
blob0b86e6b65844285249eda4b0f0ca0171640c85c8
1 // docm_prefix(///)
2 /****************************************************************************
3 * Copyright (C) 2002 by Leo Khramov
4 * email: leo@xnc.dubna.su
5 *
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"
26 #include "version.h"
28 //===========================================================================
29 /// global WellIntro()
30 /// constructor - fill table
31 /// tags WellIntro
32 WellIntro::WellIntro() : WellObject()
34 int i;
35 char buf[L_MAXPATH];
36 start_level=0;
37 txt_level=default_well_engine->new_well_image_font(imFont1,FONT3_L,FONT3_H,
38 FONT3_DX,FONT3_DY);
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
81 /// tags WellIntro
82 bool WellIntro::process_event(wEvent ev)
84 int i;
85 WellObject* wo=(WellObject*)ev.data;
87 switch(ev.type)
89 case eExpose:
90 redraw();
91 break;
92 case eKeyPress:
93 hide_by_call(wEvent(aKeyPressed,key_new_game));
94 return false;
96 case aKeyPressed:
97 if(wo==key_plus && start_level+1<NUM_LEVELS)
99 start_level++;
100 draw_start_level();
101 return true;
103 if(wo==key_minus && start_level>0)
105 start_level--;
106 draw_start_level();
107 return true;
109 break;
111 case aSwitchChanged:
112 if(wo==sw_rotation)
114 well->set_rotation(sw_rotation->get_value());
115 break;
117 if(wo==sw_next_piece)
119 well->set_next_piece(sw_next_piece->get_value());
120 break;
122 if(wo==sw_mixed)
124 well->set_mixed(sw_mixed->get_value());
125 break;
127 for(i=0;i<TOTAL_SQUARES;i++)
128 if(wo==sw_squares[i])
130 well->set_squares(MIN_SQUARES+i);
131 } else
132 sw_squares[i]->set_value(false);
133 break;
135 return true;
138 //===========================================================================
139 /// global show()
140 /// show object
141 /// tags WellIntro
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);
148 key_exit->show();
149 key_new_game->show();
150 key_top_nine->show();
151 key_plus->show();
152 key_minus->show();
153 sw_rotation->show();
154 sw_next_piece->show();
155 sw_mixed->show();
156 for(int i=0;i<TOTAL_SQUARES;i++)
157 sw_squares[i]->show();
158 draw_start_level();
159 shown=true;
162 //===========================================================================
163 /// global hide()
164 /// hide object (empty)
165 /// tags WellIntro
166 void WellIntro::hide()
168 if(shown)
170 shown=false;
171 default_well_engine->del_object(this);
172 key_exit->hide();
173 key_new_game->hide();
174 key_top_nine->hide();
175 key_plus->hide();
176 key_minus->hide();
177 sw_rotation->hide();
178 sw_next_piece->hide();
179 sw_mixed->hide();
180 for(int i=0;i<TOTAL_SQUARES;i++)
181 sw_squares[i]->hide();
186 //===========================================================================
187 /// global hide_by_call()
188 /// hide object
189 /// tags WellIntro
190 bool WellIntro::hide_by_call(wEvent ev)
192 WellObject* wo=(WellObject*)ev.data;
194 hide();
196 put_all_to_game();
197 save_options();
199 if(wo==inp_player)
201 inp_player->hide();
202 return object_on_new_game.call(wEvent(aIntroExit,this));
205 if(wo==key_exit)
207 default_well_engine->done_loop();
208 return true;
211 if(wo==key_new_game)
213 inp_player->show();
216 if(wo==key_top_nine)
218 return object_on_top_nine.call(wEvent(aIntroExit,this));
221 return true;
225 //===========================================================================
226 /// global draw_start_level()
227 /// draw start level on the screen
228 /// tags WellIntro
229 void WellIntro::draw_start_level()
231 char buf[L_MAXPATH];
232 sprintf(buf,"%02d",start_level);
233 txt_level->set_text(buf);
234 txt_level->draw_text();
235 well->set_level(start_level);
239 //===========================================================================
240 /// global redraw()
241 /// redraw all scene object
242 /// tags WellIntro
243 void WellIntro::redraw()
245 draw_start_level();
246 key_exit->redraw();
247 key_new_game->redraw();
248 key_top_nine->redraw();
249 key_plus->redraw();
250 key_minus->redraw();
251 sw_rotation->redraw();
252 sw_next_piece->redraw();
253 sw_mixed->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
261 /// tags WellIntro
262 void WellIntro::put_all_to_game()
264 int i;
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
283 /// tags WellIntro
284 void WellIntro::load_defaults()
286 sw_rotation->set_value(true);
287 sw_mixed->set_value(true);
288 sw_next_piece->set_value(true);
289 start_level=0;
290 sw_squares[4-MIN_SQUARES]->set_value(true);
291 inp_player->set_text(getenv("USER"));
293 load_options();
295 put_all_to_game();
298 //===========================================================================
299 /// global set_well_base(WellBase*)
300 /// set pointer to the game object, loads and set default values
301 /// tags WellIntro
302 void WellIntro::set_well_base(WellBase* o)
304 well=o;
305 load_defaults();
308 //===========================================================================
309 /// global save_options()
310 /// save options to file
311 /// tags WellIntro
312 void WellIntro::save_options()
314 int i;
315 char fname[L_MAXPATH];
316 FILE *fp;
317 #ifndef WIN32
318 sprintf(fname,"%s/%s",getenv("HOME"),XWELL_DIR);
319 mkdir(fname,0755);
320 sprintf(fname,"%s/%s/%s",getenv("HOME"),XWELL_DIR,RC_FILE);
321 #else
322 sprintf(fname,"data\\%s", RC_FILE);
323 #endif
324 fp=fopen(fname, "w");
325 if(fp==0)
327 fprintf(stderr,"Error in opening resource file for writing:\n\t%s\n",
328 strerror(errno));
329 return;
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");
337 else
338 fprintf(fp,"use_rotation: no\n");
340 if(sw_mixed->get_value())
341 fprintf(fp,"use_mixed: yes\n");
342 else
343 fprintf(fp,"use_mixed: no\n");
345 if(sw_next_piece->get_value())
346 fprintf(fp,"use_next_piece: yes\n");
347 else
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())
354 break;
355 fprintf(fp, "tris_squares: %d\n", i+MIN_SQUARES);
356 fprintf(fp,"#------------------ End of file ---------------------#\n");
358 fclose(fp);
361 //===========================================================================
362 /// global load_options()
363 /// load options from file
364 /// tags WellIntro
365 void WellIntro::load_options()
367 int i,n;
368 char fname[L_MAXPATH];
369 char buf[L_MAXPATH];
370 char tok[L_MAXPATH], *pbuf;
371 FILE* fp;
372 #ifndef WIN32
373 sprintf(fname,"%s/%s/%s",getenv("HOME"),XWELL_DIR,RC_FILE);
374 #else
375 sprintf(fname,"data\\%s",RC_FILE);
376 #endif
377 fp=fopen(fname, "r");
378 if(fp==0)
379 return;
381 while(fgets(buf,L_MAXPATH-1,fp))
383 if(buf[0]=='#')
384 continue;
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);
392 else
393 sw_rotation->set_value(false);
394 continue;
397 if(!strcmp(tok,"use_mixed"))
399 get_next_token(pbuf,tok);
400 if(!strcmp(tok,"yes"))
401 sw_mixed->set_value(true);
402 else
403 sw_mixed->set_value(false);
404 continue;
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);
412 else
413 sw_next_piece->set_value(false);
414 continue;
417 if(!strcmp(tok,"last_player"))
419 get_next_token(pbuf,tok);
420 inp_player->set_text(tok);
421 continue;
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)
429 start_level=0;
430 continue;
433 if(!strcmp(tok,"tris_squares"))
435 get_next_token(pbuf,tok);
436 n=atoi(tok);
437 n-=MIN_SQUARES;
438 if(n<0 || n>=TOTAL_SQUARES)
439 n=0;
440 for(i=0;i<TOTAL_SQUARES;i++)
441 sw_squares[i]->set_value(n==i ? true : false);
442 continue;
445 fclose(fp);
448 //===========================================================================
449 /// global get_next_token(char* from, char* to)
450 /// cut buffer by tokens
451 /// tags WellIntro
452 char* WellIntro::get_next_token(char* from, char* to)
454 int delta=0;
455 char *orig_to=to;
456 *to=0;
457 if(*from==':')
458 from++;
459 while(*from==' ')
460 from++;
461 while(*from!=0 && *from!=':' && *from!='\n' && *from!='\r' && *from!=0)
462 *to++=*from++;
463 while(to!=orig_to && *to==' ')
465 to--;
466 delta=1;
468 to+=delta;
469 *to=0;
470 return from;