Parcourir la source

* [new] przesuwanie mapy strzałkami
* [new] zakańczanie programu przez naciśnięce 'q' lub iksa w oknie

Michał W. Urbańczyk il y a 18 ans
Parent
commit
b531cf169a
2 fichiers modifiés avec 87 ajouts et 29 suppressions
  1. 54 4
      CMT.cpp
  2. 33 25
      mapHandler.cpp

+ 54 - 4
CMT.cpp

@@ -192,7 +192,9 @@ void SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B)
 	 SDL_UpdateRect(ekran, x, y, 1, 1);
 }
 int _tmain(int argc, _TCHAR* argv[])
-{
+{ 
+	int xx=0, yy=0;
+	SDL_Event sEvent;
 	srand ( time(NULL) );
 	SDL_Surface *screen, *temp;
 	std::vector<SDL_Surface*> Sprites;
@@ -231,17 +233,65 @@ int _tmain(int argc, _TCHAR* argv[])
 		mh->init();
 		THC std::cout<<"Inicjalizacja mapHandlera: "<<tmh.getDif()<<std::endl;
 		//SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
-		SDL_Surface * teren = mh->terrainRect(0,0,32,24);
+		SDL_Surface * teren = mh->terrainRect(xx,yy,32,24);
 		THC std::cout<<"Przygotowanie terenu do wyswietlenia: "<<tmh.getDif()<<std::endl;
 		SDL_BlitSurface(teren,NULL,ekran,NULL);
+		SDL_FreeSurface(teren);
 		SDL_Flip(ekran);
 		THC std::cout<<"Wyswietlenie terenu: "<<tmh.getDif()<<std::endl;
 
 		//SDL_Surface * ss = ac->defs[0]->ourImages[0].bitmap;
 		//SDL_BlitSurface(ss, NULL, ekran, NULL);
 
-		SDL_Delay(5000);
-			SDL_Quit();
+		for(;;) // main loop
+		{
+			try
+			{
+				if(SDL_PollEvent(&sEvent))  //wait for event...
+				{
+					if(sEvent.type==SDL_QUIT) 
+						return 0;
+					else if (sEvent.type==SDL_KEYDOWN)
+					{
+						switch (sEvent.key.keysym.sym)
+						{
+						case SDLK_LEFT:
+							{
+								xx--;
+								break;
+							}
+						case (SDLK_RIGHT):
+							{
+								xx++;
+								break;
+							}
+						case (SDLK_UP):
+							{
+								yy--;
+								break;
+							}
+						case (SDLK_DOWN):
+							{
+								yy++;
+								break;
+							}
+						case (SDLK_q):
+							{
+								return 0;
+								break;
+							}
+						}
+						SDL_FillRect(ekran, NULL, SDL_MapRGB(ekran->format, 0, 0, 0));
+						SDL_BlitSurface(mh->terrainRect(xx,yy,32,24),NULL,ekran,NULL);
+						SDL_Flip(ekran);
+					}
+				}
+			}
+			catch(...)
+			{ continue; }
+
+		SDL_Delay(100); //give time for another apps
+		}
 		return 0;
 	}
 	else

+ 33 - 25
mapHandler.cpp

@@ -17,36 +17,41 @@ void mapHandler::init()
 			std::string name = CSemiDefHandler::nameFromType(reader->map.terrain[i][j].tertype);
 			for (int k=0; k<reader->defs.size(); k++)
 			{
-				if (reader->defs[k]->defName != name)
-					continue;
-				else
+				try
 				{
-					SDL_Surface * n;
-					int ktora = reader->map.terrain[i][j].terview;
-					terrainBitmap[i][j] = reader->defs[k]->ourImages[ktora].bitmap;
-					//TODO: odwracanie	
-					switch ((reader->map.terrain[i][j].siodmyTajemniczyBajt)%4)
+					if (reader->defs[k]->defName != name)
+						continue;
+					else
 					{
-					case 1:
+						SDL_Surface * n;
+						int ktora = reader->map.terrain[i][j].terview;
+						terrainBitmap[i][j] = reader->defs[k]->ourImages[ktora].bitmap;
+						//TODO: odwracanie	
+						switch ((reader->map.terrain[i][j].siodmyTajemniczyBajt)%4)
 						{
-							terrainBitmap[i][j] = CSDL_Ext::rotate01(terrainBitmap[i][j]);
-							break;
+						case 1:
+							{
+								terrainBitmap[i][j] = CSDL_Ext::rotate01(terrainBitmap[i][j]);
+								break;
+							}
+						case 2:
+							{
+								terrainBitmap[i][j] = CSDL_Ext::hFlip(terrainBitmap[i][j]);
+								break;
+							}
+						case 3:
+							{
+								terrainBitmap[i][j] = CSDL_Ext::rotate03(terrainBitmap[i][j]);
+								break;
+							}
 						}
-					case 2:
-						{
-							terrainBitmap[i][j] = CSDL_Ext::hFlip(terrainBitmap[i][j]);
-							break;
-						}
-					case 3:
-						{
-							terrainBitmap[i][j] = CSDL_Ext::rotate03(terrainBitmap[i][j]);
-							break;
-						}
-					}
-					//SDL_BlitSurface(terrainBitmap[i][j],NULL,ekran,NULL); SDL_Flip(ekran);SDL_Delay(50);
+						//SDL_BlitSurface(terrainBitmap[i][j],NULL,ekran,NULL); SDL_Flip(ekran);SDL_Delay(50);
 
-					break;
+						break;
+					}
 				}
+				catch (...)
+				{	continue;	}
 			}
 		}
 	}
@@ -66,6 +71,8 @@ SDL_Surface * mapHandler::terrainRect(int x, int y, int dx, int dy)
 #endif
 	SDL_Surface * su = SDL_CreateRGBSurface(SDL_SWSURFACE, dx*32, dy*32, 32,
                                    rmask, gmask, bmask, amask);
+	if (((dx+x)>((reader->map.width)-1) || (dy+y)>((reader->map.height)-1)) || ((x<0)||(y<0) ) )
+		throw new std::string("Poza zakresem");
 	for (int bx=0; bx<dx; bx++)
 	{
 		for (int by=0; by<dy; by++)
@@ -74,7 +81,8 @@ SDL_Surface * mapHandler::terrainRect(int x, int y, int dx, int dy)
 			sr->y=by*32;
 			sr->x=bx*32;
 			sr->h=sr->w=32;
-			SDL_BlitSurface(terrainBitmap[bx][by],NULL,su,sr);
+			
+			SDL_BlitSurface(terrainBitmap[bx+x][by+y],NULL,su,sr);
 
 			//SDL_BlitSurface(su,NULL,ekran,NULL);SDL_Flip(ekran);
 		}