1 /*
2 * IceBreaker
3 * Copyright (c) 2000-2002 Matthew Miller <mattdm@mattdm.org>
4 *
5 * <http://www.mattdm.org/icebreaker/>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22 
23 #include <SDL.h>
24 #include "icebreaker.h"
25 #include "globals.h"
26 #include "line.h"
27 #include "penguin.h"
28 #include "level.h"
29 #include "text.h"
30 #include "cursor.h"
31 #include "laundry.h"
32 #include "hiscore.h"
33 #include "dialog.h"
34 #include "themes.h"
35 #include "event.h"
36 
37 // FIX -- it'd be better to draw text once and then scroll that graphic,
38 // of course.
39 
40 static int scrolltext(char * firsttext, SDL_Rect* firstrect, Uint32 firstcolor, char * secondtext, SDL_Rect* secondrect, Uint32 secondcolor, int doneonfinish);
41 
intermission(ScoreSheet * levelscore,int nextlevel)42 int intermission(ScoreSheet * levelscore, int nextlevel)
43 {
44 	int quit=false;
45 	SDL_Event event;
46 	SDL_Rect scorerect, bonusrect;
47 	char scoretext[30];
48 	char bonustext[30];
49 
50 	snprintf(scoretext,30,"SCORE: %d",levelscore->basescore);
51 	snprintf(bonustext,30, "BONUS: %d",levelscore->clearbonus + levelscore->lifebonus);
52 
53 	// FIX -- play some truimphant but not annoying sound
54 
55 	// clear any pending events
56 	SDL_Delay(10); // needed? probably not.
57 	while (pollevent(&event)) if (event.type == SDL_QUIT) { quit=true; }
58 
59 	setcursor(CURSORCLICK);
60 
61 	scorerect.h=CHARHEIGHT*4;
62 	scorerect.w=gettextwidth(4,scoretext);
63 
64 	bonusrect.h=CHARHEIGHT*4;
65 	bonusrect.w=gettextwidth(4,bonustext);
66 
67 	if (scorerect.w>bonusrect.w)
68 		scorerect.x=(screen->w - scorerect.w) / 2;
69 	else
70 		scorerect.x=(screen->w - bonusrect.w) / 2;
71 	scorerect.y=HEIGHT-CHARHEIGHT*5; // extra space for separation
72 
73 	bonusrect.x=scorerect.x;
74 	bonusrect.y=HEIGHT-CHARHEIGHT*4;
75 
76 	// wait for click, scroll score
77 	quit=scrolltext(scoretext, &scorerect, color.scorescrolltext, bonustext, &bonusrect, color.bonusscrolltext, false);
78 
79 	// and clear any more events, for good luck.
80 	while (pollevent(&event)) if (event.type == SDL_QUIT) quit=true;
81 
82 	//printf("Level %d completed. ",level);
83 	return(quit);
84 }
85 
gameover(long finalscore)86 int gameover(long finalscore)
87 {
88 	int done=false; int quit=false;
89 	SDL_Event event;
90 	SDL_Rect loserrect, finalrect;
91 
92 	char finaltext[30];
93 
94 	snprintf(finaltext,30,"FINAL SCORE: %ld",finalscore);
95 
96 
97 	loserrect.h=CHARHEIGHT*4;
98 	loserrect.w=gettextwidth(4,"GAME OVER");
99 	loserrect.x=(screen->w - loserrect.w) / 2;
100 	loserrect.y=HEIGHT-CHARHEIGHT*4;
101 
102 	finalrect.h=CHARHEIGHT*4;
103 	finalrect.w=gettextwidth(4,finaltext);
104 	finalrect.x=(screen->w - finalrect.w) / 2;
105 	finalrect.y=HEIGHT-CHARHEIGHT*4;
106 
107 	// clear any pending events
108 	SDL_Delay(10); // needed? probably not.
109 	while (pollevent(&event)) if (event.type == SDL_QUIT) { done=true; quit=true; }
110 
111 	if (!checkhiscore(finalscore))
112 		setcursor(CURSORCLICK);
113 	else
114 		setcursor(CURSORARROW);
115 
116 
117 	// wait for click, scroll score
118 	quit=scrolltext("GAME OVER", &loserrect, color.gameovertext, finaltext, &finalrect,  color.scorescrolltext, false);
119 
120 	if (checkhiscore(finalscore))
121 	{
122 		// FIX -- play some truimphant but not annoying sound
123 
124 		if (gethighusername(finalscore>hiscoreval[0])==POPUPQUITGAME)
125 			quit=true;
126 
127 		if(!addhiscore(username,finalscore,true))
128 		{ // I don't think we need to say this; people can figure it out by looking at the numbers
129 		  // but don't comment out the addhighscore function call above, of course. :)
130 			// fprintf(stderr,"Ouch: looks like someone beat your score while you were typing your name!\n");
131 		}
132 
133 		// jump text to top
134 		loserrect.y=(HEIGHT/2-31);
135 		puttext(loserrect.x,loserrect.y,4,color.gameovertext,"GAME OVER");
136 		soil(loserrect);
137 		finalrect.y=(HEIGHT/2);
138 		puttext(finalrect.x,finalrect.y,4,color.scorescrolltext,finaltext);
139 		soil(loserrect);
140 		clean();
141 	}
142 
143 
144 	// and clear any more events, for good luck.
145 	while (pollevent(&event)) if (event.type == SDL_QUIT) quit=true;
146 
147 	//printf("Game over.\n");
148 	return(quit);
149 }
150 
151 
scrolltext(char * firsttext,SDL_Rect * firstrect,Uint32 firstcolor,char * secondtext,SDL_Rect * secondrect,Uint32 secondcolor,int doneonfinish)152 int scrolltext(char * firsttext, SDL_Rect* firstrect, Uint32 firstcolor, char * secondtext, SDL_Rect* secondrect, Uint32 secondcolor, int doneonfinish)
153 {
154 	int quit=false;
155 	int done=false;
156 	SDL_Event event;
157 
158 	SDL_Surface * bgsave=SDL_CreateRGBSurface(SDL_SWSURFACE,WIDTH,HEIGHT,screen->format->BitsPerPixel,0,0,0,0);
159 
160 	// get background
161 	SDL_BlitSurface(screen, NULL, bgsave, NULL);
162 
163 
164 	// wait for click, scroll score (or whatever)
165 	while (!done)
166 	{
167 		while(pollevent(&event))
168 		;
169 		{
170 			if (event.type == SDL_QUIT)
171 			{
172 				done=true; quit=true;
173 			}
174 			else if (event.type==SDL_MOUSEBUTTONDOWN)
175 			{
176 				if (event.button.button==1)
177 				{
178 						done=true;
179 				}
180 			}
181 			else if (event.type == SDL_KEYUP)
182 			{
183 				switch(translatekeyevent(&event))
184 				{
185 					case KEYCANCEL:     // falls through
186 					case KEYMENU:       // falls through
187 					case KEYSWITCHLINE: // falls through
188 					case KEYSTARTLINE:
189 						done=true;
190 					default:
191 					break;
192 				}
193 			}
194 		}
195 
196 		if (firstrect->y>(HEIGHT/2-30))
197 		{
198 			firstrect->y--;
199 			puttext(firstrect->x,firstrect->y,4,firstcolor,firsttext);
200 			soil(*firstrect);
201 			clean();
202 			SDL_BlitSurface(bgsave,firstrect, screen, firstrect);
203 		}
204 		else if (firstrect->y==(HEIGHT/2-30))
205 		{
206 			// un-clean. :)
207 			firstrect->y--;
208 			puttext(firstrect->x,firstrect->y,4,firstcolor,firsttext);
209 			soil(*firstrect);
210 			clean();
211 		}
212 		else if (secondrect->y>(HEIGHT/2))
213 		{
214 			secondrect->y--;
215 			puttext(secondrect->x,secondrect->y,4,secondcolor,secondtext);
216 			soil(*secondrect);
217 			clean();
218 			SDL_BlitSurface(bgsave,secondrect, screen, secondrect);
219 		}
220 		else if (secondrect->y==(HEIGHT/2))
221 		{
222 			// un-clean. :)
223 			secondrect->y--;
224 			puttext(secondrect->x,secondrect->y,4,secondcolor,secondtext);
225 			soil(*secondrect);
226 			clean();
227 		}
228 		else
229 		{
230 			if (doneonfinish) done=true;
231 			clean();
232 		}
233 
234 		SDL_Delay(10);
235 	}
236 	SDL_FreeSurface(bgsave);
237 	return(quit);
238 }
239