浏览代码

Draw world in a dedicated function

DricomDragon 4 年之前
父节点
当前提交
ec321f4c43
共有 1 个文件被更改,包括 39 次插入30 次删除
  1. 39 30
      main.cpp

+ 39 - 30
main.cpp

@@ -2,6 +2,33 @@
 #include <iostream>
 #include <SDL/SDL.h>
 
+#define BLOC_SIZE 16
+#define WIDTH 100
+#define HEIGHT 50
+#define BLOC_TOTAL 4
+
+enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
+
+void drawWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL], SDL_Surface* tileSet[], SDL_Surface* out) {
+	SDL_Rect pos({0, 0, 0, 0});
+	SDL_Surface* select; // Current surface selected ! DO NOT FREE !
+
+	for (int y(0); y < HEIGHT; y++)
+		for (int x(0); x < WIDTH; x++) {
+			select = tileSet[0];
+			pos.x = x * BLOC_SIZE;
+			pos.y = y * BLOC_SIZE;
+
+			// Select the tile with highest level
+			for (int k(1); k < BLOC_TOTAL; k++)
+				if (world[y][x][k] > world[y][x][k - 1])
+					select = tileSet[k];
+
+			SDL_BlitSurface(select, NULL, out, &pos);
+		}
+
+}
+
 int main()
 {
 	/// [1] Start
@@ -20,12 +47,10 @@ int main()
 
 	/// [2] Create components
 	// [2.1] Create window
-	const int blocSize(16);
-	const int width(100), height(50);
-	SDL_Surface* screen = SDL_SetVideoMode(width * blocSize, height * blocSize, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
+	SDL_Surface* screen = SDL_SetVideoMode(WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
 	if ( !screen )
 	{
-		std::cout << "Unable to set " << width << "x" << height << " video: " << SDL_GetError() << std::endl;
+		std::cout << "Unable to set " << WIDTH << "x" << HEIGHT << " video: " << SDL_GetError() << std::endl;
 		return 1;
 	}
 
@@ -37,35 +62,31 @@ int main()
 	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];
+	float world[HEIGHT][WIDTH][BLOC_TOTAL];
 
 	// Clear world
-	for (int j(0); j < height; j++)
-		for (int i(0); i < width; i++)
-			for (int t(0); t < blocTotal; t++)
+	for (int j(0); j < HEIGHT; j++)
+		for (int i(0); i < WIDTH; i++)
+			for (int t(0); t < BLOC_TOTAL; 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++)
+	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++)
+	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};
+	SDL_Surface* tileSet[BLOC_TOTAL] = {water, mountain, grass, desert};
 
 	/// [3] Main loop
 	bool done(false);
@@ -97,19 +118,7 @@ int main()
 		// [3.3] Draw phase
 		SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
 
-		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;
-
-				// 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);
-			}
+		drawWorld(world, tileSet, screen);
 
 		SDL_BlitSurface(pawn, NULL, screen, &pawnPos);