1 /* This file is part of OpenBubbles.
2  *
3  * OpenBubbles is an SDL clone of Bubbles.
4  * Copyright (C) 2004  Benny Sperisen
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * highscores.cpp is the implementation file of highscores.h.
21  *
22  * Written by:
23  *  Benny Sperisen
24  *  lasindi@gmail.com
25  *  www.freewebs.com/lasindi
26  *****************************************************************************/
27 
28 #include "highscores.h"
29 
highscore(string name,int score)30 highscore::highscore(string name,int score)
31 :_name(name),_score(score)
32 {}
33 
name() const34 string highscore::name() const
35 {
36   return _name;
37 }
38 
score() const39 int highscore::score() const
40 {
41   return _score;
42 }
43 
44 vector<highscore> high_scores;
45 
loadHighScores()46 void loadHighScores()
47 {
48   string datadir,namebuffer;
49   int scorebuffer,i;
50   ifstream fromfile;
51   ofstream tofile;
52   datadir=getenv("HOME");
53   datadir+="/.openbubbles";
54   fromfile.open(datadir.c_str());
55   if(fromfile.is_open())
56   {
57 		getline(fromfile,namebuffer); // Skip the warning.
58 		getline(fromfile,namebuffer);
59 		if(namebuffer.substr(0,6)=="fading") // This is an OpenBubbles 1.1 file,
60 			getline(fromfile,namebuffer);      // so skip the next line too.
61 		else // This is (probably) an OpenBubbles 1.0 file,
62 		{
63 			fromfile.close(); // so restart ...
64 			fromfile.open(datadir.c_str());
65 			if(!fromfile.is_open())
66 			{
67 				cerr<<"ERROR: ~/.openbubbles suddenly couldn't be opened. Note: this i"
68 						<<"s an amazingly weird error, because it could be opened just a m"
69 						<<"oment ago\n";
70 				exit(1);
71 			}
72 			getline(fromfile,namebuffer); // and skip just the warning.
73 		}
74     for(i=0;!fromfile.eof()&&(i<NUMBER_OF_SCORES);i++)
75     {
76       fromfile>>namebuffer;
77       namebuffer.erase(namebuffer.length()-1,1); // Erase the last '|'.
78       if(fromfile.eof())
79       {
80         cerr<<"ERROR: ~/.openbubbles was corrupted; try restarting OpenBubbles"
81 						<<".\n";
82         fromfile.close();
83         tofile.open(datadir.c_str());
84         tofile<<"Don't mess with this file! If you do, and OpenBubbles crashes"
85 							<<", delete it and let OpenBubbles create a new one.\nfading 1"
86 							<<"\nsound 1\n";
87         for(int i=0;i<NUMBER_OF_SCORES;i++)
88           tofile<<"\nNobody| 0";
89         tofile.close();
90         exit(1);
91       }
92       fromfile>>scorebuffer;
93       high_scores.push_back(highscore(namebuffer,scorebuffer));
94     }
95     if(i<NUMBER_OF_SCORES-1)
96       for(;i<NUMBER_OF_SCORES;i++)
97         high_scores.push_back(highscore("Nobody",0));
98     fromfile.close();
99   }
100 	else
101 	{
102 		cout<<"here\n";
103 		tofile<<"Don't mess with this file! If you do, and OpenBubbles crashes"
104 					<<", delete it and let OpenBubbles create a new one.\nfading 1"
105 					<<"\nsound 1\n";
106 		for(int i=0;i<NUMBER_OF_SCORES;i++)
107 			tofile<<"\nNobody 0";
108 		tofile.close();
109 	}
110 }
111 
displayHighScores(bool fadingOn)112 void displayHighScores(bool fadingOn)
113 {
114   char *temp=new char[NAME_MAX_LENGTH+1];
115   char *result;
116   BFont_Info* font;
117   SDL_Surface* highback;
118   SDL_Surface* menuback;
119   SDL_Surface* transfer;
120   SDL_Surface* returnshot;
121   SDL_Surface* clearshot;
122   SDL_Event event;
123   SDL_Rect src,dest;
124   Uint32 colorkey,colorkey2,colorkey3,colorkey4;
125   string datadir,datadir2,datadir3;
126   int time1,time2,diff_time,pause_time; // Time loop
127   datadir=DATA_PREFIX;
128   datadir+="/highback.png";
129   highback=IMG_Load(datadir.c_str());
130   if(highback==NULL)
131   {
132     cerr<<"ERROR: unable to load highback.\n";
133     exit(1);
134   }
135   colorkey=SDL_MapRGB(highback->format,15,15,15);
136   SDL_SetColorKey(highback,SDL_SRCCOLORKEY,colorkey);
137   datadir=DATA_PREFIX;
138   datadir+="/returnshot.png";
139   returnshot=IMG_Load(datadir.c_str());
140   if(returnshot==NULL)
141   {
142     cerr<<"ERROR: unable to load returnshot.\n";
143     exit(1);
144   }
145   colorkey2=SDL_MapRGB(returnshot->format,0,0,0);
146   SDL_SetColorKey(returnshot,SDL_SRCCOLORKEY,colorkey2);
147   datadir=DATA_PREFIX;
148   datadir+="/clearshot.png";
149   clearshot=IMG_Load(datadir.c_str());
150   if(clearshot==NULL)
151   {
152     cerr<<"ERROR: unable to load clearshot.\n";
153     exit(1);
154   }
155   colorkey3=SDL_MapRGB(clearshot->format,79,229,79);
156   SDL_SetColorKey(clearshot,SDL_SRCCOLORKEY,colorkey3);
157   datadir=DATA_PREFIX;
158   datadir+="/blank.png";
159   transfer=IMG_Load(datadir.c_str());
160   if(transfer==NULL)
161   {
162     cerr<<"ERROR: unable to load transfer.\n";
163     exit(1);
164   }
165   colorkey4=SDL_MapRGB(transfer->format,112,112,112);
166   SDL_SetColorKey(transfer,SDL_SRCCOLORKEY,colorkey4);
167   datadir=DATA_PREFIX;
168   datadir+="/menushot.png";
169   menuback=IMG_Load(datadir.c_str());
170   if(menuback==NULL)
171   {
172     cerr<<"ERROR: unable to load menuback.\n";
173     exit(1);
174   }
175   src.x=src.y=0;
176   dest.x=dest.y=0;
177   src.w=dest.w=screen->w;
178   src.h=dest.h=screen->h;
179   SDL_BlitSurface(screen,&src,menuback,&dest);
180   datadir=DATA_PREFIX;
181   datadir+="/font.png";
182   font=BFont_LoadFont(datadir.c_str());
183   if(font==NULL)
184   {
185     cerr<<"ERROR: unable to load font.\n";
186     exit(1);
187   }
188   dest.x=dest.y=0;
189   src.w=dest.w=highback->w;
190   src.h=dest.h=highback->h;
191   SDL_BlitSurface(highback,&src,transfer,&dest);
192   for(unsigned int i=0;i<high_scores.size();i++)
193   {
194     for(unsigned int j=0;j<high_scores[i].name().length();j++)
195       temp[j]=high_scores[i].name().c_str()[j];
196     temp[high_scores[i].name().length()]='\0';
197     BFont_LeftPrintStringFont(transfer,font,44+i*LINE_SPACING,"  %s",temp);
198     BFont_RightPrintStringFont(transfer,font,44+i*LINE_SPACING,"%d  ",
199                                high_scores[i].score());
200   }
201   // Clear the buttons.
202   for(list<button*>::iterator i=buttons.begin();i!=buttons.end();i++)
203     (*i)->freeSurfaces();
204   buttons.clear();
205   // Set up the new buttons.
206   buttons.push_back(new button(SCREEN_WIDTH/2-139,420,
207 															 "return.png","return","return_hilight.png",
208 															 "return_pressed.png"));
209   buttons.push_back(new button(SCREEN_WIDTH/2-75,
210 															 SCREEN_HEIGHT/2-transfer->h/2+45+
211 															 high_scores.size()*LINE_SPACING+3,"clear.png",
212 															 "clear","clear_hilight.png",
213 															 "clear_pressed.png"));
214   // Fading transition.
215 	if(fadingOn)
216 	{
217 		for(int i=0;i<256;i+=TRANSITION_SPEED)
218 		{
219 			time1=clock()/CLOCKS_PER_SEC; //get time at start of loop
220 			SDL_SetAlpha(transfer,SDL_SRCALPHA,i);
221 			SDL_SetAlpha(returnshot,SDL_SRCALPHA,i);
222 			SDL_SetAlpha(clearshot,SDL_SRCALPHA,i);
223 			dest.x=dest.y=0;
224 			src.w=dest.w=menuback->w;
225 			src.h=dest.h=menuback->h;
226 			SDL_BlitSurface(menuback,&src,screen,&dest);
227 			boxRGBA(screen,0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1,0,0,0,i/2);
228 			src.w=dest.w=transfer->w;
229 			src.h=dest.h=transfer->h;
230 			dest.x=SCREEN_WIDTH/2-src.w/2;
231 			dest.y=SCREEN_HEIGHT/2-src.h/2;
232 			SDL_BlitSurface(transfer,&src,screen,&dest);
233 			src.w=dest.w=returnshot->w;
234 			src.h=dest.h=returnshot->h;
235 			dest.x=SCREEN_WIDTH/2-139;
236 			dest.y=420;
237 			SDL_BlitSurface(returnshot,&src,screen,&dest);
238 			src.w=dest.w=clearshot->w;
239 			src.h=dest.h=clearshot->h;
240 			dest.x=SCREEN_WIDTH/2-75;
241 			dest.y=SCREEN_HEIGHT/2-transfer->h/2+45+high_scores.size()*LINE_SPACING
242 				+3;
243 			SDL_BlitSurface(clearshot,&src,screen,&dest);
244 			SDL_UpdateRect(screen,0,0,screen->w,screen->h);
245 			time2=clock()/CLOCKS_PER_SEC; //get time at end of loop
246 			diff_time=time2-time1;    //compare...
247 			pause_time=25-diff_time;
248 			SDL_Delay(pause_time);    //and pause for required amount of time
249 			if((i+TRANSITION_SPEED>=256)&&(i!=255)) // This is the last run
250 				i=255-TRANSITION_SPEED;               // through the loop.
251 		}
252 	}
253   src.w=dest.w=transfer->w;
254   src.h=dest.h=transfer->h;
255   dest.x=0;
256   dest.y=0;
257   SDL_BlitSurface(transfer,&src,transfer,&dest);
258   for(;;)
259   {
260     time1=clock()/CLOCKS_PER_SEC; //get time at start of loop
261     dest.x=dest.y=0;
262     src.w=dest.w=menuback->w;
263     src.h=dest.h=menuback->h;
264     SDL_BlitSurface(menuback,&src,screen,&dest);
265     boxRGBA(screen,0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1,0,0,0,128);
266     src.w=dest.w=transfer->w;
267     src.h=dest.h=transfer->h;
268     dest.x=SCREEN_WIDTH/2-src.w/2;
269     dest.y=SCREEN_HEIGHT/2-src.h/2;
270     SDL_BlitSurface(transfer,&src,screen,&dest);
271     while(SDL_PollEvent(&event))
272       if(event.type==SDL_QUIT)
273         goto leave;
274     drawButtons();
275     SDL_UpdateRect(screen,0,0,screen->w,screen->h);
276     result=handleButtons();
277     if(strcmp(result,"return")==0)
278     {
279 			// Transition back
280 			if(fadingOn)
281 			{
282 				for(int i=0;i<256;i+=TRANSITION_SPEED)
283 				{
284 					time1=clock()/CLOCKS_PER_SEC; //get time at start of loop
285 					SDL_SetAlpha(transfer,SDL_SRCALPHA,255-i);
286 					SDL_SetAlpha(returnshot,SDL_SRCALPHA,255-i);
287 					SDL_SetAlpha(clearshot,SDL_SRCALPHA,255-i);
288 					dest.x=dest.y=0;
289 					src.w=dest.w=menuback->w;
290 					src.h=dest.h=menuback->h;
291 					SDL_BlitSurface(menuback,&src,screen,&dest);
292 					boxRGBA(screen,0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1,0,0,0,128-i/2);
293 					src.w=dest.w=transfer->w;
294 					src.h=dest.h=transfer->h;
295 					dest.x=SCREEN_WIDTH/2-src.w/2;
296 					dest.y=SCREEN_HEIGHT/2-src.h/2;
297 					SDL_BlitSurface(transfer,&src,screen,&dest);
298 					src.w=dest.w=returnshot->w;
299 					src.h=dest.h=returnshot->h;
300 					dest.x=SCREEN_WIDTH/2-139;
301 					dest.y=420;
302 					SDL_BlitSurface(returnshot,&src,screen,&dest);
303 					src.w=dest.w=clearshot->w;
304 					src.h=dest.h=clearshot->h;
305 					dest.x=SCREEN_WIDTH/2-75;
306 					dest.y=SCREEN_HEIGHT/2-transfer->h/2+45+high_scores.size()*
307 						LINE_SPACING+3;
308 					SDL_BlitSurface(clearshot,&src,screen,&dest);
309 					SDL_UpdateRect(screen,0,0,screen->w,screen->h);
310 					time2=clock()/CLOCKS_PER_SEC; //get time at end of loop
311 					diff_time=time2-time1;    //compare...
312 					pause_time=25-diff_time;
313 					SDL_Delay(pause_time);    //and pause for required amount of time
314 					if((i+TRANSITION_SPEED>=256)&&(i!=255)) // This is the last run
315 						i=255-TRANSITION_SPEED;               // through the loop.
316 				}
317 			}
318       break;
319     }
320     else if(strcmp(result,"clear")==0)
321     {
322       high_scores.clear();
323       for(int i=0;i<NUMBER_OF_SCORES;i++)
324         high_scores.push_back(highscore("Nobody",0));
325       dest.x=dest.y=0;
326       src.w=dest.w=highback->w;
327       src.h=dest.h=highback->h;
328       SDL_BlitSurface(highback,&src,transfer,&dest);
329       for(unsigned int i=0;i<high_scores.size();i++)
330       {
331         for(unsigned int j=0;j<high_scores[i].name().length();j++)
332           temp[j]=high_scores[i].name().c_str()[j];
333         temp[high_scores[i].name().length()]='\0';
334         BFont_LeftPrintStringFont(transfer,font,44+i*LINE_SPACING,"  %s",temp);
335         BFont_RightPrintStringFont(transfer,font,44+i*LINE_SPACING,"%d  ",
336                                    high_scores[i].score());
337       }
338       saveEverything();
339     }
340     else if(strcmp(result,"quit")==0)
341       goto leave;
342     time2=clock()/CLOCKS_PER_SEC; //get time at end of loop
343     diff_time=time2-time1;    //compare...
344     pause_time=30-diff_time;
345     SDL_Delay(pause_time);    //and pause for required amount of time
346   }
347 leave:
348   SDL_FreeSurface(returnshot);
349   SDL_FreeSurface(clearshot);
350   SDL_FreeSurface(highback);
351   SDL_FreeSurface(transfer);
352   SDL_FreeSurface(menuback);
353   BFont_FreeFont(font);
354   delete[] temp;
355 }
356 
typestr(SDL_Event event,string & str,int & cursor,BFont_Info * font)357 void typestr(SDL_Event event,string & str,int & cursor,BFont_Info* font)
358 {
359   int totalwidth=0;
360   if(cursor<0)
361   {
362     cerr<<"ERROR: cursor in typestr() was <0.\n";
363     exit(1);
364   }
365   else if((unsigned int)cursor>str.length())
366   {
367     cerr<<"ERROR: cursor in typestr() was >str.length().\n";
368     exit(1);
369   }
370   if(event.type!=SDL_KEYDOWN)
371     return;
372   if((event.key.keysym.sym==SDLK_BACKSPACE)&&(cursor!=0))
373   {
374     cursor--;
375     str.erase(cursor,1);
376   }
377   else if((event.key.keysym.sym==SDLK_LEFT)&&(cursor!=0))
378     cursor--;
379   else if((event.key.keysym.sym==SDLK_RIGHT)&&((unsigned)cursor!=str.length()))
380     cursor++;
381   else if((event.key.keysym.sym==SDLK_DELETE)&&
382           ((unsigned)cursor!=str.length()))
383     str.erase(cursor,1);
384   else if(event.key.keysym.sym==SDLK_HOME)
385     cursor=0;
386   else if(event.key.keysym.sym==SDLK_END)
387     cursor=str.length();
388   else if((event.key.keysym.sym==SDLK_RIGHT)&&((unsigned)cursor!=str.length()))
389     cursor++;
390   else if(event.key.keysym.sym==SDLK_a)
391   {
392     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
393           (event.key.keysym.mod & KMOD_RSHIFT))!=
394           !(event.key.keysym.mod & KMOD_CAPS))
395       if((unsigned)cursor==str.length())
396         str+="A";
397       else
398         str.insert(cursor,"A");
399     else
400       if((unsigned)cursor==str.length())
401         str+="a";
402       else
403         str.insert(cursor,"a");
404     cursor++;
405   }
406   else if(event.key.keysym.sym==SDLK_b)
407   {
408     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
409           (event.key.keysym.mod & KMOD_RSHIFT))!=
410           !(event.key.keysym.mod & KMOD_CAPS))
411       if((unsigned)cursor==str.length())
412         str+="B";
413       else
414         str.insert(cursor,"B");
415     else
416       if((unsigned)cursor==str.length())
417         str+="b";
418       else
419         str.insert(cursor,"b");
420     cursor++;
421   }
422   else if(event.key.keysym.sym==SDLK_c)
423   {
424     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
425           (event.key.keysym.mod & KMOD_RSHIFT))!=
426           !(event.key.keysym.mod & KMOD_CAPS))
427       if((unsigned)cursor==str.length())
428         str+="C";
429       else
430         str.insert(cursor,"C");
431     else
432       if((unsigned)cursor==str.length())
433         str+="c";
434       else
435         str.insert(cursor,"c");
436     cursor++;
437   }
438   else if(event.key.keysym.sym==SDLK_d)
439   {
440     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
441           (event.key.keysym.mod & KMOD_RSHIFT))!=
442           !(event.key.keysym.mod & KMOD_CAPS))
443       if((unsigned)cursor==str.length())
444         str+="D";
445       else
446         str.insert(cursor,"D");
447     else
448       if((unsigned)cursor==str.length())
449         str+="d";
450       else
451         str.insert(cursor,"d");
452     cursor++;
453   }
454   else if(event.key.keysym.sym==SDLK_e)
455   {
456     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
457           (event.key.keysym.mod & KMOD_RSHIFT))!=
458           !(event.key.keysym.mod & KMOD_CAPS))
459       if((unsigned)cursor==str.length())
460         str+="E";
461       else
462         str.insert(cursor,"E");
463     else
464       if((unsigned)cursor==str.length())
465         str+="e";
466       else
467         str.insert(cursor,"e");
468     cursor++;
469   }
470   else if(event.key.keysym.sym==SDLK_f)
471   {
472     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
473           (event.key.keysym.mod & KMOD_RSHIFT))!=
474           !(event.key.keysym.mod & KMOD_CAPS))
475       if((unsigned)cursor==str.length())
476         str+="F";
477       else
478         str.insert(cursor,"F");
479     else
480       if((unsigned)cursor==str.length())
481         str+="f";
482       else
483         str.insert(cursor,"f");
484     cursor++;
485   }
486   else if(event.key.keysym.sym==SDLK_g)
487   {
488     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
489           (event.key.keysym.mod & KMOD_RSHIFT))!=
490           !(event.key.keysym.mod & KMOD_CAPS))
491       if((unsigned)cursor==str.length())
492         str+="G";
493       else
494         str.insert(cursor,"G");
495     else
496       if((unsigned)cursor==str.length())
497         str+="g";
498       else
499         str.insert(cursor,"g");
500     cursor++;
501   }
502   else if(event.key.keysym.sym==SDLK_h)
503   {
504     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
505           (event.key.keysym.mod & KMOD_RSHIFT))!=
506           !(event.key.keysym.mod & KMOD_CAPS))
507       if((unsigned)cursor==str.length())
508         str+="H";
509       else
510         str.insert(cursor,"H");
511     else
512       if((unsigned)cursor==str.length())
513         str+="h";
514       else
515         str.insert(cursor,"h");
516     cursor++;
517   }
518   else if(event.key.keysym.sym==SDLK_i)
519   {
520     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
521           (event.key.keysym.mod & KMOD_RSHIFT))!=
522           !(event.key.keysym.mod & KMOD_CAPS))
523       if((unsigned)cursor==str.length())
524         str+="I";
525       else
526         str.insert(cursor,"I");
527     else
528       if((unsigned)cursor==str.length())
529         str+="i";
530       else
531         str.insert(cursor,"i");
532     cursor++;
533   }
534   else if(event.key.keysym.sym==SDLK_j)
535   {
536     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
537           (event.key.keysym.mod & KMOD_RSHIFT))!=
538           !(event.key.keysym.mod & KMOD_CAPS))
539       if((unsigned)cursor==str.length())
540         str+="J";
541       else
542         str.insert(cursor,"J");
543     else
544       if((unsigned)cursor==str.length())
545         str+="j";
546       else
547         str.insert(cursor,"j");
548     cursor++;
549   }
550   else if(event.key.keysym.sym==SDLK_k)
551   {
552     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
553           (event.key.keysym.mod & KMOD_RSHIFT))!=
554           !(event.key.keysym.mod & KMOD_CAPS))
555       if((unsigned)cursor==str.length())
556         str+="K";
557       else
558         str.insert(cursor,"K");
559     else
560       if((unsigned)cursor==str.length())
561         str+="k";
562       else
563         str.insert(cursor,"k");
564     cursor++;
565   }
566   else if(event.key.keysym.sym==SDLK_l)
567   {
568     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
569           (event.key.keysym.mod & KMOD_RSHIFT))!=
570           !(event.key.keysym.mod & KMOD_CAPS))
571       if((unsigned)cursor==str.length())
572         str+="L";
573       else
574         str.insert(cursor,"L");
575     else
576       if((unsigned)cursor==str.length())
577         str+="l";
578       else
579         str.insert(cursor,"l");
580     cursor++;
581   }
582   else if(event.key.keysym.sym==SDLK_m)
583   {
584     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
585           (event.key.keysym.mod & KMOD_RSHIFT))!=
586           !(event.key.keysym.mod & KMOD_CAPS))
587       if((unsigned)cursor==str.length())
588         str+="M";
589       else
590         str.insert(cursor,"M");
591     else
592       if((unsigned)cursor==str.length())
593         str+="m";
594       else
595         str.insert(cursor,"m");
596     cursor++;
597   }
598   else if(event.key.keysym.sym==SDLK_n)
599   {
600     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
601           (event.key.keysym.mod & KMOD_RSHIFT))!=
602           !(event.key.keysym.mod & KMOD_CAPS))
603       if((unsigned)cursor==str.length())
604         str+="N";
605       else
606         str.insert(cursor,"N");
607     else
608       if((unsigned)cursor==str.length())
609         str+="n";
610       else
611         str.insert(cursor,"n");
612     cursor++;
613   }
614   else if(event.key.keysym.sym==SDLK_o)
615   {
616     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
617           (event.key.keysym.mod & KMOD_RSHIFT))!=
618           !(event.key.keysym.mod & KMOD_CAPS))
619       if((unsigned)cursor==str.length())
620         str+="O";
621       else
622         str.insert(cursor,"O");
623     else
624       if((unsigned)cursor==str.length())
625         str+="o";
626       else
627         str.insert(cursor,"o");
628     cursor++;
629   }
630   else if(event.key.keysym.sym==SDLK_p)
631   {
632     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
633           (event.key.keysym.mod & KMOD_RSHIFT))!=
634           !(event.key.keysym.mod & KMOD_CAPS))
635       if((unsigned)cursor==str.length())
636         str+="P";
637       else
638         str.insert(cursor,"P");
639     else
640       if((unsigned)cursor==str.length())
641         str+="p";
642       else
643         str.insert(cursor,"p");
644     cursor++;
645   }
646   else if(event.key.keysym.sym==SDLK_q)
647   {
648     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
649           (event.key.keysym.mod & KMOD_RSHIFT))!=
650           !(event.key.keysym.mod & KMOD_CAPS))
651       if((unsigned)cursor==str.length())
652         str+="Q";
653       else
654         str.insert(cursor,"Q");
655     else
656       if((unsigned)cursor==str.length())
657         str+="q";
658       else
659         str.insert(cursor,"q");
660     cursor++;
661   }
662   else if(event.key.keysym.sym==SDLK_r)
663   {
664     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
665           (event.key.keysym.mod & KMOD_RSHIFT))!=
666           !(event.key.keysym.mod & KMOD_CAPS))
667       if((unsigned)cursor==str.length())
668         str+="R";
669       else
670         str.insert(cursor,"R");
671     else
672       if((unsigned)cursor==str.length())
673         str+="r";
674       else
675         str.insert(cursor,"r");
676     cursor++;
677   }
678   else if(event.key.keysym.sym==SDLK_s)
679   {
680     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
681           (event.key.keysym.mod & KMOD_RSHIFT))!=
682           !(event.key.keysym.mod & KMOD_CAPS))
683       if((unsigned)cursor==str.length())
684         str+="S";
685       else
686         str.insert(cursor,"S");
687     else
688       if((unsigned)cursor==str.length())
689         str+="s";
690       else
691         str.insert(cursor,"s");
692     cursor++;
693   }
694   else if(event.key.keysym.sym==SDLK_t)
695   {
696     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
697           (event.key.keysym.mod & KMOD_RSHIFT))!=
698           !(event.key.keysym.mod & KMOD_CAPS))
699       if((unsigned)cursor==str.length())
700         str+="T";
701       else
702         str.insert(cursor,"T");
703     else
704       if((unsigned)cursor==str.length())
705         str+="t";
706       else
707         str.insert(cursor,"t");
708     cursor++;
709   }
710   else if(event.key.keysym.sym==SDLK_u)
711   {
712     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
713           (event.key.keysym.mod & KMOD_RSHIFT))!=
714           !(event.key.keysym.mod & KMOD_CAPS))
715       if((unsigned)cursor==str.length())
716         str+="U";
717       else
718         str.insert(cursor,"U");
719     else
720       if((unsigned)cursor==str.length())
721         str+="u";
722       else
723         str.insert(cursor,"u");
724     cursor++;
725   }
726   else if(event.key.keysym.sym==SDLK_v)
727   {
728     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
729           (event.key.keysym.mod & KMOD_RSHIFT))!=
730           !(event.key.keysym.mod & KMOD_CAPS))
731       if((unsigned)cursor==str.length())
732         str+="V";
733       else
734         str.insert(cursor,"V");
735     else
736       if((unsigned)cursor==str.length())
737         str+="v";
738       else
739         str.insert(cursor,"v");
740     cursor++;
741   }
742   else if(event.key.keysym.sym==SDLK_w)
743   {
744     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
745           (event.key.keysym.mod & KMOD_RSHIFT))!=
746           !(event.key.keysym.mod & KMOD_CAPS))
747       if((unsigned)cursor==str.length())
748         str+="W";
749       else
750         str.insert(cursor,"W");
751     else
752       if((unsigned)cursor==str.length())
753         str+="w";
754       else
755         str.insert(cursor,"w");
756     cursor++;
757   }
758   else if(event.key.keysym.sym==SDLK_x)
759   {
760     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
761           (event.key.keysym.mod & KMOD_RSHIFT))!=
762           !(event.key.keysym.mod & KMOD_CAPS))
763       if((unsigned)cursor==str.length())
764         str+="X";
765       else
766         str.insert(cursor,"X");
767     else
768       if((unsigned)cursor==str.length())
769         str+="x";
770       else
771         str.insert(cursor,"x");
772     cursor++;
773   }
774   else if(event.key.keysym.sym==SDLK_y)
775   {
776     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
777           (event.key.keysym.mod & KMOD_RSHIFT))!=
778           !(event.key.keysym.mod & KMOD_CAPS))
779       if((unsigned)cursor==str.length())
780         str+="Y";
781       else
782         str.insert(cursor,"Y");
783     else
784       if((unsigned)cursor==str.length())
785         str+="y";
786       else
787         str.insert(cursor,"y");
788     cursor++;
789   }
790   else if(event.key.keysym.sym==SDLK_z)
791   {
792     if(!((event.key.keysym.mod & KMOD_LSHIFT)||
793           (event.key.keysym.mod & KMOD_RSHIFT))!=
794           !(event.key.keysym.mod & KMOD_CAPS))
795       if((unsigned)cursor==str.length())
796         str+="Z";
797       else
798         str.insert(cursor,"Z");
799     else
800       if((unsigned)cursor==str.length())
801         str+="z";
802       else
803         str.insert(cursor,"z");
804     cursor++;
805   }
806   else if(event.key.keysym.sym==SDLK_0)
807   {
808     if((event.key.keysym.mod & KMOD_LSHIFT)||
809         (event.key.keysym.mod & KMOD_RSHIFT))
810       if((unsigned)cursor==str.length())
811         str+=")";
812       else
813         str.insert(cursor,")");
814     else
815       if((unsigned)cursor==str.length())
816         str+="0";
817       else
818         str.insert(cursor,"0");
819     cursor++;
820   }
821   else if(event.key.keysym.sym==SDLK_1)
822   {
823     if((event.key.keysym.mod & KMOD_LSHIFT)||
824         (event.key.keysym.mod & KMOD_RSHIFT))
825       if((unsigned)cursor==str.length())
826         str+="!";
827       else
828         str.insert(cursor,"!");
829     else
830       if((unsigned)cursor==str.length())
831         str+="1";
832       else
833         str.insert(cursor,"1");
834     cursor++;
835   }
836   else if(event.key.keysym.sym==SDLK_2)
837   {
838     if((event.key.keysym.mod & KMOD_LSHIFT)||
839         (event.key.keysym.mod & KMOD_RSHIFT))
840       if((unsigned)cursor==str.length())
841         str+="@";
842       else
843         str.insert(cursor,"@");
844     else
845       if((unsigned)cursor==str.length())
846         str+="2";
847       else
848         str.insert(cursor,"2");
849     cursor++;
850   }
851   else if(event.key.keysym.sym==SDLK_3)
852   {
853     if((event.key.keysym.mod & KMOD_LSHIFT)||
854         (event.key.keysym.mod & KMOD_RSHIFT))
855       if((unsigned)cursor==str.length())
856         str+="#";
857       else
858         str.insert(cursor,"#");
859     else
860       if((unsigned)cursor==str.length())
861         str+="3";
862       else
863         str.insert(cursor,"3");
864     cursor++;
865   }
866   else if(event.key.keysym.sym==SDLK_4)
867   {
868     if((event.key.keysym.mod & KMOD_LSHIFT)||
869         (event.key.keysym.mod & KMOD_RSHIFT))
870       if((unsigned)cursor==str.length())
871         str+="$";
872       else
873         str.insert(cursor,"$");
874     else
875       if((unsigned)cursor==str.length())
876         str+="4";
877       else
878         str.insert(cursor,"4");
879     cursor++;
880   }
881   else if(event.key.keysym.sym==SDLK_5)
882   {
883     if((event.key.keysym.mod & KMOD_LSHIFT)||
884         (event.key.keysym.mod & KMOD_RSHIFT))
885       if((unsigned)cursor==str.length())
886         str+="%";
887       else
888         str.insert(cursor,"%");
889     else
890       if((unsigned)cursor==str.length())
891         str+="5";
892       else
893         str.insert(cursor,"5");
894     cursor++;
895   }
896   else if(event.key.keysym.sym==SDLK_6)
897   {
898     if((event.key.keysym.mod & KMOD_LSHIFT)||
899         (event.key.keysym.mod & KMOD_RSHIFT))
900       if((unsigned)cursor==str.length())
901         str+="^";
902       else
903         str.insert(cursor,"^");
904     else
905       if((unsigned)cursor==str.length())
906         str+="6";
907       else
908         str.insert(cursor,"6");
909     cursor++;
910   }
911   else if(event.key.keysym.sym==SDLK_7)
912   {
913     if((event.key.keysym.mod & KMOD_LSHIFT)||
914         (event.key.keysym.mod & KMOD_RSHIFT))
915       if((unsigned)cursor==str.length())
916         str+="&";
917       else
918         str.insert(cursor,"&");
919     else
920       if((unsigned)cursor==str.length())
921         str+="7";
922       else
923         str.insert(cursor,"7");
924     cursor++;
925   }
926   else if(event.key.keysym.sym==SDLK_8)
927   {
928     if((event.key.keysym.mod & KMOD_LSHIFT)||
929         (event.key.keysym.mod & KMOD_RSHIFT))
930       if((unsigned)cursor==str.length())
931         str+="*";
932       else
933         str.insert(cursor,"*");
934     else
935       if((unsigned)cursor==str.length())
936         str+="8";
937       else
938         str.insert(cursor,"8");
939     cursor++;
940   }
941   else if(event.key.keysym.sym==SDLK_9)
942   {
943     if((event.key.keysym.mod & KMOD_LSHIFT)||
944         (event.key.keysym.mod & KMOD_RSHIFT))
945       if((unsigned)cursor==str.length())
946         str+="(";
947       else
948         str.insert(cursor,"(");
949     else
950       if((unsigned)cursor==str.length())
951         str+="9";
952       else
953         str.insert(cursor,"9");
954     cursor++;
955   }
956   else if(event.key.keysym.sym==SDLK_QUOTE)
957   {
958     if((event.key.keysym.mod & KMOD_LSHIFT)||
959         (event.key.keysym.mod & KMOD_RSHIFT))
960       if((unsigned)cursor==str.length())
961         str+="\"";
962       else
963         str.insert(cursor,"\"");
964     else
965       if((unsigned)cursor==str.length())
966         str+="'";
967       else
968         str.insert(cursor,"'");
969     cursor++;
970   }
971   else if(event.key.keysym.sym==SDLK_SEMICOLON)
972   {
973     if((event.key.keysym.mod & KMOD_LSHIFT)||
974         (event.key.keysym.mod & KMOD_RSHIFT))
975       if((unsigned)cursor==str.length())
976         str+=":";
977       else
978         str.insert(cursor,":");
979     else
980       if((unsigned)cursor==str.length())
981         str+=";";
982       else
983         str.insert(cursor,";");
984     cursor++;
985   }
986   else if(event.key.keysym.sym==SDLK_LEFTBRACKET)
987   {
988     if((event.key.keysym.mod & KMOD_LSHIFT)||
989         (event.key.keysym.mod & KMOD_RSHIFT))
990       if((unsigned)cursor==str.length())
991         str+="{";
992       else
993         str.insert(cursor,"{");
994     else
995       if((unsigned)cursor==str.length())
996         str+="[";
997       else
998         str.insert(cursor,"[");
999     cursor++;
1000   }
1001   else if(event.key.keysym.sym==SDLK_RIGHTBRACKET)
1002   {
1003     if((event.key.keysym.mod & KMOD_LSHIFT)||
1004         (event.key.keysym.mod & KMOD_RSHIFT))
1005       if((unsigned)cursor==str.length())
1006         str+="}";
1007       else
1008         str.insert(cursor,"}");
1009     else
1010       if((unsigned)cursor==str.length())
1011         str+="]";
1012       else
1013         str.insert(cursor,"]");
1014     cursor++;
1015   }
1016   else if(event.key.keysym.sym==SDLK_MINUS)
1017   {
1018     if((event.key.keysym.mod & KMOD_LSHIFT)||
1019         (event.key.keysym.mod & KMOD_RSHIFT))
1020       if((unsigned)cursor==str.length())
1021         str+="_";
1022       else
1023         str.insert(cursor,"_");
1024     else
1025       if((unsigned)cursor==str.length())
1026         str+="-";
1027       else
1028         str.insert(cursor,"-");
1029     cursor++;
1030   }
1031   else if(event.key.keysym.sym==SDLK_SLASH)
1032   {
1033     if((event.key.keysym.mod & KMOD_LSHIFT)||
1034         (event.key.keysym.mod & KMOD_RSHIFT))
1035       if((unsigned)cursor==str.length())
1036         str+="?";
1037       else
1038         str.insert(cursor,"?");
1039     else
1040       if((unsigned)cursor==str.length())
1041         str+="/";
1042       else
1043         str.insert(cursor,"/");
1044     cursor++;
1045   }
1046   else if(event.key.keysym.sym==SDLK_EQUALS)
1047   {
1048     if((event.key.keysym.mod & KMOD_LSHIFT)||
1049         (event.key.keysym.mod & KMOD_RSHIFT))
1050       if((unsigned)cursor==str.length())
1051         str+="+";
1052       else
1053         str.insert(cursor,"+");
1054     else
1055       if((unsigned)cursor==str.length())
1056         str+="=";
1057       else
1058         str.insert(cursor,"=");
1059     cursor++;
1060   }
1061   else if(event.key.keysym.sym==SDLK_COMMA)
1062   {
1063     if((event.key.keysym.mod & KMOD_LSHIFT)||
1064         (event.key.keysym.mod & KMOD_RSHIFT))
1065       if((unsigned)cursor==str.length())
1066         str+="<";
1067       else
1068         str.insert(cursor,"<");
1069     else
1070       if((unsigned)cursor==str.length())
1071         str+=",";
1072       else
1073         str.insert(cursor,",");
1074     cursor++;
1075   }
1076   else if(event.key.keysym.sym==SDLK_PERIOD)
1077   {
1078     if((event.key.keysym.mod & KMOD_LSHIFT)||
1079         (event.key.keysym.mod & KMOD_RSHIFT))
1080       if((unsigned)cursor==str.length())
1081         str+=">";
1082       else
1083         str.insert(cursor,">");
1084     else
1085       if((unsigned)cursor==str.length())
1086         str+=".";
1087       else
1088         str.insert(cursor,".");
1089     cursor++;
1090   }
1091   else if(event.key.keysym.sym==SDLK_BACKSLASH)
1092   {
1093     if((event.key.keysym.mod & KMOD_LSHIFT)||
1094         (event.key.keysym.mod & KMOD_RSHIFT))
1095       if((unsigned)cursor==str.length())
1096         str+="|";
1097       else
1098         str.insert(cursor,"|");
1099     else
1100       if((unsigned)cursor==str.length())
1101         str+="\\";
1102       else
1103         str.insert(cursor,"\\");
1104     cursor++;
1105   }
1106   else if(event.key.keysym.sym==SDLK_BACKQUOTE)
1107   {
1108     if((event.key.keysym.mod & KMOD_LSHIFT)||
1109         (event.key.keysym.mod & KMOD_RSHIFT))
1110       if((unsigned)cursor==str.length())
1111         str+="~";
1112       else
1113         str.insert(cursor,"~");
1114     else
1115       if((unsigned)cursor==str.length())
1116         str+="`";
1117       else
1118         str.insert(cursor,"`");
1119     cursor++;
1120   }
1121  else if(event.key.keysym.sym==SDLK_SPACE)
1122   {
1123     if((unsigned)cursor==str.length())
1124       str+=" ";
1125     else
1126       str.insert(cursor," ");
1127     cursor++;
1128   }
1129   for(int i=0;i<cursor;i++)
1130     totalwidth+=BFont_CharWidth(font,str[i]);
1131   if(totalwidth>MAX_STR_WIDTH)
1132   {
1133     cursor--;
1134     str.erase(cursor,1);
1135   }
1136 }
1137