瀏覽代碼

Create level map and display highest level tile

DricomDragon 4 年之前
父節點
當前提交
55a34d6efe
共有 1 個文件被更改,包括 38 次插入2 次删除
  1. 38 2
      main.cpp

+ 38 - 2
main.cpp

@@ -29,15 +29,44 @@ int main()
 		return 1;
 	}
 
-	// [2.2] Other components
+	// [2.2] Load textures
 	SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp");
 	SDL_Surface* water = SDL_LoadBMP("textures/water.bmp");
 	SDL_Surface* desert = SDL_LoadBMP("textures/desert.bmp");
 	SDL_Surface* mountain = SDL_LoadBMP("textures/mountain.bmp");
 	SDL_Surface* pawn = SDL_LoadBMP("textures/pawn.bmp");
+
+	// [2.3] Create variables
 	SDL_Rect pos({0, 0, 0, 0});
 	SDL_Rect pawnPos({0, 0, 0, 0});
 
+	// [2.4] Create world
+	const int blocTotal = 4;
+	enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
+	float world[height][width][blocTotal];
+
+	// Clear world
+	for (int j(0); j < height; j++)
+		for (int i(0); i < width; i++)
+			for (int t(0); t < blocTotal; t++)
+				world[j][i][t] = 0.0f;
+
+	// Basic water
+	const int waterLevel = 0.01f;
+	for (int j(0); j < height; j++)
+		for (int i(0); i < width; i++)
+			world[j][i][WATER] = waterLevel;
+
+	// Top mountain line
+	const int mountainLevel = 10.0f;
+	for (int j(0); j < height; j++)
+		for (int i(0); i < width; i++)
+			world[j][i][MOUNTAIN] = mountainLevel / (1 + j);
+
+	// [2.5] Surface containers
+	SDL_Surface* select; // Current surface selected ! DO NOT FREE !
+	SDL_Surface* tileSet[blocTotal] = {water, mountain, grass, desert};
+
 	/// [3] Main loop
 	bool done(false);
 	while (!done)
@@ -70,9 +99,16 @@ int main()
 
 		for (int y(0); y < height; y++)
 			for (int x(0); x < width; x++) {
+				select = tileSet[0];
 				pos.x = x * blocSize;
 				pos.y = y * blocSize;
-				SDL_BlitSurface(water, NULL, screen, &pos);
+
+				// Select the tile with highest level
+				for (int k(1); k < blocTotal; k++)
+					if (world[y][x][k] > world[y][x][k - 1])
+						select = tileSet[k];
+
+				SDL_BlitSurface(select, NULL, screen, &pos);
 			}
 
 		SDL_BlitSurface(pawn, NULL, screen, &pawnPos);