1 #include "stdafx.h"
2 
3 int i;
4 
5 
cScene()6 cScene::cScene()
7 {
8 	this->parts = NULL;
9 
10 	BGCOLOR = 0;
11 
12 	SCREEN_RECT.w = WIDTH;
13 	SCREEN_RECT.h = HEIGHT;
14 	SCREEN_RECT.x = 0;
15 	SCREEN_RECT.y = 0;
16 
17 	TEXTUREcount = 1;
18 	Enemy_Textures[GOOMBA]     = LoadIMG("data/gfx/characters/goomba1.png");
19 	Enemy_Textures[TURTLE]     = LoadIMG("data/gfx/characters/turtle/turtler1.png");
20 	Enemy_Textures[SPINY]      = LoadIMG("data/gfx/characters/spiny1.png");
21 	Enemy_Textures[FIRECIRCLE1]= LoadIMG("data/gfx/editor/firecircle1");
22 	Enemy_Textures[FIRECIRCLE2]= LoadIMG("data/gfx/editor/firecircle2");
23 	Enemy_Textures[FIRECIRCLE3]= LoadIMG("data/gfx/editor/firecircle3");
24 	Enemy_Textures[FIRE]       = LoadIMG("data/gfx/editor/fire");
25 	Enemy_Textures[CANNON]     = LoadIMG("data/gfx/characters/cannon.PNG");
26 	Enemy_Textures[BLUME]      = LoadIMG("data/gfx/characters/pirnaha1.PNG");
27 	Enemy_Textures[BOWSER]     = LoadIMG("data/gfx/editor/bowser");
28 	Enemy_Textures[TURTLEFLY]  = LoadIMG("data/gfx/characters/turtle/flyl1.png");
29 	Enemy_Textures[BRETT]     = LoadIMG("data/gfx/editor/brett");
30 
31 	Enemy_Textures[0]= LoadIMG("data/gfx/characters/big/player1r.png");
32 	Enemy_Textures[19]= LoadIMG("data/gfx/editor/exit");
33 
34 	CASTLE.big_s = LoadIMG("data/gfx/tiles/bigcastle.PNG");
35 	CASTLE.small_s = LoadIMG("data/gfx/tiles/smallcastle.PNG");
36 
37 	COINcount = 10;
38 	for(i=0;i<MAX_X;i++)
39 		for(int n=0;n<MAX_Y;n++)
40 		{
41 			TILE[i][n] = 0;
42 			BONUS[i][n] = 0;
43 			ENEMY[i][n] = 0;
44 			COIN[i][n] = 0;
45 		}
46 
47 	for(i=0;i<MAX_TEXTURES;i++)
48 		Textures[i] = 0;
49 
50 	Textures[MAX_TEXTURES-1] = LoadIMG("data/gfx/editor/mover");
51 	Textures[MAX_TEXTURES-2] = LoadIMG("data/gfx/background/bush1.png");
52 	Textures[MAX_TEXTURES-3] = LoadIMG("data/gfx/background/bush2.png");
53 	Textures[MAX_TEXTURES-4] = LoadIMG("data/gfx/background/bush3.png");
54 	Textures[MAX_TEXTURES-5] = LoadIMG("data/gfx/background/bush4.png");
55 	Textures[MAX_TEXTURES-6] = LoadIMG("data/gfx/background/bush5.png");
56 	Textures[MAX_TEXTURES-7] = LoadIMG("data/gfx/background/bush6.png");
57 	Textures[MAX_TEXTURES-8] = LoadIMG("data/gfx/background/cloud1.png");
58 	Textures[MAX_TEXTURES-9] = LoadIMG("data/gfx/background/cloud2.png");
59 }
60 
loadLevel(char * scenename)61 void cScene::loadLevel(char* scenename)
62 {
63 	CASTLE.available = 0;
64 	CASTLE.x = 0;
65 	CASTLE.y = 0;
66 	CASTLE.big = 0;
67 	CASTLE.level = new char[2];
68 	CASTLE.startpoint = 0;
69 
70 	m_TextureCtrl.ResetContent();
71 	tileread = 0;
72 	bonusread = 0;
73 	TEXTUREcount = 1;
74 	parts = 0;
75 	MUSIC = 0;
76 	BACK = 0;
77 
78 	for(i=0;i<5;i++)
79 	{
80 		STARTPOINTS[i].x=0;
81 		STARTPOINTS[i].y =0;
82 		STARTPOINTS[i].type =0;
83 
84 		EXITPOINTS[i].x =0;
85 		EXITPOINTS[i].y =0;
86 		EXITPOINTS[i].level = new char[5];
87 		strcpy(EXITPOINTS[i].level,"NONE");
88 		EXITPOINTS[i].startpoint =0;
89 	}
90 
91 	ifstream ifs(scenename, ios::in);
92 
93 	char *contents = new char[670];
94 
95 	for (; ifs.getline(contents, 670);)
96 		if ( contents[0] != '#' && strlen(contents) > 2)
97 		{
98 			getParts(contents);
99 			interpreteSceneLine();
100 
101 		}
102 	ifs.close();
103 	delete[](contents);
104 }
105 
interpreteSceneLine()106 void cScene::interpreteSceneLine()
107 {
108 	if (!strcmp (parts[0], "TEXTURE"))
109 	{
110 		Textures[TEXTUREcount] = LoadIMG(parts[1]);
111 		m_TextureCtrl.AddString(parts[1]);
112 		m_TextureCtrl.SetCurSel(TEXTUREcount-1);
113 		TEXTUREcount++;
114 	}
115 	else if (!strcmp (parts[0], "::TILES->"))
116 	{
117 		if (!strcmp (parts[1], "BEGIN"))
118 			tileread = 1;
119 		else
120 			tileread = 0;
121 	}
122 	else if (!strcmp (parts[0], "::BONUS->"))
123 	{
124 		if (!strcmp (parts[1], "BEGIN"))
125 			bonusread = 1;
126 		else
127 			bonusread = 0;
128 	}
129 	else if (!strcmp (parts[0], "::COIN->"))
130 	{
131 		if (!strcmp (parts[1], "BEGIN"))
132 			coinread = 1;
133 		else
134 			coinread = 0;
135 	}
136 	else if (tileread)
137 	{
138 		TILE[atoi(parts[0])][atoi(parts[1])] = atoi(parts[2]);
139 	}
140 	else if (bonusread)
141 	{
142 		BONUS[atoi(parts[0])][atoi(parts[1])] = atoi(parts[2]);
143 	}
144 	else if (coinread)
145 	{
146 		COIN[atoi(parts[0])][atoi(parts[1])] = 1;
147 	}
148 	else if (!strcmp (parts[0], "BGCOLOR"))
149 		BGCOLOR = atoi(parts[1]);
150 	else if (!strcmp (parts[0], "EXIT"))
151 	{
152 		EXITPOINTS[atoi(parts[1])].x = atoi(parts[2]);
153 		EXITPOINTS[atoi(parts[1])].y = atoi(parts[3]);
154 		EXITPOINTS[atoi(parts[1])].startpoint = atoi(parts[5]);
155 		EXITPOINTS[atoi(parts[1])].type = atoi(parts[6]);
156 		EXITPOINTS[atoi(parts[1])].level = new char[100];
157 		strcpy(EXITPOINTS[atoi(parts[1])].level,parts[4]);
158 	}
159 	else if (!strcmp (parts[0], "START"))
160 	{
161 		STARTPOINTS[atoi(parts[1])].x = atoi(parts[2]);
162 		STARTPOINTS[atoi(parts[1])].y = atoi(parts[3]);
163 		STARTPOINTS[atoi(parts[1])].type = atoi(parts[4]);
164 	}
165 	else if (!strcmp (parts[0], "GOOMBA"))
166 	{
167 		ENEMY[atoi(parts[1])][atoi(parts[2])] = GOOMBA;
168 	}
169 	else if (!strcmp (parts[0], "TURTLE"))
170 	{
171 		ENEMY[atoi(parts[1])][atoi(parts[2])] = TURTLE;
172 	}
173 	else if (!strcmp (parts[0], "TURTLEFLY"))
174 	{
175 		ENEMY[atoi(parts[1])][atoi(parts[2])] = TURTLEFLY;
176 	}
177 	else if (!strcmp (parts[0], "SPINY"))
178 	{
179 		ENEMY[atoi(parts[1])][atoi(parts[2])] = SPINY;
180 	}
181 	else if (!strcmp (parts[0], "FIRECIRCLE"))
182 	{
183 		if(atoi(parts[3])<=6)
184 			ENEMY[atoi(parts[1])][atoi(parts[2])] = FIRECIRCLE1;
185 		else if(atoi(parts[3])<=10)
186 			ENEMY[atoi(parts[1])][atoi(parts[2])] = FIRECIRCLE2;
187 		else
188 			ENEMY[atoi(parts[1])][atoi(parts[2])] = FIRECIRCLE3;
189 	}
190 	else if (!strcmp (parts[0], "FIRE"))
191 	{
192 		ENEMY[atoi(parts[1])][atoi(parts[2])] = FIRE;
193 	}
194 	else if (!strcmp (parts[0], "KILLERBLUME"))
195 	{
196 		ENEMY[atoi(parts[1])][atoi(parts[2])] = BLUME;
197 	}
198 	else if (!strcmp (parts[0], "CANNON"))
199 	{
200 		ENEMY[atoi(parts[1])][atoi(parts[2])] = CANNON;
201 	}
202 	else if (!strcmp (parts[0], "BRETT"))
203 	{
204 		ENEMY[atoi(parts[1])/40][atoi(parts[2])/40] = BRETT;
205 	}
206 	else if (!strcmp (parts[0], "CASTLE"))
207 	{
208 		CASTLE.x = atoi(parts[1]);
209 		CASTLE.y = atoi(parts[2]);
210 		CASTLE.big = atoi(parts[3]);
211 		CASTLE.level = new char[100];
212 		strcpy(CASTLE.level,parts[4]);
213 		CASTLE.startpoint = atoi(parts[5]);
214 		CASTLE.available = 1;
215 	}
216 	else if (!strcmp (parts[0], "BOWSER"))
217 	{
218 		_BOWSER.x = atoi(parts[1]);
219 		_BOWSER.y = atoi(parts[2]);
220 		m_BOWSER_AV = 1;
221 		m_HAMMERS = atoi(parts[3]);
222 	}
223 	else if (!strcmp (parts[0], "RESTART"))
224 	{
225 		m_RESTARTLEVEL = 1;
226 	}
227 	else if (!strcmp (parts[0], "MUSIC"))
228 	{
229 		MUSIC = new char[100];
230 		strcpy(MUSIC,parts[1]);
231 	}
232 	else if (!strcmp (parts[0], "BACKGROUND"))
233 	{
234 		BACK = new char[100];
235 		strcpy(BACK,parts[1]);
236 	}
237 }
238 
getParts(char * Text)239 void cScene::getParts(char *Text)
240 {
241 
242 	delete[] parts;
243 
244 	char *str = Text;
245 	partcount = 0;
246 	int len = 0, i = 0;
247 
248 
249 	str += strspn(str," ");
250 
251 	while (*str)
252 	{
253 		partcount++;
254 		str += strcspn(str," ");
255 		str += strspn(str," ");
256 	}
257 
258 	str = Text;
259 
260 	parts = new char*[partcount];
261 
262 	str += strspn(str," ");
263 
264 	while (*str)
265 	{
266 		int len = (int)(strcspn(str," "));
267 		parts[i] = new char[len + 2];
268 		memcpy(parts[i],str,len);
269 		parts[i][len] = 0;
270 		str += len + strspn(str+len," ");
271 		i++;
272 	}
273 }
274 
unloadLevel()275 void cScene::unloadLevel()
276 {
277 	for(i=0;i<TEXTUREcount;i++)
278 		SDL_FreeSurface(Textures[i]);
279 
280 	TEXTUREcount = 1;
281 
282 	for(i=0;i<MAX_X;i++)
283 		for(int n=0;n<MAX_Y;n++)
284 		{
285 			ENEMY[i][n] = 0;
286 			TILE[i][n] = 0;
287 			BONUS[i][n] = 0;
288 			COIN[i][n] = 0;
289 		}
290 	CASTLE.available = 0;
291 	m_BOWSER_AV = 0;
292 
293 }
294 
295 
296 SDL_Rect tmp_rect;
297 int posx,posy;
drawLevel()298 void cScene::drawLevel()
299 {
300 	tmp_rect.y = -cam_y%600+500;
301 	tmp_rect.x = -cam_x%800+700;
302 	SDL_UpperBlit(Textures[MAX_TEXTURES-1],0,screen,&tmp_rect);
303 
304 	posx = cam_x/40;
305 	posy = cam_y/40;
306 	COINcount+=0.1f;
307 	if(COINcount>13)
308 		COINcount=10;
309 	for(i = posx;i<posx+25;i++)
310 	{
311 		for(int n = posy;n<posy+21;n++)
312 		{
313 			if(COIN[i][n])
314 			{
315 				tmp_rect.x = (i*40) - cam_x;
316 				tmp_rect.y = (n*40) - cam_y;
317 				SDL_UpperBlit(Bonus_Textures[(int)COINcount],0,screen,&tmp_rect);
318 
319 			}
320 			if(TILE[i][n])
321 			{
322 				tmp_rect.x = (i*40) - cam_x;
323 				tmp_rect.y = (n*40) - cam_y;
324 				SDL_UpperBlit(Textures[TILE[i][n]],0,screen,&tmp_rect);
325 			}
326 			else if(BONUS[i][n])
327 			{
328 				tmp_rect.x = (i*40) - cam_x;
329 				tmp_rect.y = (n*40) - cam_y;
330 				SDL_UpperBlit(Bonus_Textures[BONUS[i][n]],0,screen,&tmp_rect);
331 			}
332 			if(ENEMY[i][n])
333 			{
334 				tmp_rect.x = (i*40) - cam_x;
335 				tmp_rect.y = (n*40) - cam_y;
336 				SDL_UpperBlit(Enemy_Textures[ENEMY[i][n]],0,screen,&tmp_rect);
337 
338 			}
339 
340 		}
341 	}
342 	if(m_BOWSER_AV)
343 	{
344 		tmp_rect.x = _BOWSER.x*40 - cam_x;
345 		tmp_rect.y = _BOWSER.y*40 - cam_y;
346 		SDL_UpperBlit(Enemy_Textures[BOWSER],0,screen,&tmp_rect);
347 	}
348 	if(CASTLE.available)
349 	{
350 		tmp_rect.x = CASTLE.x - cam_x;
351 		tmp_rect.y = CASTLE.y - cam_y;
352 		if(CASTLE.big)
353 			SDL_UpperBlit(CASTLE.big_s,0,screen,&tmp_rect);
354 		else
355 			SDL_UpperBlit(CASTLE.small_s,0,screen,&tmp_rect);
356 	}
357 	for(i=0;i<5;i++)
358 	{
359 		char* c = new char[2];
360 		sprintf(c,"%d",i);
361 
362 		tmp_rect.x = STARTPOINTS[i].x - cam_x;
363 		tmp_rect.y = STARTPOINTS[i].y - cam_y;
364 		SDL_UpperBlit(Enemy_Textures[0],0,screen,&tmp_rect);
365 		StringColor(screen,tmp_rect.x,tmp_rect.y,c,1440);
366 
367 		tmp_rect.x = EXITPOINTS[i].x - cam_x;
368 		tmp_rect.y = EXITPOINTS[i].y - cam_y;
369 		SDL_UpperBlit(Enemy_Textures[19],0,screen,&tmp_rect);
370 		StringColor(screen,tmp_rect.x+22,tmp_rect.y,c,1450);
371 		delete[] (c);
372 	}
373 
374 }
375 
376 
377 
378 
379 
saveLevel()380 void cScene::saveLevel()
381 {
382 	ofstream out(Levelname);
383 
384 	out<<"# Level for Mega Mario"<<endl<<endl;
385 
386 	CString PATH_FILE = _T("");
387 
388 	for(i=0;i<m_TextureCtrl.GetCount();i++)
389 	{
390 		m_TextureCtrl.GetText(i,PATH_FILE);
391 		out<<"TEXTURE "<<PATH_FILE<<endl;
392 	}
393 
394 	out<<"::TILES-> BEGIN"<<endl;
395 	for(i=0;i<MAX_X;i++)
396 		for(int n=0;n<MAX_Y;n++)
397 		{
398 			if(TILE[i][n])
399 				out<<i<<" "<<n<<" "<<TILE[i][n]<<endl;
400 
401 		}
402 	out<<"::TILES-> END"<<endl;
403 	out<<"::BONUS-> BEGIN"<<endl;
404 	for(i=0;i<MAX_X;i++)
405 		for(int n=0;n<MAX_Y;n++)
406 		{
407 			if(BONUS[i][n])
408 				out<<i<<" "<<n<<" "<<BONUS[i][n]<<endl;
409 
410 		}
411 	out<<"::BONUS-> END"<<endl;
412 	out<<"::COIN-> BEGIN"<<endl;
413 	for(i=0;i<MAX_X;i++)
414 		for(int n=0;n<MAX_Y;n++)
415 		{
416 			if(COIN[i][n])
417 				out<<i<<" "<<n<<endl;
418 
419 		}
420 	out<<"::COIN-> END"<<endl;
421 
422 	for(i=0;i<MAX_X;i++)
423 		for(int n=0;n<MAX_Y;n++)
424 		{
425 			switch(ENEMY[i][n])
426 			{
427 				case GOOMBA:
428 					out<<"GOOMBA "<<i<<" "<<n<<endl;
429 				break;
430 				case TURTLE:
431 					out<<"TURTLE "<<i<<" "<<n<<endl;
432 				break;
433 				case SPINY:
434 					out<<"SPINY "<<i<<" "<<n<<endl;
435 				break;
436 				case FIRE:
437 					out<<"FIRE "<<i<<" "<<n<<endl;
438 				break;
439 				case FIRECIRCLE1:
440 					out<<"FIRECIRCLE "<<i<<" "<<n<<" "<<6<<endl;
441 				break;
442 				case FIRECIRCLE2:
443 					out<<"FIRECIRCLE "<<i<<" "<<n<<" "<<8<<endl;
444 				break;
445 				case FIRECIRCLE3:
446 					out<<"FIRECIRCLE "<<i<<" "<<n<<" "<<16<<endl;
447 				break;
448 				case CANNON:
449 					out<<"CANNON "<<i<<" "<<n<<endl;
450 				break;
451 				case BLUME:
452 					out<<"KILLERBLUME "<<i<<" "<<n<<endl;
453 				break;
454 				case TURTLEFLY:
455 					out<<"TURTLEFLY "<<i<<" "<<n<<endl;
456 				break;
457 				case BRETT:
458 					out<<"BRETT "<<i*40<<" "<<n*40<<endl;
459 				break;
460 
461 			}
462 		}
463 	if(m_RESTARTLEVEL)
464 		out<<"RESTART "<<Levelname<<endl;
465 
466 	for(i=0;i<5;i++)
467 	{
468 		out<<"START "<<i<<" "<<STARTPOINTS[i].x<<" "<<STARTPOINTS[i].y<<" "<<STARTPOINTS[i].type<<endl;
469 		out<<"EXIT "<<i<<" "<<EXITPOINTS[i].x<<" "<<EXITPOINTS[i].y<<" "<<EXITPOINTS[i].level<<" "<<EXITPOINTS[i].startpoint<<" "<<EXITPOINTS[i].type<<endl;
470 	}
471 	out<<"BGCOLOR "<<BGCOLOR<<endl;
472 	out<<endl;
473 
474 	if(m_BOWSER_AV)
475 	{
476 		out<<"BOWSER "<<_BOWSER.x<<" "<<_BOWSER.y<<" "<<m_HAMMERS<<endl;
477 	}
478 	if(CASTLE.available)
479 	{
480 		out<<"CASTLE "<<CASTLE.x<<" "<<CASTLE.y<<" "<<CASTLE.big<<" "<<CASTLE.level<<" "<<CASTLE.startpoint<<endl;;
481 	}
482 
483 	if(MUSIC)
484 		out<<"MUSIC "<<MUSIC<<endl;
485 	else
486 		out<<"#If you want Background music:\n#MUSIC megamusic.mid"<<endl;
487 
488 	if(BACK)
489 		out<<"BACKGROUND "<<BACK<<endl;
490 	else
491 		out<<"#if you want a big static background:\n#BACKGROUND data/gfx/background/VH.jpg\n"<<endl;
492 
493 
494 }
495 
496 
497 
498